]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - utility.h
Updated changelog
[mirror_smartmontools-debian.git] / utility.h
1 /*
2 * utility.h
3 *
4 * Home page of code is: http://smartmontools.sourceforge.net
5 *
6 * Copyright (C) 2002-11 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2008-11 Christian Franke <smartmontools-support@lists.sourceforge.net>
8 * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
9 *
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)
13 * any later version.
14 *
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.
18 *
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/
23 *
24 */
25
26 #ifndef UTILITY_H_
27 #define UTILITY_H_
28
29 #define UTILITY_H_CVSID "$Id: utility.h 3475 2011-11-10 21:43:40Z chrfranke $"
30
31 #include <time.h>
32 #include <sys/types.h> // for regex.h (according to POSIX)
33 #include <regex.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <string>
38
39 #ifndef __GNUC__
40 #define __attribute_format_printf(x, y) /**/
41 #elif defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO
42 // Check format of __mingw_*printf() instead of MSVCRT.DLL:*printf()
43 #define __attribute_format_printf(x, y) __attribute__((format (gnu_printf, x, y)))
44 #define HAVE_WORKING_SNPRINTF 1
45 #else
46 #define __attribute_format_printf(x, y) __attribute__((format (printf, x, y)))
47 #endif
48
49 // Make version information string
50 std::string format_version_info(const char * prog_name, bool full = false);
51
52 // return (v)sprintf() formated std::string
53 std::string strprintf(const char * fmt, ...)
54 __attribute_format_printf(1, 2);
55 std::string vstrprintf(const char * fmt, va_list ap);
56
57 #ifndef HAVE_WORKING_SNPRINTF
58 // Substitute by safe replacement functions
59 int safe_snprintf(char *buf, int size, const char *fmt, ...)
60 __attribute_format_printf(3, 4);
61 int safe_vsnprintf(char *buf, int size, const char *fmt, va_list ap);
62 #define snprintf safe_snprintf
63 #define vsnprintf safe_vsnprintf
64 #endif
65
66 #ifndef HAVE_STRTOULL
67 // Replacement for missing strtoull() (Linux with libc < 6, MSVC)
68 uint64_t strtoull(const char * p, char * * endp, int base);
69 #endif
70
71 // Utility function prints current date and time and timezone into a
72 // character buffer of length>=64. All the fuss is needed to get the
73 // right timezone info (sigh).
74 #define DATEANDEPOCHLEN 64
75 void dateandtimezone(char *buffer);
76 // Same, but for time defined by epoch tval
77 void dateandtimezoneepoch(char *buffer, time_t tval);
78
79 // like printf() except that we can control it better. Note --
80 // although the prototype is given here in utility.h, the function
81 // itself is defined differently in smartctl and smartd. So the
82 // function definition(s) are in smartd.c and in smartctl.c.
83 void pout(const char *fmt, ...)
84 __attribute_format_printf(1, 2);
85
86 // replacement for perror() with redirected output.
87 void syserror(const char *message);
88
89 // Function for processing -r option in smartctl and smartd
90 int split_report_arg(char *s, int *i);
91 // Function for processing -c option in smartctl and smartd
92 int split_report_arg2(char *s, int *i);
93
94 // Function for processing -t selective... option in smartctl
95 int split_selective_arg(char *s, uint64_t *start, uint64_t *stop, int *mode);
96
97
98 // Guess device type (ata or scsi) based on device name
99 // Guessing will now use Controller Type defines below
100
101 // Moved to C++ interface
102 //int guess_device_type(const char * dev_name);
103
104 // Create and return the list of devices to probe automatically
105 // if the DEVICESCAN option is in the smartd config file
106 // Moved to C++ interface
107 //int make_device_names (char ***devlist, const char* name);
108
109 // Replacement for exit(status)
110 // (exit is not compatible with C++ destructors)
111 #define EXIT(status) { throw (int)(status); }
112
113
114 #ifdef OLD_INTERFACE
115
116 // replacement for calloc() that tracks memory usage
117 void *Calloc(size_t nmemb, size_t size);
118
119 // Utility function to free memory
120 void *FreeNonZero1(void* address, int size, int whatline, const char* file);
121
122 // Typesafe version of above
123 template <class T>
124 inline T * FreeNonZero(T * address, int size, int whatline, const char* file)
125 { return (T *)FreeNonZero1((void *)address, size, whatline, file); }
126
127 // A custom version of strdup() that keeps track of how much memory is
128 // being allocated. If mustexist is set, it also throws an error if we
129 // try to duplicate a NULL string.
130 char *CustomStrDup(const char *ptr, int mustexist, int whatline, const char* file);
131
132 // To help with memory checking. Use when it is known that address is
133 // NOT null.
134 void *CheckFree1(void *address, int whatline, const char* file);
135
136 // Typesafe version of above
137 template <class T>
138 inline T * CheckFree(T * address, int whatline, const char* file)
139 { return (T *)CheckFree1((void *)address, whatline, file); }
140
141 #endif // OLD_INTERFACE
142
143 // Compile time check of byte ordering
144 // (inline const function allows compiler to remove dead code)
145 inline bool isbigendian()
146 {
147 #ifdef WORDS_BIGENDIAN
148 return true;
149 #else
150 return false;
151 #endif
152 }
153
154 // Runtime check of byte ordering, throws if different from isbigendian().
155 void check_endianness();
156
157 // This value follows the peripheral device type value as defined in
158 // SCSI Primary Commands, ANSI INCITS 301:1997. It is also used in
159 // the ATA standard for packet devices to define the device type.
160 const char *packetdevicetype(int type);
161
162 // Moved to C++ interface
163 //int deviceopen(const char *pathname, char *type);
164
165 //int deviceclose(int fd);
166
167 // Optional functions of os_*.c
168 #ifdef HAVE_GET_OS_VERSION_STR
169 // Return build host and OS version as static string
170 //const char * get_os_version_str(void);
171 #endif
172
173 // returns true if any of the n bytes are nonzero, else zero.
174 bool nonempty(const void * data, int size);
175
176 // needed to fix glibc bug
177 void FixGlibcTimeZoneBug();
178
179 // convert time in msec to a text string
180 void MsecToText(unsigned int msec, char *txt);
181
182 // Format integer with thousands separator
183 const char * format_with_thousands_sep(char * str, int strsize, uint64_t val,
184 const char * thousands_sep = 0);
185
186 // Format capacity with SI prefixes
187 const char * format_capacity(char * str, int strsize, uint64_t val,
188 const char * decimal_point = 0);
189
190 // Wrapper class for a raw data buffer
191 class raw_buffer
192 {
193 public:
194 explicit raw_buffer(unsigned sz, unsigned char val = 0)
195 : m_data(new unsigned char[sz]),
196 m_size(sz)
197 { memset(m_data, val, m_size); }
198
199 ~raw_buffer()
200 { delete [] m_data; }
201
202 unsigned size() const
203 { return m_size; }
204
205 unsigned char * data()
206 { return m_data; }
207 const unsigned char * data() const
208 { return m_data; }
209
210 private:
211 unsigned char * m_data;
212 unsigned m_size;
213
214 raw_buffer(const raw_buffer &);
215 void operator=(const raw_buffer &);
216 };
217
218 /// Wrapper class for FILE *.
219 class stdio_file
220 {
221 public:
222 explicit stdio_file(FILE * f = 0, bool owner = false)
223 : m_file(f), m_owner(owner) { }
224
225 stdio_file(const char * name, const char * mode)
226 : m_file(fopen(name, mode)), m_owner(true) { }
227
228 ~stdio_file()
229 {
230 if (m_file && m_owner)
231 fclose(m_file);
232 }
233
234 bool open(const char * name, const char * mode)
235 {
236 m_file = fopen(name, mode);
237 m_owner = true;
238 return !!m_file;
239 }
240
241 void open(FILE * f, bool owner = false)
242 {
243 m_file = f;
244 m_owner = owner;
245 }
246
247 bool close()
248 {
249 if (!m_file)
250 return true;
251 bool ok = !ferror(m_file);
252 if (fclose(m_file))
253 ok = false;
254 m_file = 0;
255 return ok;
256 }
257
258 operator FILE * ()
259 { return m_file; }
260
261 bool operator!() const
262 { return !m_file; }
263
264 private:
265 FILE * m_file;
266 bool m_owner;
267
268 stdio_file(const stdio_file &);
269 void operator=(const stdio_file &);
270 };
271
272 /// Wrapper class for regex(3).
273 /// Supports copy & assignment and is compatible with STL containers.
274 class regular_expression
275 {
276 public:
277 // Construction & assignment
278 regular_expression();
279
280 regular_expression(const char * pattern, int flags,
281 bool throw_on_error = true);
282
283 ~regular_expression();
284
285 regular_expression(const regular_expression & x);
286
287 regular_expression & operator=(const regular_expression & x);
288
289 /// Set and compile new pattern, return false on error.
290 bool compile(const char * pattern, int flags);
291
292 // Get pattern from last compile().
293 const char * get_pattern() const
294 { return m_pattern.c_str(); }
295
296 /// Get error message from last compile().
297 const char * get_errmsg() const
298 { return m_errmsg.c_str(); }
299
300 // Return true if pattern is not set or bad.
301 bool empty() const
302 { return (m_pattern.empty() || !m_errmsg.empty()); }
303
304 /// Return true if substring matches pattern
305 bool match(const char * str, int flags = 0) const
306 { return !regexec(&m_regex_buf, str, 0, (regmatch_t*)0, flags); }
307
308 /// Return true if full string matches pattern
309 bool full_match(const char * str, int flags = 0) const
310 {
311 regmatch_t range;
312 return ( !regexec(&m_regex_buf, str, 1, &range, flags)
313 && range.rm_so == 0 && range.rm_eo == (int)strlen(str));
314 }
315
316 /// Return true if substring matches pattern, fill regmatch_t array.
317 bool execute(const char * str, unsigned nmatch, regmatch_t * pmatch, int flags = 0) const
318 { return !regexec(&m_regex_buf, str, nmatch, pmatch, flags); }
319
320 private:
321 std::string m_pattern;
322 int m_flags;
323 regex_t m_regex_buf;
324 std::string m_errmsg;
325
326 void free_buf();
327 void copy(const regular_expression & x);
328 bool compile();
329 };
330
331 #ifdef _WIN32
332 // Get exe directory
333 //(implemented in os_win32.cpp)
334 std::string get_exe_dir();
335 #endif
336
337
338 #ifdef OLD_INTERFACE
339 // remaining controller types in old interface modules
340 #define CONTROLLER_UNKNOWN 0x00
341 #define CONTROLLER_ATA 0x01
342 #define CONTROLLER_SCSI 0x02
343 #endif
344
345 #endif
346