From 38c1ad30b2ad8d71f946045e803fd8c4aa5f2e29 Mon Sep 17 00:00:00 2001 From: PA4WDH Date: Wed, 24 May 2023 10:22:28 +0200 Subject: Detect program name from argv[0] --- argparse.c | 18 +++++++++++++++--- cputemp2maxfreq.c | 3 ++- cputemp2maxfreq.h | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/argparse.c b/argparse.c index 888f445..7df1239 100644 --- a/argparse.c +++ b/argparse.c @@ -11,7 +11,7 @@ extern struct s_config config; void printhelp() { printf( - "Usage: cputemp2maxfreq \n" + "Usage: %s \n" "\n" "Options:\n" "-f Fallback frequency in KHz, used when everything fails\n" @@ -30,7 +30,7 @@ void printhelp() " Default: %ld\n" "-t Temperature limit\n" " Default: %ld\n", - config.fallback_freq,config.governor,config.temp_input,config.logger_name, + config.name,config.fallback_freq,config.governor,config.temp_input,config.logger_name, config.interval,config.freq_step,config.max_temp/1000 ); } @@ -39,9 +39,19 @@ void argparse(int argc, char **argv) { int opt; struct s_config userconfig; - + char *lastslash; DEBUG1_ARGPARSE("Started\n"); + lastslash=strrchr(argv[0],'/'); + if (lastslash==NULL) + { + lastslash=argv[0]; + } else { + lastslash++; + } + strncpy(config.name,lastslash,sizeof(config.name)); + DEBUG2_ARGPARSE("Detected program name %s\n",config.name); + // Copy defaults memcpy(&userconfig,&config,sizeof(struct s_config)); @@ -85,6 +95,7 @@ void argparse(int argc, char **argv) } // Change the logger function pointer + DEBUG1_ARGPARSE("Logger is set to %s\n",userconfig.logger_name); if (strcmp(userconfig.logger_name,"none")==0) userconfig.logger=&logger_none; else if (strcmp(userconfig.logger_name,"stdout")==0) userconfig.logger=&logger_stdout; else if (strcmp(userconfig.logger_name,"syslog")==0) userconfig.logger=&logger_syslog; @@ -93,6 +104,7 @@ void argparse(int argc, char **argv) config.logger("Invalid value for logger, exiting"); exit(1); } + DEBUG2_ARGPARSE("Logger pointer is %p\n",userconfig.logger); // Copy modified config to global config memcpy(&config,&userconfig,sizeof(struct s_config)); diff --git a/cputemp2maxfreq.c b/cputemp2maxfreq.c index dd58e16..826655a 100644 --- a/cputemp2maxfreq.c +++ b/cputemp2maxfreq.c @@ -15,6 +15,7 @@ // Set default config struct s_config config={ + "", // Name of this program, set by argparse "conservative", // Governor 70000, // Temperature "/sys/devices/virtual/thermal/thermal_zone0/temp", // Temperature input @@ -39,7 +40,7 @@ int main(int argc,char **argv) long int newfreq; argparse(argc,argv); - config.logger("Version %s, buildtime %s %s",version(),__DATE__,__TIME__); + config.logger("%s version %s, buildtime %s %s",config.name,version(),__DATE__,__TIME__); // Log configuration config.logger("Configuration:"); diff --git a/cputemp2maxfreq.h b/cputemp2maxfreq.h index 0e4c424..6eb5726 100644 --- a/cputemp2maxfreq.h +++ b/cputemp2maxfreq.h @@ -26,6 +26,7 @@ struct s_cpudata { }; struct s_config { + char name[255]; // Name of this program char governor[255]; // The governor to use long int max_temp; // The target temperature char temp_input[255]; // Input file to read the temperature -- cgit v1.2.3