X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=misc.c;h=60c826ab53a5b9135b097c621441522e5bae8df3;hb=HEAD;hp=d181ff250fadb79c4e12ef6feab0071f699a908e;hpb=261dc4945116580cb8d26b33d5bf6caa4e04bbac;p=platform%2Fupstream%2Fmtools.git diff --git a/misc.c b/misc.c index d181ff2..60c826a 100644 --- a/misc.c +++ b/misc.c @@ -18,9 +18,7 @@ */ #include "sysincludes.h" -#include "msdos.h" #include "stream.h" -#include "vfat.h" #include "mtools.h" @@ -36,29 +34,29 @@ char *get_homedir(void) uid_t uid; char *homedir; char *username; - - homedir = getenv ("HOME"); - /* - * first we call getlogin. - * There might be several accounts sharing one uid + + homedir = getenv ("HOME"); + /* + * first we call getlogin. + * There might be several accounts sharing one uid */ if ( homedir ) return homedir; - + pw = 0; - + username = getenv("LOGNAME"); if ( !username ) username = getlogin(); if ( username ) pw = getpwnam( username); - + if ( pw == 0 ){ /* if we can't getlogin, look up the pwent by uid */ uid = geteuid(); pw = getpwuid(uid); } - + /* we might still get no entry */ if ( pw ) return pw->pw_dir; @@ -100,7 +98,7 @@ FILE *open_mcwd(const char *mode) struct MT_STAT sbuf; char file[MAXPATHLEN+1]; time_t now; - + get_mcwd_file_name(file); if (*mode == 'r'){ if (MT_STAT(file, &sbuf) < 0) @@ -117,10 +115,10 @@ FILE *open_mcwd(const char *mode) return NULL; } } - + return fopen(file, mode); } - + void *safe_malloc(size_t size) @@ -141,10 +139,10 @@ void print_sector(const char *message, unsigned char *data, int size) int row; printf("%s:\n", message); - + for(row = 0; row * 16 < size; row++){ printf("%03x ", row * 16); - for(col = 0; col < 16; col++) + for(col = 0; col < 16; col++) printf("%02x ", data [row*16+col]); for(col = 0; col < 16; col++) { if(isprint(data [row*16+col])) @@ -156,6 +154,11 @@ void print_sector(const char *message, unsigned char *data, int size) } } +#if (SIZEOF_TIME_T > SIZEOF_LONG) && defined (HAVE_STRTOLL) +# define STRTOTIME strtoll +#else +# define STRTOTIME strtol +#endif time_t getTimeNow(time_t *now) { @@ -166,31 +169,29 @@ time_t getTimeNow(time_t *now) const char *source_date_epoch = getenv("SOURCE_DATE_EPOCH"); if (source_date_epoch) { char *endptr; - unsigned long long epoch = - strtoll(source_date_epoch, &endptr, 10); + time_t epoch = + STRTOTIME(source_date_epoch, &endptr, 10); + errno = 0; if (endptr == source_date_epoch) - fprintf(stderr, "SOURCE_DATE_EPOCH invalid\n"); - else if ((errno == ERANGE && - (epoch == ULLONG_MAX || epoch == 0)) - || (errno != 0 && epoch == 0)) fprintf(stderr, - "SOURCE_DATE_EPOCH: strtoll: %s: %llu\n", - strerror(errno), epoch); - else if (*endptr != '\0') + "SOURCE_DATE_EPOCH \"%s\" invalid\n", + source_date_epoch); + else if (errno != 0) fprintf(stderr, - "SOURCE_DATE_EPOCH has trailing garbage\n"); - else if (epoch > ULONG_MAX) + "SOURCE_DATE_EPOCH: strtoll: %s: %s\n", + strerror(errno), source_date_epoch); + else if (*endptr != '\0') fprintf(stderr, - "SOURCE_DATE_EPOCH must be <= %lu but saw: %llu\n", - ULONG_MAX, epoch); + "SOURCE_DATE_EPOCH has trailing garbage \"%s\"\n", + endptr); else { sharedNow = epoch; haveTime = 1; } } } - + if(!haveTime) { time(&sharedNow); haveTime = 1; @@ -203,31 +204,65 @@ time_t getTimeNow(time_t *now) /* Convert a string to an offset. The string should be a number, optionally followed by S (sectors), K (K-Bytes), M (Megabytes), G (Gigabytes) */ -off_t str_to_offset(char *str) { - char s, *endp = NULL; +off_t str_to_offset_with_end(const char *str, char **endp) { + char s; off_t ofs; - ofs = strtol(str, &endp, 0); + *endp = NULL; + ofs = strtol(str, endp, 0); + s = **endp; + /* trailing char, see if it is a size specifier */ + if (s == 's' || s == 'S') /* sector */ + ofs <<= 9; + else if (s == 'k' || s == 'K') /* kb */ + ofs <<= 10; + else if (s == 'm' || s == 'M') /* Mb */ + ofs <<= 20; + else if (s == 'g' || s == 'G') /* Gb */ + ofs <<= 30; + else + return ofs; /* invalid character */ + (*endp)++; + return ofs; +} + +/* Convert a string to a size. The string should be a number, + optionally followed by S (sectors), K (K-Bytes), M (Megabytes), G + (Gigabytes) */ +mt_off_t str_to_off_with_end(const char *str, char **endp) { + char s; + mt_off_t siz; + + *endp = NULL; + siz = strtol(str, endp, 0); + s = **endp; + /* trailing char, see if it is a size specifier */ + if (s == 's' || s == 'S') /* sector */ + siz <<= 9; + else if (s == 'k' || s == 'K') /* kb */ + siz <<= 10; + else if (s == 'm' || s == 'M') /* Mb */ + siz <<= 20; + else if (s == 'g' || s == 'G') /* Gb */ + siz <<= 30; + else + return siz; /* invalid character */ + (*endp)++; + return siz; +} + +off_t str_to_offset(char *str) { + char *end; + off_t ofs = str_to_offset_with_end(str, &end); if (ofs <= 0) return 0; /* invalid or missing offset */ - s = *endp++; - if (s) { /* trailing char, see if it is a size specifier */ - if (s == 's' || s == 'S') /* sector */ - ofs <<= 9; - else if (s == 'k' || s == 'K') /* kb */ - ofs <<= 10; - else if (s == 'm' || s == 'M') /* Mb */ - ofs <<= 20; - else if (s == 'g' || s == 'G') /* Gb */ - ofs <<= 30; - else - return 0; /* invalid character */ - if (*endp) - return 0; /* extra char, invalid */ - } + if (*end) + return 0; /* extra char, invalid */ return ofs; } + + #if 0 #undef free @@ -239,7 +274,7 @@ void myfree(void *ptr) { int *size = ((int *) ptr)-1; total -= *size; - fprintf(stderr, "freeing %d bytes at %p total alloced=%d\n", + fprintf(stderr, "freeing %d bytes at %p total allocated=%d\n", *size, ptr, total); free(size); }