ref: 8478b0052b56358930eb37f2221708d9f460402b
parent: 85e1246b9c64a0c95cb8b2490a00329378df1fe1
author: Erik de Castro Lopo <[email protected]>
date: Tue Feb 22 15:28:21 EST 2011
tests/util.[ch] : Rename functio print_cpu_name() to get_cpu_name().
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2011-02-22 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
+
+ * tests/util.[ch]
+ Rename functio print_cpu_name() to get_cpu_name(). Add code for Mac OSX and
+ FreeBSD.
+
2010-11-04 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
* src/*.[ch]
--- a/tests/util.c
+++ b/tests/util.c
@@ -166,34 +166,65 @@
} /* reverse_data */
-void
-print_cpu_name (void)
-{ char buffer [512] ;
- FILE * file ;
+const char *
+get_cpu_name (void)
+{
+ const char *name = "Unknown", *search = NULL ;
+ static char buffer [512] ;
+ FILE * file = NULL ;
+ int is_pipe = 0 ;
- if ((file = fopen ("/proc/cpuinfo", "r")) == NULL)
- { puts ("Unknown") ;
- return ;
+#if defined (__linux__)
+ file = fopen ("/proc/cpuinfo", "r") ;
+ search = "model name" ;
+#elif defined (__APPLE__)
+ file = popen ("/usr/sbin/system_profiler -detailLevel full SPHardwareDataType", "r") ;
+ search = "Processor Name" ;
+ is_pipe = 1 ;
+#elif defined (__FreeBSD__)
+ file = popen ("sysctl -a", "r") ;
+ search = "hw.model" ;
+ is_pipe = 1 ;
+#else
+ file = NULL ;
+#endif
+
+ if (file == NULL)
+ return name ;
+
+ if (search == NULL)
+ { printf ("Error : search is NULL in function %s.\n", __func__) ;
+ return name ;
} ;
while (fgets (buffer, sizeof (buffer), file) != NULL)
- if (strstr (buffer, "model name") == buffer)
- { const char * cptr ;
+ if (strstr (buffer, search) == buffer)
+ { char *src, *dest ;
- if ((cptr = strchr (buffer, ':')) != NULL)
- { cptr ++ ;
- while (isspace (cptr [0])) cptr ++ ;
- printf ("%s", cptr) ;
- goto complete ;
+ if ((src = strchr (buffer, ':')) != NULL)
+ { src ++ ;
+ while (isspace (src [0]))
+ src ++ ;
+ name = src ;
+
+ /* Remove consecutive spaces. */
+ src ++ ;
+ for (dest = src ; src [0] ; src ++)
+ { if (isspace (src [0]) && isspace (dest [-1]))
+ continue ;
+ dest [0] = src [0] ;
+ dest ++ ;
+ } ;
+ dest [0] = 0 ;
+ break ;
} ;
} ;
- fclose (file) ;
- puts ("Unknown") ;
- return ;
+ if (is_pipe)
+ pclose (file) ;
+ else
+ fclose (file) ;
-complete :
- fclose (file) ;
- return ;
-} /* print_cpu_name */
+ return name ;
+} /* get_cpu_name */
--- a/tests/util.h
+++ b/tests/util.h
@@ -35,7 +35,7 @@
double calculate_snr (float *data, int len, int expected_peaks) ;
-void print_cpu_name (void) ;
+const char * get_cpu_name (void) ;
#if OS_IS_WIN32
/*