00001
00021 #include <stdio.h>
00022 #include "extra.h"
00023
00027
00028 #define EXTRA_RLIMIT_DATA_DEFAULT 67108864 // assume 64MB by default
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 char * globalUtilOptarg;
00042 int globalUtilOptind = 0;
00043
00044 static char *pScanStr;
00045
00049
00061 long Extra_CpuTime()
00062 {
00063 return clock();
00064 }
00065
00077 int Extra_GetSoftDataLimit()
00078 {
00079 return EXTRA_RLIMIT_DATA_DEFAULT;
00080 }
00081
00093 void Extra_UtilGetoptReset()
00094 {
00095 globalUtilOptarg = 0;
00096 globalUtilOptind = 0;
00097 pScanStr = 0;
00098 }
00099
00111 int Extra_UtilGetopt( int argc, char *argv[], char *optstring )
00112 {
00113 register int c;
00114 register char *place;
00115
00116 globalUtilOptarg = NULL;
00117
00118 if (pScanStr == NULL || *pScanStr == '\0') {
00119 if (globalUtilOptind == 0) globalUtilOptind++;
00120 if (globalUtilOptind >= argc) return EOF;
00121 place = argv[globalUtilOptind];
00122 if (place[0] != '-' || place[1] == '\0') return EOF;
00123 globalUtilOptind++;
00124 if (place[1] == '-' && place[2] == '\0') return EOF;
00125 pScanStr = place+1;
00126 }
00127
00128 c = *pScanStr++;
00129 place = strchr(optstring, c);
00130 if (place == NULL || c == ':') {
00131 (void) fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
00132 return '?';
00133 }
00134 if (*++place == ':') {
00135 if (*pScanStr != '\0') {
00136 globalUtilOptarg = pScanStr;
00137 pScanStr = NULL;
00138 } else {
00139 if (globalUtilOptind >= argc) {
00140 (void) fprintf(stderr, "%s: %c requires an argument\n",
00141 argv[0], c);
00142 return '?';
00143 }
00144 globalUtilOptarg = argv[globalUtilOptind];
00145 globalUtilOptind++;
00146 }
00147 }
00148 return c;
00149 }
00150
00162 char * Extra_UtilPrintTime( long t )
00163 {
00164 static char s[40];
00165
00166 (void) sprintf(s, "%ld.%02ld sec", t/1000, (t%1000)/10);
00167 return s;
00168 }
00169
00170
00182 char * Extra_UtilStrsav( char *s )
00183 {
00184 if(s == NULL) {
00185 return s;
00186 }
00187 else {
00188 return strcpy(ALLOC(char, strlen(s)+1), s);
00189 }
00190 }
00191
00203 char * Extra_UtilTildeExpand( char *fname )
00204 {
00205 return Extra_UtilStrsav( fname );
00206 }
00207
00219 int Extra_UtilCheckFile(char *filename, char *mode)
00220 {
00221 FILE *fp;
00222 int got_file;
00223
00224 if (strcmp(mode, "x") == 0) {
00225 mode = "r";
00226 }
00227 fp = fopen(filename, mode);
00228 got_file = (fp != 0);
00229 if (fp != 0) {
00230 (void) fclose(fp);
00231 }
00232 return got_file;
00233 }
00234
00246 char * Extra_UtilFileSearch(char *file, char *path, char *mode)
00247
00248
00249
00250 {
00251 int quit;
00252 char *buffer, *filename, *save_path, *cp;
00253
00254 if (path == 0 || strcmp(path, "") == 0) {
00255 path = ".";
00256 }
00257
00258 save_path = path = Extra_UtilStrsav(path);
00259 quit = 0;
00260 do {
00261 cp = strchr(path, ':');
00262 if (cp != 0) {
00263 *cp = '\0';
00264 } else {
00265 quit = 1;
00266 }
00267
00268
00269 if (strcmp(path, ".") == 0) {
00270 buffer = Extra_UtilStrsav(file);
00271 } else {
00272 buffer = ALLOC(char, strlen(path) + strlen(file) + 4);
00273 (void) sprintf(buffer, "%s/%s", path, file);
00274 }
00275 filename = Extra_UtilTildeExpand(buffer);
00276 FREE(buffer);
00277
00278
00279 if (Extra_UtilCheckFile(filename, mode)) {
00280 FREE(save_path);
00281 return filename;
00282 }
00283 FREE(filename);
00284 path = ++cp;
00285 } while (! quit);
00286
00287 FREE(save_path);
00288 return 0;
00289 }
00290
00302
00303 void Extra_UtilMMout_Of_Memory( long size )
00304 {
00305 (void) fflush(stdout);
00306 (void) fprintf(stderr, "\nout of memory allocating %u bytes\n",
00307 (unsigned) size);
00308 assert( 0 );
00309 exit(1);
00310 }
00311
00323 void (*Extra_UtilMMoutOfMemory)() = Extra_UtilMMout_Of_Memory;
00324
00325
00329
00330