File: os_test.c

package info (click to toggle)
valgrind 1%3A3.10.0-4~bpo7%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 97,940 kB
  • sloc: ansic: 589,429; xml: 21,096; exp: 8,751; cpp: 7,366; asm: 6,526; perl: 5,656; sh: 5,334; makefile: 4,946; haskell: 195
file content (91 lines) | stat: -rw-r--r-- 2,405 bytes parent folder | download | duplicates (3)
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

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

// This program determines which OS that this Valgrind installation
// supports, which depends on what was chosen at configure-time.
//
// We return:
// - 0 if the machine matches the asked-for OS and satisfies a
//     version requirement, if any
// - 1 if it doesn't match but does match the name of another OS
// - 2 if it doesn't match the name of any OS
// - 3 if there was a usage error (it also prints an error message)

// Nb: When updating this file for a new OS, add the name to
// 'all_OSes' as well as adding go().

#define False  0
#define True   1
typedef int    Bool;

char* all_OSes[] = {
   "linux",
   "darwin",
   NULL
};

#if defined(VGO_linux)
static Bool matches_version(char *min_version)
{
   int a1, a2, a3, g1, g2, g3;  // 'a' = actual;  'g' = given

   if (min_version == NULL)  return True;  // no version specified

   // get actual version number
   FILE *fp = fopen("/proc/sys/kernel/osrelease", "r");
   if (fp == NULL || fscanf(fp, "%d.%d.%d", &a1, &a2, &a3) != 3) return False;
   fclose(fp);

   // parse given version number
   if (sscanf(min_version, "%d.%d.%d", &g1, &g2, &g3) != 3) return False;

//   printf("actual %d %d %d\n", a1, a2,a3);
//   printf("given  %d %d %d\n", g1, g2,g3);

   if (a1 > g1) return True;
   if (a1 < g1) return False;
   if (a2 > g2) return True;
   if (a2 < g2) return False;
   if (a3 >= g3) return True;

   return False;
}
#endif

static Bool go(char* OS, char *min_version)
{ 
#if defined(VGO_linux)
   if ( 0 == strcmp( OS, "linux" ) && matches_version( min_version )) return True;

#elif defined(VGO_darwin)
   if ( 0 == strcmp( OS, "darwin" ) ) return True;

#else
#  error Unknown OS
#endif   // VGO_*

   return False;
}

//---------------------------------------------------------------------------
// main
//---------------------------------------------------------------------------
int main(int argc, char **argv)
{
   int i;
   if ( argc < 2 ) {
      fprintf( stderr, "usage: os_test <OS-type> [<min-version>]\n" );
      exit(3);             // Usage error.
   }
   if (go( argv[1], argv[2] )) {
      return 0;            // Matched.
   }
   for (i = 0; NULL != all_OSes[i]; i++) {
      if ( 0 == strcmp( argv[1], all_OSes[i] ) )
         return 1;         // Didn't match, but named another OS.
   }
   return 2;               // Didn't match any OSes.
}