[PATCH] Update usr.bin/uname/uname.c

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Update usr.bin/uname/uname.c

Przemyslaw Nowaczyk
- sorted #includes, arguments in getopt(3) and variable declarations,
- since fputs(3) is able to set errno, i think it's good to check for that,
- KNF,
- killed some whitespaces,
- make lint happy.

Index: uname.c
===================================================================
RCS file: /cvs/src/usr.bin/uname/uname.c,v
retrieving revision 1.9
diff -u -r1.9 uname.c
--- uname.c 2005/12/28 15:41:29 1.9
+++ uname.c 2006/08/29 11:57:21
@@ -15,7 +15,7 @@
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *      This product includes software developed by Winning Strategies, Inc.
- * 4. The name of Winning Strategies, Inc. may not be used to endorse or
+ * 4. The name of Winning Strategies, Inc. may not be used to endorse or
  *    promote products derived from this software without specific prior
  *    written permission.
  *
@@ -36,15 +36,16 @@
 #endif /* not lint */
 
 #include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/utsname.h>
+
+#include <err.h>
+#include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <locale.h>
 #include <unistd.h>
-#include <sys/utsname.h>
-#include <sys/sysctl.h>
-#include <err.h>
 
-static void usage(void);
+__dead void usage(void);
 
 #define PRINT_SYSNAME 0x01
 #define PRINT_NODENAME 0x02
@@ -57,15 +58,15 @@
 int
 main(int argc, char *argv[])
 {
- struct utsname u;
- int c;
- int space = 0;
- int print_mask = 0;
+ struct utsname u;
+ int c, print_mask, space;
 
- setlocale(LC_ALL, "");
+ (void)setlocale(LC_ALL, "");
 
- while ((c = getopt(argc,argv,"amnrsvp")) != -1 ) {
- switch ( c ) {
+ print_mask = space = 0;
+
+ while ((c = getopt(argc, argv, "amnprsv")) != -1)
+ switch (c) {
  case 'a':
  print_mask |= PRINT_ALL;
  break;
@@ -75,80 +76,87 @@
  case 'n':
  print_mask |= PRINT_NODENAME;
  break;
- case 'r':
+ case 'p':
+ print_mask |= PRINT_PROCESSOR;
+ break;
+ case 'r':
  print_mask |= PRINT_RELEASE;
  break;
- case 's':
+ case 's':
  print_mask |= PRINT_SYSNAME;
  break;
  case 'v':
  print_mask |= PRINT_VERSION;
  break;
- case 'p':
- print_mask |= PRINT_PROCESSOR;
- break;
  default:
  usage();
  /* NOTREACHED */
  }
- }
-
- if (optind != argc) {
+ argc -= optind;
+ argv += optind;
+
+ if (argc)
  usage();
- /* NOTREACHED */
- }
 
- if (!print_mask) {
+ if (!print_mask)
  print_mask = PRINT_SYSNAME;
- }
 
- if (uname(&u)) {
+ if (uname(&u))
  err(1, NULL);
- /* NOTREACHED */
- }
 
  if (print_mask & PRINT_SYSNAME) {
  space++;
- fputs(u.sysname, stdout);
+ if (fputs(u.sysname, stdout) == EOF)
+ err(1, "fputs");
  }
  if (print_mask & PRINT_NODENAME) {
- if (space++) putchar(' ');
- fputs(u.nodename, stdout);
+ if (space++)
+ (void)putchar(' ');
+ if (fputs(u.nodename, stdout) == EOF)
+ err(1, "fputs");
  }
  if (print_mask & PRINT_RELEASE) {
- if (space++) putchar(' ');
- fputs(u.release, stdout);
+ if (space++)
+ (void)putchar(' ');
+ if (fputs(u.release, stdout) == EOF)
+ err(1, "fputs");
  }
  if (print_mask & PRINT_VERSION) {
- if (space++) putchar(' ');
- fputs(u.version, stdout);
+ if (space++)
+ (void)putchar(' ');
+ if (fputs(u.version, stdout) == EOF)
+ err(1, "fputs");
  }
  if (print_mask & PRINT_MACHINE) {
- if (space++) putchar(' ');
- fputs(u.machine, stdout);
+ if (space++)
+ (void)putchar(' ');
+ if (fputs(u.machine, stdout) == EOF)
+ err(1, "fputs");
  }
  if (print_mask & PRINT_PROCESSOR) {
- char buf[1024];
- size_t len;
- int mib[2];
+ char buf[1024];
+ int mib[2];
+ size_t len;
 
- if (space++) putchar(' ');
+ if (space++)
+ (void)putchar(' ');
  mib[0] = CTL_HW;
  mib[1] = HW_MODEL;
  len = sizeof(buf);
  if (sysctl(mib, 2, buf, &len, NULL, 0) == -1)
  err(1, "sysctl");
- printf("%.*s", (int)len, buf);
- }
- putchar('\n');
+ (void)printf("%.*s", (int)len, buf);
+ }
+ (void)putchar('\n');
 
  exit(0);
- /* NOTREACHED */
 }
 
-static void
+__dead void
 usage(void)
 {
- fprintf(stderr, "usage: uname [-amnprsv]\n");
+ extern char *__progname;
+
+ (void)fprintf(stderr, "usage: %s [-amnprsv]\n", __progname);
  exit(1);
 }

--
Przemyslaw Nowaczyk <[hidden email]>
CS student @ Poznan University of Technology
http://unixlab.cs.put.poznan.pl/~inf73015/