time: threadsafe functions
authorAlan Cox <alan@linux.intel.com>
Fri, 2 Jan 2015 21:50:47 +0000 (21:50 +0000)
committerAlan Cox <alan@linux.intel.com>
Fri, 2 Jan 2015 21:50:47 +0000 (21:50 +0000)
Not much use at the moment but since many modern apps favour them anyway we
might as well provide them given they are pretty much zero cost.

Library/include/time.h
Library/libs/Makefile
Library/libs/asctime.c
Library/libs/ctime_r.c [new file with mode: 0644]
Library/libs/gmtime_r.c [new file with mode: 0644]
Library/libs/localtim_r.c [new file with mode: 0644]

index 2c8a579..8a3d273 100644 (file)
@@ -39,17 +39,18 @@ extern clock_t clock __P ((void));
 extern time_t mktime __P ((struct tm * __tp));
 extern double difftime __P ((time_t *__time2, time_t *__time1));
 
-extern time_t *gtime(time_t *tvec);
 
 extern void __tm_conv __P((struct tm *tmbuf, time_t *t, int offset));
-extern void __asctime __P((char *, struct tm *));
 extern char *asctime __P ((struct tm * __tp));
+extern char *asctime_r __P((struct tm *, char * __buf));
 extern char *ctime __P ((time_t * __tp));
+extern char *ctime_r __P ((time_t * __tp, char * __buf));
 extern void tzset __P ((void));
 
 extern struct tm *gmtime __P ((time_t *__tp));
 extern struct tm *localtime __P ((time_t * __tp));
-extern unsigned long convtime __P ((time_t *time_field));
+extern struct tm *gmtime_r(time_t *tvec, struct tm *result);
+extern struct tm *localtime_r(time_t *tvec, struct tm *result);
 
 typedef int clockid_t;
 
index 28acf62..dd8d66a 100644 (file)
@@ -21,9 +21,9 @@ SRC_C += fgetpos.c fgets.c fopen.c fprintf.c fputc.c fputs.c fread.c free.c
 SRC_C += fsetpos.c ftell.c fwrite.c getcwd.c
 SRC_C += getenv.c __getgrent.c getgrgid.c getgrnam.c getopt.c
 SRC_C += getpw.c __getpwent.c getpwnam.c getpwuid.c gets.c gettimeofday.c
-SRC_C += gmtime.c grent.c index.c isatty.c itoa.c killpg.c
-SRC_C += localtim.c lseek.c lsearch.c lstat.c ltoa.c ltostr.c malloc.c
-SRC_C += mkfifo.c nanosleep.c opendir.c pause.c perror.c
+SRC_C += gmtime.c gmtime_r.c grent.c index.c isatty.c itoa.c killpg.c
+SRC_C += localtim.c localtim_r.c lseek.c lsearch.c lstat.c ltoa.c ltostr.c
+SRC_C += malloc.c mkfifo.c nanosleep.c opendir.c pause.c perror.c
 SRC_C += popen.c printf.c putenv.c putgetch.c putpwent.c pwent.c qsort.c
 SRC_C += raise.c rand.c readdir.c readlink.c realloc.c regerror.c
 SRC_C += regsub.c remove.c rewind.c rindex.c setbuffer.c setenv.c setjmp.c
index 9170fcc..1f6caa8 100644 (file)
@@ -16,7 +16,7 @@ static void hit(char *buf, int val)
 static char days[] = "SunMonTueWedThuFriSat";
 static char mons[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
 
-void __asctime(char *buffer, struct tm *ptm)
+char *asctime_r(struct tm *ptm, char *buffer)
 {
        int year;
 
@@ -38,6 +38,7 @@ void __asctime(char *buffer, struct tm *ptm)
        hit(buffer + 21, year / 100);
        hit(buffer + 22, year / 10);
        hit(buffer + 23, year);
+       return buffer;
 }
 
 /* asctime - convert date and time to ascii.
@@ -49,6 +50,5 @@ char *asctime(struct tm *timeptr)
 
        if (timeptr == 0)
                return 0;
-       __asctime(timebuf, timeptr);
-       return timebuf;
+       return asctime_r(timeptr, timebuf);
 }
diff --git a/Library/libs/ctime_r.c b/Library/libs/ctime_r.c
new file mode 100644 (file)
index 0000000..5976004
--- /dev/null
@@ -0,0 +1,10 @@
+/*************************** CTIME ************************************/  \r
+    \r
+#include <time.h>\r
+#include <string.h>\r
+\r
+char *ctime_r(time_t * timep, char *buf) \r
+{\r
+       struct tm tmtmp;
+       return asctime_r(localtime_r(timep, &tmtmp), buf);
+}
diff --git a/Library/libs/gmtime_r.c b/Library/libs/gmtime_r.c
new file mode 100644 (file)
index 0000000..c8f2ebe
--- /dev/null
@@ -0,0 +1,10 @@
+/*************************** GMTIME ************************************/ 
+
+#include <time.h>
+#include <string.h>
+
+struct tm *gmtime_r(time_t *timep, struct tm *result)
+{
+       __tm_conv(result, timep, (int) (timezone / 60));
+       return result;
+}
diff --git a/Library/libs/localtim_r.c b/Library/libs/localtim_r.c
new file mode 100644 (file)
index 0000000..04bad7a
--- /dev/null
@@ -0,0 +1,8 @@
+#include <time.h>
+#include <string.h>
+
+struct tm *localtime_r(time_t * timep, struct tm *result)
+{
+       __tm_conv(result, timep, 0);
+       return result;
+}