]>
git.proxmox.com Git - mirror_smartmontools-debian.git/blob - utility.h
4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2002-9 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2008-9 Christian Franke <smartmontools-support@lists.sourceforge.net>
8 * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * You should have received a copy of the GNU General Public License
16 * (for example COPYING); if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * This code was originally developed as a Senior Thesis by Michael Cornwell
20 * at the Concurrent Systems Laboratory (now part of the Storage Systems
21 * Research Center), Jack Baskin School of Engineering, University of
22 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
29 #define UTILITY_H_CVSID "$Id: utility.h 2848 2009-07-18 20:14:38Z chrfranke $"
32 #include <sys/types.h> // for regex.h (according to POSIX)
39 #if !defined(__GNUC__) && !defined(__attribute__)
40 #define __attribute__(x) /**/
43 // Make version information string
44 std::string
format_version_info(const char * prog_name
, bool full
= false);
46 // return (v)sprintf() formated std::string
47 std::string
strprintf(const char * fmt
, ...)
48 __attribute__ ((format (printf
, 1, 2)));
49 std::string
vstrprintf(const char * fmt
, va_list ap
);
51 #ifndef HAVE_WORKING_SNPRINTF
52 // Substitute by safe replacement functions
53 int safe_snprintf(char *buf
, int size
, const char *fmt
, ...)
54 __attribute__ ((format (printf
, 3, 4)));
55 int safe_vsnprintf(char *buf
, int size
, const char *fmt
, va_list ap
);
56 #define snprintf safe_snprintf
57 #define vsnprintf safe_vsnprintf
61 // Replacement for missing strtoull() (Linux with libc < 6, MSVC)
62 uint64_t strtoull(const char * p
, char * * endp
, int base
);
65 // Utility function prints current date and time and timezone into a
66 // character buffer of length>=64. All the fuss is needed to get the
67 // right timezone info (sigh).
68 #define DATEANDEPOCHLEN 64
69 void dateandtimezone(char *buffer
);
70 // Same, but for time defined by epoch tval
71 void dateandtimezoneepoch(char *buffer
, time_t tval
);
73 // like printf() except that we can control it better. Note --
74 // although the prototype is given here in utility.h, the function
75 // itself is defined differently in smartctl and smartd. So the
76 // function definition(s) are in smartd.c and in smartctl.c.
77 void pout(const char *fmt
, ...)
78 __attribute__ ((format (printf
, 1, 2)));
80 // replacement for perror() with redirected output.
81 void syserror(const char *message
);
83 // Function for processing -r option in smartctl and smartd
84 int split_report_arg(char *s
, int *i
);
85 // Function for processing -c option in smartctl and smartd
86 int split_report_arg2(char *s
, int *i
);
88 // Function for processing -t selective... option in smartctl
89 int split_selective_arg(char *s
, uint64_t *start
, uint64_t *stop
, int *mode
);
92 // Guess device type (ata or scsi) based on device name
93 // Guessing will now use Controller Type defines below
95 // Moved to C++ interface
96 //int guess_device_type(const char * dev_name);
98 // Create and return the list of devices to probe automatically
99 // if the DEVICESCAN option is in the smartd config file
100 // Moved to C++ interface
101 //int make_device_names (char ***devlist, const char* name);
103 // Replacement for exit(status)
104 // (exit is not compatible with C++ destructors)
105 #define EXIT(status) { throw (int)(status); }
110 // replacement for calloc() that tracks memory usage
111 void *Calloc(size_t nmemb
, size_t size
);
113 // Utility function to free memory
114 void *FreeNonZero1(void* address
, int size
, int whatline
, const char* file
);
116 // Typesafe version of above
118 inline T
* FreeNonZero(T
* address
, int size
, int whatline
, const char* file
)
119 { return (T
*)FreeNonZero1((void *)address
, size
, whatline
, file
); }
121 // A custom version of strdup() that keeps track of how much memory is
122 // being allocated. If mustexist is set, it also throws an error if we
123 // try to duplicate a NULL string.
124 char *CustomStrDup(const char *ptr
, int mustexist
, int whatline
, const char* file
);
126 // To help with memory checking. Use when it is known that address is
128 void *CheckFree1(void *address
, int whatline
, const char* file
);
130 // Typesafe version of above
132 inline T
* CheckFree(T
* address
, int whatline
, const char* file
)
133 { return (T
*)CheckFree1((void *)address
, whatline
, file
); }
135 #endif // OLD_INTERFACE
137 // This function prints either to stdout or to the syslog as needed
139 // [From GLIBC Manual: Since the prototype doesn't specify types for
140 // optional arguments, in a call to a variadic function the default
141 // argument promotions are performed on the optional argument
142 // values. This means the objects of type char or short int (whether
143 // signed or not) are promoted to either int or unsigned int, as
145 void PrintOut(int priority
, const char *fmt
, ...) __attribute__ ((format(printf
, 2, 3)));
147 // run time, determine byte ordering
150 // This value follows the peripheral device type value as defined in
151 // SCSI Primary Commands, ANSI INCITS 301:1997. It is also used in
152 // the ATA standard for packet devices to define the device type.
153 const char *packetdevicetype(int type
);
155 // Moved to C++ interface
156 //int deviceopen(const char *pathname, char *type);
158 //int deviceclose(int fd);
160 // Optional functions of os_*.c
161 #ifdef HAVE_GET_OS_VERSION_STR
162 // Return build host and OS version as static string
163 //const char * get_os_version_str(void);
166 // returns true if any of the n bytes are nonzero, else zero.
167 bool nonempty(const void * data
, int size
);
169 // needed to fix glibc bug
170 void FixGlibcTimeZoneBug();
172 // convert time in msec to a text string
173 void MsecToText(unsigned int msec
, char *txt
);
175 // Wrapper class for a raw data buffer
179 explicit raw_buffer(unsigned sz
, unsigned char val
= 0)
180 : m_data(new unsigned char[sz
]),
182 { memset(m_data
, val
, m_size
); }
185 { delete [] m_data
; }
187 unsigned size() const
190 unsigned char * data()
192 const unsigned char * data() const
196 unsigned char * m_data
;
199 raw_buffer(const raw_buffer
&);
200 void operator=(const raw_buffer
&);
203 /// Wrapper class for FILE *.
207 explicit stdio_file(FILE * f
= 0, bool owner
= false)
208 : m_file(f
), m_owner(owner
) { }
210 stdio_file(const char * name
, const char * mode
)
211 : m_file(fopen(name
, mode
)), m_owner(true) { }
215 if (m_file
&& m_owner
)
219 bool open(const char * name
, const char * mode
)
221 m_file
= fopen(name
, mode
);
226 void open(FILE * f
, bool owner
= false)
236 bool ok
= !ferror(m_file
);
246 bool operator!() const
253 stdio_file(const stdio_file
&);
254 void operator=(const stdio_file
&);
257 /// Wrapper class for regex(3).
258 /// Supports copy & assignment and is compatible with STL containers.
259 class regular_expression
262 // Construction & assignment
263 regular_expression();
265 regular_expression(const char * pattern
, int flags
);
267 ~regular_expression();
269 regular_expression(const regular_expression
& x
);
271 regular_expression
& operator=(const regular_expression
& x
);
273 /// Set and compile new pattern, return false on error.
274 bool compile(const char * pattern
, int flags
);
276 // Get pattern from last compile().
277 const char * get_pattern() const
278 { return m_pattern
.c_str(); }
280 /// Get error message from last compile().
281 const char * get_errmsg() const
282 { return m_errmsg
.c_str(); }
284 // Return true if pattern is not set or bad.
286 { return (m_pattern
.empty() || !m_errmsg
.empty()); }
288 /// Return true if substring matches pattern
289 bool match(const char * str
, int flags
= 0) const
290 { return !regexec(&m_regex_buf
, str
, 0, (regmatch_t
*)0, flags
); }
292 /// Return true if full string matches pattern
293 bool full_match(const char * str
, int flags
= 0) const
296 return ( !regexec(&m_regex_buf
, str
, 1, &range
, flags
)
297 && range
.rm_so
== 0 && range
.rm_eo
== (int)strlen(str
));
300 /// Return true if substring matches pattern, fill regmatch_t array.
301 bool execute(const char * str
, unsigned nmatch
, regmatch_t
* pmatch
, int flags
= 0) const
302 { return !regexec(&m_regex_buf
, str
, nmatch
, pmatch
, flags
); }
305 std::string m_pattern
;
308 std::string m_errmsg
;
311 void copy(const regular_expression
& x
);
315 // macros to control printing
316 #define PRINT_ON(control) {if (control->printing_switchable) control->dont_print=false;}
317 #define PRINT_OFF(control) {if (control->printing_switchable) control->dont_print=true;}
320 // possible values for controller_type in extern.h
321 #define CONTROLLER_UNKNOWN 0x00
322 #define CONTROLLER_ATA 0x01
323 #define CONTROLLER_SCSI 0x02
324 #define CONTROLLER_3WARE 0x03 // set by -d option, but converted to one of three types below
325 #define CONTROLLER_3WARE_678K 0x04 // NOT set by guess_device_type()
326 #define CONTROLLER_3WARE_9000_CHAR 0x05 // set by guess_device_type()
327 #define CONTROLLER_3WARE_678K_CHAR 0x06 // set by guess_device_type()
328 #define CONTROLLER_MARVELL_SATA 0x07 // SATA drives behind Marvell controllers
329 #define CONTROLLER_SAT 0x08 // SATA device behind a SCSI ATA Translation (SAT) layer
330 #define CONTROLLER_HPT 0x09 // SATA drives behind HighPoint Raid controllers
331 #define CONTROLLER_CCISS 0x10 // CCISS controller
332 #define CONTROLLER_PARSEDEV 0x11 // "smartctl -r ataioctl,2 ..." output parser pseudo-device
333 #define CONTROLLER_USBCYPRESS 0x12 // ATA device behind Cypress USB bridge
334 #define CONTROLLER_ARECA 0x13 // Areca controller