Deleted Added
full compact
sysctl.c (41019) sysctl.c (42456)
1/*
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
1/*
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
45 "$Id: sysctl.c,v 1.18 1998/08/25 07:38:19 dfr Exp $";
45 "$Id: sysctl.c,v 1.19 1998/11/08 19:27:43 phk Exp $";
46#endif /* not lint */
47
48#include <sys/types.h>
49#include <sys/stat.h>
50#include <sys/sysctl.h>
51#include <sys/resource.h>
52
53#include <ctype.h>
54#include <err.h>
55#include <errno.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <unistd.h>
60
46#endif /* not lint */
47
48#include <sys/types.h>
49#include <sys/stat.h>
50#include <sys/sysctl.h>
51#include <sys/resource.h>
52
53#include <ctype.h>
54#include <err.h>
55#include <errno.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <unistd.h>
60
61static int Aflag, aflag, nflag, wflag, Xflag, bflag;
61static int Aflag, aflag, bflag, dflag, nflag, wflag, Xflag;
62
63static int oidfmt(int *, int, char *, u_int *);
64static void parse(char *);
65static int show_var(int *, int);
66static int sysctl_all (int *oid, int len);
67static int name2oid(char *, int *);
68
69static void
70usage(void)
71{
72
62
63static int oidfmt(int *, int, char *, u_int *);
64static void parse(char *);
65static int show_var(int *, int);
66static int sysctl_all (int *oid, int len);
67static int name2oid(char *, int *);
68
69static void
70usage(void)
71{
72
73 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
74 "usage: sysctl [-bnX] variable ...",
75 " sysctl [-bnX] -w variable=value ...",
76 " sysctl [-bnX] -a",
77 " sysctl [-bnX] -A");
73 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
74 "usage: sysctl [-bdn] variable ...",
75 " sysctl [-bn] -w variable=value ...",
76 " sysctl [-bdn] -a",
77 " sysctl [-bdn] -A",
78 " sysctl [-bdn] -X");
78 exit(1);
79}
80
81int
82main(int argc, char **argv)
83{
84 int ch;
85 setbuf(stdout,0);
86 setbuf(stderr,0);
87
79 exit(1);
80}
81
82int
83main(int argc, char **argv)
84{
85 int ch;
86 setbuf(stdout,0);
87 setbuf(stderr,0);
88
88 while ((ch = getopt(argc, argv, "AabnwX")) != -1) {
89 while ((ch = getopt(argc, argv, "AabdnwX")) != -1) {
89 switch (ch) {
90 case 'A': Aflag = 1; break;
91 case 'a': aflag = 1; break;
92 case 'b': bflag = 1; break;
90 switch (ch) {
91 case 'A': Aflag = 1; break;
92 case 'a': aflag = 1; break;
93 case 'b': bflag = 1; break;
94 case 'd': dflag = 1; break;
93 case 'n': nflag = 1; break;
94 case 'w': wflag = 1; break;
95 case 'X': Xflag = Aflag = 1; break;
96 default: usage();
97 }
98 }
99 argc -= optind;
100 argv += optind;
101
95 case 'n': nflag = 1; break;
96 case 'w': wflag = 1; break;
97 case 'X': Xflag = Aflag = 1; break;
98 default: usage();
99 }
100 }
101 argc -= optind;
102 argv += optind;
103
104 if (wflag && (Aflag || aflag || dflag))
105 usage();
102 if (Aflag || aflag)
103 exit (sysctl_all(0, 0));
104 if (argc == 0)
105 usage();
106 while (argc-- > 0)
107 parse(*argv++);
108 exit(0);
109}

--- 221 unchanged lines hidden (view full) ---

331 * Returns one if didn't know what to do with this.
332 * Return minus one if we had errors.
333 */
334
335static int
336show_var(int *oid, int nlen)
337{
338 u_char buf[BUFSIZ], *val, *p;
106 if (Aflag || aflag)
107 exit (sysctl_all(0, 0));
108 if (argc == 0)
109 usage();
110 while (argc-- > 0)
111 parse(*argv++);
112 exit(0);
113}

--- 221 unchanged lines hidden (view full) ---

335 * Returns one if didn't know what to do with this.
336 * Return minus one if we had errors.
337 */
338
339static int
340show_var(int *oid, int nlen)
341{
342 u_char buf[BUFSIZ], *val, *p;
339 char name[BUFSIZ], *fmt;
343 char name[BUFSIZ], descr[BUFSIZ], *fmt;
340 int qoid[CTL_MAXNAME+2];
341 int i;
342 size_t j, len;
343 u_int kind;
344 int (*func)(int, void *) = 0;
345
344 int qoid[CTL_MAXNAME+2];
345 int i;
346 size_t j, len;
347 u_int kind;
348 int (*func)(int, void *) = 0;
349
350 qoid[0] = 0;
351 memcpy(qoid + 2, oid, nlen * sizeof(int));
352
353 qoid[1] = 1;
354 j = sizeof name;
355 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
356 if (i || !j)
357 err(1, "sysctl name %d %d %d", i, j, errno);
358
359 if (dflag) {
360 qoid[1] = 5;
361 j = sizeof descr;
362 i = sysctl(qoid, nlen + 2, descr, &j, 0, 0);
363 if (i || !j)
364 err(1, "sysctl name %d %d %d", i, j, errno);
365 if (!nflag)
366 printf("%s: ", name);
367 printf("%s", descr[0] ? descr : "[no description]");
368 return (0);
369 }
370
346 /* find an estimate of how much we need for this var */
347 j = 0;
348 i = sysctl(oid, nlen, 0, &j, 0, 0);
349 j += j; /* we want to be sure :-) */
350
351 val = alloca(j);
352 len = j;
353 i = sysctl(oid, nlen, val, &len, 0, 0);
354 if (i || !len)
355 return (1);
356
357 if (bflag) {
358 fwrite(val, 1, len, stdout);
359 return (0);
360 }
361
371 /* find an estimate of how much we need for this var */
372 j = 0;
373 i = sysctl(oid, nlen, 0, &j, 0, 0);
374 j += j; /* we want to be sure :-) */
375
376 val = alloca(j);
377 len = j;
378 i = sysctl(oid, nlen, val, &len, 0, 0);
379 if (i || !len)
380 return (1);
381
382 if (bflag) {
383 fwrite(val, 1, len, stdout);
384 return (0);
385 }
386
362 qoid[0] = 0;
363 qoid[1] = 4;
387 qoid[1] = 4;
364 memcpy(qoid + 2, oid, nlen * sizeof(int));
365
366 j = sizeof buf;
367 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
368 if (i || !j)
369 err(1, "sysctl fmt %d %d %d", i, j, errno);
370
371 kind = *(u_int *)buf;
372
373 fmt = (char *)(buf + sizeof(u_int));
374
388 j = sizeof buf;
389 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
390 if (i || !j)
391 err(1, "sysctl fmt %d %d %d", i, j, errno);
392
393 kind = *(u_int *)buf;
394
395 fmt = (char *)(buf + sizeof(u_int));
396
375 qoid[1] = 1;
376 j = sizeof name;
377 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
378 if (i || !j)
379 err(1, "sysctl name %d %d %d", i, j, errno);
380
381 p = val;
382 switch (*fmt) {
383 case 'A':
384 if (!nflag)
385 printf("%s: ", name);
386 printf("%s", p);
387 return (0);
388

--- 98 unchanged lines hidden ---
397 p = val;
398 switch (*fmt) {
399 case 'A':
400 if (!nflag)
401 printf("%s: ", name);
402 printf("%s", p);
403 return (0);
404

--- 98 unchanged lines hidden ---