uname.c

Go to the documentation of this file.
00001 /*  uname - print system name                   Author: Earl Chew */
00002 
00003 /* Print the following system information as returned by the uname()
00004  * function:
00005  *
00006  *      system name             Minix
00007  *      node name               waddles
00008  *      release name            1.5
00009  *      version                 10
00010  *      machine name            i86
00011  *      arch                    i86     (Minix specific)
00012  */
00013 
00014 #include <sys/types.h>
00015 #include <sys/utsname.h>
00016 #include <stdarg.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 #include <unistd.h>
00020 
00021 /* Define the uname components. */
00022 #define ALL      ((unsigned) 0x1F)
00023 #define SYSNAME  ((unsigned) 0x01)
00024 #define NODENAME ((unsigned) 0x02)
00025 #define RELEASE  ((unsigned) 0x04)
00026 #define VERSION  ((unsigned) 0x08)
00027 #define U_MACHINE  ((unsigned) 0x10)
00028 #define ARCH     ((unsigned) 0x20)
00029 
00030 _PROTOTYPE(int main, (int argc, char **argv ));
00031 _PROTOTYPE(void print, (int fd, ... ));
00032 _PROTOTYPE(void usage, (void ));
00033 
00034 #ifdef __STDC__
00035 void print(int fd, ...)
00036 #else
00037 void print(fd)
00038 int fd;
00039 #endif
00040 {
00041 /* Print a sequence of strings onto the named channel. */
00042   va_list argp;
00043   char *p;
00044 
00045   va_start(argp, fd);
00046   while (1) {
00047         p = va_arg(argp, char *);
00048         if (p == (char *) NULL) break;
00049         write(fd, p, strlen(p));
00050   }
00051   va_end(argp);
00052 }
00053 
00054 char *name;
00055 
00056 void usage()
00057 {
00058   print(STDERR_FILENO, "Usage: ", name, " -snrvmpa\n", (char *) NULL);
00059   exit(EXIT_FAILURE);
00060 }
00061 
00062 int main(argc, argv)
00063 int argc;
00064 char **argv;
00065 {
00066   int info;
00067   char *p;
00068   struct utsname un;
00069 
00070   name = strrchr(argv[0], '/');
00071   if (name == NULL) name = argv[0]; else name++;
00072 
00073   for (info = 0; argc > 1; argc--, argv++) {
00074         if (argv[1][0] == '-') {
00075                 for (p = &argv[1][1]; *p; p++) {
00076                         switch (*p) {
00077                                 case 'a': info |= ALL;      break;
00078                                 case 'm': info |= U_MACHINE;  break;
00079                                 case 'n': info |= NODENAME; break;
00080                                 case 'r': info |= RELEASE;  break;
00081                                 case 's': info |= SYSNAME;  break;
00082                                 case 'v': info |= VERSION;  break;
00083                                 case 'p': info |= ARCH;     break;
00084                                 default: usage();
00085                         }
00086                 }
00087         } else {
00088                 usage();
00089         }
00090   }
00091 
00092   if (info == 0) info = strcmp(name, "arch") == 0 ? ARCH : SYSNAME;
00093 
00094   if (uname(&un) != 0) {
00095         print(STDERR_FILENO, "unable to determine uname values\n", (char *) NULL);
00096         exit(EXIT_FAILURE);
00097   }
00098 
00099   if ((info & SYSNAME) != 0)
00100         print(STDOUT_FILENO, un.sysname, (char *) NULL);
00101   if ((info & NODENAME) != 0) {
00102         if ((info & (SYSNAME)) != 0)
00103                 print(STDOUT_FILENO, " ", (char *) NULL);
00104         print(STDOUT_FILENO, un.nodename, (char *) NULL);
00105   }
00106   if ((info & RELEASE) != 0) {
00107         if ((info & (SYSNAME|NODENAME)) != 0)
00108                 print(STDOUT_FILENO, " ", (char *) NULL);
00109         print(STDOUT_FILENO, un.release, (char *) NULL);
00110   }
00111   if ((info & VERSION) != 0) {
00112         if ((info & (SYSNAME|NODENAME|RELEASE)) != 0)
00113                 print(STDOUT_FILENO, " ", (char *) NULL);
00114         print(STDOUT_FILENO, un.version, (char *) NULL);
00115   }
00116   if ((info & U_MACHINE) != 0) {
00117         if ((info & (SYSNAME|NODENAME|RELEASE|VERSION)) != 0)
00118                 print(STDOUT_FILENO, " ", (char *) NULL);
00119         print(STDOUT_FILENO, un.machine, (char *) NULL);
00120   }
00121   if ((info & ARCH) != 0) {
00122         if ((info & (SYSNAME|NODENAME|RELEASE|VERSION|U_MACHINE)) != 0)
00123                 print(STDOUT_FILENO, " ", (char *) NULL);
00124         print(STDOUT_FILENO, un.arch, (char *) NULL);
00125   }
00126   print(STDOUT_FILENO, "\n", (char *) NULL);
00127   return EXIT_SUCCESS;
00128 }

Generated on Fri Apr 14 22:57:12 2006 for minix by  doxygen 1.4.6