imapext-2007

view src/osdep/os2/env_os2.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
line source
1 /* ========================================================================
2 * Copyright 1988-2006 University of Washington
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *
11 * ========================================================================
12 */
14 /*
15 * Program: OS/2 environment routines
16 *
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 * Internet: MRC@CAC.Washington.EDU
24 *
25 * Date: 1 August 1988
26 * Last Edited: 30 August 2006
27 */
30 static char *myLocalHost = NIL; /* local host name */
31 static char *myHomeDir = NIL; /* home directory name */
32 static char *myNewsrc = NIL; /* newsrc file name */
33 static short no822tztext = NIL; /* disable RFC [2]822 timezone text */
35 #include "write.c" /* include safe writing routines */
36 #include "pmatch.c" /* include wildcard pattern matcher */
39 /* Get all authenticators */
41 #include "auths.c"
43 /* Environment manipulate parameters
44 * Accepts: function code
45 * function-dependent value
46 * Returns: function-dependent return value
47 */
49 void *env_parameters (long function,void *value)
50 {
51 void *ret = NIL;
52 switch ((int) function) {
53 case SET_HOMEDIR:
54 myHomeDir = cpystr ((char *) value);
55 case GET_HOMEDIR:
56 ret = (void *) myHomeDir;
57 break;
58 case SET_LOCALHOST:
59 myLocalHost = cpystr ((char *) value);
60 case GET_LOCALHOST:
61 ret = (void *) myLocalHost;
62 break;
63 case SET_NEWSRC:
64 if (myNewsrc) fs_give ((void **) &myNewsrc);
65 myNewsrc = cpystr ((char *) value);
66 case GET_NEWSRC:
67 if (!myNewsrc) { /* set news file name if not defined */
68 char tmp[MAILTMPLEN];
69 sprintf (tmp,"%s\\newsrc",myhomedir ());
70 myNewsrc = cpystr (tmp);
71 }
72 ret = (void *) myNewsrc;
73 break;
74 case SET_DISABLE822TZTEXT:
75 no822tztext = value ? T : NIL;
76 case GET_DISABLE822TZTEXT:
77 ret = (void *) (no822tztext ? VOIDT : NIL);
78 break;
79 }
80 return ret;
81 }
83 /* Write current time
84 * Accepts: destination string
85 * optional format of day-of-week prefix
86 * format of date and time
87 * flag whether to append symbolic timezone
88 */
90 static void do_date (char *date,char *prefix,char *fmt,int suffix)
91 {
92 time_t tn = time (0);
93 struct tm *t = gmtime (&tn);
94 int zone = t->tm_hour * 60 + t->tm_min;
95 int julian = t->tm_yday;
96 t = localtime (&tn); /* get local time now */
97 /* minus UTC minutes since midnight */
98 zone = t->tm_hour * 60 + t->tm_min - zone;
99 /* julian can be one of:
100 * 36x local time is December 31, UTC is January 1, offset -24 hours
101 * 1 local time is 1 day ahead of UTC, offset +24 hours
102 * 0 local time is same day as UTC, no offset
103 * -1 local time is 1 day behind UTC, offset -24 hours
104 * -36x local time is January 1, UTC is December 31, offset +24 hours
105 */
106 if (julian = t->tm_yday -julian)
107 zone += ((julian < 0) == (abs (julian) == 1)) ? -24*60 : 24*60;
108 if (prefix) { /* want day of week? */
109 sprintf (date,prefix,days[t->tm_wday]);
110 date += strlen (date); /* make next sprintf append */
111 }
112 /* output the date */
113 sprintf (date,fmt,t->tm_mday,months[t->tm_mon],t->tm_year+1900,
114 t->tm_hour,t->tm_min,t->tm_sec,zone/60,abs (zone) % 60);
115 if (suffix) { /* append timezone suffix if desired */
116 char *tz;
117 tzset (); /* get timezone from TZ environment stuff */
118 tz = tzname[daylight ? (((struct tm *) t)->tm_isdst > 0) : 0];
119 if (tz && tz[0]) {
120 char *s;
121 for (s = tz; *s; s++) if (*s & 0x80) return;
122 sprintf (date + strlen (date)," (%.50s)",tz);
123 }
124 }
125 }
128 /* Write current time in RFC 822 format
129 * Accepts: destination string
130 */
132 void rfc822_date (char *date)
133 {
134 do_date (date,"%s, ","%d %s %d %02d:%02d:%02d %+03d%02d",
135 no822tztext ? NIL : T);
136 }
139 /* Write current time in fixed-width RFC 822 format
140 * Accepts: destination string
141 */
143 void rfc822_fixed_date (char *date)
144 {
145 do_date (date,NIL,"%02d %s %4d %02d:%02d:%02d %+03d%02d",NIL);
146 }
149 /* Write current time in internal format
150 * Accepts: destination string
151 */
153 void internal_date (char *date)
154 {
155 do_date (date,NIL,"%02d-%s-%d %02d:%02d:%02d %+03d%02d",NIL);
156 }
158 /* Return my home directory name
159 * Returns: my home directory name
160 */
162 char *myhomedir ()
163 {
164 if (!myHomeDir) { /* get home directory name if not yet known */
165 char *s;
166 if ((s = getenv ("PINEHOME")) || (s = getenv ("HOME")) ||
167 (s = getenv ("ETC"))) {
168 myHomeDir = cpystr (s);
169 while (s = strchr (myHomeDir,'/')) *s = '\\';
170 if ((s = strrchr (myHomeDir,'\\')) && !s[1]) *s = '\0';
171 }
172 else myHomeDir = cpystr ("");
173 }
174 return myHomeDir;
175 }
178 /* Return mailbox file name
179 * Accepts: destination buffer
180 * mailbox name
181 * Returns: file name
182 */
184 char *mailboxfile (char *dst,char *name)
185 {
186 char *s;
187 char *ext = (char *) mail_parameters (NIL,GET_EXTENSION,NIL);
188 /* forbid extraneous extensions */
189 if ((s = strchr ((s = strrchr (name,'\\')) ? s : name,'.')) &&
190 ((ext = (char *) mail_parameters (NIL,GET_EXTENSION,NIL)) ||
191 strchr (s+1,'.'))) return NIL;
192 /* absolute path name? */
193 if ((*name == '\\') || (name[1] == ':')) strcpy (dst,name);
194 else sprintf (dst,"%s\\%s",myhomedir (),name);
195 if (ext) sprintf (dst + strlen (dst),".%s",ext);
196 return dst;
197 }
199 /* Lock file name
200 * Accepts: return buffer for file name
201 * file name
202 * locking to be placed on file if non-NIL
203 * Returns: file descriptor of lock or -1 if error
204 */
206 int lockname (char *lock,char *fname,int op)
207 {
208 int ld;
209 char c,*s;
210 if (!((s = lockdir (lock,getenv ("TEMP"),NIL)) ||
211 (s = lockdir (lock,getenv ("TMP"),NIL)) ||
212 (s = lockdir (lock,getenv ("TMPDIR"),NIL)) ||
213 /* C:\TEMP is last resort */
214 (s = lockdir (lock,defaultDrive (),"TEMP")))) {
215 mm_log ("Unable to find temporary directory",ERROR);
216 return -1;
217 }
218 /* generate file name */
219 while (c = *fname++) switch (c) {
220 case '/': case '\\': case ':':
221 *s++ = '!'; /* convert bad chars to ! */
222 break;
223 default:
224 *s++ = c;
225 break;
226 }
227 *s++ = c; /* tie off name */
228 /* get the lock */
229 if (((ld = open (lock,O_BINARY|O_RDWR|O_CREAT,S_IREAD|S_IWRITE)) >= 0) && op)
230 flock (ld,op); /* apply locking function */
231 return ld; /* return locking file descriptor */
232 }
234 /* Build lock directory, check to see if it exists
235 * Accepts: return buffer for lock directory
236 * first part of possible name
237 * optional second part
238 * Returns: pointer to end of buffer if buffer has a good name, else NIL
239 */
241 char *lockdir (char *lock,char *first,char *last)
242 {
243 struct stat sbuf;
244 char c,*s;
245 if (first && *first) { /* first part must be non-NIL */
246 /* copy first part */
247 for (s = lock; *first; c = *s++ = *first++);
248 if (last && *last) { /* copy last part if specified */
249 /* write trailing \ in case not in first */
250 if (c != '\\') *s++ = '\\';
251 while (*last) c = *s++ = *last++;
252 }
253 if (c == '\\') --s; /* delete trailing \ if any */
254 *s = '\0'; /* tie off name at this point */
255 return stat (lock,&sbuf) ? NIL : s;
256 }
257 return NIL; /* failed */
258 }
261 /* Unlock file descriptor
262 * Accepts: file descriptor
263 * lock file name from lockfd()
264 */
266 void unlockfd (int fd,char *lock)
267 {
268 flock (fd,LOCK_UN); /* unlock it */
269 close (fd); /* close it */
270 }
273 /* Determine default prototype stream to user
274 * Accepts: type (NIL for create, T for append)
275 * Returns: default prototype stream
276 */
278 MAILSTREAM *default_proto (long type)
279 {
280 extern MAILSTREAM DEFAULTPROTO;
281 return &DEFAULTPROTO; /* return default driver's prototype */
282 }
284 /* Global data */
286 static unsigned rndm = 0; /* initial `random' number */
289 /* Return random number
290 */
292 long random ()
293 {
294 if (!rndm) srand (rndm = (unsigned) time (0L));
295 return (long) rand ();
296 }
299 /* Emulator for BSD syslog() routine
300 * Accepts: priority
301 * message
302 * parameters
303 */
305 void syslog (int priority,const char *message,...)
306 {
307 }
310 /* Emulator for BSD openlog() routine
311 * Accepts: identity
312 * options
313 * facility
314 */
316 void openlog (const char *ident,int logopt,int facility)
317 {
318 }

UW-IMAP'd extensions by yuuji