1 /* 2 * uname - print system information. Jeff Comstock - Bloomington, MN USA 1992 3 * Usage: uname [-asnrvm] 4 * -s prints system name 5 * -n prints nodename 6 * -r prints software release 7 * -v prints os version 8 * -m prints machine name 9 * -a prinst all the above information 10 */ 11 #include <stdio.h> 12 #include <unistd.h> 13 #include <sys/utsname.h> 14 15 #define SYSNAME 0 16 #define NODENAME 1 17 #define RELEASE 2 18 #define VERSION 3 19 #define MACHINE 4 20 21 struct utsname u; 22 23 struct utstab { 24 char *str; 25 int requested; 26 } uttab[] = { 27 { u.sysname, 0 }, 28 { u.nodename, 0 }, 29 { u.release, 0 }, 30 { u.version, 0 }, 31 { u.machine, 0 } 32 }; 33 34 main(int argc, char **argv) { 35 char *opts="amnrsv"; 36 register int c,space, all=0; 37 38 if ( ! uname(&u) ) { 39 if ( argc == 1 ) { 40 puts(u.sysname); 41 } else { 42 while ( (c = getopt(argc,argv,opts)) != -1 ) { 43 switch ( c ) { 44 case 'a' : all++; 45 break; 46 case 'm' : uttab[MACHINE].requested++; 47 break; 48 case 'n' : uttab[NODENAME].requested++; 49 break; 50 case 'r' : uttab[RELEASE].requested++; 51 break; 52 case 's' : uttab[SYSNAME].requested++; 53 break; 54 case 'v' : uttab[VERSION].requested++; 55 break; 56 } 57 } 58 space=0; 59 for(c=0; c <= MACHINE; c++) { 60 if ( uttab[c].requested || all ) { 61 if ( space ) 62 putchar(' '); 63 printf("%s", uttab[c].str); 64 space++; 65 } 66 } 67 puts(""); 68 } 69 } 70 else 71 perror("uname"); 72 } 73