Imported Upstream version 4.6.0
[debian/atlc] / tests / try_portable.c
1 /*
2 atlc - arbitrary transmission line calculator, for the analysis of
3 transmission lines are directional couplers. 
4
5 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either package_version 2
10 of the License, or (at your option) any later package_version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
20 USA.
21
22 Dr. David Kirkby, e-mail drkirkby@ntlworld.com 
23
24 */
25 /* Try to get data. This should work on just about any 
26 system going. However, the amount of data collected 
27 is not very much, so other C files, that try to 
28 get more information are about. */
29
30 #include "config.h"
31
32
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif  
36
37 #ifdef HAVE_STDIO_H
38 #include <stdio.h>
39 #endif
40
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44
45 #ifdef HAVE_SYS_UTSNAME_H
46 #include <sys/utsname.h>
47 #endif
48
49 #ifdef HAVE_ERRNO_H
50 #include <errno.h>
51 #endif
52
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
56
57 #include "defs.h"
58
59 extern int errno;
60 #define BYTES_PER_MB  1048576
61
62 int try_portable(struct computer_data *data)
63 {
64
65 /* Check for both uname and sysconf. Any UNIX system should 
66 have both, but since someone has done a Windows port, I  
67 will start by assming neither sysconf or uname exist */
68
69 #ifdef _SC_PHYS_PAGES
70 #ifdef _SC_PAGESIZE
71 #ifdef HAVE_SYSCONF  /* Most UNIX systems have it */
72   double ram;
73 #endif
74 #endif
75 #endif
76
77 #ifdef HAVE_UNAME /* Most if not all UNIX system have it */
78   int ret, i, string_length;
79   struct utsname operating_system;
80
81   ret=uname(&operating_system);
82   if (ret == -1)
83   {
84 #ifdef HAVE_ERRNO_H
85     fprintf(stderr,"failure in call to uname in try_portable.c errno=%d\n", errno);
86 #else
87     fprintf(stderr,"failure in call to uname in try_portable.c\n");
88 #endif /* #ifdef HAVE_ERRNO_H */
89     return(ret);
90   }
91   else  /* the call to uname succeesed */
92   {
93     /* There is a distint posibility that some data in the operating
94     system structure will have spaces in it. This will screw things
95     up, since benchmark.test is expect a fixed number of arguments.
96     Hence to avoid this, any spaces are replaced by underscores */
97
98     strcpy(data->sysname,operating_system.sysname);
99     string_length=strlen(data->sysname);
100     for(i=0;i<string_length; ++i)
101     {
102       if (data->sysname[i] == ' ')
103         data->sysname[i]='_';
104     }
105
106     /* nodename should be safe, but I'll take no chances */
107     strcpy(data->nodename,operating_system.nodename);
108     string_length=strlen(data->nodename);
109     for(i=0;i<string_length; ++i)
110     {
111       if (data->nodename[i] == ' ')
112         data->nodename[i]='_';
113     }
114
115     /* not so with release */
116     strcpy(data->release,operating_system.release);
117     string_length=strlen(data->release);
118     for(i=0;i<string_length; ++i)
119     {
120       if (data->release[i] == ' ')
121         data->release[i]='_';
122     }
123
124     /* or version */
125     strcpy(data->version,operating_system.version);
126     string_length=strlen(data->version);
127     for(i=0;i<string_length; ++i)
128     {
129       if (data->version[i] == ' ')
130         data->version[i]='_';
131     }
132
133     strcpy(data->machine,operating_system.machine);
134     string_length=strlen(data->machine);
135     for(i=0;i<string_length; ++i)
136     {
137       if (data->machine[i] == ' ')
138         data->machine[i]='_';
139     }
140   } /* end of code executed if uname() passed */
141 #endif /* End of #ifdef HAVE_UNAME */
142
143 /* Try to get the number of processors online. This seems to be 
144 at least semi portable, as its used by both AIX and Solaris.
145 */
146
147 #ifdef HAVE_SYSCONF  /* many systems seem to have sysconf(), 
148 which takes an integer argument and returns a long. The 
149 arugment detemines what gets returned. */
150
151 #ifdef _SC_NPROCESSORS_ONLN  /* Get the CPUs online */
152     if(sysconf(_SC_NPROCESSORS_ONLN) >= 1)
153       sprintf(data->cpus,"%ld",sysconf(_SC_NPROCESSORS_ONLN));
154 #endif
155
156   /* Obtain the RAM. This is a bit of a hack using doubles for 
157   this, but longs overflow and long long is not strictly 
158   in the ANSI standard. */
159
160 #ifdef _SC_PHYS_PAGES
161 #ifdef _SC_PAGESIZE
162
163 /* This might look a bit of a hack to use a double to compute
164 memory size in Mb, when any machine now will have an
165 integer multiple of Mb. However, the problem is that the
166 way to do this (multiplying the size of a memory page in 
167 bytes by the number of memory pages), produce the ram in bytes.
168 That will likely overflow a long on a 32-bit machine. So a 
169 double is used, divided by the number of  bytes per Mb, then
170 printed as a long. 
171
172 I hope that makes some sense */
173
174   if ((long) sysconf(_SC_PHYS_PAGES) > 0L)
175   {
176     if ((long) sysconf(_SC_PAGESIZE) > 0L)
177     {
178       ram=(double) sysconf(_SC_PAGESIZE);
179       ram*= (double) sysconf(_SC_PHYS_PAGES);
180       {
181         ram=ram/BYTES_PER_MB;
182         sprintf(data->memory,"%ld",(long) (ram+0.05));
183       }
184     }
185   }
186 #endif /* End of HAVE__SC_PHYS_PAGES */
187 #endif  /* End of  HAVE__SC_PHYS_PAGES */
188 #endif /* End of #ifdef HAVE_SYSCONF */
189   return(0);
190 }