File: sysinfo_common.h

package info (click to toggle)
linuxlogo 2.07-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 296 kB
  • ctags: 73
  • sloc: ansic: 982; sh: 125; makefile: 60
file content (73 lines) | stat: -rw-r--r-- 2,282 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* sysinfo_common.h                                               *\
\* Just trying to cut back on overly-repeated code.  --vmw        */


/* The following utility functions stolen from my game SEABATTLE */

int my_string_comp(const char *cs, const char *ct)
{                           /* partly borrowed /usr/src/linux/lib/string.c */
      register signed char __res;  /* like strcmp, but case-insensitive    */
   
      while(1) {
	 if ((__res = toupper(*cs)-toupper(*ct++))!=0 || !*cs++) break;
      }
      return __res;
}

char *read_string_from_disk(FILE *fff,char *string)
{                                 /* My own version of fgets */
     int ch,i=0;                  /* Handles \n much better */
     char temp[150];
   
     strcpy(temp,"");
     while ((ch=fgetc(fff))==' ');
     while ( (ch!='\n') && (ch!=EOF) ) {
	        temp[i]=ch; i++;
	        ch=fgetc(fff);
     }
     if(ch==EOF) return NULL;
     temp[i]='\0';
     strcpy(string,temp);
     return string;
}

char *linux_get_proc_uptime (char *string) 
{            /* This code modeled on the linux sh-utils 1.16 uptime.c code */
    FILE *fff;
    float uptime_seconds;
    int up_days,up_hrs,up_mins;
    char temp_string[BUFSIZ];
   
    fff=fopen("/proc/uptime","r");
    if (fff!=NULL) {
       fscanf(fff,"%f",&uptime_seconds);
       fclose (fff);
    
       up_days=uptime_seconds/86400;
       up_hrs=(uptime_seconds-(up_days*86400))/3600;
       up_mins=(uptime_seconds-(up_days*86400)-(up_hrs*3600))/60;
  
       if (up_days<=0) 
	  sprintf(temp_string,"Uptime %d %s %d %s",
		  up_hrs,(up_hrs==1 ? "hour":"hours"),
		  up_mins,(up_mins==1 ? "minute":"minutes"));
       else 
	  sprintf(temp_string,"Uptime %d %s %d %s %d %s",
		  up_days,(up_days==1 ? "day":"days"),
		  up_hrs,(up_hrs==1 ? "hour":"hours"),
		  up_mins,(up_mins==1 ? "minute":"minutes"));
       strcpy(string,temp_string);
       return string;
    }
    return NULL;
}

char *utmp_get_uptime(char *string) 
{
    /* To implement uptime on architechtures w/o a /proc/uptime one *\
    \* has to scan the /var/utmp file.  Very annoying thing to do   */
    /* Check out the linux sh-utils uptime.c for a reference.       *\
    \* Currently not implemented here.                              */
    return NULL;
   
}