]>
git.proxmox.com Git - mirror_smartmontools-debian.git/blob - utility.h
4 * Home page of code is: http://www.smartmontools.org
6 * Copyright (C) 2002-11 Bruce Allen
7 * Copyright (C) 2008-16 Christian Franke
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, see <http://www.gnu.org/licenses/>.
18 * This code was originally developed as a Senior Thesis by Michael Cornwell
19 * at the Concurrent Systems Laboratory (now part of the Storage Systems
20 * Research Center), Jack Baskin School of Engineering, University of
21 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
28 #define UTILITY_H_CVSID "$Id: utility.h 4309 2016-04-24 14:59:15Z chrfranke $"
31 #include <sys/types.h> // for regex.h (according to POSIX)
39 #define __attribute_format_printf(x, y) /**/
40 #elif defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO
41 // Check format of __mingw_*printf() instead of MSVCRT.DLL:*printf()
42 #define __attribute_format_printf(x, y) __attribute__((format (gnu_printf, x, y)))
43 #define HAVE_WORKING_SNPRINTF 1
45 #define __attribute_format_printf(x, y) __attribute__((format (printf, x, y)))
48 // Make version information string
49 std::string
format_version_info(const char * prog_name
, bool full
= false);
51 // return (v)sprintf() formated std::string
52 std::string
strprintf(const char * fmt
, ...)
53 __attribute_format_printf(1, 2);
54 std::string
vstrprintf(const char * fmt
, va_list ap
);
56 // Return true if STR starts with PREFIX
57 inline bool str_starts_with(const char * str
, const char * prefix
)
58 { return !strncmp(str
, prefix
, strlen(prefix
)); }
60 inline bool str_starts_with(const std::string
& str
, const char * prefix
)
61 { return !strncmp(str
.c_str(), prefix
, strlen(prefix
)); }
63 #ifndef HAVE_WORKING_SNPRINTF
64 // Substitute by safe replacement functions
65 int safe_snprintf(char *buf
, int size
, const char *fmt
, ...)
66 __attribute_format_printf(3, 4);
67 int safe_vsnprintf(char *buf
, int size
, const char *fmt
, va_list ap
);
68 #define snprintf safe_snprintf
69 #define vsnprintf safe_vsnprintf
73 // Replacement for missing strtoull() (Linux with libc < 6, MSVC)
74 uint64_t strtoull(const char * p
, char * * endp
, int base
);
77 // Utility function prints current date and time and timezone into a
78 // character buffer of length>=64. All the fuss is needed to get the
79 // right timezone info (sigh).
80 #define DATEANDEPOCHLEN 64
81 void dateandtimezone(char *buffer
);
82 // Same, but for time defined by epoch tval
83 void dateandtimezoneepoch(char *buffer
, time_t tval
);
85 // like printf() except that we can control it better. Note --
86 // although the prototype is given here in utility.h, the function
87 // itself is defined differently in smartctl and smartd. So the
88 // function definition(s) are in smartd.c and in smartctl.c.
89 void pout(const char *fmt
, ...)
90 __attribute_format_printf(1, 2);
92 // replacement for perror() with redirected output.
93 void syserror(const char *message
);
95 // Function for processing -t selective... option in smartctl
96 int split_selective_arg(char *s
, uint64_t *start
, uint64_t *stop
, int *mode
);
98 // Replacement for exit(status)
99 // (exit is not compatible with C++ destructors)
100 #define EXIT(status) { throw (int)(status); }
102 // Compile time check of byte ordering
103 // (inline const function allows compiler to remove dead code)
104 inline bool isbigendian()
106 #ifdef WORDS_BIGENDIAN
113 // Runtime check of ./configure result, throws on error.
116 // This value follows the peripheral device type value as defined in
117 // SCSI Primary Commands, ANSI INCITS 301:1997. It is also used in
118 // the ATA standard for packet devices to define the device type.
119 const char *packetdevicetype(int type
);
121 // returns true if any of the n bytes are nonzero, else zero.
122 bool nonempty(const void * data
, int size
);
124 // needed to fix glibc bug
125 void FixGlibcTimeZoneBug();
127 // Copy not null terminated char array to null terminated string.
128 // Replace non-ascii characters. Remove leading and trailing blanks.
129 const char * format_char_array(char * str
, int strsize
, const char * chr
, int chrsize
);
131 // Version for fixed size buffers.
132 template<size_t STRSIZE
, size_t CHRSIZE
>
133 inline const char * format_char_array(char (& str
)[STRSIZE
], const char (& chr
)[CHRSIZE
])
134 { return format_char_array(str
, (int)STRSIZE
, chr
, (int)CHRSIZE
); }
136 // Format integer with thousands separator
137 const char * format_with_thousands_sep(char * str
, int strsize
, uint64_t val
,
138 const char * thousands_sep
= 0);
140 // Format capacity with SI prefixes
141 const char * format_capacity(char * str
, int strsize
, uint64_t val
,
142 const char * decimal_point
= 0);
144 // Wrapper class for a raw data buffer
148 explicit raw_buffer(unsigned sz
, unsigned char val
= 0)
149 : m_data(new unsigned char[sz
]),
151 { memset(m_data
, val
, m_size
); }
154 { delete [] m_data
; }
156 unsigned size() const
159 unsigned char * data()
161 const unsigned char * data() const
165 unsigned char * m_data
;
168 raw_buffer(const raw_buffer
&);
169 void operator=(const raw_buffer
&);
172 /// Wrapper class for FILE *.
176 explicit stdio_file(FILE * f
= 0, bool owner
= false)
177 : m_file(f
), m_owner(owner
) { }
179 stdio_file(const char * name
, const char * mode
)
180 : m_file(fopen(name
, mode
)), m_owner(true) { }
184 if (m_file
&& m_owner
)
188 bool open(const char * name
, const char * mode
)
190 if (m_file
&& m_owner
)
192 m_file
= fopen(name
, mode
);
197 void open(FILE * f
, bool owner
= false)
199 if (m_file
&& m_owner
)
209 bool ok
= !ferror(m_file
);
219 bool operator!() const
226 stdio_file(const stdio_file
&);
227 void operator=(const stdio_file
&);
230 /// Wrapper class for regex(3).
231 /// Supports copy & assignment and is compatible with STL containers.
232 class regular_expression
235 // Construction & assignment
236 regular_expression();
238 regular_expression(const char * pattern
, int flags
,
239 bool throw_on_error
= true);
241 ~regular_expression();
243 regular_expression(const regular_expression
& x
);
245 regular_expression
& operator=(const regular_expression
& x
);
247 /// Set and compile new pattern, return false on error.
248 bool compile(const char * pattern
, int flags
);
250 // Get pattern from last compile().
251 const char * get_pattern() const
252 { return m_pattern
.c_str(); }
254 /// Get error message from last compile().
255 const char * get_errmsg() const
256 { return m_errmsg
.c_str(); }
258 // Return true if pattern is not set or bad.
260 { return (m_pattern
.empty() || !m_errmsg
.empty()); }
262 /// Return true if substring matches pattern
263 bool match(const char * str
, int flags
= 0) const
264 { return !regexec(&m_regex_buf
, str
, 0, (regmatch_t
*)0, flags
); }
266 /// Return true if full string matches pattern
267 bool full_match(const char * str
, int flags
= 0) const
270 return ( !regexec(&m_regex_buf
, str
, 1, &range
, flags
)
271 && range
.rm_so
== 0 && range
.rm_eo
== (int)strlen(str
));
274 /// Return true if substring matches pattern, fill regmatch_t array.
275 bool execute(const char * str
, unsigned nmatch
, regmatch_t
* pmatch
, int flags
= 0) const
276 { return !regexec(&m_regex_buf
, str
, nmatch
, pmatch
, flags
); }
279 std::string m_pattern
;
282 std::string m_errmsg
;
285 void copy(const regular_expression
& x
);
291 //(implemented in os_win32.cpp)
292 std::string
get_exe_dir();
297 // remaining controller types in old interface modules
298 #define CONTROLLER_UNKNOWN 0x00
299 #define CONTROLLER_ATA 0x01
300 #define CONTROLLER_SCSI 0x02