|
|
@@ -830,3 +830,110 @@ long m2dstrimpostfix(char *s)
|
|
|
}
|
|
|
return (long)s;
|
|
|
}
|
|
|
+
|
|
|
+/* ---------------- TimeString / wrapclock / wraptime / wrapc -------- */
|
|
|
+
|
|
|
+#include <sys/stat.h>
|
|
|
+#include <sys/time.h>
|
|
|
+#include <pwd.h>
|
|
|
+#include <math.h>
|
|
|
+#include <fcntl.h>
|
|
|
+
|
|
|
+long m2timestring(long *desc)
|
|
|
+{
|
|
|
+ char buf[128];
|
|
|
+ char *dst = (char *)(desc + 1);
|
|
|
+ long cap = desc[0], n;
|
|
|
+ time_t t = time(NULL);
|
|
|
+ struct tm tmv;
|
|
|
+ localtime_r(&t, &tmv);
|
|
|
+ if (strftime(buf, sizeof buf, "%c", &tmv) == 0) buf[0] = 0;
|
|
|
+ n = (long)strlen(buf);
|
|
|
+ if (cap < 0) cap = 0;
|
|
|
+ if (n > cap) n = cap;
|
|
|
+ memcpy(dst, buf, (size_t)n);
|
|
|
+ dst[n] = 0;
|
|
|
+ return n;
|
|
|
+}
|
|
|
+
|
|
|
+long m2wctimezone(void) { return (long)timezone; }
|
|
|
+long m2wcistimezone(void) { return 1; }
|
|
|
+long m2wcdaylight(void) { return (long)daylight; }
|
|
|
+long m2wcisdst(void) { return 1; }
|
|
|
+long m2wctzname(long d) { return (long)(d ? tzname[1] : tzname[0]); }
|
|
|
+void *m2wctsnew(void) { return calloc(1, sizeof(struct timespec)); }
|
|
|
+void *m2wctskill(void *ts){ free(ts); return NULL; }
|
|
|
+long m2wctsget(void *ts, long *sec, long *nano)
|
|
|
+{
|
|
|
+ struct timespec *t = (struct timespec *)ts;
|
|
|
+ if (t == NULL) return -1;
|
|
|
+ *sec = (long)t->tv_sec; *nano = (long)t->tv_nsec; return 0;
|
|
|
+}
|
|
|
+long m2wctsset(void *ts, long sec, long nano)
|
|
|
+{
|
|
|
+ struct timespec *t = (struct timespec *)ts;
|
|
|
+ if (t == NULL) return -1;
|
|
|
+ t->tv_sec = (time_t)sec; t->tv_nsec = (long)nano; return 0;
|
|
|
+}
|
|
|
+long m2wctsrealtime(void *ts)
|
|
|
+{ return clock_gettime(CLOCK_REALTIME, (struct timespec *)ts); }
|
|
|
+long m2wctssetrealtime(void *ts)
|
|
|
+{ return clock_settime(CLOCK_REALTIME, (struct timespec *)ts); }
|
|
|
+
|
|
|
+void *m2wttvnew(void) { return calloc(1, sizeof(struct timeval)); }
|
|
|
+void *m2wttvkill(void *p) { free(p); return NULL; }
|
|
|
+void *m2wttznew(void) { return calloc(1, sizeof(struct timezone)); }
|
|
|
+void *m2wttzkill(void *p) { free(p); return NULL; }
|
|
|
+void *m2wttmnew(void) { return calloc(1, sizeof(struct tm)); }
|
|
|
+void *m2wttmkill(void *p) { free(p); return NULL; }
|
|
|
+long m2wtgettimeofday(void *tv, void *tz)
|
|
|
+{ return gettimeofday((struct timeval *)tv, (struct timezone *)tz); }
|
|
|
+long m2wtsettimeofday(void *tv, void *tz)
|
|
|
+{ return settimeofday((struct timeval *)tv, (struct timezone *)tz); }
|
|
|
+long m2wtfractions(void *tv)
|
|
|
+{ struct timeval *t = (struct timeval *)tv; return t ? (long)t->tv_usec : 0; }
|
|
|
+void *m2wtlocaltime(void *tv, void *tm)
|
|
|
+{ struct timeval *t = (struct timeval *)tv; return localtime_r(&t->tv_sec, (struct tm *)tm); }
|
|
|
+long m2wtyear(void *m) { return ((struct tm *)m)->tm_year + 1900; }
|
|
|
+long m2wtmonth(void *m) { return ((struct tm *)m)->tm_mon + 1; }
|
|
|
+long m2wtday(void *m) { return ((struct tm *)m)->tm_mday; }
|
|
|
+long m2wthour(void *m) { return ((struct tm *)m)->tm_hour; }
|
|
|
+long m2wtminute(void *m) { return ((struct tm *)m)->tm_min; }
|
|
|
+long m2wtsecond(void *m) { return ((struct tm *)m)->tm_sec; }
|
|
|
+void m2wttvset(void *tv, long sec, long usec)
|
|
|
+{ struct timeval *t = (struct timeval *)tv; t->tv_sec = (time_t)sec; t->tv_usec = (long)usec; }
|
|
|
+void m2wttzset(void *tz, long minuteswest, long dsttime)
|
|
|
+{ struct timezone *z = (struct timezone *)tz; z->tz_minuteswest = (int)minuteswest; z->tz_dsttime = (int)dsttime; }
|
|
|
+
|
|
|
+long m2cstrtime(void) { time_t t = time(NULL); return (long)ctime(&t); }
|
|
|
+long m2cfilesize(long f, long *low, long *high)
|
|
|
+{
|
|
|
+ struct stat st;
|
|
|
+ if (fstat((int)f, &st) != 0) { *low = 0; *high = 0; return -1; }
|
|
|
+ *low = (long)(st.st_size & 0xffffffffL);
|
|
|
+ *high = (long)((unsigned long long)st.st_size >> 32);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+long m2cfileinode(long f, long *low, long *high)
|
|
|
+{
|
|
|
+ struct stat st;
|
|
|
+ if (fstat((int)f, &st) != 0) { *low = 0; *high = 0; return -1; }
|
|
|
+ *low = (long)(st.st_ino & 0xffffffffL);
|
|
|
+ *high = (long)((unsigned long long)st.st_ino >> 32);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+long m2cfilemtime(long f)
|
|
|
+{ struct stat st; if (fstat((int)f, &st) != 0) return -1; return (long)st.st_mtime; }
|
|
|
+long m2cgetrand(long n) { return n <= 0 ? 0 : (long)(random() % n); }
|
|
|
+long m2cgetusername(void)
|
|
|
+{ struct passwd *pw = getpwuid(getuid()); return pw ? (long)pw->pw_name : (long)""; }
|
|
|
+void m2cgetnameuidgid(long name, long *uid, long *gid)
|
|
|
+{ struct passwd *pw = getpwnam((const char *)name);
|
|
|
+ if (pw == NULL) { *uid = -1; *gid = -1; } else { *uid = (long)pw->pw_uid; *gid = (long)pw->pw_gid; } }
|
|
|
+long m2csignbit(double r) { return signbit(r) ? 1 : 0; }
|
|
|
+long m2cisfinite(double x) { return isfinite(x) ? 1 : 0; }
|
|
|
+long m2cisnan(double x) { return isnan(x) ? 1 : 0; }
|
|
|
+long m2cseekset(void) { return (long)SEEK_SET; }
|
|
|
+long m2cseekend(void) { return (long)SEEK_END; }
|
|
|
+long m2creadonly(void) { return (long)O_RDONLY; }
|
|
|
+long m2cwriteonly(void) { return (long)O_WRONLY; }
|