summaryrefslogtreecommitdiffstats
path: root/clockB/util.c
blob: 8a1a6060c20de4934069d072c12a97a199c6e3a0 (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**************************************************************************

  This is a component of the hwclock program.

  This file contains the code for various basic utility routines
  needed by the other modules.

****************************************************************************/

#include <stdio.h>
#include <string.h>

#include "hwclock.h"

bool
is_in_cpuinfo(const char * const fmt, const char * const str) {
/*----------------------------------------------------------------------------
  Return true iff the /proc/cpuinfo file shows the value 'str' for the
  keyword 'fmt'.  Both arguments are null-terminated strings.

  If for any reason we can't read /proc/cpuinfo, return false.
-----------------------------------------------------------------------------*/
  FILE *cpuinfo;
  char field[256];
  char format[256];
  bool found;

  sprintf(format, "%s : %s", fmt, "%255s");

  found = FALSE;  /* initial value */
  if ((cpuinfo = fopen ("/proc/cpuinfo", "r")) != NULL) {
    while (!feof(cpuinfo)) {
      if (fscanf (cpuinfo, format, field) == 1) {
        if (strncmp(field, str, strlen(str)) == 0)
          found = TRUE;
        break;
      }
      fgets (field, 256, cpuinfo);
    }
    fclose(cpuinfo);
  }
  return found;
}



char *
ctime2(const time_t time) {
/*----------------------------------------------------------------------------
  Same as ctime() from the standard C library, except it takes a time_t
  as an argument instead of a pointer to a time_t, so it is much more
  useful.

  Also, don't include a newline at the end of the returned string.  If
  the user wants a newline, he can provide it himself.
  
  return value is in static storage within.
-----------------------------------------------------------------------------*/
  static char retval[30];

  strncpy(retval, ctime(&time), sizeof(retval));
  retval[sizeof(retval)-1] = '\0';

  /* Now chop off the last character, which is the newline */
  if (strlen(retval) >= 1)   /* for robustness */
    retval[strlen(retval)-1] = '\0';
  return(retval);

}



struct timeval
t2tv(time_t argument) {
/*----------------------------------------------------------------------------
   Convert from "time_t" format to "timeval" format.
-----------------------------------------------------------------------------*/
  struct timeval retval;

  retval.tv_sec = argument;
  retval.tv_usec = 0;
  return(retval);
}



float 
time_diff(struct timeval subtrahend, struct timeval subtractor) {
/*---------------------------------------------------------------------------
  The difference in seconds between two times in "timeval" format.
----------------------------------------------------------------------------*/
  return( (subtrahend.tv_sec - subtractor.tv_sec)
           + (subtrahend.tv_usec - subtractor.tv_usec) / 1E6 );
}


struct timeval
time_inc(struct timeval addend, float increment) {
/*----------------------------------------------------------------------------
  The time, in "timeval" format, which is <increment> seconds after
  the time <addend>.  Of course, <increment> may be negative.
-----------------------------------------------------------------------------*/
  struct timeval newtime;

  newtime.tv_sec = addend.tv_sec + (int) increment;
  newtime.tv_usec = addend.tv_usec + (increment - (int) increment) * 1E6;

  /* Now adjust it so that the microsecond value is between 0 and 1 million */
  if (newtime.tv_usec < 0) {
    newtime.tv_usec += 1E6;
    newtime.tv_sec -= 1;
  } else if (newtime.tv_usec >= 1E6) {
    newtime.tv_usec -= 1E6;
    newtime.tv_sec += 1;
  }
  return(newtime);
}