]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - utility.h
Imported Upstream version 5.40+svn3296
[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 3285 2011-03-04 22:08:49Z 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 #if !defined(__GNUC__) && !defined(__attribute__)
40 #define __attribute__(x) /**/
41 #endif
42
43 // Make version information string
44 std::string format_version_info(const char * prog_name, bool full = false);
45
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);
50
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
58 #endif
59
60 #ifndef HAVE_STRTOULL
61 // Replacement for missing strtoull() (Linux with libc < 6, MSVC)
62 uint64_t strtoull(const char * p, char * * endp, int base);
63 #endif
64
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);
72
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)));
79
80 // replacement for perror() with redirected output.
81 void syserror(const char *message);
82
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);
87
88 // Function for processing -t selective... option in smartctl
89 int split_selective_arg(char *s, uint64_t *start, uint64_t *stop, int *mode);
90
91
92 // Guess device type (ata or scsi) based on device name
93 // Guessing will now use Controller Type defines below
94
95 // Moved to C++ interface
96 //int guess_device_type(const char * dev_name);
97
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);
102
103 // Replacement for exit(status)
104 // (exit is not compatible with C++ destructors)
105 #define EXIT(status) { throw (int)(status); }
106
107
108 #ifdef OLD_INTERFACE
109
110 // replacement for calloc() that tracks memory usage
111 void *Calloc(size_t nmemb, size_t size);
112
113 // Utility function to free memory
114 void *FreeNonZero1(void* address, int size, int whatline, const char* file);
115
116 // Typesafe version of above
117 template <class T>
118 inline T * FreeNonZero(T * address, int size, int whatline, const char* file)
119 { return (T *)FreeNonZero1((void *)address, size, whatline, file); }
120
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);
125
126 // To help with memory checking. Use when it is known that address is
127 // NOT null.
128 void *CheckFree1(void *address, int whatline, const char* file);
129
130 // Typesafe version of above
131 template <class T>
132 inline T * CheckFree(T * address, int whatline, const char* file)
133 { return (T *)CheckFree1((void *)address, whatline, file); }
134
135 #endif // OLD_INTERFACE
136
137 // Compile time check of byte ordering
138 // (inline const function allows compiler to remove dead code)
139 inline bool isbigendian()
140 {
141 #ifdef WORDS_BIGENDIAN
142 return true;
143 #else
144 return false;
145 #endif
146 }
147
148 // Runtime check of byte ordering, throws if different from isbigendian().
149 void check_endianness();
150
151 // This value follows the peripheral device type value as defined in
152 // SCSI Primary Commands, ANSI INCITS 301:1997. It is also used in
153 // the ATA standard for packet devices to define the device type.
154 const char *packetdevicetype(int type);
155
156 // Moved to C++ interface
157 //int deviceopen(const char *pathname, char *type);
158
159 //int deviceclose(int fd);
160
161 // Optional functions of os_*.c
162 #ifdef HAVE_GET_OS_VERSION_STR
163 // Return build host and OS version as static string
164 //const char * get_os_version_str(void);
165 #endif
166
167 // returns true if any of the n bytes are nonzero, else zero.
168 bool nonempty(const void * data, int size);
169
170 // needed to fix glibc bug
171 void FixGlibcTimeZoneBug();
172
173 // convert time in msec to a text string
174 void MsecToText(unsigned int msec, char *txt);
175
176 // Wrapper class for a raw data buffer
177 class raw_buffer
178 {
179 public:
180 explicit raw_buffer(unsigned sz, unsigned char val = 0)
181 : m_data(new unsigned char[sz]),
182 m_size(sz)
183 { memset(m_data, val, m_size); }
184
185 ~raw_buffer()
186 { delete [] m_data; }
187
188 unsigned size() const
189 { return m_size; }
190
191 unsigned char * data()
192 { return m_data; }
193 const unsigned char * data() const
194 { return m_data; }
195
196 private:
197 unsigned char * m_data;
198 unsigned m_size;
199
200 raw_buffer(const raw_buffer &);
201 void operator=(const raw_buffer &);
202 };
203
204 /// Wrapper class for FILE *.
205 class stdio_file
206 {
207 public:
208 explicit stdio_file(FILE * f = 0, bool owner = false)
209 : m_file(f), m_owner(owner) { }
210
211 stdio_file(const char * name, const char * mode)
212 : m_file(fopen(name, mode)), m_owner(true) { }
213
214 ~stdio_file()
215 {
216 if (m_file && m_owner)
217 fclose(m_file);
218 }
219
220 bool open(const char * name, const char * mode)
221 {
222 m_file = fopen(name, mode);
223 m_owner = true;
224 return !!m_file;
225 }
226
227 void open(FILE * f, bool owner = false)
228 {
229 m_file = f;
230 m_owner = owner;
231 }
232
233 bool close()
234 {
235 if (!m_file)
236 return true;
237 bool ok = !ferror(m_file);
238 if (fclose(m_file))
239 ok = false;
240 m_file = 0;
241 return ok;
242 }
243
244 operator FILE * ()
245 { return m_file; }
246
247 bool operator!() const
248 { return !m_file; }
249
250 private:
251 FILE * m_file;
252 bool m_owner;
253
254 stdio_file(const stdio_file &);
255 void operator=(const stdio_file &);
256 };
257
258 /// Wrapper class for regex(3).
259 /// Supports copy & assignment and is compatible with STL containers.
260 class regular_expression
261 {
262 public:
263 // Construction & assignment
264 regular_expression();
265
266 regular_expression(const char * pattern, int flags,
267 bool throw_on_error = true);
268
269 ~regular_expression();
270
271 regular_expression(const regular_expression & x);
272
273 regular_expression & operator=(const regular_expression & x);
274
275 /// Set and compile new pattern, return false on error.
276 bool compile(const char * pattern, int flags);
277
278 // Get pattern from last compile().
279 const char * get_pattern() const
280 { return m_pattern.c_str(); }
281
282 /// Get error message from last compile().
283 const char * get_errmsg() const
284 { return m_errmsg.c_str(); }
285
286 // Return true if pattern is not set or bad.
287 bool empty() const
288 { return (m_pattern.empty() || !m_errmsg.empty()); }
289
290 /// Return true if substring matches pattern
291 bool match(const char * str, int flags = 0) const
292 { return !regexec(&m_regex_buf, str, 0, (regmatch_t*)0, flags); }
293
294 /// Return true if full string matches pattern
295 bool full_match(const char * str, int flags = 0) const
296 {
297 regmatch_t range;
298 return ( !regexec(&m_regex_buf, str, 1, &range, flags)
299 && range.rm_so == 0 && range.rm_eo == (int)strlen(str));
300 }
301
302 /// Return true if substring matches pattern, fill regmatch_t array.
303 bool execute(const char * str, unsigned nmatch, regmatch_t * pmatch, int flags = 0) const
304 { return !regexec(&m_regex_buf, str, nmatch, pmatch, flags); }
305
306 private:
307 std::string m_pattern;
308 int m_flags;
309 regex_t m_regex_buf;
310 std::string m_errmsg;
311
312 void free_buf();
313 void copy(const regular_expression & x);
314 bool compile();
315 };
316
317 #ifdef _WIN32
318 // Get exe directory
319 //(implemented in os_win32.cpp)
320 std::string get_exe_dir();
321 #endif
322
323
324 #ifdef OLD_INTERFACE
325 // remaining controller types in old interface modules
326 #define CONTROLLER_UNKNOWN 0x00
327 #define CONTROLLER_ATA 0x01
328 #define CONTROLLER_SCSI 0x02
329 #endif
330
331 #endif
332