1
/* ========================================================================
2
* Copyright 1988-2006 University of Washington
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
11
* ========================================================================
15
* Program: WCE environment routines
17
* Author: Mark Crispin
18
* Networks and Distributed Computing
19
* Computing & Communications
20
* University of Washington
21
* Administration Building, AG-44
23
* Internet: MRC@CAC.Washington.EDU
26
* Last Edited: 30 August 2006
30
static char *myUserName = NIL; /* user name */
31
static char *myLocalHost = NIL; /* local host name */
32
static char *myClientHost = NIL;/* client host name */
33
static char *myServerHost = NIL;/* server host name */
34
static char *myHomeDir = NIL; /* home directory name */
35
static char *myNewsrc = NIL; /* newsrc file name */
36
static char *sysInbox = NIL; /* system inbox name */
37
static long list_max_level = 5; /* maximum level of list recursion */
38
static short no822tztext = NIL; /* disable RFC [2]822 timezone text */
40
static NAMESPACE nshome = {"",'\\',NIL,NIL};
42
static NAMESPACE *nslist[3] = {&nshome,NIL,NIL};
43
static long alarm_countdown = 0;/* alarm count down */
44
static void (*alarm_rang) (); /* alarm interrupt function */
45
static unsigned int rndm = 0; /* initial `random' number */
48
/* Dummy definitions to prevent errors */
50
#define server_login(user,pass,authuser,argc,argv) NIL
51
#define authserver_login(user,authuser,argc,argv) NIL
52
#define myusername() ""
53
#define MD5ENABLE "\\.nosuch.."
55
#include "pmatch.c" /* include wildcard pattern matcher */
57
/* Environment manipulate parameters
58
* Accepts: function code
59
* function-dependent value
60
* Returns: function-dependent return value
63
void *env_parameters (long function,void *value)
66
switch ((int) function) {
68
ret = (void *) nslist;
71
myHomeDir = cpystr ((char *) value);
73
ret = (void *) myHomeDir;
76
myLocalHost = cpystr ((char *) value);
78
ret = (void *) myLocalHost;
81
if (myNewsrc) fs_give ((void **) &myNewsrc);
82
myNewsrc = cpystr ((char *) value);
84
if (!myNewsrc) { /* set news file name if not defined */
86
sprintf (tmp,"%s\\NEWSRC",myhomedir ());
87
myNewsrc = cpystr (tmp);
89
ret = (void *) myNewsrc;
92
if (sysInbox) fs_give ((void **) &sysInbox);
93
sysInbox = cpystr ((char *) value);
95
ret = (void *) sysInbox;
97
case SET_LISTMAXLEVEL:
98
list_max_level = (long) value;
99
case GET_LISTMAXLEVEL:
100
ret = (void *) list_max_level;
102
case SET_DISABLE822TZTEXT:
103
no822tztext = value ? T : NIL;
104
case GET_DISABLE822TZTEXT:
105
ret = (void *) (no822tztext ? VOIDT : NIL);
111
/* Write current time
112
* Accepts: destination string
113
* optional format of day-of-week prefix
114
* format of date and time
115
* flag whether to append symbolic timezone
118
static void do_date (char *date,char *prefix,char *fmt,int suffix)
120
time_t tn = time (0);
121
struct tm *t = gmtime (&tn);
122
int zone = t->tm_hour * 60 + t->tm_min;
123
int julian = t->tm_yday;
124
t = localtime (&tn); /* get local time now */
125
/* minus UTC minutes since midnight */
126
zone = t->tm_hour * 60 + t->tm_min - zone;
127
/* julian can be one of:
128
* 36x local time is December 31, UTC is January 1, offset -24 hours
129
* 1 local time is 1 day ahead of UTC, offset +24 hours
130
* 0 local time is same day as UTC, no offset
131
* -1 local time is 1 day behind UTC, offset -24 hours
132
* -36x local time is January 1, UTC is December 31, offset +24 hours
134
if (julian = t->tm_yday -julian)
135
zone += ((julian < 0) == (abs (julian) == 1)) ? -24*60 : 24*60;
136
if (prefix) { /* want day of week? */
137
sprintf (date,prefix,days[t->tm_wday]);
138
date += strlen (date); /* make next sprintf append */
140
/* output the date */
141
sprintf (date,fmt,t->tm_mday,months[t->tm_mon],t->tm_year+1900,
142
t->tm_hour,t->tm_min,t->tm_sec,zone/60,abs (zone) % 60);
143
if (suffix) { /* append timezone suffix if desired */
145
tzset (); /* get timezone from TZ environment stuff */
146
tz = tzname[daylight ? (((struct tm *) t)->tm_isdst > 0) : 0];
147
if (tz && tz[0]) sprintf (date + strlen (date)," (%s)",tz);
152
/* Write current time in RFC 822 format
153
* Accepts: destination string
156
void rfc822_date (char *date)
158
do_date (date,"%s, ","%d %s %d %02d:%02d:%02d %+03d%02d",
159
no822tztext ? NIL : T);
163
/* Write current time in internal format
164
* Accepts: destination string
167
void internal_date (char *date)
169
do_date (date,NIL,"%02d-%s-%d %02d:%02d:%02d %+03d%02d",NIL);
172
/* Return random number
177
if (!rndm) srand (rndm = (unsigned) time (0L));
178
return (long) rand ();
181
/* Return default drive
182
* Returns: default drive
185
static char *defaultDrive (void)
188
return ((s = getenv ("SystemDrive")) && *s) ? s : "C:";
192
/* Return home drive from environment variables
193
* Returns: home drive
196
static char *homeDrive (void)
199
return ((s = getenv ("HOMEDRIVE")) && *s) ? s : defaultDrive ();
203
/* Return home path from environment variables
204
* Accepts: path to write into
205
* Returns: home path or NIL if it can't be determined
208
static char *homePath (char *path)
212
if (!((s = getenv ("HOMEPATH")) && (i = strlen (s)))) return NIL;
213
if (((s[i-1] == '\\') || (s[i-1] == '/'))) s[i-1] = '\0';
214
sprintf (path,"%s%s",homeDrive (),s);
218
/* Return my home directory name
219
* Returns: my home directory name
224
char tmp[MAILTMPLEN];
225
/* initialize if first time */
226
if (!myHomeDir) myHomeDir = homePath (tmp);
227
return myHomeDir ? myHomeDir : homeDrive ();
230
/* Return system standard INBOX
231
* Accepts: buffer string
236
char tmp[MAILTMPLEN];
237
if (!sysInbox) { /* initialize if first time */
238
sprintf (tmp,"%s\\INBOX",myhomedir ());
239
sysInbox = cpystr (tmp); /* system inbox is from mail spool */
245
/* Return mailbox file name
246
* Accepts: destination buffer
251
char *mailboxfile (char *dst,char *name)
253
char *dir = myhomedir ();
254
*dst = '\0'; /* default to empty string */
255
if (((name[0] == 'I') || (name[0] == 'i')) &&
256
((name[1] == 'N') || (name[1] == 'n')) &&
257
((name[2] == 'B') || (name[2] == 'b')) &&
258
((name[3] == 'O') || (name[3] == 'o')) &&
259
((name[4] == 'X') || (name[4] == 'x')) && !name[5]) name = NIL;
260
/* reject namespace names or names with / */
261
if (name && ((*name == '#') || strchr (name,'/'))) return NIL;
262
else if (!name) return dst; /* driver selects the INBOX name */
263
/* absolute path name? */
264
else if ((*name == '\\') || (name[1] == ':')) return strcpy (dst,name);
265
/* build resulting name */
266
sprintf (dst,"%s\\%s",dir,name);
267
return dst; /* return it */
271
/* Determine default prototype stream to user
272
* Accepts: type (NIL for create, T for append)
273
* Returns: default prototype stream
276
MAILSTREAM *default_proto (long type)
278
extern MAILSTREAM CREATEPROTO,APPENDPROTO;
279
return type ? &APPENDPROTO : &CREATEPROTO;
282
/* Emulator for BSD syslog() routine
288
void syslog (int priority,const char *message,...)
293
/* Emulator for BSD openlog() routine
299
void openlog (const char *ident,int logopt,int facility)