imapext-2007
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/osdep/os2/env_os2.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,318 @@ 1.4 +/* ======================================================================== 1.5 + * Copyright 1988-2006 University of Washington 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * 1.14 + * ======================================================================== 1.15 + */ 1.16 + 1.17 +/* 1.18 + * Program: OS/2 environment routines 1.19 + * 1.20 + * Author: Mark Crispin 1.21 + * Networks and Distributed Computing 1.22 + * Computing & Communications 1.23 + * University of Washington 1.24 + * Administration Building, AG-44 1.25 + * Seattle, WA 98195 1.26 + * Internet: MRC@CAC.Washington.EDU 1.27 + * 1.28 + * Date: 1 August 1988 1.29 + * Last Edited: 30 August 2006 1.30 + */ 1.31 + 1.32 + 1.33 +static char *myLocalHost = NIL; /* local host name */ 1.34 +static char *myHomeDir = NIL; /* home directory name */ 1.35 +static char *myNewsrc = NIL; /* newsrc file name */ 1.36 +static short no822tztext = NIL; /* disable RFC [2]822 timezone text */ 1.37 + 1.38 +#include "write.c" /* include safe writing routines */ 1.39 +#include "pmatch.c" /* include wildcard pattern matcher */ 1.40 + 1.41 + 1.42 +/* Get all authenticators */ 1.43 + 1.44 +#include "auths.c" 1.45 + 1.46 +/* Environment manipulate parameters 1.47 + * Accepts: function code 1.48 + * function-dependent value 1.49 + * Returns: function-dependent return value 1.50 + */ 1.51 + 1.52 +void *env_parameters (long function,void *value) 1.53 +{ 1.54 + void *ret = NIL; 1.55 + switch ((int) function) { 1.56 + case SET_HOMEDIR: 1.57 + myHomeDir = cpystr ((char *) value); 1.58 + case GET_HOMEDIR: 1.59 + ret = (void *) myHomeDir; 1.60 + break; 1.61 + case SET_LOCALHOST: 1.62 + myLocalHost = cpystr ((char *) value); 1.63 + case GET_LOCALHOST: 1.64 + ret = (void *) myLocalHost; 1.65 + break; 1.66 + case SET_NEWSRC: 1.67 + if (myNewsrc) fs_give ((void **) &myNewsrc); 1.68 + myNewsrc = cpystr ((char *) value); 1.69 + case GET_NEWSRC: 1.70 + if (!myNewsrc) { /* set news file name if not defined */ 1.71 + char tmp[MAILTMPLEN]; 1.72 + sprintf (tmp,"%s\\newsrc",myhomedir ()); 1.73 + myNewsrc = cpystr (tmp); 1.74 + } 1.75 + ret = (void *) myNewsrc; 1.76 + break; 1.77 + case SET_DISABLE822TZTEXT: 1.78 + no822tztext = value ? T : NIL; 1.79 + case GET_DISABLE822TZTEXT: 1.80 + ret = (void *) (no822tztext ? VOIDT : NIL); 1.81 + break; 1.82 + } 1.83 + return ret; 1.84 +} 1.85 + 1.86 +/* Write current time 1.87 + * Accepts: destination string 1.88 + * optional format of day-of-week prefix 1.89 + * format of date and time 1.90 + * flag whether to append symbolic timezone 1.91 + */ 1.92 + 1.93 +static void do_date (char *date,char *prefix,char *fmt,int suffix) 1.94 +{ 1.95 + time_t tn = time (0); 1.96 + struct tm *t = gmtime (&tn); 1.97 + int zone = t->tm_hour * 60 + t->tm_min; 1.98 + int julian = t->tm_yday; 1.99 + t = localtime (&tn); /* get local time now */ 1.100 + /* minus UTC minutes since midnight */ 1.101 + zone = t->tm_hour * 60 + t->tm_min - zone; 1.102 + /* julian can be one of: 1.103 + * 36x local time is December 31, UTC is January 1, offset -24 hours 1.104 + * 1 local time is 1 day ahead of UTC, offset +24 hours 1.105 + * 0 local time is same day as UTC, no offset 1.106 + * -1 local time is 1 day behind UTC, offset -24 hours 1.107 + * -36x local time is January 1, UTC is December 31, offset +24 hours 1.108 + */ 1.109 + if (julian = t->tm_yday -julian) 1.110 + zone += ((julian < 0) == (abs (julian) == 1)) ? -24*60 : 24*60; 1.111 + if (prefix) { /* want day of week? */ 1.112 + sprintf (date,prefix,days[t->tm_wday]); 1.113 + date += strlen (date); /* make next sprintf append */ 1.114 + } 1.115 + /* output the date */ 1.116 + sprintf (date,fmt,t->tm_mday,months[t->tm_mon],t->tm_year+1900, 1.117 + t->tm_hour,t->tm_min,t->tm_sec,zone/60,abs (zone) % 60); 1.118 + if (suffix) { /* append timezone suffix if desired */ 1.119 + char *tz; 1.120 + tzset (); /* get timezone from TZ environment stuff */ 1.121 + tz = tzname[daylight ? (((struct tm *) t)->tm_isdst > 0) : 0]; 1.122 + if (tz && tz[0]) { 1.123 + char *s; 1.124 + for (s = tz; *s; s++) if (*s & 0x80) return; 1.125 + sprintf (date + strlen (date)," (%.50s)",tz); 1.126 + } 1.127 + } 1.128 +} 1.129 + 1.130 + 1.131 +/* Write current time in RFC 822 format 1.132 + * Accepts: destination string 1.133 + */ 1.134 + 1.135 +void rfc822_date (char *date) 1.136 +{ 1.137 + do_date (date,"%s, ","%d %s %d %02d:%02d:%02d %+03d%02d", 1.138 + no822tztext ? NIL : T); 1.139 +} 1.140 + 1.141 + 1.142 +/* Write current time in fixed-width RFC 822 format 1.143 + * Accepts: destination string 1.144 + */ 1.145 + 1.146 +void rfc822_fixed_date (char *date) 1.147 +{ 1.148 + do_date (date,NIL,"%02d %s %4d %02d:%02d:%02d %+03d%02d",NIL); 1.149 +} 1.150 + 1.151 + 1.152 +/* Write current time in internal format 1.153 + * Accepts: destination string 1.154 + */ 1.155 + 1.156 +void internal_date (char *date) 1.157 +{ 1.158 + do_date (date,NIL,"%02d-%s-%d %02d:%02d:%02d %+03d%02d",NIL); 1.159 +} 1.160 + 1.161 +/* Return my home directory name 1.162 + * Returns: my home directory name 1.163 + */ 1.164 + 1.165 +char *myhomedir () 1.166 +{ 1.167 + if (!myHomeDir) { /* get home directory name if not yet known */ 1.168 + char *s; 1.169 + if ((s = getenv ("PINEHOME")) || (s = getenv ("HOME")) || 1.170 + (s = getenv ("ETC"))) { 1.171 + myHomeDir = cpystr (s); 1.172 + while (s = strchr (myHomeDir,'/')) *s = '\\'; 1.173 + if ((s = strrchr (myHomeDir,'\\')) && !s[1]) *s = '\0'; 1.174 + } 1.175 + else myHomeDir = cpystr (""); 1.176 + } 1.177 + return myHomeDir; 1.178 +} 1.179 + 1.180 + 1.181 +/* Return mailbox file name 1.182 + * Accepts: destination buffer 1.183 + * mailbox name 1.184 + * Returns: file name 1.185 + */ 1.186 + 1.187 +char *mailboxfile (char *dst,char *name) 1.188 +{ 1.189 + char *s; 1.190 + char *ext = (char *) mail_parameters (NIL,GET_EXTENSION,NIL); 1.191 + /* forbid extraneous extensions */ 1.192 + if ((s = strchr ((s = strrchr (name,'\\')) ? s : name,'.')) && 1.193 + ((ext = (char *) mail_parameters (NIL,GET_EXTENSION,NIL)) || 1.194 + strchr (s+1,'.'))) return NIL; 1.195 + /* absolute path name? */ 1.196 + if ((*name == '\\') || (name[1] == ':')) strcpy (dst,name); 1.197 + else sprintf (dst,"%s\\%s",myhomedir (),name); 1.198 + if (ext) sprintf (dst + strlen (dst),".%s",ext); 1.199 + return dst; 1.200 +} 1.201 + 1.202 +/* Lock file name 1.203 + * Accepts: return buffer for file name 1.204 + * file name 1.205 + * locking to be placed on file if non-NIL 1.206 + * Returns: file descriptor of lock or -1 if error 1.207 + */ 1.208 + 1.209 +int lockname (char *lock,char *fname,int op) 1.210 +{ 1.211 + int ld; 1.212 + char c,*s; 1.213 + if (!((s = lockdir (lock,getenv ("TEMP"),NIL)) || 1.214 + (s = lockdir (lock,getenv ("TMP"),NIL)) || 1.215 + (s = lockdir (lock,getenv ("TMPDIR"),NIL)) || 1.216 + /* C:\TEMP is last resort */ 1.217 + (s = lockdir (lock,defaultDrive (),"TEMP")))) { 1.218 + mm_log ("Unable to find temporary directory",ERROR); 1.219 + return -1; 1.220 + } 1.221 + /* generate file name */ 1.222 + while (c = *fname++) switch (c) { 1.223 + case '/': case '\\': case ':': 1.224 + *s++ = '!'; /* convert bad chars to ! */ 1.225 + break; 1.226 + default: 1.227 + *s++ = c; 1.228 + break; 1.229 + } 1.230 + *s++ = c; /* tie off name */ 1.231 + /* get the lock */ 1.232 + if (((ld = open (lock,O_BINARY|O_RDWR|O_CREAT,S_IREAD|S_IWRITE)) >= 0) && op) 1.233 + flock (ld,op); /* apply locking function */ 1.234 + return ld; /* return locking file descriptor */ 1.235 +} 1.236 + 1.237 +/* Build lock directory, check to see if it exists 1.238 + * Accepts: return buffer for lock directory 1.239 + * first part of possible name 1.240 + * optional second part 1.241 + * Returns: pointer to end of buffer if buffer has a good name, else NIL 1.242 + */ 1.243 + 1.244 +char *lockdir (char *lock,char *first,char *last) 1.245 +{ 1.246 + struct stat sbuf; 1.247 + char c,*s; 1.248 + if (first && *first) { /* first part must be non-NIL */ 1.249 + /* copy first part */ 1.250 + for (s = lock; *first; c = *s++ = *first++); 1.251 + if (last && *last) { /* copy last part if specified */ 1.252 + /* write trailing \ in case not in first */ 1.253 + if (c != '\\') *s++ = '\\'; 1.254 + while (*last) c = *s++ = *last++; 1.255 + } 1.256 + if (c == '\\') --s; /* delete trailing \ if any */ 1.257 + *s = '\0'; /* tie off name at this point */ 1.258 + return stat (lock,&sbuf) ? NIL : s; 1.259 + } 1.260 + return NIL; /* failed */ 1.261 +} 1.262 + 1.263 + 1.264 +/* Unlock file descriptor 1.265 + * Accepts: file descriptor 1.266 + * lock file name from lockfd() 1.267 + */ 1.268 + 1.269 +void unlockfd (int fd,char *lock) 1.270 +{ 1.271 + flock (fd,LOCK_UN); /* unlock it */ 1.272 + close (fd); /* close it */ 1.273 +} 1.274 + 1.275 + 1.276 +/* Determine default prototype stream to user 1.277 + * Accepts: type (NIL for create, T for append) 1.278 + * Returns: default prototype stream 1.279 + */ 1.280 + 1.281 +MAILSTREAM *default_proto (long type) 1.282 +{ 1.283 + extern MAILSTREAM DEFAULTPROTO; 1.284 + return &DEFAULTPROTO; /* return default driver's prototype */ 1.285 +} 1.286 + 1.287 +/* Global data */ 1.288 + 1.289 +static unsigned rndm = 0; /* initial `random' number */ 1.290 + 1.291 + 1.292 +/* Return random number 1.293 + */ 1.294 + 1.295 +long random () 1.296 +{ 1.297 + if (!rndm) srand (rndm = (unsigned) time (0L)); 1.298 + return (long) rand (); 1.299 +} 1.300 + 1.301 + 1.302 +/* Emulator for BSD syslog() routine 1.303 + * Accepts: priority 1.304 + * message 1.305 + * parameters 1.306 + */ 1.307 + 1.308 +void syslog (int priority,const char *message,...) 1.309 +{ 1.310 +} 1.311 + 1.312 + 1.313 +/* Emulator for BSD openlog() routine 1.314 + * Accepts: identity 1.315 + * options 1.316 + * facility 1.317 + */ 1.318 + 1.319 +void openlog (const char *ident,int logopt,int facility) 1.320 +{ 1.321 +}