sysctl: restore old -p handling

The previous version of sysctl had the form:
	sysctl -p [file]
In other words, it required a space between the -p and the [file].
Omitting the space would lead to an error.

The new version though is the opposite:
	sysctl -p[file]
In other words, it requires there to not be a space.

Considering the old behavior has been around for a decade, and runtime
checking for this mismatch in behavior is silly, and supporting the old
syntax is trivial, add support for it.

When '-p regexp' is glob is used to make reqular expression to be
expanded to argument list, which also means that -p option will
allow multiple files being specified as input.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
This commit is contained in:
Mike Frysinger
2012-05-03 01:24:20 -04:00
committed by Sami Kerola
parent 156dd0b5a3
commit e2987888e2
2 changed files with 73 additions and 44 deletions

111
sysctl.c
View File

@ -29,6 +29,7 @@
#include <dirent.h>
#include <errno.h>
#include <getopt.h>
#include <glob.h>
#include <libgen.h>
#include <limits.h>
#include <regex.h>
@ -492,53 +493,61 @@ static int Preload(const char *restrict const filename)
int n = 0;
int rc = 0;
char *name, *value;
glob_t globbuf;
int globerr;
int j;
fp = (filename[0] == '-' && !filename[1])
? stdin : fopen(filename, "r");
globerr = glob(filename, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf);
if (globerr != 0 && globerr != GLOB_NOMATCH)
xerr(EXIT_FAILURE, _("glob failed"));
if (!fp) {
xwarn(_("cannot open \"%s\""), filename);
return -1;
}
while (fgets(oneline, sizeof oneline, fp)) {
n++;
t = StripLeadingAndTrailingSpaces(oneline);
if (strlen(t) < 2)
continue;
if (*t == '#' || *t == ';')
continue;
name = strtok(t, "=");
if (!name || !*name) {
xwarnx(_("%s(%d): invalid syntax, continuing..."),
filename, n);
continue;
for (j = 0; j < globbuf.gl_pathc; j++) {
fp = (globbuf.gl_pathv[j][0] == '-' && !globbuf.gl_pathv[j][1])
? stdin : fopen(globbuf.gl_pathv[j], "r");
if (!fp) {
xwarn(_("cannot open \"%s\""), globbuf.gl_pathv[j]);
return -1;
}
StripLeadingAndTrailingSpaces(name);
while (fgets(oneline, sizeof oneline, fp)) {
n++;
t = StripLeadingAndTrailingSpaces(oneline);
if (pattern && !pattern_match(name, pattern))
continue;
if (strlen(t) < 2)
continue;
value = strtok(NULL, "\n\r");
if (!value || !*value) {
xwarnx(_("%s(%d): invalid syntax, continuing..."),
filename, n);
continue;
if (*t == '#' || *t == ';')
continue;
name = strtok(t, "=");
if (!name || !*name) {
xwarnx(_("%s(%d): invalid syntax, continuing..."),
globbuf.gl_pathv[j], n);
continue;
}
StripLeadingAndTrailingSpaces(name);
if (pattern && !pattern_match(name, pattern))
continue;
value = strtok(NULL, "\n\r");
if (!value || !*value) {
xwarnx(_("%s(%d): invalid syntax, continuing..."),
globbuf.gl_pathv[j], n);
continue;
}
while ((*value == ' ' || *value == '\t') && *value != 0)
value++;
/* should NameOnly affect this? */
sprintf(buffer, "%s=%s", name, value);
rc |= WriteSetting(buffer);
}
while ((*value == ' ' || *value == '\t') && *value != 0)
value++;
/* should NameOnly affect this? */
sprintf(buffer, "%s=%s", name, value);
rc |= WriteSetting(buffer);
fclose(fp);
}
fclose(fp);
return rc;
}
@ -641,7 +650,7 @@ int main(int argc, char *argv[])
bool preloadfileOpt = false;
int ReturnCode = 0;
int c;
const char *preloadfile = DEFAULT_PRELOAD;
const char *preloadfile = NULL;
enum {
DEPRECATED_OPTION = CHAR_MAX + 1,
@ -744,14 +753,28 @@ int main(int argc, char *argv[])
}
}
if (DisplayAllOpt)
return DisplayAll(PROC_PATH);
if (preloadfileOpt)
return Preload(preloadfile);
argc -= optind;
argv += optind;
if (DisplayAllOpt)
return DisplayAll(PROC_PATH);
if (preloadfileOpt) {
int ret = EXIT_SUCCESS, i;
if (!preloadfile) {
if (!argc) {
ret != Preload(DEFAULT_PRELOAD);
}
} else {
/* This happens when -pfile option is
* used without space. */
Preload(preloadfile);
}
for (i = 0; i < argc; i++)
Preload(argv[i]);
return ret;
}
if (argc < 1)
xerrx(EXIT_FAILURE, _("no variables specified\n"
"Try `%s --help' for more information."),