From 588c4598dc1236f074829b63a8dfb66ec654eb2f Mon Sep 17 00:00:00 2001 From: ballen4705 <ballen4705@4ea69e1a-61f1-4043-bf83-b5c94c648137> Date: Sat, 26 Oct 2002 19:33:40 +0000 Subject: [PATCH] cleaned up routines that print CVS ids to use strtok routines git-svn-id: https://smartmontools.svn.sourceforge.net/svnroot/smartmontools/trunk@143 4ea69e1a-61f1-4043-bf83-b5c94c648137 --- sm5/CHANGELOG | 5 ++- sm5/atacmds.c | 60 +++++++++++++++++++++++++++- sm5/atacmds.cpp | 60 +++++++++++++++++++++++++++- sm5/atacmds.h | 6 ++- sm5/smartctl.c | 84 ++++++-------------------------------- sm5/smartctl.cpp | 84 ++++++-------------------------------- sm5/smartd.c | 102 +++++++++++++++-------------------------------- sm5/smartd.cpp | 102 +++++++++++++++-------------------------------- 8 files changed, 215 insertions(+), 288 deletions(-) diff --git a/sm5/CHANGELOG b/sm5/CHANGELOG index 72d728848..c73584e07 100644 --- a/sm5/CHANGELOG +++ b/sm5/CHANGELOG @@ -1,6 +1,6 @@ CHANGELOG for smartmontools -$Id: CHANGELOG,v 1.24 2002/10/26 09:38:26 ballen4705 Exp $ +$Id: CHANGELOG,v 1.25 2002/10/26 19:33:39 ballen4705 Exp $ Copyright (C) 2002 Bruce Allen <smartmontools-support@lists.sourceforge.net> @@ -27,6 +27,9 @@ NOTES FOR FUTURE RELEASES: see TODO file. CURRENT RELEASE (see VERSION file in this directory): smartmontools-5.0-VERSION + cleaned up functions used for printing CVS IDs. Now use string + library, as it should be. + modified length of device name string in smartd internal structure to accomodate max length device name strings diff --git a/sm5/atacmds.c b/sm5/atacmds.c index 778c4d674..9f2e84254 100644 --- a/sm5/atacmds.c +++ b/sm5/atacmds.c @@ -27,9 +27,10 @@ #include <string.h> #include <syslog.h> #include <errno.h> +#include <stdlib.h> #include "atacmds.h" -const char *CVSid1="$Id: atacmds.c,v 1.27 2002/10/26 11:44:01 ballen4705 Exp $" CVSID1; +const char *CVSid1="$Id: atacmds.c,v 1.28 2002/10/26 19:33:39 ballen4705 Exp $" CVSID1; // These Drive Identity tables are taken from hdparm 5.2, and are also // given in the ATA/ATAPI specs for the IDENTIFY DEVICE command. Note @@ -773,3 +774,60 @@ void ataPrintSmartAttribName(char *out, unsigned char id){ sprintf(out,"%3d %s",id,name); return; } + + +// These are two utility functions for printing CVS IDs. They don't +// really belong here. But it's the only common source file included +// in both smartd and smartctl. returns distance that it has moved +// ahead in the input string +int massagecvs(char *out, const char *cvsid){ + char *copy,*filename,*date,*version; + const char delimiters[] = " ,$"; + + // make a copy on stack, go to first token, + if (!(copy=strdup(cvsid)) || !(filename=strtok(copy, delimiters))) + return 0; + + // move to first instance of "Id:" + while (strcmp(filename,"Id:")) + if (!(filename=strtok(NULL, delimiters))) + return 0; + + // get filename, skip "v", get version and date + if (!( filename=strtok(NULL, delimiters) ) || + !( strtok(NULL, delimiters) ) || + !( version=strtok(NULL, delimiters) ) || + !( date=strtok(NULL, delimiters) ) ) + return 0; + + sprintf(out,"%-13s revision: %-6s date: %-15s", filename, version, date); + free(copy); + return (date-copy)+strlen(date); +} + +// prints a single set of CVS ids +void printone(char *block, const char *cvsid){ + char strings[CVSMAXLEN]; + const char *here=cvsid; + int line=1,len=strlen(cvsid)+1; + + // check that the size of the output block is sufficient + if (len>=CVSMAXLEN) { + fprintf(stderr,"CVSMAXLEN=%d must be at least %d\n",CVSMAXLEN,len+1); + exit(1); + } + + // loop through the different strings + while ((len=massagecvs(strings,here))){ + switch (line++){ + case 1: + block+=snprintf(block,CVSMAXLEN,"Module:"); + break; + default: + block+=snprintf(block,CVSMAXLEN," uses:"); + } + block+=snprintf(block,CVSMAXLEN," %s\n",strings); + here+=len; + } + return; +} diff --git a/sm5/atacmds.cpp b/sm5/atacmds.cpp index 54d72c496..25f512cbc 100644 --- a/sm5/atacmds.cpp +++ b/sm5/atacmds.cpp @@ -27,9 +27,10 @@ #include <string.h> #include <syslog.h> #include <errno.h> +#include <stdlib.h> #include "atacmds.h" -const char *CVSid1="$Id: atacmds.cpp,v 1.27 2002/10/26 11:44:01 ballen4705 Exp $" CVSID1; +const char *CVSid1="$Id: atacmds.cpp,v 1.28 2002/10/26 19:33:39 ballen4705 Exp $" CVSID1; // These Drive Identity tables are taken from hdparm 5.2, and are also // given in the ATA/ATAPI specs for the IDENTIFY DEVICE command. Note @@ -773,3 +774,60 @@ void ataPrintSmartAttribName(char *out, unsigned char id){ sprintf(out,"%3d %s",id,name); return; } + + +// These are two utility functions for printing CVS IDs. They don't +// really belong here. But it's the only common source file included +// in both smartd and smartctl. returns distance that it has moved +// ahead in the input string +int massagecvs(char *out, const char *cvsid){ + char *copy,*filename,*date,*version; + const char delimiters[] = " ,$"; + + // make a copy on stack, go to first token, + if (!(copy=strdup(cvsid)) || !(filename=strtok(copy, delimiters))) + return 0; + + // move to first instance of "Id:" + while (strcmp(filename,"Id:")) + if (!(filename=strtok(NULL, delimiters))) + return 0; + + // get filename, skip "v", get version and date + if (!( filename=strtok(NULL, delimiters) ) || + !( strtok(NULL, delimiters) ) || + !( version=strtok(NULL, delimiters) ) || + !( date=strtok(NULL, delimiters) ) ) + return 0; + + sprintf(out,"%-13s revision: %-6s date: %-15s", filename, version, date); + free(copy); + return (date-copy)+strlen(date); +} + +// prints a single set of CVS ids +void printone(char *block, const char *cvsid){ + char strings[CVSMAXLEN]; + const char *here=cvsid; + int line=1,len=strlen(cvsid)+1; + + // check that the size of the output block is sufficient + if (len>=CVSMAXLEN) { + fprintf(stderr,"CVSMAXLEN=%d must be at least %d\n",CVSMAXLEN,len+1); + exit(1); + } + + // loop through the different strings + while ((len=massagecvs(strings,here))){ + switch (line++){ + case 1: + block+=snprintf(block,CVSMAXLEN,"Module:"); + break; + default: + block+=snprintf(block,CVSMAXLEN," uses:"); + } + block+=snprintf(block,CVSMAXLEN," %s\n",strings); + here+=len; + } + return; +} diff --git a/sm5/atacmds.h b/sm5/atacmds.h index a55cf89ea..511f6c287 100644 --- a/sm5/atacmds.h +++ b/sm5/atacmds.h @@ -26,7 +26,7 @@ #define _ATACMDS_H_ #ifndef CVSID1 -#define CVSID1 "$Id: atacmds.h,v 1.18 2002/10/24 09:54:02 ballen4705 Exp $\n" +#define CVSID1 "$Id: atacmds.h,v 1.19 2002/10/26 19:33:39 ballen4705 Exp $\n" #endif // These are the major and minor versions for smartd and smartctl @@ -380,6 +380,10 @@ void ataPrintSmartAttribName(char *output, unsigned char id); // like printf() except that we can control it better.... void pout(char *fmt, ...); +// utility function for printing out CVS strings +#define CVSMAXLEN 512 +void printone(char *block, const char *cvsid); + // MACROS to control printing behavior #define QUIETON {if (quietmode) veryquietmode=0;} #define QUIETOFF {if (quietmode && !veryquietmode) veryquietmode=1;} diff --git a/sm5/smartctl.c b/sm5/smartctl.c index 3487d7e89..bfec094cd 100644 --- a/sm5/smartctl.c +++ b/sm5/smartctl.c @@ -38,7 +38,7 @@ #include "scsiprint.h" extern const char *CVSid1, *CVSid2, *CVSid4, *CVSid5; -const char* CVSid6="$Id: smartctl.c,v 1.20 2002/10/25 08:50:21 ballen4705 Exp $" +const char* CVSid6="$Id: smartctl.c,v 1.21 2002/10/26 19:33:40 ballen4705 Exp $" CVSID1 CVSID2 CVSID4 CVSID5 CVSID6; unsigned char driveinfo = FALSE; @@ -74,83 +74,23 @@ void printslogan(){ } - -int massagecvs(char *out,const char *in){ - char filename[128], version[128], date[128]; - int i=0; - const char *savein=in; - - // skip to I of $Id: - while (*in !='\0' && *in!='I') - in++; - - // skip to start of filename - if (!*in) - return 0; - in+=4; - - // copy filename - i=0; - while (i<100 && *in!=',' && *in) - filename[i++]=*in++; - filename[i]='\0'; - if (!*in) - return 0; - - // skip ,v and space - in+=3; - - i=0; - // copy version number - while (i<100 && *in!=' ' && *in) - version[i++]=*in++; - version[i]='\0'; - if (!*in) - return 0; - - // skip space - in++; - // copy date - i=0; - while (i<100 && *in!=' ' && *in) - date[i++]=*in++; - date[i]='\0'; - - sprintf(out,"%-13s revision: %-6s date: %-15s", filename, version, date); - return in-savein; -} - -// prints a single set of CVS ids -void printone(const char *cvsid){ - char strings[512]; - const char *here; - int len,line=1; - here=cvsid; - while ((len=massagecvs(strings,here))){ - switch (line++){ - case 1: - pout("Module:"); - break; - default: - pout(" uses:"); - } - pout(" %s\n",strings); - here+=len; - } - return; -} - void printcopy(){ + char out[CVSMAXLEN]; pout("smartctl comes with ABSOLUTELY NO WARRANTY. This\n"); pout("is free software, and you are welcome to redistribute it\n"); pout("under the terms of the GNU General Public License Version 2.\n"); pout("See http://www.gnu.org for further details.\n\n"); pout("CVS version IDs of files used to build this code are:\n"); - printone(CVSid6); - printone(CVSid1); - printone(CVSid2); - printone(CVSid4); - printone(CVSid5); + printone(out,CVSid6); + pout("%s",out); + printone(out,CVSid1); + pout("%s",out); + printone(out,CVSid2); + pout("%s",out); + printone(out,CVSid4); + pout("%s",out); + printone(out,CVSid5); + pout("%s",out); return; } diff --git a/sm5/smartctl.cpp b/sm5/smartctl.cpp index 75b8b2cf2..904421e37 100644 --- a/sm5/smartctl.cpp +++ b/sm5/smartctl.cpp @@ -38,7 +38,7 @@ #include "scsiprint.h" extern const char *CVSid1, *CVSid2, *CVSid4, *CVSid5; -const char* CVSid6="$Id: smartctl.cpp,v 1.20 2002/10/25 08:50:21 ballen4705 Exp $" +const char* CVSid6="$Id: smartctl.cpp,v 1.21 2002/10/26 19:33:40 ballen4705 Exp $" CVSID1 CVSID2 CVSID4 CVSID5 CVSID6; unsigned char driveinfo = FALSE; @@ -74,83 +74,23 @@ void printslogan(){ } - -int massagecvs(char *out,const char *in){ - char filename[128], version[128], date[128]; - int i=0; - const char *savein=in; - - // skip to I of $Id: - while (*in !='\0' && *in!='I') - in++; - - // skip to start of filename - if (!*in) - return 0; - in+=4; - - // copy filename - i=0; - while (i<100 && *in!=',' && *in) - filename[i++]=*in++; - filename[i]='\0'; - if (!*in) - return 0; - - // skip ,v and space - in+=3; - - i=0; - // copy version number - while (i<100 && *in!=' ' && *in) - version[i++]=*in++; - version[i]='\0'; - if (!*in) - return 0; - - // skip space - in++; - // copy date - i=0; - while (i<100 && *in!=' ' && *in) - date[i++]=*in++; - date[i]='\0'; - - sprintf(out,"%-13s revision: %-6s date: %-15s", filename, version, date); - return in-savein; -} - -// prints a single set of CVS ids -void printone(const char *cvsid){ - char strings[512]; - const char *here; - int len,line=1; - here=cvsid; - while ((len=massagecvs(strings,here))){ - switch (line++){ - case 1: - pout("Module:"); - break; - default: - pout(" uses:"); - } - pout(" %s\n",strings); - here+=len; - } - return; -} - void printcopy(){ + char out[CVSMAXLEN]; pout("smartctl comes with ABSOLUTELY NO WARRANTY. This\n"); pout("is free software, and you are welcome to redistribute it\n"); pout("under the terms of the GNU General Public License Version 2.\n"); pout("See http://www.gnu.org for further details.\n\n"); pout("CVS version IDs of files used to build this code are:\n"); - printone(CVSid6); - printone(CVSid1); - printone(CVSid2); - printone(CVSid4); - printone(CVSid5); + printone(out,CVSid6); + pout("%s",out); + printone(out,CVSid1); + pout("%s",out); + printone(out,CVSid2); + pout("%s",out); + printone(out,CVSid4); + pout("%s",out); + printone(out,CVSid5); + pout("%s",out); return; } diff --git a/sm5/smartd.c b/sm5/smartd.c index 32f9f5100..5bb4c54c4 100644 --- a/sm5/smartd.c +++ b/sm5/smartd.c @@ -39,7 +39,7 @@ // CVS ID strings extern const char *CVSid1, *CVSid2; -const char *CVSid3="$Id: smartd.c,v 1.35 2002/10/26 10:19:16 ballen4705 Exp $" +const char *CVSid3="$Id: smartd.c,v 1.36 2002/10/26 19:33:40 ballen4705 Exp $" CVSID1 CVSID4 CVSID7; // This function prints either to stdout or to the syslog as needed @@ -401,72 +401,6 @@ void CheckDevices ( atadevices_t *atadevices, scsidevices_t *scsidevices){ } } - -int massagecvs(char *out,const char *in){ - char filename[128], version[128], date[128]; - int i=0; - const char *savein=in; - - // skip to I of $Id: - while (*in && *in!='I') - in++; - - // skip to start of filename - if (!*in) - return 0; - in+=4; - - // copy filename - i=0; - while (i<100 && *in!=',' && *in) - filename[i++]=*in++; - filename[i]='\0'; - if (!*in) - return 0; - - // skip ,v and space - in+=3; - - i=0; - // copy version number - while (i<100 && *in!=' ' && *in) - version[i++]=*in++; - version[i]='\0'; - if (!*in) - return 0; - - // skip space - in++; - // copy date - i=0; - while (i<100 && *in!=' ' && *in) - date[i++]=*in++; - date[i]='\0'; - - sprintf(out,"%-13s revision: %-6s date: %-15s", filename, version, date); - return in-savein; -} - -// prints a single set of CVS ids -void printone(const char *cvsid){ - char strings[512]; - const char *here; - int len,line=1; - here=cvsid; - while ((len=massagecvs(strings,here))){ - switch (line++){ - case 1: - printout(LOG_INFO,"Module:"); - break; - default: - printout(LOG_INFO," uses:"); - } - printout(LOG_INFO," %s\n",strings); - here+=len; - } - return; -} - char copyleftstring[]= "smartd comes with ABSOLUTELY NO WARRANTY. This\n" "is free software, and you are welcome to redistribute it\n" @@ -536,6 +470,30 @@ int parseconfigfile(){ if (!len || *dev=='#') continue; +#if 0 + // This is the start of some code to handle continuation + // characters in the /etc/smartd.conf file. Note that these must + // be the final character on a line, with just a newline after + // them. No other white space afterwards. + + if (continuation+len>maxlinelen) + error; + + curr=config[entry].name+continuation; + strcpy(curr,dev); + + + if (cur[len-1]=='\ '){ + cur[len-1]=' '; + continuation+=len; + continue; + } + else { + continuation=0; + lineno++; + } +#endif + // We've got a legit entry if (entry>=MAXENTRIES){ printout(LOG_CRIT,"Error: configuration file %s can have no more than %d entries\n", @@ -608,13 +566,17 @@ void ParseOpts(int argc, char **argv){ // If needed print copyright, license and version information if (printcopyleft){ + char out[CVSMAXLEN]; debugmode=1; printhead(); printout(LOG_INFO,copyleftstring); printout(LOG_INFO,"CVS version IDs of files used to build this code are:\n"); - printone(CVSid3); - printone(CVSid1); - printone(CVSid2); + printone(out,CVSid3); + printout(LOG_INFO,"%s",out); + printone(out,CVSid1); + printout(LOG_INFO,"%s",out); + printone(out,CVSid2); + printout(LOG_INFO,"%s",out); exit(0); } diff --git a/sm5/smartd.cpp b/sm5/smartd.cpp index 34abbce27..7e3fe7e40 100644 --- a/sm5/smartd.cpp +++ b/sm5/smartd.cpp @@ -39,7 +39,7 @@ // CVS ID strings extern const char *CVSid1, *CVSid2; -const char *CVSid3="$Id: smartd.cpp,v 1.35 2002/10/26 10:19:16 ballen4705 Exp $" +const char *CVSid3="$Id: smartd.cpp,v 1.36 2002/10/26 19:33:40 ballen4705 Exp $" CVSID1 CVSID4 CVSID7; // This function prints either to stdout or to the syslog as needed @@ -401,72 +401,6 @@ void CheckDevices ( atadevices_t *atadevices, scsidevices_t *scsidevices){ } } - -int massagecvs(char *out,const char *in){ - char filename[128], version[128], date[128]; - int i=0; - const char *savein=in; - - // skip to I of $Id: - while (*in && *in!='I') - in++; - - // skip to start of filename - if (!*in) - return 0; - in+=4; - - // copy filename - i=0; - while (i<100 && *in!=',' && *in) - filename[i++]=*in++; - filename[i]='\0'; - if (!*in) - return 0; - - // skip ,v and space - in+=3; - - i=0; - // copy version number - while (i<100 && *in!=' ' && *in) - version[i++]=*in++; - version[i]='\0'; - if (!*in) - return 0; - - // skip space - in++; - // copy date - i=0; - while (i<100 && *in!=' ' && *in) - date[i++]=*in++; - date[i]='\0'; - - sprintf(out,"%-13s revision: %-6s date: %-15s", filename, version, date); - return in-savein; -} - -// prints a single set of CVS ids -void printone(const char *cvsid){ - char strings[512]; - const char *here; - int len,line=1; - here=cvsid; - while ((len=massagecvs(strings,here))){ - switch (line++){ - case 1: - printout(LOG_INFO,"Module:"); - break; - default: - printout(LOG_INFO," uses:"); - } - printout(LOG_INFO," %s\n",strings); - here+=len; - } - return; -} - char copyleftstring[]= "smartd comes with ABSOLUTELY NO WARRANTY. This\n" "is free software, and you are welcome to redistribute it\n" @@ -536,6 +470,30 @@ int parseconfigfile(){ if (!len || *dev=='#') continue; +#if 0 + // This is the start of some code to handle continuation + // characters in the /etc/smartd.conf file. Note that these must + // be the final character on a line, with just a newline after + // them. No other white space afterwards. + + if (continuation+len>maxlinelen) + error; + + curr=config[entry].name+continuation; + strcpy(curr,dev); + + + if (cur[len-1]=='\ '){ + cur[len-1]=' '; + continuation+=len; + continue; + } + else { + continuation=0; + lineno++; + } +#endif + // We've got a legit entry if (entry>=MAXENTRIES){ printout(LOG_CRIT,"Error: configuration file %s can have no more than %d entries\n", @@ -608,13 +566,17 @@ void ParseOpts(int argc, char **argv){ // If needed print copyright, license and version information if (printcopyleft){ + char out[CVSMAXLEN]; debugmode=1; printhead(); printout(LOG_INFO,copyleftstring); printout(LOG_INFO,"CVS version IDs of files used to build this code are:\n"); - printone(CVSid3); - printone(CVSid1); - printone(CVSid2); + printone(out,CVSid3); + printout(LOG_INFO,"%s",out); + printone(out,CVSid1); + printout(LOG_INFO,"%s",out); + printone(out,CVSid2); + printout(LOG_INFO,"%s",out); exit(0); } -- GitLab