Index: cygwin.din =================================================================== RCS file: /cvs/src/src/winsup/cygwin/cygwin.din,v retrieving revision 1.37.2.4 diff -u -p -r1.37.2.4 cygwin.din --- cygwin.din 2002/01/04 03:56:06 1.37.2.4 +++ cygwin.din 2002/01/16 22:12:34 @@ -777,6 +778,7 @@ strncpy _strncpy = strncpy strpbrk _strpbrk = strpbrk +strptime strrchr _strrchr = strrchr strspn Index: times.cc =================================================================== RCS file: /cvs/src/src/winsup/cygwin/times.cc,v retrieving revision 1.23.2.1 diff -u -p -r1.23.2.1 times.cc --- times.cc 2002/01/15 12:52:52 1.23.2.1 +++ times.cc 2002/01/16 22:12:34 @@ -24,36 +24,41 @@ details. */ #include "sync.h" #include "sigproc.h" #include "pinfo.h" +#include #define FACTOR (0x19db1ded53e8000LL) #define NSPERSEC 10000000LL -static void __stdcall timeval_to_filetime (timeval *time, FILETIME *out); +static void __stdcall timeval_to_filetime (timeval * time, FILETIME * out); /* Cygwin internal */ static unsigned long long __stdcall __to_clock_t (FILETIME * src, int flag) { - unsigned long long total = ((unsigned long long) src->dwHighDateTime << 32) + ((unsigned)src->dwLowDateTime); - syscall_printf ("dwHighDateTime %u, dwLowDateTime %u", src->dwHighDateTime, src->dwLowDateTime); + unsigned long long total = + ((unsigned long long) src->dwHighDateTime << 32) + + ((unsigned) src->dwLowDateTime); + syscall_printf ("dwHighDateTime %u, dwLowDateTime %u", src->dwHighDateTime, + src->dwLowDateTime); /* Convert into clock ticks - the total is in 10ths of a usec. */ if (flag) total -= FACTOR; total /= (unsigned long long) (NSPERSEC / CLOCKS_PER_SEC); - syscall_printf ("total %08x %08x\n", (unsigned)(total>>32), (unsigned)(total)); + syscall_printf ("total %08x %08x\n", (unsigned) (total >> 32), + (unsigned) (total)); return total; } /* times: POSIX 4.5.2.1 */ extern "C" clock_t -times (struct tms * buf) +times (struct tms *buf) { FILETIME creation_time, exit_time, kernel_time, user_time; if (check_null_invalid_struct_errno (buf)) - return ((clock_t) -1); + return ((clock_t) - 1); DWORD ticks = GetTickCount (); /* Ticks is in milliseconds, convert to our ticks. Use long long to prevent @@ -65,8 +70,9 @@ times (struct tms * buf) &kernel_time, &user_time); syscall_printf ("ticks %d, CLOCKS_PER_SEC %d", ticks, CLOCKS_PER_SEC); - syscall_printf ("user_time %d, kernel_time %d, creation_time %d, exit_time %d", - user_time, kernel_time, creation_time, exit_time); + syscall_printf + ("user_time %d, kernel_time %d, creation_time %d, exit_time %d", + user_time, kernel_time, creation_time, exit_time); buf->tms_stime = __to_clock_t (&kernel_time, 0); buf->tms_utime = __to_clock_t (&user_time, 0); timeval_to_filetime (&myself->rusage_children.ru_stime, &kernel_time); @@ -85,11 +91,11 @@ times (struct tms * buf) buf->tms_cutime = 0; } - return tc; + return tc; } extern "C" clock_t -_times (struct tms * buf) +_times (struct tms *buf) { return times (buf); } @@ -104,17 +110,17 @@ settimeofday (const struct timeval *tv, tz = tz; /* silence warning about unused variable */ - ptm = gmtime(&tv->tv_sec); - st.wYear = ptm->tm_year + 1900; - st.wMonth = ptm->tm_mon + 1; - st.wDayOfWeek = ptm->tm_wday; - st.wDay = ptm->tm_mday; - st.wHour = ptm->tm_hour; - st.wMinute = ptm->tm_min; - st.wSecond = ptm->tm_sec; + ptm = gmtime (&tv->tv_sec); + st.wYear = ptm->tm_year + 1900; + st.wMonth = ptm->tm_mon + 1; + st.wDayOfWeek = ptm->tm_wday; + st.wDay = ptm->tm_mday; + st.wHour = ptm->tm_hour; + st.wMinute = ptm->tm_min; + st.wSecond = ptm->tm_sec; st.wMilliseconds = tv->tv_usec / 1000; - res = !SetSystemTime(&st); + res = !SetSystemTime (&st); syscall_printf ("%d = settimeofday (%x, %x)", res, tv, tz); @@ -126,26 +132,27 @@ extern "C" char * timezone () { #ifdef _MT_SAFE - char *b=_reent_winsup()->timezone_buf; + char *b = _reent_winsup ()->timezone_buf; #else - static NO_COPY char b[20] = {0}; + static NO_COPY char b[20] = { 0 }; #endif - tzset(); - __small_sprintf (b,"GMT%+d:%02d", (int) (-_timezone / 3600), (int) (abs(_timezone / 60) % 60)); + tzset (); + __small_sprintf (b, "GMT%+d:%02d", (int) (-_timezone / 3600), + (int) (abs (_timezone / 60) % 60)); return b; } /* Cygwin internal */ void __stdcall -totimeval (struct timeval *dst, FILETIME *src, int sub, int flag) +totimeval (struct timeval *dst, FILETIME * src, int sub, int flag) { long long x = __to_clock_t (src, flag); - x *= (int) (1e6) / CLOCKS_PER_SEC; /* Turn x into usecs */ - x -= (long long) sub * (int) (1e6); + x *= (int) (1e6) / CLOCKS_PER_SEC; /* Turn x into usecs */ + x -= (long long) sub *(int) (1e6); - dst->tv_usec = x % (long long) (1e6); /* And split */ + dst->tv_usec = x % (long long) (1e6); /* And split */ dst->tv_sec = x / (long long) (1e6); } @@ -165,7 +172,7 @@ gettimeofday (struct timeval *p, struct if (z != NULL) { - tzset(); + tzset (); z->tz_minuteswest = _timezone / 60; z->tz_dsttime = _daylight; } @@ -175,8 +182,7 @@ gettimeofday (struct timeval *p, struct return res; } -extern "C" -int +extern "C" int _gettimeofday (struct timeval *p, struct timezone *z) { return gettimeofday (p, z); @@ -198,15 +204,13 @@ genf () s.wMilliseconds = 0; SystemTimeToFileTime (&s, &f); - small_printf ("FILE TIME is %08x%08x\n", - f.dwHighDateTime, - f.dwLowDateTime); + small_printf ("FILE TIME is %08x%08x\n", f.dwHighDateTime, f.dwLowDateTime); } #endif /* Cygwin internal */ void -time_t_to_filetime (time_t time_in, FILETIME *out) +time_t_to_filetime (time_t time_in, FILETIME * out) { long long x = time_in * NSPERSEC + FACTOR; out->dwHighDateTime = x >> 32; @@ -215,10 +219,10 @@ time_t_to_filetime (time_t time_in, FILE /* Cygwin internal */ static void __stdcall -timeval_to_filetime (timeval *time_in, FILETIME *out) +timeval_to_filetime (timeval * time_in, FILETIME * out) { long long x = time_in->tv_sec * NSPERSEC + - time_in->tv_usec * (NSPERSEC/1000000) + FACTOR; + time_in->tv_usec * (NSPERSEC / 1000000) + FACTOR; out->dwHighDateTime = x >> 32; out->dwLowDateTime = x; } @@ -236,31 +240,31 @@ time_t_to_timeval (time_t in) /* Cygwin internal */ /* Convert a Win32 time to "UNIX" format. */ long __stdcall -to_time_t (FILETIME *ptr) +to_time_t (FILETIME * ptr) { /* A file time is the number of 100ns since jan 1 1601 stuffed into two long words. A time_t is the number of seconds since jan 1 1970. */ long rem; - long long x = ((long long) ptr->dwHighDateTime << 32) + ((unsigned)ptr->dwLowDateTime); + long long x = + ((long long) ptr->dwHighDateTime << 32) + ((unsigned) ptr->dwLowDateTime); /* pass "no time" as epoch */ if (x == 0) return 0; x -= FACTOR; /* number of 100ns between 1601 and 1970 */ - rem = x % ((long long)NSPERSEC); + rem = x % ((long long) NSPERSEC); rem += (NSPERSEC / 2); - x /= (long long) NSPERSEC; /* number of 100ns in a second */ + x /= (long long) NSPERSEC; /* number of 100ns in a second */ x += (long long) (rem / NSPERSEC); return x; } /* time: POSIX 4.5.1.1, C 4.12.2.4 */ /* Return number of seconds since 00:00 UTC on jan 1, 1970 */ -extern "C" -time_t +extern "C" time_t time (time_t * ptr) { time_t res; @@ -301,7 +305,7 @@ time (time_t * ptr) #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) -#if 0 /* POSIX_LOCALTIME */ +#if 0 /* POSIX_LOCALTIME */ static _CONST int mon_lengths[2][MONSPERYEAR] = { {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, @@ -319,7 +323,7 @@ static _CONST int year_lengths[2] = { */ /* Cygwin internal */ -static struct tm * __stdcall +static struct tm *__stdcall corelocaltime (const time_t * tim_p) { long days, rem; @@ -327,9 +331,9 @@ corelocaltime (const time_t * tim_p) int yleap; _CONST int *ip; #ifdef _MT_SAFE - struct tm &localtime_buf=_reent_winsup()->_localtime_buf; + struct tm &localtime_buf = _reent_winsup ()->_localtime_buf; #else - static NO_COPY struct tm localtime_buf = {0}; + static NO_COPY struct tm localtime_buf = { 0 }; #endif time_t tim = *tim_p; @@ -379,7 +383,8 @@ corelocaltime (const time_t * tim_p) --y; yleap = isleap (y); days += year_lengths[yleap]; - } while (days < 0); + } + while (days < 0); } res->tm_year = y - YEAR_BASE; @@ -402,14 +407,13 @@ corelocaltime (const time_t * tim_p) * localtime takes a time_t (which is in UTC) * and formats it into a struct tm as a local time. */ -extern "C" -struct tm * -localtime (const time_t *tim_p) +extern "C" struct tm * +localtime (const time_t * tim_p) { time_t tim = *tim_p; struct tm *rtm; - tzset(); + tzset (); tim -= _timezone; @@ -427,9 +431,8 @@ localtime (const time_t *tim_p) * gmtime takes a time_t (which is already in UTC) * and just puts it into a struct tm. */ -extern "C" -struct tm * -gmtime (const time_t *tim_p) +extern "C" struct tm * +gmtime (const time_t * tim_p) { time_t tim = *tim_p; @@ -445,8 +448,7 @@ gmtime (const time_t *tim_p) #endif /* POSIX_LOCALTIME */ /* utimes: standards? */ -extern "C" -int +extern "C" int utimes (const char *path, struct timeval *tvp) { int res = 0; @@ -465,7 +467,7 @@ utimes (const char *path, struct timeval /* Note: It's not documented in MSDN that FILE_WRITE_ATTRIBUTES is sufficient to change the timestamps... */ HANDLE h = CreateFileA (win32.get_win32 (), - wincap.has_specific_access_rights () ? + wincap.has_specific_access_rights ()? FILE_WRITE_ATTRIBUTES : GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &sec_none_nih, @@ -502,15 +504,14 @@ utimes (const char *path, struct timeval timeval_to_filetime (tvp + 1, &lastwrite); debug_printf ("incoming lastaccess %08x %08x", - tvp->tv_sec, - tvp->tv_usec); + tvp->tv_sec, tvp->tv_usec); // dump_filetime (lastaccess); // dump_filetime (lastwrite); /* FIXME: SetFileTime needs a handle with a write lock - on the file whose time is being modified. So calls to utime() - fail for read only files. */ + on the file whose time is being modified. So calls to utime() + fail for read only files. */ if (!SetFileTime (h, 0, &lastaccess, &lastwrite)) { @@ -522,14 +523,12 @@ utimes (const char *path, struct timeval CloseHandle (h); } - syscall_printf ("%d = utimes (%s, %x); (h%d)", - res, path, tvp, h); + syscall_printf ("%d = utimes (%s, %x); (h%d)", res, path, tvp, h); return res; } /* utime: POSIX 5.6.6.1 */ -extern "C" -int +extern "C" int utime (const char *path, struct utimbuf *buf) { struct timeval tmp[2]; @@ -545,8 +544,7 @@ utime (const char *path, struct utimbuf } /* ftime: standards? */ -extern "C" -int +extern "C" int ftime (struct timeb *tp) { struct timeval tv; @@ -564,8 +562,188 @@ ftime (struct timeb *tp) } /* obsolete, changed to cygwin_tzset when localtime.c was added - dj */ -extern "C" -void +extern "C" void cygwin_tzset () { +} + +static const char *days[] = + { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", + "Saturday" +}; +static const char *abbr_days[] = + { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Say" }; +static const char *months[] = + { "January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December" +}; +static const char *abbr_months[] = + { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep" "Oct", + "Nov", "Dec" +}; + +/* strptime (SUSV2) */ +// FIXME: Implement the rest. (built to handle "%a %b %e %T %Y" +extern "C" char * +strptime (const char *buf, const char *format, struct tm *tm) +{ + // FIXME: Locale lookup for weeknames + // FIXME: check for tm validity + if (!tm) + return NULL; + const char *curr = format, *b = buf; + char f; + while ((f = *curr++)) + { + if (isspace (f)) + continue; + /* prime the scann buffer */ + while (isspace (*b++)); + --b; + if (f != '%') + return NULL; + + switch (f) + { + case 'a': + case 'A': + { + bool found = false; + /* Day of the week in locale format, short or full */ + for (int i = 0; !found && i < 7; ++i) + { + if (!strncmp (b, days[i], strlen (days[i]))) + { + tm->tm_wday = i; + b += strlen (days[i]); + found = true; + } + } + for (int i = 0; !found && i < 7; ++i) + { + if (!strncmp (b, abbr_days[i], strlen (abbr_days[i]))) + { + tm->tm_wday = i; + b += strlen (abbr_days[i]); + found = true; + } + } + + if (!found) + return (char *) b; + + } + break; + case 'b': + case 'B': + { + /* month in locale name, short or full */ + bool found = false; + for (int i = 0; !found && i < 7; ++i) + { + if (!strncmp (b, months[i], strlen (months[i]))) + { + tm->tm_wday = i; + b += strlen (months[i]); + found = true; + } + } + for (int i = 0; !found && i < 7; ++i) + { + if (!strncmp (b, abbr_months[i], strlen (abbr_months[i]))) + { + tm->tm_wday = i; + b += strlen (abbr_months[i]); + found = true; + } + } + if (!found) + return (char *) b; + } + break; + case 'd': + case 'e': + { + /* day of the month (1, 31) allowing leading 0's. */ + unsigned int temp; + if (sscanf (b, "%u", &temp) != 1) + return (char *) b; + if (0 < temp && temp < 32) + { + tm->tm_mday = temp; + if (temp < 10) + b += 1; + else + b += 2; + } + else + return (char *) b; + } + break; + case 'T': + { + /* Time: %H:%M:%S */ + char tempstr[3]; + int n = 0; + while (isdigit (*b)) + { + tempstr[n++] = *b++; + } + if (*b != ':') + return (char *) (b - n); + if (sscanf (tempstr, "%u", &tm->tm_hour) != 1) + return (char *) (b - n); + if (0 < tm->tm_hour || tm->tm_hour > 23) + return (char *) (b - n); + ++b; + n = 0; + while (isdigit (*b)) + { + tempstr[n++] = *b++; + } + if (*b != ':') + return (char *) (b - n); + if (sscanf (tempstr, "%u", &tm->tm_min) != 1) + return (char *) (b - n); + if (0 < tm->tm_min || tm->tm_min > 59) + return (char *) (b - n); + ++b; + n = 0; + while (isdigit (*b)) + { + tempstr[n++] = *b++; + } + if (*b != ':') + return (char *) (b - n); + if (sscanf (tempstr, "%u", &tm->tm_sec) != 1) + return (char *) (b - n); + if (0 < tm->tm_sec || tm->tm_sec > 61) + return (char *) (b - n); + break; + } + case 'Y': + { + /* four digit year */ + char tempstr[5]; + tempstr[4] = 0; + for (int i = 0; i < 4; ++i) + { + if (!isdigit (*b)) + return (char *) b; + tempstr[i] = *b++; + } + if (sscanf (tempstr, "%u", &tm->tm_year) != 1) + return (char *) b; + tm->tm_year -= 1900; + if (tm->tm_year < 0) + return (char *) b; + b += 4; + } + break; + default: + /* so people notice! */ + return NULL; + } + } + return (char *) b; }