diff --git a/smartmontools/CHANGELOG b/smartmontools/CHANGELOG index 02806084a70edc45ad47db33c591e34c2257deca..d85fbb282c7722b0177e9ac0a2aa1fda31c14527 100644 --- a/smartmontools/CHANGELOG +++ b/smartmontools/CHANGELOG @@ -43,6 +43,10 @@ NOTES FOR FUTURE RELEASES: see TODO file. <DEVELOPERS: ADDITIONS TO THE CHANGE LOG GO JUST BELOW HERE, PLEASE> + [CF] Return info strings from 'smart_interface::get_*()' functions as + 'std::string' instead of 'const char *'. Static buffers are no + longer needed. + [SZ] FreeBSD: Fix highpoint type detection and ioctl failed for parameter error. diff --git a/smartmontools/dev_interface.cpp b/smartmontools/dev_interface.cpp index 5a048e9dda5582c4af0697c9bf08106eb4a30ada..ae7fefe577ed693729733492d92efbf0b394d0a9 100644 --- a/smartmontools/dev_interface.cpp +++ b/smartmontools/dev_interface.cpp @@ -217,29 +217,27 @@ void tunnelled_device_base::release(const smart_device * dev) // Pointer to (usually singleton) interface object returned by ::smi() smart_interface * smart_interface::s_instance; -const char * smart_interface::get_os_version_str() +std::string smart_interface::get_os_version_str() { return SMARTMONTOOLS_BUILD_HOST; } -const char * smart_interface::get_valid_dev_types_str() +std::string smart_interface::get_valid_dev_types_str() { - static std::string buf; - if (!buf.empty()) - return buf.c_str(); // default - buf = "ata, scsi, sat[,N][+TYPE], usbcypress[,X], usbjmicron[,x][,N], usbsunplus"; + std::string s = + "ata, scsi, sat[,N][+TYPE], usbcypress[,X], usbjmicron[,x][,N], usbsunplus"; // append custom - const char * add = get_valid_custom_dev_types_str(); - if (!add || !*add) - return buf.c_str(); - buf += ", "; buf += add; - return buf.c_str(); + std::string s2 = get_valid_custom_dev_types_str(); + if (!s2.empty()) { + s += ", "; s += s2; + } + return s; } -const char * smart_interface::get_app_examples(const char * /*appname*/) +std::string smart_interface::get_app_examples(const char * /*appname*/) { - return 0; + return ""; } void smart_interface::set_err(int no, const char * msg, ...) @@ -342,7 +340,7 @@ smart_device * smart_interface::get_custom_smart_device(const char * /*name*/, c return 0; } -const char * smart_interface::get_valid_custom_dev_types_str() +std::string smart_interface::get_valid_custom_dev_types_str() { - return 0; + return ""; } diff --git a/smartmontools/dev_interface.h b/smartmontools/dev_interface.h index 7550c3aa3476ae6e42f2bcbc93a7d92f7db4ab4a..5d932a8a8d5e230eb83dce2af221061eea75b187 100644 --- a/smartmontools/dev_interface.h +++ b/smartmontools/dev_interface.h @@ -18,7 +18,7 @@ #ifndef DEV_INTERFACE_H #define DEV_INTERFACE_H -#define DEV_INTERFACE_H_CVSID "$Id: dev_interface.h,v 1.9 2009/03/12 20:31:12 chrfranke Exp $\n" +#define DEV_INTERFACE_H_CVSID "$Id$\n" #include <stdarg.h> #include <string> @@ -590,20 +590,21 @@ public: virtual ~smart_interface() throw() { } - /// Return build host and OS version as static string - virtual const char * get_os_version_str(); + /// Return info string about build host and/or OS version. + /// Default implementation returns SMARTMONTOOLS_BUILD_HOST. + virtual std::string get_os_version_str(); /// Return valid args for device type option/directive. - /// Default implementation returns "ata, scsi" concatenated - /// with result from get_valid_custom_dev_types_str() below. - virtual const char * get_valid_dev_types_str(); + /// Default implementation returns "ata, scsi, sat, usb*..." + /// concatenated with result from get_valid_custom_dev_types_str(). + virtual std::string get_valid_dev_types_str(); /// Return example string for program 'appname'. - /// Default implementation returns 0. + /// Default implementation returns empty string. /// For the migration of print_smartctl_examples(), /// function is allowed to print examples to stdout. /// TODO: Remove this hack. - virtual const char * get_app_examples(const char * appname); + virtual std::string get_app_examples(const char * appname); /////////////////////////////////////////////// // Last error information @@ -674,8 +675,8 @@ protected: /// Return valid 'type' args accepted by above. /// This is called in get_valid_dev_types_str(). - /// Default implementation returns 0. - virtual const char * get_valid_custom_dev_types_str(); + /// Default implementation returns empty string. + virtual std::string get_valid_custom_dev_types_str(); /// Return ATA->SCSI filter for SAT or USB. /// Override only if platform needs special handling. diff --git a/smartmontools/dev_legacy.cpp b/smartmontools/dev_legacy.cpp index 091554dd7dd93705d22aae9ea78127bc4b0b6c46..54c319d9e6881ca6508c7190516538c43b833078 100644 --- a/smartmontools/dev_legacy.cpp +++ b/smartmontools/dev_legacy.cpp @@ -457,10 +457,10 @@ class legacy_smart_interface { public: #ifdef HAVE_GET_OS_VERSION_STR - virtual const char * get_os_version_str(); + virtual std::string get_os_version_str(); #endif - virtual const char * get_app_examples(const char * appname); + virtual std::string get_app_examples(const char * appname); virtual bool scan_smart_devices(smart_device_list & devlist, const char * type, const char * pattern = 0); @@ -474,24 +474,24 @@ protected: virtual smart_device * get_custom_smart_device(const char * name, const char * type); - virtual const char * get_valid_custom_dev_types_str(); + virtual std::string get_valid_custom_dev_types_str(); }; ////////////////////////////////////////////////////////////////////// #ifdef HAVE_GET_OS_VERSION_STR -const char * legacy_smart_interface::get_os_version_str() +std::string legacy_smart_interface::get_os_version_str() { return ::get_os_version_str(); } #endif -const char * legacy_smart_interface::get_app_examples(const char * appname) +std::string legacy_smart_interface::get_app_examples(const char * appname) { if (!strcmp(appname, "smartctl")) ::print_smartctl_examples(); // this prints to stdout ... - return 0; // ... so don't print again. + return ""; // ... so don't print again. } ata_device * legacy_smart_interface::get_ata_device(const char * name, const char * type) @@ -651,7 +651,7 @@ smart_device * legacy_smart_interface::get_custom_smart_device(const char * name return 0; } -const char * legacy_smart_interface::get_valid_custom_dev_types_str() +std::string legacy_smart_interface::get_valid_custom_dev_types_str() { return "marvell, areca,N, 3ware,N, hpt,L/M/N, cciss,N"; } diff --git a/smartmontools/os_freebsd.cpp b/smartmontools/os_freebsd.cpp index 50cf39bd41d2487c5b2b3f46a4b31cb973c48155..851a5f5cc9900091063bc956f8bc7d245d063dbc 100644 --- a/smartmontools/os_freebsd.cpp +++ b/smartmontools/os_freebsd.cpp @@ -1558,9 +1558,9 @@ class freebsd_smart_interface : public /*implements*/ smart_interface { public: - virtual const char * get_os_version_str(); + virtual std::string get_os_version_str(); - virtual const char * get_app_examples(const char * appname); + virtual std::string get_app_examples(const char * appname); virtual bool scan_smart_devices(smart_device_list & devlist, const char * type, const char * pattern = 0); @@ -1574,26 +1574,24 @@ protected: virtual smart_device * get_custom_smart_device(const char * name, const char * type); - virtual const char * get_valid_custom_dev_types_str(); + virtual std::string get_valid_custom_dev_types_str(); }; ////////////////////////////////////////////////////////////////////// -char sysname[256]; -const char * freebsd_smart_interface::get_os_version_str() + +std::string freebsd_smart_interface::get_os_version_str() { struct utsname osname; uname(&osname); - snprintf(sysname, sizeof(sysname),"%s %s %s",osname.sysname, osname.release, - osname.machine); - return sysname; + return strprintf("%s %s %s", osname.sysname, osname.release, osname.machine); } -const char * freebsd_smart_interface::get_app_examples(const char * appname) +std::string freebsd_smart_interface::get_app_examples(const char * appname) { if (!strcmp(appname, "smartctl")) return smartctl_examples; - return 0; + return ""; } ata_device * freebsd_smart_interface::get_ata_device(const char * name, const char * type) @@ -2238,7 +2236,7 @@ smart_device * freebsd_smart_interface::get_custom_smart_device(const char * nam return 0; } -const char * freebsd_smart_interface::get_valid_custom_dev_types_str() +std::string freebsd_smart_interface::get_valid_custom_dev_types_str() { return "3ware,N, hpt,L/M/N, cciss,N"; } diff --git a/smartmontools/os_generic.cpp b/smartmontools/os_generic.cpp index d57e17f81962ed3b1c9505db72add3ca7346d39e..6624e23958db4f5890ec5b83b3f10b043c973ec2 100644 --- a/smartmontools/os_generic.cpp +++ b/smartmontools/os_generic.cpp @@ -82,7 +82,7 @@ // should have one *_H_CVSID macro appearing below for each file // appearing with #include "*.h" above. Please list these (below) in // alphabetic/dictionary order. -const char *os_XXXX_c_cvsid="$Id: os_generic.cpp,v 1.28 2009/01/20 00:31:17 dlukes Exp $" \ +const char *os_XXXX_c_cvsid="$Id$" \ ATACMDS_H_CVSID CONFIG_H_CVSID OS_GENERIC_H_CVSID UTILITY_H_CVSID; // This is here to prevent compiler warnings for unused arguments of @@ -175,7 +175,7 @@ public: virtual const char * get_os_version_str(); #endif - virtual const char * get_app_examples(const char * appname); + virtual std::string get_app_examples(const char * appname); virtual bool scan_smart_devices(smart_device_list & devlist, const char * type, const char * pattern = 0); @@ -189,7 +189,7 @@ protected: virtual smart_device * get_custom_smart_device(const char * name, const char * type); - virtual const char * get_valid_custom_dev_types_str(); + virtual std::string get_valid_custom_dev_types_str(); }; @@ -203,11 +203,11 @@ const char * generic_smart_interface::get_os_version_str() } #endif -const char * generic_smart_interface::get_app_examples(const char * appname) +std::string generic_smart_interface::get_app_examples(const char * appname) { if (!strcmp(appname, "smartctl")) ::print_smartctl_examples(); // this prints to stdout ... - return 0; // ... so don't print again. + return ""; // ... so don't print again. } // Return ATA device object for the given device name or NULL @@ -267,7 +267,7 @@ smart_device * generic_smart_interface::get_custom_smart_device(const char * nam return NULL; } -const char * generic_smart_interface::get_valid_custom_dev_types_str() +std::string generic_smart_interface::get_valid_custom_dev_types_str() { return ""; } diff --git a/smartmontools/os_linux.cpp b/smartmontools/os_linux.cpp index a0721d5f334a0b8f6c76dd41ee7799342e3a40d3..98ffceee2f6bb420d6396f06e79714400bbb7971 100644 --- a/smartmontools/os_linux.cpp +++ b/smartmontools/os_linux.cpp @@ -2755,7 +2755,7 @@ class linux_smart_interface : public /*implements*/ smart_interface { public: - virtual const char * get_app_examples(const char * appname); + virtual std::string get_app_examples(const char * appname); virtual bool scan_smart_devices(smart_device_list & devlist, const char * type, const char * pattern = 0); @@ -2769,7 +2769,7 @@ protected: virtual smart_device * get_custom_smart_device(const char * name, const char * type); - virtual const char * get_valid_custom_dev_types_str(); + virtual std::string get_valid_custom_dev_types_str(); private: bool get_dev_list(smart_device_list & devlist, const char * pattern, @@ -2778,11 +2778,11 @@ private: smart_device * missing_option(const char * opt); }; -const char * linux_smart_interface::get_app_examples(const char * appname) +std::string linux_smart_interface::get_app_examples(const char * appname) { if (!strcmp(appname, "smartctl")) return smartctl_examples; - return 0; + return ""; } @@ -3143,7 +3143,7 @@ smart_device * linux_smart_interface::get_custom_smart_device(const char * name, return 0; } -const char * linux_smart_interface::get_valid_custom_dev_types_str() +std::string linux_smart_interface::get_valid_custom_dev_types_str() { return "marvell, areca,N, 3ware,N, hpt,L/M/N, megaraid,N" #ifdef HAVE_LINUX_CCISS_IOCTL_H diff --git a/smartmontools/os_win32.cpp b/smartmontools/os_win32.cpp index d9d99e93c406ae28ad71811f93f027597a0cd59b..3780f87603e92c8f08292cf376498ca99bc5e6d5 100644 --- a/smartmontools/os_win32.cpp +++ b/smartmontools/os_win32.cpp @@ -48,7 +48,7 @@ extern smartmonctrl * con; // con->permissive,reportataioctl // Needed by '-V' option (CVS versioning) of smartd/smartctl -const char *os_XXXX_c_cvsid="$Id: os_win32.cpp,v 1.75 2009/06/02 19:43:06 chrfranke Exp $" +const char *os_XXXX_c_cvsid="$Id$" ATACMDS_H_CVSID CONFIG_H_CVSID EXTERN_H_CVSID INT64_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID; @@ -189,9 +189,9 @@ class win_smart_interface : public /*implements part of*/ smart_interface { public: - virtual const char * get_os_version_str(); + virtual std::string get_os_version_str(); - virtual const char * get_app_examples(const char * appname); + virtual std::string get_app_examples(const char * appname); virtual bool scan_smart_devices(smart_device_list & devlist, const char * type, const char * pattern = 0); @@ -259,10 +259,10 @@ static bool is_wow64() return !!w64; } -// Return build host and OS version as static string -const char * win_smart_interface::get_os_version_str() +// Return info string about build host and OS version +std::string win_smart_interface::get_os_version_str() { - static char vstr[sizeof(SMARTMONTOOLS_BUILD_HOST)-1+sizeof("-2003r2(64)-sp2.1")+13] + char vstr[sizeof(SMARTMONTOOLS_BUILD_HOST)-1+sizeof("-2003r2(64)-sp2.1")+13] = SMARTMONTOOLS_BUILD_HOST; if (vstr[1] < '6') vstr[1] = '6'; @@ -479,13 +479,11 @@ bool win_smart_interface::scan_smart_devices(smart_device_list & devlist, // get examples for smartctl -const char * win_smart_interface::get_app_examples(const char * appname) +std::string win_smart_interface::get_app_examples(const char * appname) { if (strcmp(appname, "smartctl")) - return 0; - static char buf[2048]; // TODO: Let this function return std::string ? - snprintf(buf, sizeof(buf), - "=================================================== SMARTCTL EXAMPLES =====\n\n" + return ""; + return "=================================================== SMARTCTL EXAMPLES =====\n\n" " smartctl -a /dev/hda (Prints all SMART information)\n\n" " smartctl --smart=on --offlineauto=on --saveauto=on /dev/hda\n" " (Enables SMART on first disk)\n\n" @@ -510,9 +508,9 @@ const char * win_smart_interface::get_app_examples(const char * appname) " 's': SMART_* IOCTLs, 'a': IOCTL_ATA_PASS_THROUGH,\n" " 'i': IOCTL_IDE_PASS_THROUGH, 'c': ATA via IOCTL_SCSI_PASS_THROUGH,\n" " 'f': IOCTL_STORAGE_*, 'm': IOCTL_SCSI_MINIPORT_*.\n" + + strprintf( " The default on this system is /dev/sdX:%s\n", ata_get_def_options() - ); - return buf; + ); } diff --git a/smartmontools/smartctl.cpp b/smartmontools/smartctl.cpp index 589ef1c576be711a86383d17f7c83df0fe359494..545a41f31833eac8fe3e67f1198470ff0ebf032f 100644 --- a/smartmontools/smartctl.cpp +++ b/smartmontools/smartctl.cpp @@ -73,7 +73,7 @@ void UsageSummary(){ return; } -static const char *getvalidarglist(char opt); +static std::string getvalidarglist(char opt); /* void prints help information for command syntax */ void Usage (void){ @@ -105,7 +105,7 @@ void Usage (void){ " Report transactions (see man page)\n\n" " -n MODE, --nocheck=MODE (ATA)\n" " No check if: never, sleep, standby, idle (see man page)\n\n", - getvalidarglist('d')); // TODO: Use this function also for other options ? + getvalidarglist('d').c_str()); // TODO: Use this function also for other options ? printf( "============================== DEVICE FEATURE ENABLE/DISABLE COMMANDS =====\n\n" " -s VALUE, --smart=VALUE\n" @@ -153,24 +153,20 @@ void Usage (void){ " -X, --abort\n" " Abort any non-captive test on device\n\n" ); - const char * examples = smi()->get_app_examples("smartctl"); - if (examples) - printf("%s\n", examples); + std::string examples = smi()->get_app_examples("smartctl"); + if (!examples.empty()) + printf("%s\n", examples.c_str()); } -/* Returns a pointer to a static string containing a formatted list of the valid - arguments to the option opt or NULL on failure. Note 'v' case different */ -static const char *getvalidarglist(char opt) +/* Returns a string containing a formatted list of the valid arguments + to the option opt or empty on failure. Note 'v' case different */ +static std::string getvalidarglist(char opt) { switch (opt) { case 'q': return "errorsonly, silent, noserial"; case 'd': - { // TODO: let this function return std::string ? - static std::string buf = smi()->get_valid_dev_types_str(); - buf += ", test"; - return buf.c_str(); - } + return smi()->get_valid_dev_types_str() + ", test"; case 'T': return "normal, conservative, permissive, verypermissive"; case 'b': @@ -195,7 +191,7 @@ static const char *getvalidarglist(char opt) return "never, sleep, standby, idle"; case 'v': default: - return NULL; + return ""; } } @@ -210,9 +206,9 @@ void printvalidarglistmessage(char opt) { else { // getvalidarglist() might produce a multiline or single line string. We // need to figure out which to get the formatting right. - const char * s = getvalidarglist(opt); - char separator = strchr(s, '\n') ? '\n' : ' '; - pout("=======> VALID ARGUMENTS ARE:%c%s%c<=======\n", separator, s, separator); + std::string s = getvalidarglist(opt); + char separator = strchr(s.c_str(), '\n') ? '\n' : ' '; + pout("=======> VALID ARGUMENTS ARE:%c%s%c<=======\n", separator, s.c_str(), separator); } return; diff --git a/smartmontools/smartd.cpp b/smartmontools/smartd.cpp index 00737f7b0a60647b32ca945154e652064adf3db6..9966e3a9f3b8fe2e50aafa256db399a39f0fbd47 100644 --- a/smartmontools/smartd.cpp +++ b/smartmontools/smartd.cpp @@ -1412,7 +1412,7 @@ void Directives() { "Attribute ID is a decimal integer 1 <= ID <= 255\n" "Use ID = 0 to turn off -C and/or -U Directives\n" "Example: /dev/hda -a\n", - configfile, smi()->get_valid_dev_types_str()); + configfile, smi()->get_valid_dev_types_str().c_str()); return; } @@ -3009,7 +3009,7 @@ void printoutvaliddirectiveargs(int priority, char d) { PrintOut(priority, "valid_regular_expression"); break; case 'd': - PrintOut(priority, "%s", smi()->get_valid_dev_types_str()); + PrintOut(priority, "%s", smi()->get_valid_dev_types_str().c_str()); break; case 'T': PrintOut(priority, "normal, permissive"); diff --git a/smartmontools/utility.cpp b/smartmontools/utility.cpp index 345e1d63f2c853ace02a9232acad12eb4d360295..6a8f54624996da17d7cf72d2e3555d1bfec82a49 100644 --- a/smartmontools/utility.cpp +++ b/smartmontools/utility.cpp @@ -91,7 +91,7 @@ std::string format_version_info(const char * prog_name, bool full /*= false*/) "%s "PACKAGE_VERSION" "SMARTMONTOOLS_SVN_DATE" r"SMARTMONTOOLS_SVN_REV " [%s] "BUILD_INFO"\n" "Copyright (C) 2002-9 by Bruce Allen, http://smartmontools.sourceforge.net\n", - prog_name, smi()->get_os_version_str() + prog_name, smi()->get_os_version_str().c_str() ); if (!full) return info;