sl@0: /*
sl@0:  * LIBOIL - Library of Optimized Inner Loops
sl@0:  * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
sl@0:  * All rights reserved.
sl@0:  *
sl@0:  * Redistribution and use in source and binary forms, with or without
sl@0:  * modification, are permitted provided that the following conditions
sl@0:  * are met:
sl@0:  * 1. Redistributions of source code must retain the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer.
sl@0:  * 2. Redistributions in binary form must reproduce the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer in the
sl@0:  *    documentation and/or other materials provided with the distribution.
sl@0:  * 
sl@0:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
sl@0:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
sl@0:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
sl@0:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
sl@0:  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
sl@0:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
sl@0:  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
sl@0:  * POSSIBILITY OF SUCH DAMAGE.
sl@0:  */
sl@0: 
sl@0: #ifdef HAVE_CONFIG_H
sl@0: #include "config.h"
sl@0: #endif
sl@0: #include <liboil/liboilfunction.h>
sl@0: #include <liboil/liboildebug.h>
sl@0: #include <liboil/liboilutils.h>
sl@0: 
sl@0: #ifdef HAVE_UNISTD_H
sl@0: #include <unistd.h>
sl@0: #endif
sl@0: #include <fcntl.h>
sl@0: #include <stdlib.h>
sl@0: #include <string.h>
sl@0: 
sl@0: int
sl@0: get_file_int (const char *file, int *value)
sl@0: {
sl@0:   char buffer[20];
sl@0:   char *endptr;
sl@0:   int fd;
sl@0:   int n;
sl@0: 
sl@0:   fd = open (file, O_RDONLY);
sl@0:   if (fd < 0) return 0;
sl@0: 
sl@0:   n = read(fd, buffer, 19);
sl@0:   close(fd);
sl@0:   if (n < 0) {
sl@0:     return 0;
sl@0:   }
sl@0:   buffer[n] = 0;
sl@0: 
sl@0:   *value = strtol (buffer, &endptr, 0);
sl@0: 
sl@0:   if (endptr[0] == 0 || endptr[0] == '\n') return 1;
sl@0:   return 0;
sl@0: }
sl@0: 
sl@0: char *
sl@0: get_file (const char *filename)
sl@0: {
sl@0:   char *cpuinfo;
sl@0:   int fd;
sl@0:   int n;
sl@0: 
sl@0:   cpuinfo = malloc(4096);
sl@0:   if (cpuinfo == NULL) return NULL;
sl@0: 
sl@0:   fd = open(filename, O_RDONLY);
sl@0:   if (fd < 0) {
sl@0:     free (cpuinfo);
sl@0:     return NULL;
sl@0:   }
sl@0: 
sl@0:   n = read(fd, cpuinfo, 4095);
sl@0:   if (n < 0) {
sl@0:     free (cpuinfo);
sl@0:     close (fd);
sl@0:     return NULL;
sl@0:   }
sl@0:   cpuinfo[n] = 0;
sl@0: 
sl@0:   close (fd);
sl@0: 
sl@0:   return cpuinfo;
sl@0: }
sl@0: 
sl@0: char *
sl@0: get_cpuinfo_line (char *cpuinfo, const char *tag)
sl@0: {
sl@0:   char *flags;
sl@0:   char *end;
sl@0:   char *colon;
sl@0: 
sl@0:   flags = strstr(cpuinfo,tag);
sl@0:   if (flags == NULL) return NULL;
sl@0: 
sl@0:   end = strchr(flags, '\n');
sl@0:   if (end == NULL) return NULL;
sl@0:   colon = strchr (flags, ':');
sl@0:   if (colon == NULL) return NULL;
sl@0:   colon++;
sl@0:   if(colon >= end) return NULL;
sl@0: 
sl@0:   return _strndup (colon, end-colon);
sl@0: }
sl@0: 
sl@0: char *
sl@0: _strndup (const char *s, int n)
sl@0: {
sl@0:   char *r;
sl@0:   r = malloc (n+1);
sl@0:   memcpy(r,s,n);
sl@0:   r[n]=0;
sl@0: 
sl@0:   return r;
sl@0: }
sl@0: 
sl@0: char **
sl@0: strsplit (char *s)
sl@0: {
sl@0:   char **list = NULL;
sl@0:   char *tok;
sl@0:   int n = 0;
sl@0: 
sl@0:   while (*s == ' ') s++;
sl@0: 
sl@0:   list = malloc (1 * sizeof(char *));
sl@0:   while (*s) {
sl@0:     tok = s;
sl@0:     while (*s && *s != ' ') s++;
sl@0: 
sl@0:     list[n] = _strndup (tok, s - tok);
sl@0:     while (*s && *s == ' ') s++;
sl@0:     list = realloc (list, (n + 2) * sizeof(char *));
sl@0:     n++;
sl@0:   }
sl@0: 
sl@0:   list[n] = NULL;
sl@0:   return list;
sl@0: }
sl@0: 
sl@0: char *
sl@0: get_tag_value (char *s, const char *tag)
sl@0: {
sl@0:   char *flags;
sl@0:   char *end;
sl@0:   char *colon;
sl@0: 
sl@0:   flags = strstr(s,tag);
sl@0:   if (flags == NULL) return NULL;
sl@0: 
sl@0:   end = strchr(flags, '\n');
sl@0:   if (end == NULL) return NULL;
sl@0:   colon = strchr (flags, ':');
sl@0:   if (colon == NULL) return NULL;
sl@0:   colon++;
sl@0:   if(colon >= end) return NULL;
sl@0: 
sl@0:   return _strndup (colon, end-colon);
sl@0: }
sl@0: 
sl@0: