1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/liboil/src/liboilutils.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,170 @@
1.4 +/*
1.5 + * LIBOIL - Library of Optimized Inner Loops
1.6 + * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
1.7 + * All rights reserved.
1.8 + *
1.9 + * Redistribution and use in source and binary forms, with or without
1.10 + * modification, are permitted provided that the following conditions
1.11 + * are met:
1.12 + * 1. Redistributions of source code must retain the above copyright
1.13 + * notice, this list of conditions and the following disclaimer.
1.14 + * 2. Redistributions in binary form must reproduce the above copyright
1.15 + * notice, this list of conditions and the following disclaimer in the
1.16 + * documentation and/or other materials provided with the distribution.
1.17 + *
1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1.20 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1.22 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1.23 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1.24 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.25 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.26 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
1.27 + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1.28 + * POSSIBILITY OF SUCH DAMAGE.
1.29 + */
1.30 +
1.31 +#ifdef HAVE_CONFIG_H
1.32 +#include "config.h"
1.33 +#endif
1.34 +#include <liboil/liboilfunction.h>
1.35 +#include <liboil/liboildebug.h>
1.36 +#include <liboil/liboilutils.h>
1.37 +
1.38 +#ifdef HAVE_UNISTD_H
1.39 +#include <unistd.h>
1.40 +#endif
1.41 +#include <fcntl.h>
1.42 +#include <stdlib.h>
1.43 +#include <string.h>
1.44 +
1.45 +int
1.46 +get_file_int (const char *file, int *value)
1.47 +{
1.48 + char buffer[20];
1.49 + char *endptr;
1.50 + int fd;
1.51 + int n;
1.52 +
1.53 + fd = open (file, O_RDONLY);
1.54 + if (fd < 0) return 0;
1.55 +
1.56 + n = read(fd, buffer, 19);
1.57 + close(fd);
1.58 + if (n < 0) {
1.59 + return 0;
1.60 + }
1.61 + buffer[n] = 0;
1.62 +
1.63 + *value = strtol (buffer, &endptr, 0);
1.64 +
1.65 + if (endptr[0] == 0 || endptr[0] == '\n') return 1;
1.66 + return 0;
1.67 +}
1.68 +
1.69 +char *
1.70 +get_file (const char *filename)
1.71 +{
1.72 + char *cpuinfo;
1.73 + int fd;
1.74 + int n;
1.75 +
1.76 + cpuinfo = malloc(4096);
1.77 + if (cpuinfo == NULL) return NULL;
1.78 +
1.79 + fd = open(filename, O_RDONLY);
1.80 + if (fd < 0) {
1.81 + free (cpuinfo);
1.82 + return NULL;
1.83 + }
1.84 +
1.85 + n = read(fd, cpuinfo, 4095);
1.86 + if (n < 0) {
1.87 + free (cpuinfo);
1.88 + close (fd);
1.89 + return NULL;
1.90 + }
1.91 + cpuinfo[n] = 0;
1.92 +
1.93 + close (fd);
1.94 +
1.95 + return cpuinfo;
1.96 +}
1.97 +
1.98 +char *
1.99 +get_cpuinfo_line (char *cpuinfo, const char *tag)
1.100 +{
1.101 + char *flags;
1.102 + char *end;
1.103 + char *colon;
1.104 +
1.105 + flags = strstr(cpuinfo,tag);
1.106 + if (flags == NULL) return NULL;
1.107 +
1.108 + end = strchr(flags, '\n');
1.109 + if (end == NULL) return NULL;
1.110 + colon = strchr (flags, ':');
1.111 + if (colon == NULL) return NULL;
1.112 + colon++;
1.113 + if(colon >= end) return NULL;
1.114 +
1.115 + return _strndup (colon, end-colon);
1.116 +}
1.117 +
1.118 +char *
1.119 +_strndup (const char *s, int n)
1.120 +{
1.121 + char *r;
1.122 + r = malloc (n+1);
1.123 + memcpy(r,s,n);
1.124 + r[n]=0;
1.125 +
1.126 + return r;
1.127 +}
1.128 +
1.129 +char **
1.130 +strsplit (char *s)
1.131 +{
1.132 + char **list = NULL;
1.133 + char *tok;
1.134 + int n = 0;
1.135 +
1.136 + while (*s == ' ') s++;
1.137 +
1.138 + list = malloc (1 * sizeof(char *));
1.139 + while (*s) {
1.140 + tok = s;
1.141 + while (*s && *s != ' ') s++;
1.142 +
1.143 + list[n] = _strndup (tok, s - tok);
1.144 + while (*s && *s == ' ') s++;
1.145 + list = realloc (list, (n + 2) * sizeof(char *));
1.146 + n++;
1.147 + }
1.148 +
1.149 + list[n] = NULL;
1.150 + return list;
1.151 +}
1.152 +
1.153 +char *
1.154 +get_tag_value (char *s, const char *tag)
1.155 +{
1.156 + char *flags;
1.157 + char *end;
1.158 + char *colon;
1.159 +
1.160 + flags = strstr(s,tag);
1.161 + if (flags == NULL) return NULL;
1.162 +
1.163 + end = strchr(flags, '\n');
1.164 + if (end == NULL) return NULL;
1.165 + colon = strchr (flags, ':');
1.166 + if (colon == NULL) return NULL;
1.167 + colon++;
1.168 + if(colon >= end) return NULL;
1.169 +
1.170 + return _strndup (colon, end-colon);
1.171 +}
1.172 +
1.173 +