warning: new file /sys/src/ape/lib/bsd/sethostent.c not on sources added sethostent() and a few xxx_r() functions to APE. I added: ctime, localtime, gmtime, asctime, strtok, strerror, getlogin, ctermid, tmpnam, Solaris 8 also includes these... gethostbyname, gethostbyaddr, getservbyname, getservbyport, getprotobyname, getnetbyname, getnetbyaddr, getrpcbyname, getrpcbynumber, getrpcent, rand, readdir, getpwent, getpwnam, getpwuid, getspent, fgetspent, getspnam, getgrnam, getgrgid, getnetgrent, getrpcbyname, tempnam, fgetpwent, fgetgrent, ecvt, gcvt, getservent, gethostent, getgrent, fcvt however I don't need them. I was going to add readdir_r() but it is "considered harmful" [http://womble.decadentplace.org.uk/readdir_r-advisory.html] so it seems chirlish to implement it. All the reentrant function prototypes are under the control of _REENTRANT_SOURCE in the hearder files. -Steve Reference: /n/sources/patch/applied/ape-reentrant Date: Fri May 16 16:43:00 CES 2008 Signed-off-by: steve@quintile.net --- /sys/src/ape/lib/ap/stdio/strerror.c Fri May 16 16:37:29 2008 +++ /sys/src/ape/lib/ap/stdio/strerror.c Fri May 16 16:37:26 2008 @@ -81,7 +81,9 @@ int sys_nerr = _IO_nerr; extern char _plan9err[]; -char *strerror(int n){ +char * +strerror(int n) +{ if(n == EPLAN9) return _plan9err; if(n >= 0 && n < _IO_nerr) @@ -92,4 +94,11 @@ return "Range error"; else return "Unknown error"; +} + +char * +strerror_r(int n, char *buf, int len) +{ + strncpy(buf, strerror(n), len); + buf[len-1] = 0; } --- /sys/src/ape/lib/ap/plan9/ctermid.c Fri May 16 16:37:35 2008 +++ /sys/src/ape/lib/ap/plan9/ctermid.c Fri May 16 16:37:32 2008 @@ -12,3 +12,7 @@ strncpy(s, "/dev/cons", sizeof buf); return(s); } + +char *ctermid_r(char *s) { + return s ? ctermid(s) : NULL; +} --- /sys/src/ape/lib/ap/plan9/ctime.c Fri May 16 16:37:42 2008 +++ /sys/src/ape/lib/ap/plan9/ctime.c Fri May 16 16:37:38 2008 @@ -72,37 +72,11 @@ } struct tm* -localtime(const time_t *timp) -{ - struct tm *ct; - time_t t, tim; - long *p; - int i, dlflag; - - tim = *timp; - if(timezone.stname[0] == 0) - readtimezone(); - t = tim + timezone.stdiff; - dlflag = 0; - for(p = timezone.dlpairs; *p; p += 2) - if(t >= p[0]) - if(t < p[1]) { - t = tim + timezone.dldiff; - dlflag++; - break; - } - ct = gmtime(&t); - ct->tm_isdst = dlflag; - return ct; -} - -struct tm* -gmtime(const time_t *timp) +gmtime_r(const time_t *timp, struct tm *result) { int d0, d1; long hms, day; time_t tim; - static struct tm xtime; tim = *timp; /* @@ -118,11 +92,11 @@ /* * generate hours:minutes:seconds */ - xtime.tm_sec = hms % 60; + result->tm_sec = hms % 60; d1 = hms / 60; - xtime.tm_min = d1 % 60; + result->tm_min = d1 % 60; d1 /= 60; - xtime.tm_hour = d1; + result->tm_hour = d1; /* * day is the day number. @@ -130,7 +104,7 @@ * The addend is 4 mod 7 (1/1/1970 was Thursday) */ - xtime.tm_wday = (day + 7340036L) % 7; + result->tm_wday = (day + 7340036L) % 7; /* * year number @@ -141,8 +115,8 @@ else for (d1 = 70; day < 0; d1--) day += dysize(d1-1); - xtime.tm_year = d1; - xtime.tm_yday = d0 = day; + result->tm_year = d1; + result->tm_yday = d0 = day; /* * generate month @@ -153,37 +127,85 @@ for(d1 = 0; d0 >= dmsize[d1]; d1++) d0 -= dmsize[d1]; dmsize[1] = 28; - xtime.tm_mday = d0 + 1; - xtime.tm_mon = d1; - xtime.tm_isdst = 0; - return &xtime; + result->tm_mday = d0 + 1; + result->tm_mon = d1; + result->tm_isdst = 0; + return result; +} + +struct tm* +gmtime(const time_t *timp) +{ + static struct tm xtime; + + return gmtime_r(timp, &xtime); +} + +struct tm* +localtime_r(const time_t *timp, struct tm *result) +{ + struct tm *ct; + time_t t, tim; + long *p; + int i, dlflag; + + tim = *timp; + if(timezone.stname[0] == 0) + readtimezone(); + t = tim + timezone.stdiff; + dlflag = 0; + for(p = timezone.dlpairs; *p; p += 2) + if(t >= p[0]) + if(t < p[1]) { + t = tim + timezone.dldiff; + dlflag++; + break; + } + ct = gmtime_r(&t, result); + ct->tm_isdst = dlflag; + return ct; +} + +struct tm* +localtime(const time_t *timp) +{ + static struct tm xtime; + + return localtime_r(timp, &xtime); } char* -asctime(const struct tm *t) +asctime_r(const struct tm *t, char *buf) { char *ncp; - static char cbuf[30]; - strcpy(cbuf, "Thu Jan 01 00:00:00 1970\n"); + strcpy(buf, "Thu Jan 01 00:00:00 1970\n"); ncp = &"SunMonTueWedThuFriSat"[t->tm_wday*3]; - cbuf[0] = *ncp++; - cbuf[1] = *ncp++; - cbuf[2] = *ncp; + buf[0] = *ncp++; + buf[1] = *ncp++; + buf[2] = *ncp; ncp = &"JanFebMarAprMayJunJulAugSepOctNovDec"[t->tm_mon*3]; - cbuf[4] = *ncp++; - cbuf[5] = *ncp++; - cbuf[6] = *ncp; - ct_numb(cbuf+8, t->tm_mday); - ct_numb(cbuf+11, t->tm_hour+100); - ct_numb(cbuf+14, t->tm_min+100); - ct_numb(cbuf+17, t->tm_sec+100); + buf[4] = *ncp++; + buf[5] = *ncp++; + buf[6] = *ncp; + ct_numb(buf+8, t->tm_mday); + ct_numb(buf+11, t->tm_hour+100); + ct_numb(buf+14, t->tm_min+100); + ct_numb(buf+17, t->tm_sec+100); if(t->tm_year >= 100) { - cbuf[20] = '2'; - cbuf[21] = '0'; + buf[20] = '2'; + buf[21] = '0'; } - ct_numb(cbuf+22, t->tm_year+100); - return cbuf; + ct_numb(buf+22, t->tm_year+100); + return buf; +} + +char* +asctime(const struct tm *t) +{ + static char cbuf[30]; + + return asctime_r(t, cbuf); } static --- /sys/src/ape/lib/ap/plan9/getlogin.c Fri May 16 16:37:49 2008 +++ /sys/src/ape/lib/ap/plan9/getlogin.c Fri May 16 16:37:44 2008 @@ -5,15 +5,23 @@ #include char * -getlogin(void) +getlogin_r(char *buf, int len) { - static char buf[NAME_MAX+1]; int f, n; f = open("/dev/user", O_RDONLY); if(f < 0) return 0; - n = read(f, buf, NAME_MAX+1); + n = read(f, buf, len); + buf[len-1] = 0; close(f); return (n>=0)? buf : 0; +} + +char * +getlogin(void) +{ + static char buf[NAME_MAX+1]; + return getlogin_r(buf, sizeof(buf)); + } --- /sys/src/ape/lib/ap/stdio/tmpnam.c Fri May 16 16:37:56 2008 +++ /sys/src/ape/lib/ap/stdio/tmpnam.c Fri May 16 16:37:53 2008 @@ -19,3 +19,8 @@ } return name; } + +char *tmpnam_r(char *s) { + return s ? tmpnam(s) : NULL; +} + --- /sys/src/ape/lib/ap/gen/strtok.c Fri May 16 16:38:02 2008 +++ /sys/src/ape/lib/ap/gen/strtok.c Fri May 16 16:37:59 2008 @@ -3,16 +3,15 @@ #define N 256 char* -strtok(char *s, const char *b) +strtok_r(char *s, const char *b, char **last) { - static char *under_rock; char map[N], *os; memset(map, 0, N); while(*b) map[*(unsigned char*)b++] = 1; if(s == 0) - s = under_rock; + s = *last; while(map[*(unsigned char*)s++]) ; if(*--s == 0) @@ -20,10 +19,17 @@ os = s; while(map[*(unsigned char*)s] == 0) if(*s++ == 0) { - under_rock = s-1; + *last = s-1; return os; } *s++ = 0; - under_rock = s; + *last = s; return os; +} + +char* +strtok(char *s, const char *b) +{ + static char *under_rock; + return strtok_r(s, b, &under_rock); } --- /sys/src/ape/lib/bsd/sethostent.c Thu Jan 1 00:00:00 1970 +++ /sys/src/ape/lib/bsd/sethostent.c Fri May 16 16:38:04 2008 @@ -0,0 +1,5 @@ +int +sethostent(int) +{ + return 0; +} --- /sys/include/ape/string.h Fri May 16 16:38:11 2008 +++ /sys/include/ape/string.h Fri May 16 16:38:06 2008 @@ -31,6 +31,11 @@ extern char *strerror(int); extern size_t strlen(const char *); +#ifdef _REENTRANT_SOURCE +extern char *strerror_r(int, const char *, int); +extern char *strtok_r(char *, const char *, char **); +#endif + #ifdef _BSD_EXTENSION #include #endif --- /sys/include/ape/stdio.h Fri May 16 16:38:18 2008 +++ /sys/include/ape/stdio.h Fri May 16 16:38:13 2008 @@ -133,6 +133,11 @@ extern char *ctermid(char *); #endif +#ifdef _REENTRANT_SOURCE +extern char *tmpnam_r(char *); +extern char *ctermid_r(char *); +#endif + #ifdef _BSD_EXTENSION #pragma lib "/$M/lib/ape/libbsd.a" extern FILE *popen(char *, char *); --- /sys/include/ape/time.h Fri May 16 16:38:25 2008 +++ /sys/include/ape/time.h Fri May 16 16:38:21 2008 @@ -44,6 +44,12 @@ extern struct tm *localtime(const time_t *); extern size_t strftime(char *, size_t, const char *, const struct tm *); +#ifdef _REENTRANT_SOURCE +extern struct tm *gmtime_r(const time_t *, struct tm *); +extern struct tm *localtime_r(const time_t *, struct tm *); +extern char *ctime_r(const time_t *, char *); +#endif + #ifdef _POSIX_SOURCE extern void tzset(void); #endif --- /sys/include/ape/unistd.h Fri May 16 16:38:31 2008 +++ /sys/include/ape/unistd.h Fri May 16 16:38:28 2008 @@ -149,6 +149,10 @@ extern int tcsetpgrp(int, pid_t); #endif +#ifdef _REENTRANT_SOURCE +extern char *getlogin_r(char *, int); +#endif + /* berkeley specific functions */ #ifdef _BSD_EXTENSION #include