sysctl warns6 cleanup
Chris Pressey
cpressey at catseye.mine.nu
Sat Jan 8 13:25:29 PST 2005
OK, latest attempt attached.
I just noticed the new 'standardized' error message I added during the
conversion is a bit weird, though:
% ./sysctl foo.bar
sysctl: sysctlnametomib(foo.bar): No such file or directory
Should I try to preserve the old error message (for any script or
whatever that might be expecting it exactly)?
-Chris
On Sat, 8 Jan 2005 21:37:04 +0100
Joerg Sonnenberger <joerg at xxxxxxxxxxxxxxxxx> wrote:
> On Sat, Jan 08, 2005 at 12:14:40PM -0800, Chris Pressey wrote:
> > @@ -355,7 +353,7 @@
> > * may seem, there are various conflicting requirements.
> > */
> >
> > -static int
> > +static size_t
> > name2oid(char *name, int *oidp)
> > {
> > int oid[2];
> > @@ -367,14 +365,14 @@
> >
> > j = CTL_MAXNAME * sizeof(int);
> > i = sysctl(oid, 2, oidp, &j, name, strlen(name));
> > - if (i < 0)
> > - return i;
> > + if (i < 0)
> > + errx(1, "unknown oid '%s'", name);
> > j /= sizeof(int);
> > return (j);
> > }
>
> Eliminate this helper function and use sysctlnametomib from libc.
>
> > @@ -616,10 +615,11 @@
> > }
> >
> > static int
> > -sysctl_all (int *oid, int len)
> > +sysctl_all(int *oid, size_t len)
> > {
> > int name1[22], name2[22];
> > int i, j;
> > + unsigned int k;
>
> size_t k ? It seems you can merge i and j, name it retval or so :)
>
> Rest is fine.
>
> Joerg
>
Index: Makefile
===================================================================
RCS file: /home/dcvs/src/sbin/sysctl/Makefile,v
retrieving revision 1.2
diff -u -r1.2 Makefile
--- Makefile 17 Jun 2003 04:27:34 -0000 1.2
+++ Makefile 7 Jan 2005 03:06:34 -0000
@@ -4,5 +4,6 @@
PROG= sysctl
MAN= sysctl.8
+WARNS?= 6
.include <bsd.prog.mk>
Index: sysctl.c
===================================================================
RCS file: /home/dcvs/src/sbin/sysctl/sysctl.c,v
retrieving revision 1.6
diff -u -r1.6 sysctl.c
--- sysctl.c 18 Dec 2004 21:43:46 -0000 1.6
+++ sysctl.c 8 Jan 2005 21:03:44 -0000
@@ -56,13 +56,11 @@
static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
-static int oidfmt(int *, int, char *, u_int *);
-static void parse(char *);
-static int show_var(int *, int);
-static int sysctl_all (int *oid, int len);
-static int name2oid(char *, int *);
-
-static void set_T_dev_t (char *, void **, int *);
+static int oidfmt(int *, size_t, char *, u_int *);
+static void parse(const char *);
+static int show_var(int *, size_t);
+static int sysctl_all(int *, size_t);
+static void set_T_dev_t(const char *, void **, int *);
static void
usage(void)
@@ -143,9 +141,10 @@
* Set a new value if requested.
*/
static void
-parse(char *string)
+parse(const char *string)
{
- int len, i, j;
+ size_t len;
+ int i, j;
void *newval = 0;
int intval;
unsigned int uintval;
@@ -167,10 +166,10 @@
newval = cp;
newsize = strlen(cp);
}
- len = name2oid(bufp, mib);
- if (len < 0)
- errx(1, "unknown oid '%s'", bufp);
+ len = CTL_MAXNAME;
+ if (sysctlnametomib(bufp, mib, &len) < 0)
+ err(1, "sysctlnametomib(%s)", bufp);
if (oidfmt(mib, len, fmt, &kind))
err(1, "couldn't find format of oid '%s'", bufp);
@@ -326,7 +325,7 @@
}
static void
-set_T_dev_t (char *path, void **val, int *size)
+set_T_dev_t(const char *path, void **val, int *size)
{
static struct stat statb;
@@ -356,25 +355,7 @@
*/
static int
-name2oid(char *name, int *oidp)
-{
- int oid[2];
- int i;
- size_t j;
-
- oid[0] = 0;
- oid[1] = 3;
-
- j = CTL_MAXNAME * sizeof(int);
- i = sysctl(oid, 2, oidp, &j, name, strlen(name));
- if (i < 0)
- return i;
- j /= sizeof(int);
- return (j);
-}
-
-static int
-oidfmt(int *oid, int len, char *fmt, u_int *kind)
+oidfmt(int *oid, size_t len, char *fmt, u_int *kind)
{
int qoid[CTL_MAXNAME+2];
u_char buf[BUFSIZ];
@@ -405,7 +386,7 @@
*/
struct _foo {
int majdev;
- char *name;
+ const char *name;
} maj2name[] = {
{ 30, "ad" },
{ 0, "wd" },
@@ -456,10 +437,11 @@
*/
static int
-show_var(int *oid, int nlen)
+show_var(int *oid, size_t nlen)
{
u_char buf[BUFSIZ], *val, *p;
- char name[BUFSIZ], *fmt, *sep;
+ char name[BUFSIZ], *fmt;
+ const char *sep, *spacer;
int qoid[CTL_MAXNAME+2];
int i;
size_t j, len;
@@ -518,20 +500,20 @@
case 'A':
if (!nflag)
printf("%s%s", name, sep);
- printf("%.*s", len, p);
+ fwrite(p, len, 1, stdout);
return (0);
case 'I':
if (!nflag)
printf("%s%s", name, sep);
fmt++;
- val = "";
+ spacer = "";
while (len >= sizeof(int)) {
if(*fmt == 'U')
- printf("%s%u", val, *(unsigned int *)p);
+ printf("%s%u", spacer, *(unsigned int *)p);
else
- printf("%s%d", val, *(int *)p);
- val = " ";
+ printf("%s%d", spacer, *(int *)p);
+ spacer = " ";
len -= sizeof(int);
p += sizeof(int);
}
@@ -545,13 +527,13 @@
if (!strcmp(name, "machdep.guessed_bootdev"))
return machdep_bootdev(*(unsigned long *)p);
#endif
- val = "";
+ spacer = "";
while (len >= sizeof(long)) {
if(*fmt == 'U')
- printf("%s%lu", val, *(unsigned long *)p);
+ printf("%s%lu", spacer, *(unsigned long *)p);
else
- printf("%s%ld", val, *(long *)p);
- val = " ";
+ printf("%s%ld", spacer, *(long *)p);
+ spacer = " ";
len -= sizeof(long);
p += sizeof(long);
}
@@ -567,13 +549,13 @@
if (!nflag)
printf("%s%s", name, sep);
fmt++;
- val = "";
+ spacer = "";
while (len >= sizeof(int64_t)) {
if(*fmt == 'U')
- printf("%s%llu", val, (long long)*(u_int64_t *)p);
+ printf("%s%llu", spacer, (long long)*(u_int64_t *)p);
else
- printf("%s%lld", val, (long long)*(int64_t *)p);
- val = " ";
+ printf("%s%lld", spacer, (long long)*(int64_t *)p);
+ spacer = " ";
len -= sizeof(int64_t);
p += sizeof(int64_t);
}
@@ -616,11 +598,11 @@
}
static int
-sysctl_all (int *oid, int len)
+sysctl_all(int *oid, size_t len)
{
int name1[22], name2[22];
- int i, j;
- size_t l1, l2;
+ int retval;
+ size_t i, l1, l2;
name1[0] = 0;
name1[1] = 2;
@@ -634,12 +616,12 @@
}
for (;;) {
l2 = sizeof(name2);
- j = sysctl(name1, l1, name2, &l2, 0, 0);
- if (j < 0) {
+ retval = sysctl(name1, l1, name2, &l2, 0, 0);
+ if (retval < 0) {
if (errno == ENOENT)
return 0;
else
- err(1, "sysctl(getnext) %d %d", j, l2);
+ err(1, "sysctl(getnext) %d %d", retval, l2);
}
l2 /= sizeof(int);
@@ -651,8 +633,8 @@
if (name2[i] != oid[i])
return 0;
- i = show_var(name2, l2);
- if (!i && !bflag)
+ retval = show_var(name2, l2);
+ if (retval == 0 && !bflag)
putchar('\n');
memcpy(name1+2, name2, l2 * sizeof(int));
More information about the Submit
mailing list