Debian Codegraph @

libsrc/os/unix/osh.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 .TYPE Module
30 .NAME osh
31 .LANGUAGE C
32 .AUTHOR IPG-ESO Garching
33 .CATEGORY Host operating system interfaces. Host services.
34 .COMMENTS
35 Provide miscellaneous host services.
36 The routines return time and date stamps,
37 return cpu time used by the current process, translates host
38 error codes and executes host commands.
39 Time resolution is host dependent parameter.
40 The routines return always
41 a non-negative integer number on successful return.
42 Otherwise, a value of -1 is set to indicate an error
43 condition and the variable ``oserror'' contains the
44 symbolic error code.
45 Symbolic error codes for each function to be defined.
46 .VERSION 0.0 25-Aug-1986 Definition. J. D. Ponz
47 .VERSION 1.0 28-Oct-1986 Programmation. B. Pirenne
48 .VERSION 1.1 15-Mar-1987 Add oshela. B. Pirenne
49 .VERSION 1.5 14-Apr-1987 bsd-sysv compatible. B. Pirenne
50 .VERSION 1.6 15-Jul-1987 oshcpu returns a real number; date through a
51 structure. B. Pirenne
52 .VERSION 1.7 10-Dec-1987 Cosmetic changes I. Suisalu
53 .VERSION 1.8 05-Oct-1988 Time edition simplifications. F. Ochsenbein
54 .VERSION 1.9 01-Aug-1990 oshcmd takes care of buffer overflow. F. O.
55 .VERSION 2.0 07-Aug-1990 Added oshtm oshtl (conversion long to structure)
56 FO
57 .VERSION 2.1 910911 Using memcpy() instead of bcopy(). CG
58 .VERSION 2.2 950502 Adding oshmidvers. CG.
59
60 090429 last modif
61 ------------------------------------------------------------*/
62 /*
63 * Define _POSIX_SOURCE to indicate
64 * that this is a POSIX program
65 */
66 #define _POSIX_SOURCE 1
67
68 #include <unistd.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <errno.h>
72 #include <string.h>
73 #include <sys/types.h>
74 #include <sys/times.h>
75 #include <time.h>
76 #include <osparms.h>
77 #include <proto_os.h> /* ANSI-C prototyping */
78
79 #ifndef CLK_TCK
80 #define CLK_TCK 50
81 #endif
82
83 int oshcpu(op, ctime)
84 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
85 .PURPOSE Gets CPU time. Precision is : hundreds of seconds.
86 Cpu time returned is the sum of the time spend for both system
87 tasks and process itself needed for the completion of the task.
88 The cpu time is expressed in seconds, in a floating point format.
89 .RETURNS Value 0 on normal return.
90 value -1 if the operation to do is unknown or if
91 the get operation was not preceded by an initialization.
92 .REMARKS System dependencies:
93 --UNIX: times(3c)
94 ------------------------------------------------------------*/
95 int op; /* IN : operation to execute : INIT_CPU_CLOCK \
96 to initialize timer, GET_CPU_CLOCK to get the time. */
97 float *ctime; /* OUT: ctime is the cpu time passed since the init. op.; */
98 {
99 struct tms buffer; /* buffer to receive time accounting information */
100 static time_t firstime=0L;
101 time_t lastime;
102
103 switch (op) {
104 case INIT_CPU_CLOCK : /**** Initialize timer */
105 if(times(&buffer) == -1L) {
106 oserror = errno;
107 return(-1);
108 }
109 firstime = buffer.tms_utime + buffer.tms_stime
110 + buffer.tms_cutime + buffer.tms_cstime;
111 *ctime = 0.;
112 break;
113 case GET_CPU_CLOCK : /**** get time */
114 if(!(int)firstime){
115 oserror=EINVAL;
116 return(-1);
117 }
118 if(times(&buffer) == -1L) {
119 oserror = errno;
120 return(-1);
121 }
122 lastime = buffer.tms_utime + buffer.tms_stime +
123 buffer.tms_cutime + buffer.tms_cstime - firstime;
124 /*
125 ** conversion of the time from 1/HZ s. to sec.
126 */
127 *ctime = (float)lastime / CLK_TCK;
128 break;
129 default : /**** Operation code error */
130 oserror = EINVAL;
131 return(-1);
132 }
133 return (0);
134 }
135
136 long oshtime()
137 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
138 .PURPOSE Gets CURRENT time. Precision is : 1 seconds.
139 .RETURNS Time (normally as seconds elapsed since Jan. 1, 1970)
140 .REMARKS
141 ------------------------------------------------------------*/
142 {
143 return((long) time((time_t *)0));
144 }
145
146
147 struct tm *oshtm(clock)
148 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
149 .PURPOSE Convert time from long to structure
150 .RETURNS Address of a filled tm structure (see time.h)
151 ------------------------------------------------------------*/
152 long clock; /* IN: Time in seconds since Jan. 1, 1970 */
153 {
154 static time_t aclock;
155
156 aclock = clock;
157 return(gmtime(&aclock));
158 }
159
160 #define Jan1_1970 135140L /* Date in 400-yr cycle */
161 #define Jan1_1980 138792L /* Date in 400-yr cycle */
162 #define Jan1_2000 0L /* Date in 400-yr cycle */
163
164
165 long oshtl(T)
166 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
167 .PURPOSE Convert time structure into seconds since Jan. 1, 1970
168 .RETURNS The time in seconds.
169 .REMARKS Computation is done here, system call seems to be non-existent...
170 ------------------------------------------------------------*/
171 struct tm *T; /* IN: Time structure to convert */
172 {
173 register long int j, y, t;
174
175 /* Compute Day of 400 yr cycle */
176 y = T->tm_year;
177 if (y < 1000) y += 1900;
178 y %= 400;
179 y += 400; /* To be sure j is positive */
180 j = y - (11 - T->tm_mon)/10;
181 t = ((1461L*j)/4 + (306L * ((T->tm_mon+10)%12) + 5)/10
182 - (3* ((j + 100L)/100L))/4 + T->tm_mday + 59L) % 146097L;
183 /* Compute Week day */
184 T->tm_wday = (t+6L)%7L;
185
186 t -= Jan1_1970; /* Origin = Jan 1, 1970 */
187 if (t < -24855) t += 146097L;
188 if (t > 24855) t -= 146097L;
189
190
191 /* Transform Day into seconds */
192 if (t < -24855) t = -24855;
193 if (t > 24855) t = 24855;
194 t = t * 86400L + T->tm_hour * 3600L + T->tm_min * 60L + T->tm_sec;
195 return(t);
196 }
197
198 int oshdate(date_string, date_struct)
199 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
200 .PURPOSE Gets date and time stamp. No special structure adopted.
201 .RETURNS A pointer to the time structure is defined in <time.h> (system)
202 {\begin{TeX} \begin{itemize} \item int tm_year number
203 \item int tm_mon number [0-11] \item int tm_mday [1-31]
204 \item int tm_yday [0-355] \item int tm_wday [0-6] (Sunday = 0)
205 \item int tm_hour [0-23] \item int tm_min [0-59]
206 \item int tm_sec [0-59]. \item int tm_saving
207 \end{itemize} \end{TeX}}
208 .REMARKS System dependencies:
209 --UNIX: time(2)
210 ------------------------------------------------------------*/
211 char *date_string; /* OUT : date-time stamp (was date_string[28] before) */
212 struct tm *date_struct; /* OUT : date-time structure (see description) */
213 {
214 struct tm *dt;
215 time_t curseconds;
216 int i, m;
217 static char template[]="WW, DD MMM YYYY hh:mm:ss";
218 static char Day_list[]= "SuMoTuWeThFrSa";
219 static char Month_list[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
220
221 /*
222 ** get current time in seconds since 1/1/1970
223 */
224 if((curseconds = time((time_t *)0)) < 0) {
225 oserror = errno;
226 return(-1);
227 }
228
229 /*
230 ** fill in the structure with date information
231 */
232
233 dt = localtime(&curseconds);
234 if (dt->tm_year < 1900) dt->tm_year += 1900;
235 memcpy((char *)date_struct,(char *)dt,sizeof(struct tm));
236
237 /*
238 ** now create a string with time and date according
239 ** to the above picture
240 */
241
242 for (i = 2*dt->tm_wday, m=0; m <= 1; m++)
243 template[m] = Day_list[i++];
244
245 for (i = dt->tm_mday, m=5; m>=4; m--, i/=10)
246 template[m] = '0'+i%10;
247
248 for (i = 3*dt->tm_mon, m = 7; m <= 9; m++)
249 template[m] = Month_list[i++];
250
251 for (i = dt->tm_year, m = 14; m >= 11; m--, i/=10)
252 template[m] = '0' + i%10;
253
254 for (i = dt->tm_hour, m = 19; m >= 18; m--, i/=10)
255 template[m] = '0' + i%10;
256
257 for (i = dt->tm_min, m = 22; m >= 21; m--, i/=10)
258 template[m] = '0' + i%10;
259
260 for (i = dt->tm_sec, m = 25; m >= 24; m--, i/=10)
261 template[m] = '0' + i%10;
262
263 /* ** now copy template to date_string */
264
265 m = 27; /* sizeof(template) + 1 (change, if template changes!) */
266 memcpy(date_string,template,(size_t)m);
267
268 return(0);
269 }
270
271
272 int oshmsg(code, message)
273 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
274 .PURPOSE Gets system error message.
275 .RETURNS Value 0 on normal return, -1 if the error code does not exist.
276 .REMARKS System dependencies:
277 --UNIX: sys_errlist[]
278 ------------------------------------------------------------*/
279 int code; /* IN : system error code */
280 char *message; /* OUT : system error message */
281 {
282 if (code < 0)
283 {
284 oserror = EINVAL;
285 return(-1);
286 }
287
288 (void) strcpy(message,strerror(oserror));
289 return(0);
290 }
291
292 int oshcmd(command, input, output, error)
293 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
294 .PURPOSE Executes system command(maybe with arguments).
295 The routine defines also
296 the pathnames for standard input, output and error units.
297 Enviroment variable SHELL must be defined.
298 .RETURNS Value 0 on normal return. The status of the execution of the
299 command in the other cases.
300 .REMARKS System dependencies:
301 --UNIX: system(3S)
302 ------------------------------------------------------------*/
303 char *command; /* IN : system command */
304 char *input; /* IN : standard input stream */
305 char *output; /* IN : standard output stream */
306 char *error; /* IN : error output stream */
307 {
308 char buffer[256];
309 char *p;
310 #define strappend(s) strncpy(p,s,buffer+sizeof(buffer)-(p+1)), p += strlen(p)
311
312 strncpy(buffer,command,sizeof(buffer)-1);
313 buffer[sizeof(buffer)-1] = '\0';
314 p = buffer + strlen(buffer);
315 if(input != NULL) if (*input) {
316 strappend(" <");
317 strappend(input);
318 }
319 if(output != NULL) if (*output) {
320 strappend(" >");
321 strappend(output);
322 }
323 if(error != NULL) if (*error) {
324 strappend(" 2>");
325 strappend(error);
326 }
327 return(system(buffer));
328 }
329
330 #define MAX_CURRENT_WORKING_DIR 128
331 static char current_working_dir[MAX_CURRENT_WORKING_DIR];
332 int oshgetcwd(path)
333 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
334 .PURPOSE Return current working directory.
335 .RETURNS Value 0 on normal return. -1 on failure and oserror set.
336 .REMARKS System dependencies:
337 --UNIX: getcwd(3)
338 ------------------------------------------------------------*/
339 char **path;
340 {
341 if (getcwd(current_working_dir,(size_t)MAX_CURRENT_WORKING_DIR) == NULL) {
342 oserror = errno;
343 return(-1);
344 }
345 else {
346 *path = current_working_dir;
347 return(0);
348 }
349 }
350
351 int oshchdir(path)
352 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
353 .PURPOSE Change current working directory.
354 .RETURNS Value 0 on normal return. -1 on failure and oserror set.
355 .REMARKS System dependencies:
356 --UNIX: chdir(3)
357 ------------------------------------------------------------*/
358 char *path;
359 {
360 if (chdir(path) == -1) {
361 oserror = errno;
362 return(-1);
363 }
364 else
365 return(0);
366 }
367
368 char *oshmidvers()
369 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
370 .PURPOSE Get MIDVERS string (located in patchlevel.h).
371 .RETURNS Pointer to static variable MIDVERS
372 -------------------------------------------------------------------- */
373 {
374 return("Midas version...");
375 }