From f8b1e7516fa0838c48edf8f93ec11a5ec1673a43 Mon Sep 17 00:00:00 2001 From: Olaf Barthel Date: Sun, 30 Jan 2005 09:37:59 +0000 Subject: [PATCH] - Folded duplicate code stdio_init_exit.c into a common function. - Simplified the code in time_asctime_r.c which builds the time string. It gracefully handles buffer sizes which are too short by returning an empty string. - Moved the tm->tm_wday initialization out of the hook function in time_strftime.c since it was to be called only once anyway. - Lost a few compiler warnings in unistd_time_delay.c and time_gettimeofday.c. - Folded duplicate code in time_mktime.c; also, errno is no longer modified unless the library is built with the CHECK_FOR_NULL_POINTERS option. git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14818 87f5fb63-7c3d-0410-a384-fd976d0f7a62 --- library/changes | 17 ++++ library/macros.h | 9 +- library/stat_convertfileinfo.c | 6 +- library/stat_stat.c | 6 +- library/stdio_init_exit.c | 63 ++++++------ library/time_asctime_r.c | 172 +++++++++++++++++---------------- library/time_gettimeofday.c | 7 +- library/time_headers.h | 9 +- library/time_mktime.c | 43 +++------ library/time_strftime.c | 42 ++++---- library/unistd_time_delay.c | 5 +- 11 files changed, 197 insertions(+), 182 deletions(-) diff --git a/library/changes b/library/changes index cf1927b..388c683 100644 --- a/library/changes +++ b/library/changes @@ -1,3 +1,20 @@ +- Folded duplicate code stdio_init_exit.c into a common function. + +- Simplified the code in time_asctime_r.c which builds the time + string. It gracefully handles buffer sizes which are too short + by returning an empty string. + +- Moved the tm->tm_wday initialization out of the hook function + in time_strftime.c since it was to be called only once anyway. + +- Lost a few compiler warnings in unistd_time_delay.c and + time_gettimeofday.c. + +- Folded duplicate code in time_mktime.c; also, errno is no longer + modified unless the library is built with the CHECK_FOR_NULL_POINTERS + option. + + c.lib 1.187 (29.1.2005) - The default console output window opened when a program is launched diff --git a/library/macros.h b/library/macros.h index 85fd296..08f261b 100644 --- a/library/macros.h +++ b/library/macros.h @@ -1,5 +1,5 @@ /* - * $Id: macros.h,v 1.7 2005-01-29 18:05:14 obarthel Exp $ + * $Id: macros.h,v 1.8 2005-01-30 09:37:59 obarthel Exp $ * * :ts=4 * @@ -60,13 +60,6 @@ /****************************************************************************/ -/* This is the difference (in seconds) between the Unix epoch (which began - on January 1st, 1970) and the AmigaOS epoch (which began eight years - later on January 1st 1978). */ -#define UNIX_TIME_OFFSET 252460800 - -/****************************************************************************/ - #define NUM_ENTRIES(t) (sizeof(t) / sizeof(t[0])) /****************************************************************************/ diff --git a/library/stat_convertfileinfo.c b/library/stat_convertfileinfo.c index 7d9b233..9cc3911 100644 --- a/library/stat_convertfileinfo.c +++ b/library/stat_convertfileinfo.c @@ -1,5 +1,5 @@ /* - * $Id: stat_convertfileinfo.c,v 1.5 2005-01-24 10:25:46 obarthel Exp $ + * $Id: stat_convertfileinfo.c,v 1.6 2005-01-30 09:37:59 obarthel Exp $ * * :ts=4 * @@ -129,8 +129,8 @@ __convert_file_info_to_stat( st->st_dev = (dev_t)file_system; st->st_mode = mode; st->st_mtime = mtime; - st->st_atime = st->st_mtime; - st->st_ctime = st->st_mtime; + st->st_atime = mtime; + st->st_ctime = mtime; st->st_uid = fib->fib_OwnerUID; st->st_gid = fib->fib_OwnerGID; st->st_blksize = 512; diff --git a/library/stat_stat.c b/library/stat_stat.c index 50b228b..4059b84 100644 --- a/library/stat_stat.c +++ b/library/stat_stat.c @@ -1,5 +1,5 @@ /* - * $Id: stat_stat.c,v 1.4 2005-01-24 10:25:46 obarthel Exp $ + * $Id: stat_stat.c,v 1.5 2005-01-30 09:37:59 obarthel Exp $ * * :ts=4 * @@ -116,8 +116,8 @@ stat(const char * path_name, struct stat * st) st->st_mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; st->st_mtime = mtime; - st->st_atime = st->st_mtime; - st->st_ctime = st->st_mtime; + st->st_atime = mtime; + st->st_ctime = mtime; st->st_nlink = 2; st->st_blksize = 512; diff --git a/library/stdio_init_exit.c b/library/stdio_init_exit.c index d9ed98b..f3b1c22 100644 --- a/library/stdio_init_exit.c +++ b/library/stdio_init_exit.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_init_exit.c,v 1.14 2005-01-15 08:17:10 obarthel Exp $ + * $Id: stdio_init_exit.c,v 1.15 2005-01-30 09:37:59 obarthel Exp $ * * :ts=4 * @@ -106,10 +106,33 @@ CLIB_DESTRUCTOR(__stdio_exit) /****************************************************************************/ +static LONG +get_console_mode(BPTR console_fh) +{ + struct FileHandle * fh; + LONG result = 0; + + assert( console_fh != ZERO ); + + fh = BADDR(console_fh); + if(fh->fh_Type != NULL) /* Check if this is really bound to "NIL:". */ + { + D_S(struct InfoData,id); + + if(DoPkt(fh->fh_Type,ACTION_DISK_INFO,MKBADDR(id),0,0,0,0)) + result = id->id_DiskType; + } + + return(result); +} + +/****************************************************************************/ + int __stdio_init(void) { - const int num_standard_files = 3; + const int num_standard_files = (STDERR_FILENO-STDIN_FILENO+1); + BPTR default_file; ULONG fd_flags,iob_flags; int result = ERROR; @@ -119,8 +142,6 @@ __stdio_init(void) ENTER(); - assert( num_standard_files == (STDERR_FILENO-STDIN_FILENO+1) ); - __iob = malloc(sizeof(*__iob) * num_standard_files); if(__iob == NULL) goto out; @@ -203,21 +224,10 @@ __stdio_init(void) from closing, or end up making it visible. */ if(__WBenchMsg == NULL) { - struct FileHandle * fh; - - fh = BADDR(default_file); - if(fh->fh_Type != NULL) + if(get_console_mode(default_file) == ID_RAWCON) { - D_S(struct InfoData,id); - - if(DoPkt(fh->fh_Type,ACTION_DISK_INFO,MKBADDR(id),0,0,0,0)) - { - if(id->id_DiskType == ID_RAWCON) - { - SET_FLAG(fd_flags,FDF_NON_BLOCKING); - SET_FLAG(fd_flags,FDF_DEFAULT_NON_BLOCKING); - } - } + SET_FLAG(fd_flags,FDF_NON_BLOCKING); + SET_FLAG(fd_flags,FDF_DEFAULT_NON_BLOCKING); } } } @@ -295,21 +305,10 @@ __stdio_init(void) can avoid it. */ if(__WBenchMsg == NULL) { - struct FileHandle * fh; - - fh = BADDR(__fd[STDERR_FILENO]->fd_DefaultFile); - if(fh->fh_Type != NULL) + if(get_console_mode(__fd[STDERR_FILENO]->fd_DefaultFile) == ID_RAWCON) { - D_S(struct InfoData,id); - - if(DoPkt(fh->fh_Type,ACTION_DISK_INFO,MKBADDR(id),0,0,0,0)) - { - if(id->id_DiskType == ID_RAWCON) - { - SET_FLAG(__fd[STDERR_FILENO]->fd_Flags,FDF_NON_BLOCKING); - SET_FLAG(__fd[STDERR_FILENO]->fd_Flags,FDF_DEFAULT_NON_BLOCKING); - } - } + SET_FLAG(__fd[STDERR_FILENO]->fd_Flags,FDF_NON_BLOCKING); + SET_FLAG(__fd[STDERR_FILENO]->fd_Flags,FDF_DEFAULT_NON_BLOCKING); } } } diff --git a/library/time_asctime_r.c b/library/time_asctime_r.c index a3a90a0..1456f78 100644 --- a/library/time_asctime_r.c +++ b/library/time_asctime_r.c @@ -1,5 +1,5 @@ /* - * $Id: time_asctime_r.c,v 1.4 2005-01-26 18:41:39 obarthel Exp $ + * $Id: time_asctime_r.c,v 1.5 2005-01-30 09:37:59 obarthel Exp $ * * :ts=4 * @@ -46,7 +46,7 @@ static void add_to_string(char * to,size_t to_size,const char * string,size_t * offset_ptr) { - size_t offset, len; + size_t offset,len; assert( to != NULL && to_size > 0 && string != NULL && offset_ptr != NULL ); @@ -55,18 +55,15 @@ add_to_string(char * to,size_t to_size,const char * string,size_t * offset_ptr) assert( offset < to_size ); len = strlen(string); - if(offset + len > (to_size-1)) - len = (to_size-1) - offset; + if(offset + len > to_size) + len = to_size - offset; if(len > 0) { - memmove(&to[offset],string,(size_t)len); + memmove(&to[offset],string,len); + offset += len; - assert( offset < to_size ); - - to[offset] = '\0'; - (*offset_ptr) = offset; } } @@ -76,11 +73,7 @@ add_to_string(char * to,size_t to_size,const char * string,size_t * offset_ptr) char * __asctime_r(const struct tm *tm,char * buffer,size_t buffer_size) { - struct tm copy_tm; - char number[16]; char * result = NULL; - const char * b; - size_t offset = 0; ENTER(); @@ -98,81 +91,94 @@ __asctime_r(const struct tm *tm,char * buffer,size_t buffer_size) } #endif /* CHECK_FOR_NULL_POINTERS */ - /* Fill in the week day if it's not in proper range. */ - if(tm->tm_wday < 0 || tm->tm_wday > 6) + if(buffer_size > 0) { - /* We use a peculiar algorithm rather than falling back onto - mktime() here in order to avoid trouble with skewed results - owing to time zone influence. */ - copy_tm = (*tm); - copy_tm.tm_wday = __calculate_weekday(tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday); + struct tm copy_tm; + char number[16]; + const char * b; + size_t offset = 0; - tm = ©_tm; - } + buffer_size--; + + /* Fill in the week day if it's not in proper range. */ + if(tm->tm_wday < 0 || tm->tm_wday > 6) + { + /* We use a peculiar algorithm rather than falling back onto + mktime() here in order to avoid trouble with skewed results + owing to time zone influence. */ + copy_tm = (*tm); + copy_tm.tm_wday = __calculate_weekday(tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday); + + tm = ©_tm; + } + + assert( 0 <= tm->tm_wday && tm->tm_wday <= 6 ); - if(0 <= tm->tm_wday && tm->tm_wday <= 6) b = __abbreviated_week_day_names[tm->tm_wday]; + + add_to_string(buffer,buffer_size,b, &offset); + add_to_string(buffer,buffer_size," ", &offset); + + if(0 <= tm->tm_mon && tm->tm_mon <= 11) + b = __abbreviated_month_names[tm->tm_mon]; + else + b = "---"; + + add_to_string(buffer,buffer_size,b, &offset); + add_to_string(buffer,buffer_size," ", &offset); + + if(1 <= tm->tm_mday && tm->tm_mday <= 31) + b = __number_to_string((unsigned int)tm->tm_mday,number,sizeof(number),2); + else + b = "--"; + + add_to_string(buffer,buffer_size,b, &offset); + add_to_string(buffer,buffer_size," ", &offset); + + if(0 <= tm->tm_hour && tm->tm_hour <= 23) + b = __number_to_string((unsigned int)tm->tm_hour,number,sizeof(number),2); + else + b = "--"; + + add_to_string(buffer,buffer_size,b, &offset); + add_to_string(buffer,buffer_size,":", &offset); + + if(0 <= tm->tm_min && tm->tm_min <= 59) + b = __number_to_string((unsigned int)tm->tm_min,number,sizeof(number),2); + else + b = "--"; + + add_to_string(buffer,buffer_size,b, &offset); + add_to_string(buffer,buffer_size,":", &offset); + + if(0 <= tm->tm_sec && tm->tm_sec <= 59) + b = __number_to_string((unsigned int)tm->tm_sec,number,sizeof(number),2); + else + b = "--"; + + add_to_string(buffer,buffer_size,b, &offset); + add_to_string(buffer,buffer_size," ", &offset); + + if(0 <= tm->tm_year) + b = __number_to_string((unsigned int)1900 + tm->tm_year,number,sizeof(number),0); + else + b = "----"; + + add_to_string(buffer,buffer_size,b,&offset); + + SHOWSTRING(buffer); + + add_to_string(buffer,buffer_size,"\n",&offset); + + assert( offset <= buffer_size ); + buffer[offset] = '\0'; + + result = buffer; + } else - b = "---"; - - add_to_string(buffer,buffer_size,b, &offset); - add_to_string(buffer,buffer_size," ", &offset); - - if(0 <= tm->tm_mon && tm->tm_mon <= 11) - b = __abbreviated_month_names[tm->tm_mon]; - else - b = "---"; - - add_to_string(buffer,buffer_size,b, &offset); - add_to_string(buffer,buffer_size," ", &offset); - - if(1 <= tm->tm_mday && tm->tm_mday <= 31) - b = __number_to_string((unsigned int)tm->tm_mday,number,sizeof(number),2); - else - b = "--"; - - add_to_string(buffer,buffer_size,b, &offset); - add_to_string(buffer,buffer_size," ", &offset); - - if(0 <= tm->tm_hour && tm->tm_hour <= 23) - b = __number_to_string((unsigned int)tm->tm_hour,number,sizeof(number),2); - else - b = "--"; - - add_to_string(buffer,buffer_size,b, &offset); - add_to_string(buffer,buffer_size,":", &offset); - - if(0 <= tm->tm_min && tm->tm_min <= 59) - b = __number_to_string((unsigned int)tm->tm_min,number,sizeof(number),2); - else - b = "--"; - - add_to_string(buffer,buffer_size,b, &offset); - add_to_string(buffer,buffer_size,":", &offset); - - if(0 <= tm->tm_sec && tm->tm_sec <= 59) - b = __number_to_string((unsigned int)tm->tm_sec,number,sizeof(number),2); - else - b = "--"; - - add_to_string(buffer,buffer_size,b, &offset); - add_to_string(buffer,buffer_size," ", &offset); - - if(0 <= tm->tm_year) - b = __number_to_string((unsigned int)1900 + tm->tm_year,number,sizeof(number),0); - else - b = "----"; - - add_to_string(buffer,buffer_size,b,&offset); - - SHOWSTRING(buffer); - - add_to_string(buffer,buffer_size,"\n",&offset); - - assert( offset < buffer_size ); - assert( strlen(buffer) < buffer_size ); - - result = buffer; + { + result = ""; + } out: diff --git a/library/time_gettimeofday.c b/library/time_gettimeofday.c index de86a84..06ef410 100644 --- a/library/time_gettimeofday.c +++ b/library/time_gettimeofday.c @@ -1,5 +1,5 @@ /* - * $Id: time_gettimeofday.c,v 1.5 2005-01-24 10:25:46 obarthel Exp $ + * $Id: time_gettimeofday.c,v 1.6 2005-01-30 09:37:59 obarthel Exp $ * * :ts=4 * @@ -60,9 +60,10 @@ int gettimeofday(struct timeval *tp, struct timezone *tzp) { - struct Library * TimerBase = __TimerBase; #if defined(__amigaos4__) struct TimerIFace * ITimer = __ITimer; + #else + struct Library * TimerBase = __TimerBase; #endif /* __amigaos4__ */ ULONG seconds,microseconds; @@ -71,7 +72,9 @@ gettimeofday(struct timeval *tp, struct timezone *tzp) ENTER(); /* Obtain the current system time. */ + PROFILE_OFF(); GetSysTime(&tv); + PROFILE_ON(); /* Convert the number of seconds so that they match the Unix epoch, which starts (January 1st, 1970) eight years before the AmigaOS epoch. */ diff --git a/library/time_headers.h b/library/time_headers.h index c4566a8..9231d5a 100644 --- a/library/time_headers.h +++ b/library/time_headers.h @@ -1,5 +1,5 @@ /* - * $Id: time_headers.h,v 1.8 2005-01-29 18:05:14 obarthel Exp $ + * $Id: time_headers.h,v 1.9 2005-01-30 09:37:59 obarthel Exp $ * * :ts=4 * @@ -84,6 +84,13 @@ /****************************************************************************/ +/* This is the difference (in seconds) between the Unix epoch (which began + on January 1st, 1970) and the AmigaOS epoch (which began eight years + later on January 1st 1978). */ +#define UNIX_TIME_OFFSET 252460800 + +/****************************************************************************/ + extern const char * const NOCOMMON __abbreviated_week_day_names[7]; extern const char * const NOCOMMON __week_day_names[7]; extern const char * const NOCOMMON __abbreviated_month_names[12]; diff --git a/library/time_mktime.c b/library/time_mktime.c index 8cbb62a..5176a39 100644 --- a/library/time_mktime.c +++ b/library/time_mktime.c @@ -1,5 +1,5 @@ /* - * $Id: time_mktime.c,v 1.4 2005-01-25 11:21:00 obarthel Exp $ + * $Id: time_mktime.c,v 1.5 2005-01-30 09:37:59 obarthel Exp $ * * :ts=4 * @@ -54,7 +54,7 @@ mktime(struct tm *tm) struct ClockData clock_data; ULONG seconds, delta; time_t result = (time_t)-1; - int error = EINVAL; + int max_month_days; ENTER(); @@ -67,7 +67,7 @@ mktime(struct tm *tm) { SHOWMSG("invalid tm parameter"); - error = EFAULT; + errno = EFAULT; goto out; } } @@ -100,16 +100,15 @@ mktime(struct tm *tm) /* Is this the month of February? */ if(tm->tm_mon == 1) { - char max_month_days; int year; /* We need to have the full year number for the leap year calculation below. */ year = tm->tm_year + 1900; - /* Now for the famous leap year calculation rules... We - need to find out if the number of days in the month - of February are appropriate for the data given. */ + /* Now for the famous leap year calculation rules... In + the given year, how many days are there in the month + of February? */ if((year % 4) != 0) max_month_days = 28; else if ((year % 400) == 0) @@ -118,18 +117,10 @@ mktime(struct tm *tm) max_month_days = 28; else max_month_days = 29; - - /* The day of the month must be valid. */ - if(tm->tm_mday > max_month_days) - { - SHOWVALUE(tm->tm_mday); - SHOWMSG("invalid day of month"); - goto out; - } } else { - static const char max_month_days[12] = + static const char days_per_month[12] = { 31, 0,31, 30,31,30, @@ -137,13 +128,15 @@ mktime(struct tm *tm) 31,30,31 }; - /* The day of the month must be valid. */ - if(tm->tm_mday > max_month_days[tm->tm_mon]) - { - SHOWVALUE(tm->tm_mday); - SHOWMSG("invalid day of month"); - goto out; - } + max_month_days = days_per_month[tm->tm_mon]; + } + + /* The day of the month must be valid. */ + if(tm->tm_mday > max_month_days) + { + SHOWVALUE(tm->tm_mday); + SHOWMSG("invalid day of month"); + goto out; } /* The hour must be valid. */ @@ -198,12 +191,8 @@ mktime(struct tm *tm) AmigaOS epochs, which differ by 8 years. */ result = seconds + UNIX_TIME_OFFSET; - error = 0; - out: - errno = error; - RETURN(result); return(result); } diff --git a/library/time_strftime.c b/library/time_strftime.c index dfe3f97..dacd07e 100644 --- a/library/time_strftime.c +++ b/library/time_strftime.c @@ -1,5 +1,5 @@ /* - * $Id: time_strftime.c,v 1.5 2005-01-29 18:05:14 obarthel Exp $ + * $Id: time_strftime.c,v 1.6 2005-01-30 09:37:59 obarthel Exp $ * * :ts=4 * @@ -105,25 +105,12 @@ store_string_via_hook(const char * string,int len,struct Hook * hook) static void format_date(const char *format,const struct tm *tm,struct Hook * hook) { - struct tm copy_tm; char buffer[40]; const char * str; char c; assert( format != NULL && tm != NULL && hook != NULL); - /* Fill in the week day if it's not in proper range. */ - if(tm->tm_wday < 0 || tm->tm_wday > 6) - { - /* We use a peculiar algorithm rather than falling back onto - mktime() here in order to avoid trouble with skewed results - owing to time zone influence. */ - copy_tm = (*tm); - copy_tm.tm_wday = __calculate_weekday(tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday); - - tm = ©_tm; - } - while((c = (*format++)) != '\0') { /* This is the simple case. */ @@ -224,7 +211,6 @@ format_date(const char *format,const struct tm *tm,struct Hook * hook) assert( 0 <= tm->tm_hour && tm->tm_hour <= 23 ); __number_to_string((unsigned int)((tm->tm_hour > 12) ? (tm->tm_hour - 12) : tm->tm_hour),buffer,sizeof(buffer),2); - store_string_via_hook(buffer,2,hook); break; @@ -374,8 +360,6 @@ size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *tm) { DECLARE_LOCALEBASE(); - struct format_hook_data data; - struct Hook hook; size_t result = 0; ENTER(); @@ -400,10 +384,12 @@ strftime(char *s, size_t maxsize, const char *format, const struct tm *tm) } #endif /* CHECK_FOR_NULL_POINTERS */ - data.len = 0; - if(maxsize > 0) { + struct format_hook_data data; + struct Hook hook; + + data.len = 0; data.buffer = s; data.max_size = maxsize-1; @@ -452,15 +438,29 @@ strftime(char *s, size_t maxsize, const char *format, const struct tm *tm) } else { + struct tm copy_tm; + + /* Fill in the week day if it's not in proper range. */ + if(tm->tm_wday < 0 || tm->tm_wday > 6) + { + /* We use a peculiar algorithm rather than falling back onto + mktime() here in order to avoid trouble with skewed results + owing to time zone influence. */ + copy_tm = (*tm); + copy_tm.tm_wday = __calculate_weekday(tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday); + + tm = ©_tm; + } + format_date(format,tm,&hook); } (*data.buffer) = '\0'; SHOWSTRING(s); - } - result = data.len; + result = data.len; + } out: diff --git a/library/unistd_time_delay.c b/library/unistd_time_delay.c index 8eb09da..d3b0a02 100644 --- a/library/unistd_time_delay.c +++ b/library/unistd_time_delay.c @@ -1,5 +1,5 @@ /* - * $Id: unistd_time_delay.c,v 1.3 2005-01-02 09:07:19 obarthel Exp $ + * $Id: unistd_time_delay.c,v 1.4 2005-01-30 09:37:59 obarthel Exp $ * * :ts=4 * @@ -59,9 +59,10 @@ __time_delay(unsigned long seconds,unsigned long microseconds) if((seconds > 0 || microseconds > 0) && NOT __timer_busy) { - struct Library * TimerBase = __TimerBase; #if defined(__amigaos4__) struct TimerIFace * ITimer = __ITimer; + #else + struct Library * TimerBase = __TimerBase; #endif /* __amigaos4__ */ ULONG signals_to_wait_for;