|
1 /* |
|
2 * LIBOIL - Library of Optimized Inner Loops |
|
3 * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org> |
|
4 * All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions |
|
8 * are met: |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * 2. Redistributions in binary form must reproduce the above copyright |
|
12 * notice, this list of conditions and the following disclaimer in the |
|
13 * documentation and/or other materials provided with the distribution. |
|
14 * |
|
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
|
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
|
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
25 * POSSIBILITY OF SUCH DAMAGE. |
|
26 */ |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include "config.h" |
|
30 #endif |
|
31 #include <liboil/liboilfunction.h> |
|
32 #include <liboil/liboildebug.h> |
|
33 #include <liboil/liboilutils.h> |
|
34 |
|
35 #ifdef HAVE_UNISTD_H |
|
36 #include <unistd.h> |
|
37 #endif |
|
38 #include <fcntl.h> |
|
39 #include <stdlib.h> |
|
40 #include <string.h> |
|
41 |
|
42 int |
|
43 get_file_int (const char *file, int *value) |
|
44 { |
|
45 char buffer[20]; |
|
46 char *endptr; |
|
47 int fd; |
|
48 int n; |
|
49 |
|
50 fd = open (file, O_RDONLY); |
|
51 if (fd < 0) return 0; |
|
52 |
|
53 n = read(fd, buffer, 19); |
|
54 close(fd); |
|
55 if (n < 0) { |
|
56 return 0; |
|
57 } |
|
58 buffer[n] = 0; |
|
59 |
|
60 *value = strtol (buffer, &endptr, 0); |
|
61 |
|
62 if (endptr[0] == 0 || endptr[0] == '\n') return 1; |
|
63 return 0; |
|
64 } |
|
65 |
|
66 char * |
|
67 get_file (const char *filename) |
|
68 { |
|
69 char *cpuinfo; |
|
70 int fd; |
|
71 int n; |
|
72 |
|
73 cpuinfo = malloc(4096); |
|
74 if (cpuinfo == NULL) return NULL; |
|
75 |
|
76 fd = open(filename, O_RDONLY); |
|
77 if (fd < 0) { |
|
78 free (cpuinfo); |
|
79 return NULL; |
|
80 } |
|
81 |
|
82 n = read(fd, cpuinfo, 4095); |
|
83 if (n < 0) { |
|
84 free (cpuinfo); |
|
85 close (fd); |
|
86 return NULL; |
|
87 } |
|
88 cpuinfo[n] = 0; |
|
89 |
|
90 close (fd); |
|
91 |
|
92 return cpuinfo; |
|
93 } |
|
94 |
|
95 char * |
|
96 get_cpuinfo_line (char *cpuinfo, const char *tag) |
|
97 { |
|
98 char *flags; |
|
99 char *end; |
|
100 char *colon; |
|
101 |
|
102 flags = strstr(cpuinfo,tag); |
|
103 if (flags == NULL) return NULL; |
|
104 |
|
105 end = strchr(flags, '\n'); |
|
106 if (end == NULL) return NULL; |
|
107 colon = strchr (flags, ':'); |
|
108 if (colon == NULL) return NULL; |
|
109 colon++; |
|
110 if(colon >= end) return NULL; |
|
111 |
|
112 return _strndup (colon, end-colon); |
|
113 } |
|
114 |
|
115 char * |
|
116 _strndup (const char *s, int n) |
|
117 { |
|
118 char *r; |
|
119 r = malloc (n+1); |
|
120 memcpy(r,s,n); |
|
121 r[n]=0; |
|
122 |
|
123 return r; |
|
124 } |
|
125 |
|
126 char ** |
|
127 strsplit (char *s) |
|
128 { |
|
129 char **list = NULL; |
|
130 char *tok; |
|
131 int n = 0; |
|
132 |
|
133 while (*s == ' ') s++; |
|
134 |
|
135 list = malloc (1 * sizeof(char *)); |
|
136 while (*s) { |
|
137 tok = s; |
|
138 while (*s && *s != ' ') s++; |
|
139 |
|
140 list[n] = _strndup (tok, s - tok); |
|
141 while (*s && *s == ' ') s++; |
|
142 list = realloc (list, (n + 2) * sizeof(char *)); |
|
143 n++; |
|
144 } |
|
145 |
|
146 list[n] = NULL; |
|
147 return list; |
|
148 } |
|
149 |
|
150 char * |
|
151 get_tag_value (char *s, const char *tag) |
|
152 { |
|
153 char *flags; |
|
154 char *end; |
|
155 char *colon; |
|
156 |
|
157 flags = strstr(s,tag); |
|
158 if (flags == NULL) return NULL; |
|
159 |
|
160 end = strchr(flags, '\n'); |
|
161 if (end == NULL) return NULL; |
|
162 colon = strchr (flags, ':'); |
|
163 if (colon == NULL) return NULL; |
|
164 colon++; |
|
165 if(colon >= end) return NULL; |
|
166 |
|
167 return _strndup (colon, end-colon); |
|
168 } |
|
169 |
|
170 |