From: albert <> Date: Mon, 25 Nov 2002 21:42:17 +0000 (+0000) Subject: move functions X-Git-Tag: v3.3.0~357 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3f6928c4fb21f1605e8b807384802032f45b5a85;p=procps-ng move functions --- diff --git a/sysctl.c b/sysctl.c index ab497540..911ff7a9 100644 --- a/sysctl.c +++ b/sysctl.c @@ -36,16 +36,6 @@ static bool true = 1; static bool false = 0; -/* - * Function Prototypes - */ -static int Usage(const char *name); -static void Preload(const char *filename); -static int WriteSetting(const char *setting); -static int ReadSetting(const char *setting); -static int DisplayAll(const char *path, bool ShowTableUtil); - - /* * Globals... */ @@ -80,74 +70,6 @@ static void slashdot(char *p, char old, char new){ } } -/* - * Main... - * - */ -int main(int argc, char **argv) { - const char *me = (const char *)basename(argv[0]); - bool SwitchesAllowed = true; - bool WriteMode = false; - int ReturnCode = 0; - const char *preloadfile = DEFAULT_PRELOAD; - - PrintName = true; - PrintNewline = true; - - if (argc < 2) { - return Usage(me); - } /* endif */ - - argv++; - - for (; argv && *argv && **argv; argv++) { - if (SwitchesAllowed && **argv == '-') { /* we have a switch */ - switch((*argv)[1]) { - case 'b': - /* This is "binary" format, which means more for BSD. */ - PrintNewline = false; - /* FALL THROUGH */ - case 'n': - PrintName = false; - break; - case 'w': - SwitchesAllowed = false; - WriteMode = true; - break; - case 'p': - argv++; - if (argv && *argv && **argv) { - preloadfile = *argv; - } /* endif */ - - Preload(preloadfile); - return(0); - break; - case 'a': /* string and integer values (for Linux, all of them) */ - case 'A': /* the above, including "opaques" (would be unprintable) */ - case 'X': /* the above, with opaques completly printed in hex */ - SwitchesAllowed = false; - return DisplayAll(PROC_PATH, ((*argv)[1] == 'a') ? false : true); - case 'h': - case '?': - return Usage(me); - default: - fprintf(stderr, ERR_UNKNOWN_PARAMETER, *argv); - return Usage(me); - } /* end switch */ - } else { - SwitchesAllowed = false; - if (WriteMode) - ReturnCode = WriteSetting(*argv); - else ReadSetting(*argv); - } /* end if */ - } /* end for */ - -return ReturnCode; -} /* end main */ - - - /* @@ -191,58 +113,108 @@ static char *StripLeadingAndTrailingSpaces(char *oneline) { /* - * Preload the sysctl's from the conf file - * - we parse the file and then reform it (strip out whitespace) + * Read a sysctl setting * */ -static void Preload(const char *filename) { +static int ReadSetting(const char *setting) { + int rc = 0; + char *tmpname, *outname; + char inbuf[1025]; + const char *name = setting; FILE *fp; - char oneline[257]; - char buffer[257]; - char *t; - int n = 0; - char *name, *value; - if (!filename || ((fp = fopen(filename, "r")) == NULL)) { - fprintf(stderr, ERR_PRELOAD_FILE, filename); - return; + if (!setting || !*setting) { + fprintf(stderr, ERR_INVALID_KEY, setting); } /* endif */ - while (fgets(oneline, 256, fp)) { - oneline[256] = 0; - n++; - t = StripLeadingAndTrailingSpaces(oneline); + /* used to open the file */ + tmpname = malloc(strlen(name)+strlen(PROC_PATH)+1); + strcpy(tmpname, PROC_PATH); + strcat(tmpname, name); + slashdot(tmpname+strlen(PROC_PATH),'.','/'); /* change . to / */ - if (strlen(t) < 2) - continue; + /* used to display the output */ + outname = strdup(name); + slashdot(outname,'/','.'); /* change / to . */ - if (*t == '#' || *t == ';') - continue; + fp = fopen(tmpname, "r"); - name = strtok(t, "="); - if (!name || !*name) { - fprintf(stderr, WARN_BAD_LINE, filename, n); - continue; - } /* endif */ + if (!fp) { + switch(errno) { + case ENOENT: + fprintf(stderr, ERR_INVALID_KEY, outname); + break; + case EACCES: + fprintf(stderr, ERR_PERMISSION_DENIED, outname); + break; + default: + fprintf(stderr, ERR_UNKNOWN_READING, errno, outname); + break; + } /* end switch */ + rc = -1; + } else { + while(fgets(inbuf, 1024, fp)) { + /* already has the \n in it */ + if (PrintName) { + fprintf(stdout, "%s = %s", outname, inbuf); + } else { + if (!PrintNewline) { + char *nlptr = strchr(inbuf,'\n'); + if(nlptr) *nlptr='\0'; + } + fprintf(stdout, "%s", inbuf); + } + } /* endwhile */ + fclose(fp); + } /* endif */ - StripLeadingAndTrailingSpaces(name); + free(tmpname); + free(outname); + return rc; +} /* end ReadSetting() */ - value = strtok(NULL, "\n\r"); - if (!value || !*value) { - fprintf(stderr, WARN_BAD_LINE, filename, n); - continue; - } /* endif */ - while ((*value == ' ' || *value == '\t') && *value != 0) - value++; - sprintf(buffer, "%s=%s", name, value); - WriteSetting(buffer); - } /* endwhile */ +/* + * Display all the sysctl settings + * + */ +static int DisplayAll(const char *path, bool ShowTableUtil) { + int rc = 0; + int rc2; + DIR *dp; + struct dirent *de; + char *tmpdir; + struct stat ts; - fclose(fp); -} /* end Preload() */ + dp = opendir(path); + + if (!dp) { + fprintf(stderr, ERR_OPENING_DIR, path); + rc = -1; + } else { + readdir(dp); readdir(dp); /* skip . and .. */ + while (( de = readdir(dp) )) { + tmpdir = (char *)malloc(strlen(path)+strlen(de->d_name)+2); + sprintf(tmpdir, "%s%s", path, de->d_name); + rc2 = stat(tmpdir, &ts); /* should check this return code */ + if (rc2 != 0) { + perror(tmpdir); + } else { + if (S_ISDIR(ts.st_mode)) { + strcat(tmpdir, "/"); + DisplayAll(tmpdir, ShowTableUtil); + } else { + rc |= ReadSetting(tmpdir+strlen(PROC_PATH)); + } /* endif */ + } /* endif */ + free(tmpdir); + } /* end while */ + closedir(dp); + } /* endif */ + return rc; +} /* end DisplayAll() */ /* @@ -326,106 +298,124 @@ static int WriteSetting(const char *setting) { /* - * Read a sysctl setting + * Preload the sysctl's from the conf file + * - we parse the file and then reform it (strip out whitespace) * */ -static int ReadSetting(const char *setting) { - int rc = 0; - char *tmpname, *outname; - char inbuf[1025]; - const char *name = setting; +static void Preload(const char *filename) { FILE *fp; + char oneline[257]; + char buffer[257]; + char *t; + int n = 0; + char *name, *value; - if (!setting || !*setting) { - fprintf(stderr, ERR_INVALID_KEY, setting); + if (!filename || ((fp = fopen(filename, "r")) == NULL)) { + fprintf(stderr, ERR_PRELOAD_FILE, filename); + return; } /* endif */ - /* used to open the file */ - tmpname = malloc(strlen(name)+strlen(PROC_PATH)+1); - strcpy(tmpname, PROC_PATH); - strcat(tmpname, name); - slashdot(tmpname+strlen(PROC_PATH),'.','/'); /* change . to / */ + while (fgets(oneline, 256, fp)) { + oneline[256] = 0; + n++; + t = StripLeadingAndTrailingSpaces(oneline); - /* used to display the output */ - outname = strdup(name); - slashdot(outname,'/','.'); /* change / to . */ + if (strlen(t) < 2) + continue; - fp = fopen(tmpname, "r"); + if (*t == '#' || *t == ';') + continue; - if (!fp) { - switch(errno) { - case ENOENT: - fprintf(stderr, ERR_INVALID_KEY, outname); - break; - case EACCES: - fprintf(stderr, ERR_PERMISSION_DENIED, outname); - break; - default: - fprintf(stderr, ERR_UNKNOWN_READING, errno, outname); - break; - } /* end switch */ - rc = -1; - } else { - while(fgets(inbuf, 1024, fp)) { - /* already has the \n in it */ - if (PrintName) { - fprintf(stdout, "%s = %s", outname, inbuf); - } else { - if (!PrintNewline) { - char *nlptr = strchr(inbuf,'\n'); - if(nlptr) *nlptr='\0'; - } - fprintf(stdout, "%s", inbuf); - } - } /* endwhile */ - fclose(fp); - } /* endif */ + name = strtok(t, "="); + if (!name || !*name) { + fprintf(stderr, WARN_BAD_LINE, filename, n); + continue; + } /* endif */ - free(tmpname); - free(outname); - return rc; -} /* end ReadSetting() */ + StripLeadingAndTrailingSpaces(name); + + value = strtok(NULL, "\n\r"); + if (!value || !*value) { + fprintf(stderr, WARN_BAD_LINE, filename, n); + continue; + } /* endif */ + + while ((*value == ' ' || *value == '\t') && *value != 0) + value++; + + sprintf(buffer, "%s=%s", name, value); + WriteSetting(buffer); + } /* endwhile */ + + fclose(fp); +} /* end Preload() */ /* - * Display all the sysctl settings + * Main... * */ -static int DisplayAll(const char *path, bool ShowTableUtil) { - int rc = 0; - int rc2; - DIR *dp; - struct dirent *de; - char *tmpdir; - struct stat ts; +int main(int argc, char **argv) { + const char *me = (const char *)basename(argv[0]); + bool SwitchesAllowed = true; + bool WriteMode = false; + int ReturnCode = 0; + const char *preloadfile = DEFAULT_PRELOAD; - dp = opendir(path); + PrintName = true; + PrintNewline = true; - if (!dp) { - fprintf(stderr, ERR_OPENING_DIR, path); - rc = -1; - } else { - readdir(dp); readdir(dp); /* skip . and .. */ - while (( de = readdir(dp) )) { - tmpdir = (char *)malloc(strlen(path)+strlen(de->d_name)+2); - sprintf(tmpdir, "%s%s", path, de->d_name); - rc2 = stat(tmpdir, &ts); /* should check this return code */ - if (rc2 != 0) { - perror(tmpdir); - } else { - if (S_ISDIR(ts.st_mode)) { - strcat(tmpdir, "/"); - DisplayAll(tmpdir, ShowTableUtil); - } else { - rc |= ReadSetting(tmpdir+strlen(PROC_PATH)); - } /* endif */ - } /* endif */ - free(tmpdir); - } /* end while */ - closedir(dp); + if (argc < 2) { + return Usage(me); } /* endif */ - return rc; -} /* end DisplayAll() */ + argv++; + + for (; argv && *argv && **argv; argv++) { + if (SwitchesAllowed && **argv == '-') { /* we have a switch */ + switch((*argv)[1]) { + case 'b': + /* This is "binary" format, which means more for BSD. */ + PrintNewline = false; + /* FALL THROUGH */ + case 'n': + PrintName = false; + break; + case 'w': + SwitchesAllowed = false; + WriteMode = true; + break; + case 'p': + argv++; + if (argv && *argv && **argv) { + preloadfile = *argv; + } /* endif */ + + Preload(preloadfile); + return(0); + break; + case 'a': /* string and integer values (for Linux, all of them) */ + case 'A': /* the above, including "opaques" (would be unprintable) */ + case 'X': /* the above, with opaques completly printed in hex */ + SwitchesAllowed = false; + return DisplayAll(PROC_PATH, ((*argv)[1] == 'a') ? false : true); + case 'h': + case '?': + return Usage(me); + default: + fprintf(stderr, ERR_UNKNOWN_PARAMETER, *argv); + return Usage(me); + } /* end switch */ + } else { + SwitchesAllowed = false; + if (WriteMode) + ReturnCode = WriteSetting(*argv); + else ReadSetting(*argv); + } /* end if */ + } /* end for */ + +return ReturnCode; +} /* end main */ +