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