00001 #include "utils.h"
00002 #include "logging.h"
00003 #include <assert.h>
00004 #include <errno.h>
00005 #include <ctype.h>
00006 #include <memory.h>
00007 #include <string.h>
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010
00011
00012 #ifdef CONFIG_WIN32
00013 # include <sys/types.h>
00014 # include <sys/timeb.h>
00015 #elif defined(CONFIG_OS2)
00016 # include <string.h>
00017 # include <sys/time.h>
00018 #else
00019 # include <unistd.h>
00020 # include <fcntl.h>
00021 # include <sys/time.h>
00022 #endif
00023
00024 LOG_DEFAULT_CATEGORY(utils);
00025
00026 double
00027 get_time (void)
00028 {
00029 #ifdef CONFIG_WIN32
00030
00031 struct _timeb tb;
00032
00033 _ftime(&tb);
00034 return tb.time + tb.millitm / 1e3;
00035 #else
00036 struct timeval tv;
00037
00038 gettimeofday(&tv, NULL);
00039 return tv.tv_sec + tv.tv_usec / 1e6;
00040 #endif
00041 }
00042
00043 int
00044 xstrcasecmp (const char *a, const char *b)
00045 {
00046 while (*a) {
00047 if (tolower (*a & 0xff) != tolower (*b & 0xff))
00048 break;
00049 a++;
00050 b++;
00051 }
00052 return (tolower(*a) - tolower(*b));
00053 }
00054
00055 int
00056 xstrncasecmp (const char *a, const char *b, size_t n)
00057 {
00058 while (n && *a) {
00059 if (tolower (*a & 0xff) != tolower (*b & 0xff))
00060 break;
00061 a++;
00062 b++;
00063 n--;
00064 }
00065 return (tolower(*a) - tolower(*b));
00066 }
00067
00068 void *
00069 xmalloc (size_t n)
00070 {
00071 void *p = malloc(n);
00072
00073 if (!p)
00074 {
00075 CRITICAL2("allocation failure: %s", strerror(errno));
00076 exit(EXIT_FAILURE);
00077 }
00078
00079 return (p);
00080 }
00081
00082 void *
00083 xcalloc (size_t a, size_t b)
00084 {
00085 void *p = calloc(a, b);
00086
00087 if (!p)
00088 {
00089 CRITICAL2("callocation failure: %s", strerror(errno));
00090 exit(EXIT_FAILURE);
00091 }
00092
00093 return (p);
00094 }
00095
00096
00097
00098 char *
00099 xstrdup (const char *s)
00100 {
00101 void *p;
00102
00103 p = xmalloc (strlen (s) + 1);
00104 strcpy (p, s);
00105 return (p);
00106 }
00107
00108 char *
00109 xstrcat2 (const char* s1, const char* s2)
00110 {
00111 char *s = NULL;
00112 assert(s1 && s2);
00113 s = xmalloc(strlen(s1) + strlen(s2) + 1);
00114 strcpy(s, s1);
00115 strcat(s, s2);
00116 return s;
00117 }
00118
00119 void *
00120 xrealloc(void *ptr, size_t size)
00121 {
00122 void *p = realloc(ptr, size);
00123
00124 if (!p)
00125 {
00126 CRITICAL2("reallocation failure: %s", strerror(errno));
00127 exit(EXIT_FAILURE);
00128 }
00129
00130 return (p);
00131 }
00132
00133 ssize_t
00134 fread_dyn(char **destp, size_t *n, FILE *stream)
00135 {
00136 const unsigned bsize = 8192;
00137 size_t total = 0, cnt = 0;
00138
00139 assert(destp);
00140 assert(n);
00141 assert(stream);
00142
00143 if (!*destp)
00144 *destp = xmalloc(*n = bsize);
00145
00146 while (1) {
00147 cnt = fread(*destp+total, 1, *n-total, stream);
00148 if (cnt != *n-total)
00149 {
00150 if (feof(stream))
00151 return total+cnt;
00152 else if (ferror(stream))
00153 {
00154 CWARNING3(filesys, "read error: %s", strerror(errno));
00155 return -1;
00156 }
00157 }
00158 total += cnt;
00159
00160 if (*n <= total)
00161 *destp = xrealloc(*destp, *n *= 2);
00162 }
00163 }
00164
00165