/* sysinfo.c * Neal Nelson * * Display: * CPU type and model, * kernel version, * amount of time since system was last booted in the form dd:hh:mm:ss * * COMMENTS: Clean up the time calculations (use %, dummy). */ #include #include #define MAXLINE 80 main(int argc, char *argv[]) { char buf[MAXLINE+1]; /* room for the string terminator zero */ FILE *fp; float uptime, idletime; int i,dd,hh,mm,ss; if (argc != 1) { printf("usage: sysinfo\n"); exit(1); } /* 1. print the cpu type and model in the 5th line */ if ( (fp = fopen("/proc/cpuinfo","r")) == NULL) { printf( "Unable to open /proc/cpuinfo\n" ); exit(1); } for (i = 0; i < 4; i++) fgets(buf, MAXLINE+1,fp); printf("%s",fgets(buf,MAXLINE+1,fp)); /* 2. print the system ostype and osrelease */ /* print the system ostype */ if ( (fp = fopen("/proc/sys/kernel/ostype","r")) == NULL) { printf( "Unable to open /proc/sys/kernel/ostype\n" ); exit(1); } printf("ostype : %s",fgets(buf,MAXLINE+1,fp)); /* print the osrelease */ if ( (fp = fopen("/proc/sys/kernel/osrelease","r")) == NULL) { printf( "Unable to open /proc/sys/kernel/osrelease\n" ); exit(1); } printf("osrelease : %s",fgets(buf,MAXLINE+1,fp)); /* 3. print the amout of time in seconds since the last system boot */ if ( (fp = fopen("/proc/uptime","r")) == NULL) { printf( "Unable to open /proc/uptime\n" ); exit(1); } /* fgets(buf, MAXLINE+1,fp); */ fscanf(fp, "%f %f", &uptime, &idletime); printf("uptime : %f seconds\n",uptime); mm = (int)(uptime/60); ss = (int)(uptime - mm*60); hh = (int)(mm/60); mm = mm - hh*60; dd = (int)(uptime/60/60/24); printf("uptime : %d:%d:%d:%d dd:hh:mm:ss\n",dd,hh,mm,ss); exit(0); }