blob: b755e30706b7bf61cb7b7aff99c9da1bd1b595ee [file] [log] [blame]
njn61485ab2009-03-04 04:15:16 +00001
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6// This program determines which OS that this Valgrind installation
7// supports, which depends on what was chosen at configure-time.
8//
9// We return:
florianf897b992011-07-31 03:22:45 +000010// - 0 if the machine matches the asked-for OS and satisfies a
11// version requirement, if any
njn61485ab2009-03-04 04:15:16 +000012// - 1 if it doesn't match but does match the name of another OS
13// - 2 if it doesn't match the name of any OS
14// - 3 if there was a usage error (it also prints an error message)
15
16// Nb: When updating this file for a new OS, add the name to
17// 'all_OSes' as well as adding go().
18
19#define False 0
20#define True 1
21typedef int Bool;
22
23char* all_OSes[] = {
24 "linux",
njnf76d27a2009-05-28 01:53:07 +000025 "darwin",
njn61485ab2009-03-04 04:15:16 +000026 NULL
27};
28
florianf897b992011-07-31 03:22:45 +000029#if defined(VGO_linux)
30static Bool matches_version(char *min_version)
31{
32 int a1, a2, a3, g1, g2, g3; // 'a' = actual; 'g' = given
33
34 if (min_version == NULL) return True; // no version specified
35
36 // get actual version number
37 FILE *fp = fopen("/proc/sys/kernel/osrelease", "r");
38 if (fp == NULL || fscanf(fp, "%d.%d.%d", &a1, &a2, &a3) != 3) return False;
39 fclose(fp);
40
41 // parse given version number
42 if (sscanf(min_version, "%d.%d.%d", &g1, &g2, &g3) != 3) return False;
43
44// printf("actual %d %d %d\n", a1, a2,a3);
45// printf("given %d %d %d\n", g1, g2,g3);
46
47 if (a1 > g1) return True;
48 if (a1 < g1) return False;
49 if (a2 > g2) return True;
50 if (a2 < g2) return False;
51 if (a3 >= g3) return True;
52
53 return False;
54}
55#endif
56
57static Bool go(char* OS, char *min_version)
njn61485ab2009-03-04 04:15:16 +000058{
59#if defined(VGO_linux)
florianf897b992011-07-31 03:22:45 +000060 if ( 0 == strcmp( OS, "linux" ) && matches_version( min_version )) return True;
njn61485ab2009-03-04 04:15:16 +000061
njnf76d27a2009-05-28 01:53:07 +000062#elif defined(VGO_darwin)
63 if ( 0 == strcmp( OS, "darwin" ) ) return True;
njn61485ab2009-03-04 04:15:16 +000064
65#else
66# error Unknown OS
67#endif // VGO_*
68
69 return False;
70}
71
72//---------------------------------------------------------------------------
73// main
74//---------------------------------------------------------------------------
75int main(int argc, char **argv)
76{
77 int i;
florianf897b992011-07-31 03:22:45 +000078 if ( argc < 2 ) {
79 fprintf( stderr, "usage: os_test <OS-type> [<min-version>]\n" );
njn61485ab2009-03-04 04:15:16 +000080 exit(3); // Usage error.
81 }
florianf897b992011-07-31 03:22:45 +000082 if (go( argv[1], argv[2] )) {
njn61485ab2009-03-04 04:15:16 +000083 return 0; // Matched.
84 }
85 for (i = 0; NULL != all_OSes[i]; i++) {
86 if ( 0 == strcmp( argv[1], all_OSes[i] ) )
87 return 1; // Didn't match, but named another OS.
88 }
89 return 2; // Didn't match any OSes.
90}
91