[go: up one dir, main page]

File: sysdep.c

package info (click to toggle)
libforms1 1.0-8
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,648 kB
  • ctags: 14,351
  • sloc: ansic: 90,441; makefile: 9,902; sh: 8
file content (230 lines) | stat: -rw-r--r-- 5,179 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 *
 *  This file is part of the XForms library package.
 *
 * XForms is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1, or
 * (at your option) any later version.
 *
 * XForms is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with XForms; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 *
 */


/*
 * $Id: sysdep.c,v 1.0 2002/05/08 05:16:30 zhao Release $
 *.
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
 *  All rights reserved.
 *.
 *
 *
 *  X independent but system (SysV, BSD) or OS dependent utilities.
 *
 *  listdir.c contains another part of the system dependent stuff,
 *  namely the directory structure of the system.
 *
 *  check out local.h, which defines some flags that might affect
 *  this file
 *
 */
#if defined(F_ID) || defined(DEBUG)
char *fl_id_sysd = "$Id: sysdep.c,v 1.0 2002/05/08 05:16:30 zhao Release $";
#endif

#include "forms.h"
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>

#ifndef FL_WIN32
#include <sys/time.h>
#include <unistd.h>
#include <pwd.h>
#else
#include <sys/timeb.h>
#include <process.h>
#endif

#include "local.h"

/***********************************************************
 *
 * fl_whoami() returns the username
 *
 **********************************************************/

#ifndef FL_WIN32
const char *
fl_whoami(void)
{
#ifndef __VMS
    struct passwd *passwd = getpwuid(getuid());
    const char *name = passwd ? passwd->pw_name : "Unknown";
    endpwent();
#else
    const char *name = getenv("USER");
#endif
    return name ? name : "unknown";
}
#else /* WIN/NT platform */
const char *
fl_whoami(void)
{
    static char buf[32];
    int len = 32;

    return GetUserName(buf, &len) ? buf : "unknown";
}
#endif


/***************************************************
 * fl_now() returns today's date in ASCII format
 ***************************************************/
const char *
fl_now(void)
{
    time_t t = time(0);
    static char buf[64];

    strcpy(buf, asctime(localtime(&t)));
    buf[strlen(buf) - 1] = '\0';
    return buf;
}

/*****************************************************************
 *
 * fl_msleep() is similar to sleep(), but with milli-second argument
 *
 ******************************************************************/
#ifdef __VMS
#include <lib$routines.h>

unsigned long
fl_msleep(unsigned long msec)
{
    float wait_time;
    wait_time = msec;
    wait_time = wait_time * .001;
    (void) lib$wait(&wait_time);
    return 0;
}

#else

/* Yielding the processor for msec  milli-seconds */
unsigned long
fl_msleep(unsigned long msec)
{

#ifdef __EMX__
    _sleep2(msec);		/* more accurate than select/sleep in OS2 */
    return 0;
#else
#ifdef FL_WIN32
    _sleep(msec);
    return 0;
#else
#if !defined(DONT_HAVE_USLEEP)
    usleep(msec * 1000);
    return 0;
#else
    /* seems everybody has select these days */
    struct timeval timeout;
    timeout.tv_usec = 1000 * (msec % 1000);
    timeout.tv_sec = msec / 1000;
    return select(0, (void *) 0, (void *) 0, (void *) 0, &timeout);
#endif
#endif
#endif
}

#endif /* VMS */

/******* End of fl_msleep() ***************************/

/*************************************************************
 * Return the system time since a fixed point in time (On unix
 * systems, this point is typically 01/01/70).
 * Most useful for elapsed time.
 ************************************************************/
void
fl_gettime(long *sec, long *usec)
{
#ifndef FL_WIN32
    static struct timeval tp;
#ifdef opennt
    static unsigned long tzp;
#else
    static struct timezone tzp;
#endif

    gettimeofday(&tp, &tzp);
    *sec = tp.tv_sec;
    *usec = tp.tv_usec;
#else
    struct _timeb _gtodtmp;
    _ftime(&_gtodtmp);
    *sec = _gtodtmp.time;
    *usec = _gtodtmp.millitm * 1000;
#endif
}


long fl_getpid(void)
{
    return (long)getpid();
}


#if defined(__VMS) && __VMS_VER < 70000000
/* Thanks to David Mathog (mathog@seqaxp.bio.caltech.edu) for
   stealing it from the X11KIT distribution
 */


/*
 *      gettimeofday(2) - Returns the current time
 *
 *      NOTE: The timezone portion is useless on VMS.
 *      Even on UNIX, it is only provided for backwards
 *      compatibilty and is not guaranteed to be correct.
 */
#include "forms.h"
#include <timeb.h>

int
gettimeofday(struct timeval *tv, struct timezone *tz)
{
    timeb_t tmp_time;

    ftime(&tmp_time);

    if (tv != NULL)
    {
	tv->tv_sec = tmp_time.time;
	tv->tv_usec = tmp_time.millitm * 1000;
    }
    if (tz != NULL)
    {
	tz->tz_minuteswest = tmp_time.timezone;
	tz->tz_dsttime = tmp_time.dstflag;
    }

    return (0);

}
/*** End gettimeofday() ***/

#endif /* __VMS_VER */