/*
* utility.h
*
- * Home page of code is: http://smartmontools.sourceforge.net
+ * Home page of code is: http://www.smartmontools.org
*
- * Copyright (C) 2002-7 Bruce Allen <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2002-11 Bruce Allen
+ * Copyright (C) 2008-18 Christian Franke
* Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- *
- * You should have received a copy of the GNU General Public License
- * (for example COPYING); if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * This code was originally developed as a Senior Thesis by Michael Cornwell
- * at the Concurrent Systems Laboratory (now part of the Storage Systems
- * Research Center), Jack Baskin School of Engineering, University of
- * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
- *
+ * SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef UTILITY_H_
#define UTILITY_H_
-#define UTILITY_H_CVSID "$Id: utility.h,v 1.50 2007/07/26 20:58:50 chrfranke Exp $\n"
+#define UTILITY_H_CVSID "$Id: utility.h 4848 2018-12-05 18:30:46Z chrfranke $"
+#include <float.h> // *DBL_MANT_DIG
#include <time.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <string>
+
#include <sys/types.h> // for regex.h (according to POSIX)
+#ifdef WITH_CXX11_REGEX
+#include <regex>
+#else
#include <regex.h>
+#endif
+
+#ifndef __GNUC__
+#define __attribute_format_printf(x, y) /**/
+#elif defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO
+// Check format of __mingw_*printf() instead of MSVCRT.DLL:*printf()
+#define __attribute_format_printf(x, y) __attribute__((format (gnu_printf, x, y)))
+#define HAVE_WORKING_SNPRINTF 1
+#else
+#define __attribute_format_printf(x, y) __attribute__((format (printf, x, y)))
+#endif
+
+// Make version information string
+std::string format_version_info(const char * prog_name, bool full = false);
+
+// return (v)sprintf() formatted std::string
+std::string strprintf(const char * fmt, ...)
+ __attribute_format_printf(1, 2);
+std::string vstrprintf(const char * fmt, va_list ap);
+
+// Return true if STR starts with PREFIX
+inline bool str_starts_with(const char * str, const char * prefix)
+ { return !strncmp(str, prefix, strlen(prefix)); }
+
+inline bool str_starts_with(const std::string & str, const char * prefix)
+ { return !strncmp(str.c_str(), prefix, strlen(prefix)); }
#ifndef HAVE_WORKING_SNPRINTF
// Substitute by safe replacement functions
-#include <stdarg.h>
-int safe_snprintf(char *buf, int size, const char *fmt, ...);
+int safe_snprintf(char *buf, int size, const char *fmt, ...)
+ __attribute_format_printf(3, 4);
int safe_vsnprintf(char *buf, int size, const char *fmt, va_list ap);
#define snprintf safe_snprintf
#define vsnprintf safe_vsnprintf
#endif
-// Utility function prints current date and time and timezone into a
-// character buffer of length>=64. All the fuss is needed to get the
+// Utility function prints date and time and timezone into a character
+// buffer of length 64. All the fuss is needed to get the
// right timezone info (sigh).
#define DATEANDEPOCHLEN 64
-void dateandtimezone(char *buffer);
-// Same, but for time defined by epoch tval
-void dateandtimezoneepoch(char *buffer, time_t tval);
-
-// utility function for printing out CVS strings
-#define CVSMAXLEN 1024
-void printone(char *block, const char *cvsid);
+void dateandtimezoneepoch(char (& buffer)[DATEANDEPOCHLEN], time_t tval);
// like printf() except that we can control it better. Note --
// although the prototype is given here in utility.h, the function
// itself is defined differently in smartctl and smartd. So the
// function definition(s) are in smartd.c and in smartctl.c.
-#ifndef __GNUC__
-#define __attribute__(x) /* nothing */
-#endif
void pout(const char *fmt, ...)
- __attribute__ ((format (printf, 1, 2)));
+ __attribute_format_printf(1, 2);
// replacement for perror() with redirected output.
void syserror(const char *message);
-// Prints a warning message for a failed regular expression compilation from
-// regcomp().
-void printregexwarning(int errcode, regex_t *compiled);
-
-// A wrapper for regcomp(). Returns zero for success, non-zero otherwise.
-int compileregex(regex_t *compiled, const char *pattern, int cflags);
-
-// Function for processing -r option in smartctl and smartd
-int split_report_arg(char *s, int *i);
-// Function for processing -c option in smartctl and smartd
-int split_report_arg2(char *s, int *i);
-
-// Possible values for smartselectivemode
-#define SEL_RANGE 0 // MIN-MAX
-#define SEL_REDO 1 // redo this
-#define SEL_NEXT 2 // do next range
-#define SEL_CONT 3 // redo or next depending of last test status
// Function for processing -t selective... option in smartctl
int split_selective_arg(char *s, uint64_t *start, uint64_t *stop, int *mode);
+// Compile time check of byte ordering
+// (inline const function allows compiler to remove dead code)
+inline bool isbigendian()
+{
+#ifdef WORDS_BIGENDIAN
+ return true;
+#else
+ return false;
+#endif
+}
+
+void swap2(char *location);
+void swap4(char *location);
+void swap8(char *location);
+// Typesafe variants using overloading
+inline void swapx(unsigned short * p)
+ { swap2((char*)p); }
+inline void swapx(unsigned int * p)
+ { swap4((char*)p); }
+inline void swapx(uint64_t * p)
+ { swap8((char*)p); }
+
+// Runtime check of ./configure result, throws on error.
+void check_config();
-// Guess device type (ata or scsi) based on device name
-// Guessing will now use Controller Type defines below
-
-int guess_device_type(const char * dev_name);
-
-// Create and return the list of devices to probe automatically
-// if the DEVICESCAN option is in the smartd config file
-int make_device_names (char ***devlist, const char* name);
-
-
-#define EXIT(x) { exitstatus = (x); exit((x)); }
+// This value follows the peripheral device type value as defined in
+// SCSI Primary Commands, ANSI INCITS 301:1997. It is also used in
+// the ATA standard for packet devices to define the device type.
+const char *packetdevicetype(int type);
-// replacement for calloc() that tracks memory usage
-void *Calloc(size_t nmemb, size_t size);
+// returns true if any of the n bytes are nonzero, else zero.
+bool nonempty(const void * data, int size);
-// Utility function to free memory
-void *FreeNonZero1(void* address, int size, int whatline, const char* file);
+// needed to fix glibc bug
+void FixGlibcTimeZoneBug();
-// Typesafe version of above
-template <class T>
-inline T * FreeNonZero(T * address, int size, int whatline, const char* file)
- { return (T *)FreeNonZero1((void *)address, size, whatline, file); }
+// Copy not null terminated char array to null terminated string.
+// Replace non-ascii characters. Remove leading and trailing blanks.
+const char * format_char_array(char * str, int strsize, const char * chr, int chrsize);
+
+// Version for fixed size buffers.
+template<size_t STRSIZE, size_t CHRSIZE>
+inline const char * format_char_array(char (& str)[STRSIZE], const char (& chr)[CHRSIZE])
+ { return format_char_array(str, (int)STRSIZE, chr, (int)CHRSIZE); }
+
+// Format integer with thousands separator
+const char * format_with_thousands_sep(char * str, int strsize, uint64_t val,
+ const char * thousands_sep = 0);
+
+// Format capacity with SI prefixes
+const char * format_capacity(char * str, int strsize, uint64_t val,
+ const char * decimal_point = 0);
+
+// Wrapper class for a raw data buffer
+class raw_buffer
+{
+public:
+ explicit raw_buffer(unsigned sz, unsigned char val = 0)
+ : m_data(new unsigned char[sz]),
+ m_size(sz)
+ { memset(m_data, val, m_size); }
+
+ ~raw_buffer()
+ { delete [] m_data; }
+
+ unsigned size() const
+ { return m_size; }
+
+ unsigned char * data()
+ { return m_data; }
+ const unsigned char * data() const
+ { return m_data; }
+
+private:
+ unsigned char * m_data;
+ unsigned m_size;
+
+ raw_buffer(const raw_buffer &);
+ void operator=(const raw_buffer &);
+};
+
+/// Wrapper class for FILE *.
+class stdio_file
+{
+public:
+ explicit stdio_file(FILE * f = 0, bool owner = false)
+ : m_file(f), m_owner(owner) { }
+
+ stdio_file(const char * name, const char * mode)
+ : m_file(fopen(name, mode)), m_owner(true) { }
+
+ ~stdio_file()
+ {
+ if (m_file && m_owner)
+ fclose(m_file);
+ }
+
+ bool open(const char * name, const char * mode)
+ {
+ if (m_file && m_owner)
+ fclose(m_file);
+ m_file = fopen(name, mode);
+ m_owner = true;
+ return !!m_file;
+ }
+
+ void open(FILE * f, bool owner = false)
+ {
+ if (m_file && m_owner)
+ fclose(m_file);
+ m_file = f;
+ m_owner = owner;
+ }
+
+ bool close()
+ {
+ if (!m_file)
+ return true;
+ bool ok = !ferror(m_file);
+ if (fclose(m_file))
+ ok = false;
+ m_file = 0;
+ return ok;
+ }
+
+ operator FILE * ()
+ { return m_file; }
+
+ bool operator!() const
+ { return !m_file; }
+
+private:
+ FILE * m_file;
+ bool m_owner;
+
+ stdio_file(const stdio_file &);
+ void operator=(const stdio_file &);
+};
+
+/// Wrapper class for POSIX regex(3) or std::regex
+/// Supports copy & assignment and is compatible with STL containers.
+class regular_expression
+{
+public:
+ // Construction & assignment
+#ifdef WITH_CXX11_REGEX
+ regular_expression() = default;
+
+#else
+ regular_expression();
+
+ ~regular_expression();
+
+ regular_expression(const regular_expression & x);
+
+ regular_expression & operator=(const regular_expression & x);
+#endif
-// A custom version of strdup() that keeps track of how much memory is
-// being allocated. If mustexist is set, it also throws an error if we
-// try to duplicate a NULL string.
-char *CustomStrDup(const char *ptr, int mustexist, int whatline, const char* file);
+ /// Construct with pattern, throw on error.
+ explicit regular_expression(const char * pattern);
-// To help with memory checking. Use when it is known that address is
-// NOT null.
-void *CheckFree1(void *address, int whatline, const char* file);
+ /// Set and compile new pattern, return false on error.
+ bool compile(const char * pattern);
-// Typesafe version of above
-template <class T>
-inline T * CheckFree(T * address, int whatline, const char* file)
- { return (T *)CheckFree1((void *)address, whatline, file); }
+ // Get pattern from last compile().
+ const char * get_pattern() const
+ { return m_pattern.c_str(); }
-// This function prints either to stdout or to the syslog as needed
+ /// Get error message from last compile().
+ const char * get_errmsg() const
+ { return m_errmsg.c_str(); }
-// [From GLIBC Manual: Since the prototype doesn't specify types for
-// optional arguments, in a call to a variadic function the default
-// argument promotions are performed on the optional argument
-// values. This means the objects of type char or short int (whether
-// signed or not) are promoted to either int or unsigned int, as
-// appropriate.]
-void PrintOut(int priority, const char *fmt, ...) __attribute__ ((format(printf, 2, 3)));
+ // Return true if pattern is not set or bad.
+ bool empty() const
+ { return (m_pattern.empty() || !m_errmsg.empty()); }
-// run time, determine byte ordering
-int isbigendian();
+ /// Return true if full string matches pattern
+ bool full_match(const char * str) const;
-// This value follows the peripheral device type value as defined in
-// SCSI Primary Commands, ANSI INCITS 301:1997. It is also used in
-// the ATA standard for packet devices to define the device type.
-const char *packetdevicetype(int type);
+#ifdef WITH_CXX11_REGEX
+ struct match_range { int rm_so, rm_eo; };
+#else
+ typedef regmatch_t match_range;
+#endif
-int deviceopen(const char *pathname, char *type);
+ /// Return true if substring matches pattern, fill match_range array.
+ bool execute(const char * str, unsigned nmatch, match_range * pmatch) const;
-int deviceclose(int fd);
+private:
+ std::string m_pattern;
+ std::string m_errmsg;
-// Optional functions of os_*.c
-#ifdef HAVE_GET_OS_VERSION_STR
-// Return build host and OS version as static string
-const char * get_os_version_str(void);
+#ifdef WITH_CXX11_REGEX
+ std::regex m_regex;
+#else
+ regex_t m_regex_buf;
+ void free_buf();
+ void copy_buf(const regular_expression & x);
#endif
-// returns 1 if any of the n bytes are nonzero, else zero.
-int nonempty(unsigned char *testarea,int n);
+ bool compile();
+};
-// needed to fix glibc bug
-void FixGlibcTimeZoneBug();
+// 128-bit unsigned integer to string conversion.
+// Provides full integer precision if compiler supports '__int128'.
+// Otherwise precision depends on supported floating point data types.
-// convert time in msec to a text string
-void MsecToText(unsigned int msec, char *txt);
-
-// Exit codes
-#define EXIT_BADCMD 1 // command line did not parse
-#define EXIT_BADCONF 2 // syntax error in config file
-#define EXIT_STARTUP 3 // problem forking daemon
-#define EXIT_PID 4 // problem creating pid file
-#define EXIT_NOCONF 5 // config file does not exist
-#define EXIT_READCONF 6 // config file exists but cannot be read
+#if defined(HAVE_LONG_DOUBLE_WIDER) && \
+ (!defined(__MINGW32__) || defined(__USE_MINGW_ANSI_STDIO))
+ // MinGW 'long double' type does not work with MSVCRT *printf()
+#define HAVE_LONG_DOUBLE_WIDER_PRINTF 1
+#else
+#undef HAVE_LONG_DOUBLE_WIDER_PRINTF
+#endif
-#define EXIT_NOMEM 8 // out of memory
-#define EXIT_BADCODE 10 // internal error - should NEVER happen
+// Return #bits precision provided by uint128_hilo_to_str().
+inline int uint128_to_str_precision_bits()
+{
+#if defined(HAVE___INT128)
+ return 128;
+#elif defined(HAVE_LONG_DOUBLE_WIDER_PRINTF)
+ return LDBL_MANT_DIG;
+#else
+ return DBL_MANT_DIG;
+#endif
+}
-#define EXIT_BADDEV 16 // we can't monitor this device
-#define EXIT_NODEV 17 // no devices to monitor
+// Convert 128-bit unsigned integer provided as two 64-bit halves to a string.
+const char * uint128_hilo_to_str(char * str, int strsize, uint64_t value_hi, uint64_t value_lo);
-#define EXIT_SIGNAL 254 // abort on signal
+// Version for fixed size buffers.
+template <size_t SIZE>
+inline const char * uint128_hilo_to_str(char (& str)[SIZE], uint64_t value_hi, uint64_t value_lo)
+ { return uint128_hilo_to_str(str, (int)SIZE, value_hi, value_lo); }
+#ifdef _WIN32
+// Get exe directory
+//(implemented in os_win32.cpp)
+std::string get_exe_dir();
+#endif
-// macros to control printing
-#define PRINT_ON(control) {if (control->printing_switchable) control->dont_print=0;}
-#define PRINT_OFF(control) {if (control->printing_switchable) control->dont_print=1;}
-// possible values for controller_type in extern.h
+#ifdef OLD_INTERFACE
+// remaining controller types in old interface modules
#define CONTROLLER_UNKNOWN 0x00
#define CONTROLLER_ATA 0x01
#define CONTROLLER_SCSI 0x02
-#define CONTROLLER_3WARE 0x03 // set by -d option, but converted to one of three types below
-#define CONTROLLER_3WARE_678K 0x04 // NOT set by guess_device_type()
-#define CONTROLLER_3WARE_9000_CHAR 0x05 // set by guess_device_type()
-#define CONTROLLER_3WARE_678K_CHAR 0x06 // set by guess_device_type()
-#define CONTROLLER_MARVELL_SATA 0x07 // SATA drives behind Marvell controllers
-#define CONTROLLER_SAT 0x08 // SATA device behind a SCSI ATA Translation (SAT) layer
-#define CONTROLLER_HPT 0x09 // SATA drives behind HighPoint Raid controllers
-#define CONTROLLER_CCISS 0x10 // CCISS controller
-#define CONTROLLER_PARSEDEV 0x11 // "smartctl -r ataioctl,2 ..." output parser pseudo-device
+#endif
#endif