Upload Tizen:Base source
[framework/base/util-linux-ng.git] / tests / helpers / test_sysinfo.c
1 /*
2  * Copyright (C) 2007 Karel Zak <kzak@redhat.com>
3  *
4  * This file is part of util-linux-ng.
5  *
6  * This file is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This file is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <endian.h>
22 #include <limits.h>
23
24 #if !defined __BYTE_ORDER || !(__BYTE_ORDER == __LITTLE_ENDIAN) && !(__BYTE_ORDER == __BIG_ENDIAN)
25 #error missing __BYTE_ORDER
26 #endif
27
28 typedef struct {
29         const char      *name;
30         int             (*fnc)(void);
31 } mntHlpfnc;
32
33 int
34 hlp_wordsize(void)
35 {
36         printf("%d\n", __WORDSIZE);
37         return 0;
38 }
39
40 int
41 hlp_endianness(void)
42 {
43 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
44         printf("LE\n");
45 #else
46         printf("BE\n");
47 #endif
48         return 0;
49 }
50
51
52 int
53 hlp_pagesize(void)
54 {
55         printf("%d\n", getpagesize());
56         return 0;
57 }
58
59 int
60 hlp_int_max(void)
61 {
62         printf("%d\n", INT_MAX);
63         return 0;
64 }
65
66 int
67 hlp_uint_max(void)
68 {
69         printf("%u\n", UINT_MAX);
70         return 0;
71 }
72
73 int
74 hlp_long_max(void)
75 {
76         printf("%ld\n", LONG_MAX);
77         return 0;
78 }
79
80 int
81 hlp_ulong_max(void)
82 {
83         printf("%lu\n", ULONG_MAX);
84         return 0;
85 }
86
87 int
88 hlp_ulong_max32(void)
89 {
90 #if __WORDSIZE == 64
91         printf("%lu\n", ULONG_MAX >> 32);
92 #else
93         printf("%lu\n", ULONG_MAX);
94 #endif
95         return 0;
96 }
97
98 mntHlpfnc hlps[] =
99 {
100         { "WORDSIZE",   hlp_wordsize    },
101         { "pagesize",   hlp_pagesize    },
102         { "INT_MAX",    hlp_int_max     },
103         { "UINT_MAX",   hlp_uint_max    },
104         { "LONG_MAX",   hlp_long_max    },
105         { "ULONG_MAX",  hlp_ulong_max   },
106         { "ULONG_MAX32",hlp_ulong_max32 },
107         { "byte-order", hlp_endianness  },
108         { NULL, NULL }
109 };
110
111 int
112 main(int argc, char **argv)
113 {
114         int re = 0;
115         mntHlpfnc *fn;
116
117         if (argc == 1) {
118                 for (fn = hlps; fn->name; fn++) {
119                         printf("%15s: ", fn->name);
120                         re += fn->fnc();
121                 }
122         } else {
123                 int i;
124
125                 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
126                         printf("%s <option>\n", argv[0]);
127                         fputs("options:\n", stdout);
128                         for (fn = hlps; fn->name; fn++)
129                                 printf("\t%s\n", fn->name);
130                         exit(EXIT_SUCCESS);
131                 }
132
133                 for (i=1; i < argc; i++) {
134                         for (fn = hlps; fn->name; fn++) {
135                                 if (strcmp(fn->name, argv[i]) == 0)
136                                         re += fn->fnc();
137                         }
138                 }
139         }
140
141         exit(re ? EXIT_FAILURE : EXIT_SUCCESS);
142 }
143