Usermode/lib{c,posix} - Adding clock_* POSIX functions
authorJohn Hodge <[email protected]>
Sun, 9 Jun 2013 07:07:20 +0000 (15:07 +0800)
committerJohn Hodge <[email protected]>
Sun, 9 Jun 2013 07:07:20 +0000 (15:07 +0800)
Usermode/Libraries/libc.so_src/include_exp/time.h
Usermode/Libraries/libposix.so_src/Makefile
Usermode/Libraries/libposix.so_src/clocks.c [new file with mode: 0644]
Usermode/Libraries/libposix.so_src/include_exp/libposix_time.h [new file with mode: 0644]

index ce69326..8480d68 100644 (file)
@@ -3,18 +3,18 @@
  * - By John Hodge (thePowersGang)
  *
  * time.h
- * - POSIX time functions
+ * - C99 Time Functions
  */
 #ifndef _TIME_H_
 #define _TIME_H_
 
-//#include <acess/intdefs.h>
 #include <sys/types.h> // time_t
+#include <stddef.h>    // size_t
 
 struct tm
 {
-        int    tm_sec;
-        int    tm_min;
+        int    tm_sec; // 0-60
+        int    tm_min; // 0-59
         int    tm_hour;
         int    tm_mday;
         int    tm_mon;
@@ -28,9 +28,37 @@ struct tm
 
 typedef signed long long       clock_t;
 
+//! Processor time used since invocation
 extern clock_t clock(void);
 
+//! Difference between two times in seconds
+extern double difftime(time_t time1, time_t time0);
+
+//! Convert a local time into the format returned by \a time
+//! \note Also updates tm_wday and tm_yday to the correct values
+extern time_t  mktime(struct tm *timeptr);
+
+//! Get the current calendar time
+//! \note -1 is returned if the calendar time is not avaliable
 extern time_t  time(time_t *t);
 
+//
+// Time conversions
+//
+//! Convert the time structure into a string of form 'Sun Sep 16 01:03:52 1973\n\0'
+extern char *asctime(const struct tm *timeptr);
+
+//! asctime(localtime(timer))
+extern char *ctime(const time_t *timer);
+
+//! Convert \a timter into UTC
+extern struct tm *gmtime(const time_t *timer);
+
+extern struct tm *localtime(const time_t *timer);
+
+extern size_t strftime(char*restrict s, size_t maxsize, const char*restrict format, const struct tm*restrict timeptr);
+
+#include <libposix_time.h>
+
 #endif
 
index 6e666e8..ba1cb2f 100644 (file)
@@ -4,13 +4,13 @@
 -include ../Makefile.cfg\r
 \r
 CPPFLAGS += \r
-CFLAGS   += -Werror -Wextra\r
+CFLAGS   += -Wextra\r
 ASFLAGS  +=\r
 LDFLAGS  += -soname libposix.so -Map map.txt -lc\r
 \r
 OBJ  = main.o unistd.o dirent.o stat.o utmpx.o termios.o\r
 OBJ += pwd.o syslog.o sys_time.o sys_ioctl.o sys_resource.o\r
-OBJ += fcntl.o\r
+OBJ += fcntl.o clocks.o\r
 DEPFILES := $(OBJ:%.o=%.d)\r
 BIN = libposix.so\r
 \r
diff --git a/Usermode/Libraries/libposix.so_src/clocks.c b/Usermode/Libraries/libposix.so_src/clocks.c
new file mode 100644 (file)
index 0000000..358b174
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Acess2 POSIX Emulation
+ * - By John Hodge (thePowersGang)
+ *
+ * clocks.c
+ * - clock_* functions
+ */
+#include <acess/sys.h>
+#include <time.h>
+#include <errno.h>
+
+// === CODE ===
+int clock_getres(clockid_t clk_id, struct timespec *res)
+{
+       switch(clk_id)
+       {
+       case CLOCK_REALTIME:
+       case CLOCK_MONOTONIC:
+               if( res ) {
+                       res->tv_sec = 0;
+                       res->tv_nsec = 1000*1000;
+               }
+               return 0;
+       }
+       errno = EINVAL;
+       return -1;
+}
+
+int clock_gettime(clockid_t clk_id, struct timespec *tp)
+{
+       switch(clk_id)
+       {
+       case CLOCK_REALTIME:
+       case CLOCK_MONOTONIC:
+               // TODO: True monotonic clock
+               if( tp ) {
+                       int64_t ts = _SysTimestamp();
+                       tp->tv_sec = ts / 1000;
+                       tp->tv_nsec = (ts % 1000) * 1000*1000;
+               }
+               return 0;
+       }
+       errno = EINVAL;
+       return -1;
+}
+
+int clock_settime(clockid_t clk_id, const struct timespec *tp)
+{
+       errno = EPERM;
+       return -1;
+}
diff --git a/Usermode/Libraries/libposix.so_src/include_exp/libposix_time.h b/Usermode/Libraries/libposix.so_src/include_exp/libposix_time.h
new file mode 100644 (file)
index 0000000..8a6557f
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Acess2 POSIX Emulation
+ * - By John Hodge (thePowersGang)
+ *
+ * time.h (libposix version)
+ * - Time and Clock definitions
+ *
+ * NOTE: Included by libc's time.h
+ */
+#ifndef _LIBPOSIX__TIME_H_
+#define _LIBPOSIX__TIME_H_
+
+struct timespec
+{
+       time_t  tv_sec;
+       long    tv_nsec;
+};
+
+enum clockid_e
+{
+       CLOCK_REALTIME,
+       CLOCK_MONOTONIC
+};
+
+typedef enum clockid_e clockid_t;
+
+extern int clock_getres(clockid_t clk_id, struct timespec *res);
+extern int clock_gettime(clockid_t clk_id, struct timespec *tp);
+extern int clock_settime(clockid_t clk_id, const struct timespec *tp);
+
+#endif
+

UCC git Repository :: git.ucc.asn.au