sysctl: use libc error printing facilities

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2011-10-12 22:35:56 +02:00
parent f70cdd0666
commit 9f89e336c3

View File

@ -151,7 +151,7 @@ static int ReadSetting(const char *restrict const name) {
struct stat ts; struct stat ts;
if (!name || !*name) { if (!name || !*name) {
fprintf(stderr, _("error: \"%s\" is an unknown key\n"), name); warnx(_("\"%s\" is an unknown key"), name);
return -1; return -1;
} }
@ -176,7 +176,7 @@ static int ReadSetting(const char *restrict const name) {
if (stat(tmpname, &ts) < 0) { if (stat(tmpname, &ts) < 0) {
if (!IgnoreError) { if (!IgnoreError) {
perror(tmpname); warn(_("cannot stat %s"), tmpname);
rc = -1; rc = -1;
} }
goto out; goto out;
@ -199,16 +199,16 @@ static int ReadSetting(const char *restrict const name) {
switch(errno) { switch(errno) {
case ENOENT: case ENOENT:
if (!IgnoreError) { if (!IgnoreError) {
fprintf(stderr, _("error: \"%s\" is an unknown key\n"), outname); warnx(_("\"%s\" is an unknown key"), outname);
rc = -1; rc = -1;
} }
break; break;
case EACCES: case EACCES:
fprintf(stderr, _("error: permission denied on key '%s'\n"), outname); warnx(_("permission denied on key '%s'"), outname);
rc = -1; rc = -1;
break; break;
default: default:
fprintf(stderr, _("error: \"%s\" reading key \"%s\"\n"), strerror(errno), outname); warn(_("reading key \"%s\""), outname);
rc = -1; rc = -1;
break; break;
} }
@ -236,7 +236,7 @@ static int ReadSetting(const char *restrict const name) {
} else { } else {
switch(errno) { switch(errno) {
case EACCES: case EACCES:
fprintf(stderr, _("error: permission denied on key '%s'\n"), outname); warnx(_("permission denied on key '%s'"), outname);
rc = -1; rc = -1;
break; break;
case EISDIR:{ case EISDIR:{
@ -249,7 +249,7 @@ static int ReadSetting(const char *restrict const name) {
goto out; goto out;
} }
default: default:
fprintf(stderr, _("error: \"%s\" reading key \"%s\"\n"), strerror(errno), outname); warnx(_("reading key \"%s\""), outname);
rc = -1; rc = -1;
case 0: case 0:
break; break;
@ -279,7 +279,7 @@ static int DisplayAll(const char *restrict const path) {
dp = opendir(path); dp = opendir(path);
if (!dp) { if (!dp) {
fprintf(stderr, _("error: unable to open directory \"%s\"\n"), path); warnx(_("unable to open directory \"%s\""), path);
rc = -1; rc = -1;
} else { } else {
readdir(dp); // skip . readdir(dp); // skip .
@ -290,7 +290,7 @@ static int DisplayAll(const char *restrict const path) {
sprintf(tmpdir, "%s%s", path, de->d_name); sprintf(tmpdir, "%s%s", path, de->d_name);
rc2 = stat(tmpdir, &ts); rc2 = stat(tmpdir, &ts);
if (rc2 != 0) { if (rc2 != 0) {
perror(tmpdir); warn(_("cannot stat %s"), tmpdir);
} else { } else {
if (S_ISDIR(ts.st_mode)) { if (S_ISDIR(ts.st_mode)) {
strcat(tmpdir, "/"); strcat(tmpdir, "/");
@ -328,14 +328,14 @@ static int WriteSetting(const char *setting) {
equals = strchr(setting, '='); equals = strchr(setting, '=');
if (!equals) { if (!equals) {
fprintf(stderr, _("error: \"%s\" must be of the form name=value\n"), setting); warnx(_("\"%s\" must be of the form name=value"), setting);
return -1; return -1;
} }
value = equals + 1; /* point to the value in name=value */ value = equals + 1; /* point to the value in name=value */
if (!*name || !*value || name == equals) { if (!*name || !*value || name == equals) {
fprintf(stderr, _("error: Malformed setting \"%s\"\n"), setting); warnx(_("Malformed setting \"%s\""), setting);
return -2; return -2;
} }
@ -354,19 +354,19 @@ static int WriteSetting(const char *setting) {
if (stat(tmpname, &ts) < 0) { if (stat(tmpname, &ts) < 0) {
if (!IgnoreError) { if (!IgnoreError) {
perror(tmpname); warn(_("cannot stat %s"), tmpname);
rc = -1; rc = -1;
} }
goto out; goto out;
} }
if ((ts.st_mode & S_IWUSR) == 0) { if ((ts.st_mode & S_IWUSR) == 0) {
fprintf(stderr, _("error: \"%s\" setting key \"%s\"\n"), strerror(EACCES), outname); warn(_("setting key \"%s\""), outname);
goto out; goto out;
} }
if (S_ISDIR(ts.st_mode)) { if (S_ISDIR(ts.st_mode)) {
fprintf(stderr, _("error: \"%s\" setting key \"%s\"\n"), strerror(EACCES), outname); warn(_("setting key \"%s\""), outname);
goto out; goto out;
} }
@ -376,28 +376,28 @@ static int WriteSetting(const char *setting) {
switch(errno) { switch(errno) {
case ENOENT: case ENOENT:
if (!IgnoreError) { if (!IgnoreError) {
fprintf(stderr, _("error: \"%s\" is an unknown key\n"), outname); warnx(_("\"%s\" is an unknown key"), outname);
rc = -1; rc = -1;
} }
break; break;
case EACCES: case EACCES:
fprintf(stderr, _("error: permission denied on key '%s'\n"), outname); warnx(_("permission denied on key '%s'"), outname);
rc = -1; rc = -1;
break; break;
default: default:
fprintf(stderr, _("error: \"%s\" setting key \"%s\"\n"), strerror(errno), outname); warn(_("setting key \"%s\""), outname);
rc = -1; rc = -1;
break; break;
} }
} else { } else {
rc = fprintf(fp, "%s\n", value); rc = fprintf(fp, "%s\n", value);
if (rc < 0) { if (rc < 0) {
fprintf(stderr, _("error: \"%s\" setting key \"%s\"\n"), strerror(errno), outname); warn(_("setting key \"%s\""), outname);
fclose(fp); fclose(fp);
} else { } else {
rc=fclose(fp); rc=fclose(fp);
if (rc != 0) if (rc != 0)
fprintf(stderr, _("error: \"%s\" setting key \"%s\"\n"), strerror(errno), outname); warn(_("setting key \"%s\""), outname);
} }
if (rc==0 && !Quiet) { if (rc==0 && !Quiet) {
if (NameOnly) { if (NameOnly) {
@ -456,7 +456,7 @@ static int Preload(const char *restrict const filename) {
; ;
if (!fp) { if (!fp) {
fprintf(stderr, _("error: unable to open preload file \"%s\"\n"), filename); warn(_("cannot open \"%s\""), filename);
return -1; return -1;
} }
@ -472,7 +472,7 @@ static int Preload(const char *restrict const filename) {
name = strtok(t, "="); name = strtok(t, "=");
if (!name || !*name) { if (!name || !*name) {
fprintf(stderr, _("warning: %s(%d): invalid syntax, continuing...\n"), filename, n); warnx(_("%s(%d): invalid syntax, continuing..."), filename, n);
continue; continue;
} }
@ -484,7 +484,7 @@ static int Preload(const char *restrict const filename) {
value = strtok(NULL, "\n\r"); value = strtok(NULL, "\n\r");
if (!value || !*value) { if (!value || !*value) {
fprintf(stderr, _("warning: %s(%d): invalid syntax, continuing...\n"), filename, n); warnx(_("%s(%d): invalid syntax, continuing..."), filename, n);
continue; continue;
} }