Annotation of parser3/src/main/pa_common.C, revision 1.122
1.15 paf 1: /** @file
1.16 paf 2: Parser: commonly functions.
3:
1.102 paf 4: Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.101 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.111 paf 6: */
1.16 paf 7:
1.122 ! paf 8: static const char* IDENT_COMMON_C="$Date: 2002/11/20 09:44:43 $";
1.1 paf 9:
10: #include "pa_common.h"
1.4 paf 11: #include "pa_exception.h"
1.14 paf 12: #include "pa_globals.h"
1.1 paf 13:
1.98 paf 14: #ifdef WIN32
15: # include <windows.h>
16: #endif
17:
1.93 paf 18: // some maybe-undefined constants
19:
1.82 paf 20: #ifndef _O_TEXT
21: # define _O_TEXT 0
22: #endif
23: #ifndef _O_BINARY
24: # define _O_BINARY 0
1.47 paf 25: #endif
1.80 paf 26:
1.93 paf 27: // locking constants
28:
1.99 paf 29: #ifdef HAVE_FLOCK
30:
31: static int lock_shared_blocking(int fd) { return flock(fd, LOCK_SH); }
32: static int lock_exclusive_blocking(int fd) { return flock(fd, LOCK_EX); }
33: static int lock_exclusive_nonblocking(int fd) { return flock(fd, LOCK_EX || LOCK_NB); }
34: static int unlock(int fd) { return flock(fd, LOCK_UN); }
35:
1.98 paf 36: #else
1.99 paf 37: #ifdef HAVE__LOCKING
1.98 paf 38:
1.99 paf 39: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); return _locking(fd, operation, 1)
40: static int lock_shared_blocking(int fd) { FLOCK(_LK_LOCK); }
41: static int lock_exclusive_blocking(int fd) { FLOCK(_LK_LOCK); }
42: static int lock_exclusive_nonblocking(int fd) { FLOCK(_LK_NBLCK); }
43: static int unlock(int fd) { FLOCK(_LK_UNLCK); }
1.93 paf 44:
1.99 paf 45: #else
46: #ifdef HAVE_FCNTL
1.93 paf 47:
1.99 paf 48: #define FLOCK(cmd, arg) struct flock ls={arg, SEEK_SET}; return fcntl(fd, cmd, &ls)
49: static int lock_shared_blocking(int fd) { FLOCK(F_SETLKW, F_RDLCK); }
50: static int lock_exclusive_blocking(int fd) { FLOCK(F_SETLKW, F_WRLCK); }
51: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_SETLK, F_RDLCK); }
52: static int unlock(int fd) { FLOCK(F_SETLK, F_UNLCK); }
1.93 paf 53:
54: #else
55: #ifdef HAVE_LOCKF
1.99 paf 56:
57: #define FLOCK(fd, operation) lseek(fd, 0, SEEK_SET); return lockf(fd, operation, 1)
58: static int lock_shared_blocking(int fd) { FLOCK(F_LOCK); } // on intel solaris man doesn't have doc on shared blocking
59: static int lock_exclusive_blocking(int fd) { FLOCK(F_LOCK); }
60: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_TLOCK); }
61: static int unlock(int fd) { FLOCK(F_TLOCK); }
62:
1.93 paf 63: #else
1.99 paf 64:
65: #error unable to find file locking func
66:
67: #endif
1.93 paf 68: #endif
69: #endif
70: #endif
71:
1.86 paf 72: static char *strnchr(char *buf, size_t size, char c) {
1.103 paf 73: // sanity check
74: if(!buf)
75: return 0;
76:
1.86 paf 77: for(; size-->0; buf++) {
78: if(*buf==c)
79: return buf;
80: }
81:
82: return 0;
83: }
84:
85: void fix_line_breaks(char *buf, size_t& size) {
1.87 paf 86: //_asm int 3;
1.86 paf 87: const char * const eob=buf+size;
1.87 paf 88: char *dest=buf;
1.72 parser 89: // fix DOS: \r\n -> \n
90: // fix Macintosh: \r -> \n
1.87 paf 91: char *bol=buf;
1.86 paf 92: while(char *eol=strnchr(bol, eob -bol, '\r')) {
1.72 parser 93: size_t len=eol-bol;
94: if(dest!=bol)
95: memcpy(dest, bol, len);
96: dest+=len;
97: *dest++='\n';
98:
1.87 paf 99: if(&eol[1]<eob && eol[1]=='\n') { // \r,\n = DOS
1.72 parser 100: bol=eol+2;
101: size--;
102: } else // \r,not \n = Macintosh
103: bol=eol+1;
104: }
105: // last piece without \r, including terminating 0
106: if(dest!=bol)
1.87 paf 107: memcpy(dest, bol, eob-bol);
1.72 parser 108: }
1.18 paf 109:
1.28 paf 110: char *file_read_text(Pool& pool, const String& file_spec, bool fail_on_read_problem) {
1.72 parser 111: void *result; size_t size;
112: return file_read(pool, file_spec, result, size, true, fail_on_read_problem)?(char *)result:0;
1.34 paf 113: }
114: bool file_read(Pool& pool, const String& file_spec,
1.87 paf 115: void*& data, size_t& data_size, bool as_text,
1.44 paf 116: bool fail_on_read_problem,
117: size_t offset, size_t limit) {
1.64 parser 118: const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.33 paf 119: int f;
1.2 paf 120: struct stat finfo;
1.33 paf 121:
122: // first open, next stat:
1.45 paf 123: // directory update of NTFS hard links performed on open.
1.33 paf 124: // ex:
125: // a.html:^test[] and b.html hardlink to a.html
126: // user inserts ! before ^test in a.html
127: // directory entry of b.html in NTFS not updated at once,
1.35 paf 128: // they delay update till open, so we would receive "!^test[" string
129: // if would do stat, next open.
1.98 paf 130: if((f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0) {
1.110 paf 131: if(lock_shared_blocking(f)!=0) {
132: Exception e("file.lock",
133: &file_spec,
134: "shared lock failed: %s (%d), actual filename '%s'",
135: strerror(errno), errno, fname);
136: unlock(f);
137: close(f);
138: if(fail_on_read_problem)
139: throw e;
140: return false;
141: }
1.98 paf 142: if(stat(fname, &finfo)!=0) {
1.106 paf 143: Exception e("file.missing",
1.98 paf 144: &file_spec,
145: "stat failed: %s (%d), actual filename '%s'",
146: strerror(errno), errno, fname);
1.99 paf 147: unlock(f);
1.98 paf 148: close(f);
149: if(fail_on_read_problem)
150: throw e;
151: return false;
152: }
1.105 paf 153: #ifdef NO_FOREIGN_GROUP_FILES
154: if(finfo.st_gid/*foreign?*/!=getegid()) {
1.107 paf 155: Exception e("parser.runtime",
1.105 paf 156: &file_spec,
157: "parser reading files of foreign group disabled [recompile parser without --disable-foreign-group-files configure option], actual filename '%s'",
158: fname);
159: unlock(f);
160: close(f);
161: if(fail_on_read_problem)
162: throw e;
163: return false;
164: }
165: #endif
1.98 paf 166: size_t max_size=limit?min(offset+limit, (size_t)finfo.st_size)-offset:finfo.st_size;
1.47 paf 167: if(!max_size) { // eof
1.83 paf 168: if(as_text) {
169: data=pool.malloc(1);
170: *(char*)data=0;
171: } else
172: data=0;
1.98 paf 173: data_size=0;
1.59 parser 174: } else {
1.78 paf 175: data=pool.malloc(max_size+(as_text?1:0), 3);
1.59 parser 176: if(offset)
177: lseek(f, offset, SEEK_SET);
1.98 paf 178: data_size=read(f, data, max_size);
1.47 paf 179: }
1.99 paf 180: unlock(f);
1.2 paf 181: close(f);
1.59 parser 182: if(!max_size) // eof
183: return true;
1.32 paf 184:
1.121 paf 185: if(ssize_t(data_size)<0 || data_size>max_size)
1.106 paf 186: throw Exception(0,
1.33 paf 187: &file_spec,
1.44 paf 188: "read failed: actually read %d bytes count not in [0..%lu] valid range",
1.98 paf 189: data_size, (unsigned long)max_size); //never
1.87 paf 190:
191: if(as_text) {
192: fix_line_breaks((char *)data, data_size);
193: // note: after fixing
194: ((char*&)data)[data_size]=0;
195: }
1.72 parser 196: return true;
1.118 paf 197: } else {
198: if(fail_on_read_problem)
199: throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
200: &file_spec,
201: "read failed: %s (%d), actual filename '%s'",
202: strerror(errno), errno, fname);
203: return false;
204: }
1.8 paf 205: }
206:
1.63 parser 207: static void create_dir_for_file(const String& file_spec) {
208: size_t pos_after=1;
209: int pos_before;
210: while((pos_before=file_spec.pos("/", 1, pos_after))>=0) {
1.64 parser 211: mkdir(file_spec.mid(0, pos_before).cstr(String::UL_FILE_SPEC), 0775);
1.63 parser 212: pos_after=pos_before+1;
213: }
214: }
215:
1.98 paf 216: bool file_write_action_under_lock(
1.28 paf 217: const String& file_spec,
1.96 paf 218: const char *action_name, void (*action)(int, void *), void *context,
1.80 paf 219: bool as_text,
1.98 paf 220: bool do_append,
1.110 paf 221: bool do_block,
222: bool fail_on_lock_problem) {
1.64 parser 223: const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.28 paf 224: int f;
1.80 paf 225: if(access(fname, W_OK)!=0) // no
1.63 parser 226: create_dir_for_file(file_spec);
1.50 paf 227:
1.80 paf 228: if((f=open(fname,
229: O_CREAT|O_RDWR
230: |(as_text?_O_TEXT:_O_BINARY)
1.122 ! paf 231: |(do_append?O_APPEND:0), 0664))>=0) {
1.99 paf 232: if((do_block?lock_exclusive_blocking(f):lock_exclusive_nonblocking(f))!=0) {
1.110 paf 233: Exception e("file.lock",
234: &file_spec,
235: "shared lock failed: %s (%d), actual filename '%s'",
236: strerror(errno), errno, fname);
1.98 paf 237: close(f);
1.110 paf 238: if(fail_on_lock_problem)
239: throw e;
1.98 paf 240: return false;
241: }
1.96 paf 242:
243: try {
244: action(f, context);
245: } catch(...) {
1.104 paf 246: if(!do_append)
1.122 ! paf 247: ftruncate(f, tell(f)); // one can not use O_TRUNC, read lower
1.99 paf 248: unlock(f);
1.96 paf 249: close(f);
250: /*re*/throw;
251: }
1.80 paf 252:
1.104 paf 253: if(!do_append)
1.122 ! paf 254: ftruncate(f, tell(f)); // O_TRUNC truncates even exclusevely write-locked file [thanks to Igor Milyakov <virtan@rotabanner.com> for discovering]
1.99 paf 255: unlock(f);
1.80 paf 256: close(f);
1.98 paf 257: return true;
1.80 paf 258: } else
1.106 paf 259: throw Exception(errno==EACCES?"file.access":0,
1.80 paf 260: &file_spec,
1.96 paf 261: "%s failed: %s (%d), actual filename '%s'",
262: action_name, strerror(errno), errno, fname);
263: // here should be nothing, see rethrow above
264: }
265:
266: #ifndef DOXYGEN
267: struct File_write_action_info {
268: const void *data; size_t size;
269: };
270: #endif
271: static void file_write_action(int f, void *context) {
272: File_write_action_info& info=*static_cast<File_write_action_info *>(context);
1.113 paf 273: if(info.size) {
1.116 paf 274: int written=write(f, info.data, info.size);
275: if(written<0)
1.113 paf 276: throw Exception(0,
277: 0,
1.115 paf 278: "write failed: %s (%d)", strerror(errno), errno);
1.113 paf 279: }
1.96 paf 280: }
281: void file_write(
282: const String& file_spec,
283: const void *data, size_t size,
284: bool as_text,
285: bool do_append) {
286: File_write_action_info info={data, size};
1.98 paf 287: file_write_action_under_lock(
1.96 paf 288: file_spec,
289: "write", file_write_action, &info,
290: as_text,
291: do_append);
1.30 paf 292: }
293:
1.63 parser 294: // throws nothing! [this is required in file_move & file_delete]
1.50 paf 295: static void rmdir(const String& file_spec, size_t pos_after) {
296: int pos_before;
297: if((pos_before=file_spec.pos("/", 1, pos_after))>=0)
298: rmdir(file_spec, pos_before+1);
299:
1.64 parser 300: rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::UL_FILE_SPEC));
1.50 paf 301: }
1.95 paf 302: bool file_delete(const String& file_spec, bool fail_on_read_problem) {
1.64 parser 303: const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.54 parser 304: if(unlink(fname)!=0)
1.93 paf 305: if(fail_on_read_problem)
1.106 paf 306: throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.93 paf 307: &file_spec,
308: "unlink failed: %s (%d), actual filename '%s'",
309: strerror(errno), errno, fname);
310: else
311: return false;
1.50 paf 312:
313: rmdir(file_spec, 1);
1.93 paf 314: return true;
1.60 parser 315: }
1.95 paf 316: void file_move(const String& old_spec, const String& new_spec) {
1.64 parser 317: const char *old_spec_cstr=old_spec.cstr(String::UL_FILE_SPEC);
318: const char *new_spec_cstr=new_spec.cstr(String::UL_FILE_SPEC);
1.63 parser 319:
320: create_dir_for_file(new_spec);
321:
1.60 parser 322: if(rename(old_spec_cstr, new_spec_cstr)!=0)
1.106 paf 323: throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.60 parser 324: &old_spec,
325: "rename failed: %s (%d), actual filename '%s' to '%s'",
326: strerror(errno), errno, old_spec_cstr, new_spec_cstr);
1.63 parser 327:
328: rmdir(old_spec, 1);
1.31 paf 329: }
330:
1.51 paf 331:
1.118 paf 332: bool entry_exists(const char *fname, struct stat *afinfo) {
333: struct stat lfinfo;
334: bool result=stat(fname, &lfinfo)==0;
335: if(afinfo)
336: *afinfo=lfinfo;
337: return result;
1.119 paf 338: }
339:
340: bool entry_exists(const String& file_spec) {
341: const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
342: return entry_exists(fname, 0);
1.118 paf 343: }
344:
1.51 paf 345: static bool entry_readable(const String& file_spec, bool need_dir) {
1.120 paf 346: char *fname=file_spec.cstr(String::UL_FILE_SPEC);
347: if(need_dir) {
348: size_t size=strlen(fname);
349: while(size) {
350: char c=fname[size-1];
351: if(c=='/' || c=='\\')
352: fname[--size]=0;
353: else
354: break;
355: }
356: }
1.51 paf 357: struct stat finfo;
1.118 paf 358: if(access(fname, R_OK)==0 && entry_exists(fname, &finfo)) {
1.109 paf 359: bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
1.51 paf 360: return is_dir==need_dir;
361: }
362: return false;
363: }
1.31 paf 364: bool file_readable(const String& file_spec) {
1.51 paf 365: return entry_readable(file_spec, false);
366: }
367: bool dir_readable(const String& file_spec) {
368: return entry_readable(file_spec, true);
1.65 parser 369: }
370: String *file_readable(const String& path, const String& name) {
371: String *result=new(path.pool()) String(path);
372: *result << "/";
373: *result << name;
374: return file_readable(*result)?result:0;
1.43 paf 375: }
376: bool file_executable(const String& file_spec) {
1.64 parser 377: return access(file_spec.cstr(String::UL_FILE_SPEC), X_OK)==0;
1.44 paf 378: }
379:
1.64 parser 380: bool file_stat(const String& file_spec,
1.58 parser 381: size_t& rsize,
382: time_t& ratime,
383: time_t& rmtime,
1.64 parser 384: time_t& rctime,
385: bool fail_on_read_problem) {
1.44 paf 386: Pool& pool=file_spec.pool();
1.64 parser 387: const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.44 paf 388: struct stat finfo;
389: if(stat(fname, &finfo)!=0)
1.64 parser 390: if(fail_on_read_problem)
1.106 paf 391: throw Exception("file.missing",
1.67 parser 392: &file_spec,
393: "getting file size failed: %s (%d), real filename '%s'",
394: strerror(errno), errno, fname);
1.64 parser 395: else
396: return false;
1.58 parser 397: rsize=finfo.st_size;
398: ratime=finfo.st_atime;
399: rmtime=finfo.st_mtime;
400: rctime=finfo.st_ctime;
1.64 parser 401: return true;
1.18 paf 402: }
403:
1.8 paf 404: char *getrow(char **row_ref, char delim) {
405: char *result=*row_ref;
406: if(result) {
407: *row_ref=strchr(result, delim);
408: if(*row_ref)
409: *((*row_ref)++)=0;
410: else if(!*result)
411: return 0;
412: }
413: return result;
414: }
415:
1.23 paf 416: char *lsplit(char *string, char delim) {
417: if(string) {
418: char *v=strchr(string, delim);
1.8 paf 419: if(v) {
420: *v=0;
421: return v+1;
422: }
423: }
424: return 0;
425: }
426:
427: char *lsplit(char **string_ref, char delim) {
428: char *result=*string_ref;
429: char *next=lsplit(*string_ref, delim);
430: *string_ref=next;
431: return result;
1.9 paf 432: }
433:
434: char *rsplit(char *string, char delim) {
1.18 paf 435: if(string) {
1.9 paf 436: char *v=strrchr(string, delim);
1.18 paf 437: if(v) {
1.9 paf 438: *v=0;
439: return v+1;
440: }
441: }
442: return NULL;
1.10 paf 443: }
444:
1.37 paf 445: /// @todo less stupid type detection
1.10 paf 446: char *format(Pool& pool, double value, char *fmt) {
1.108 paf 447: char local_buf[MAX_NUMBER];
448: size_t size;
449:
1.10 paf 450: if(fmt)
451: if(strpbrk(fmt, "diouxX"))
452: if(strpbrk(fmt, "ouxX"))
1.108 paf 453: size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value);
1.10 paf 454: else
1.108 paf 455: size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value);
1.10 paf 456: else
1.108 paf 457: size=snprintf(local_buf, sizeof(local_buf), fmt, value);
1.10 paf 458: else
1.108 paf 459: size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value);
1.10 paf 460:
1.108 paf 461: char *pool_buf=(char *)pool.malloc(size+1, 4);
462: memcpy(pool_buf, local_buf, size+1);
463: return pool_buf;
1.12 paf 464: }
465:
1.36 paf 466: size_t stdout_write(const void *buf, size_t size) {
1.12 paf 467: #ifdef WIN32
468: do{
469: int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
470: if(chunk_written<=0)
471: break;
472: size-=chunk_written;
1.36 paf 473: buf=((const char*)buf)+chunk_written;
1.12 paf 474: } while(size>0);
475:
476: return size;
477: #else
1.13 paf 478: return fwrite(buf, 1, size, stdout);
1.12 paf 479: #endif
1.2 paf 480: }
1.14 paf 481:
1.77 parser 482: char *unescape_chars(Pool& pool, const char *cp, int len) {
1.78 paf 483: char *s=(char *)pool.malloc(len + 1, 5);
1.14 paf 484: enum EscapeState {
1.33 paf 485: EscapeRest,
486: EscapeFirst,
1.14 paf 487: EscapeSecond
488: } escapeState=EscapeRest;
489: int escapedValue=0;
490: int srcPos=0;
491: int dstPos=0;
492: while(srcPos < len) {
493: int ch=cp[srcPos];
494: switch(escapeState) {
495: case EscapeRest:
496: if(ch=='%') {
497: escapeState=EscapeFirst;
498: } else if(ch=='+') {
499: s[dstPos++]=' ';
500: } else {
501: s[dstPos++]=ch;
502: }
503: break;
504: case EscapeFirst:
505: escapedValue=hex_value[ch] << 4;
506: escapeState=EscapeSecond;
507: break;
508: case EscapeSecond:
509: escapedValue +=hex_value[ch];
510: s[dstPos++]=escapedValue;
511: escapeState=EscapeRest;
512: break;
513: }
514: srcPos++;
515: }
516: s[dstPos]=0;
517: return s;
1.24 paf 518: }
519:
520: #ifdef WIN32
521: void back_slashes_to_slashes(char *s) {
522: if(s)
523: for(; *s; s++)
524: if(*s=='\\')
525: *s='/';
526: }
1.42 paf 527: /*
528: void slashes_to_back_slashes(char *s) {
529: if(s)
530: for(; *s; s++)
531: if(*s=='/')
532: *s='\\';
533: }
534: */
1.24 paf 535: #endif
1.41 paf 536:
537: bool StrEqNc(const char *s1, const char *s2, bool strict) {
538: while(true) {
539: if(!(*s1)) {
540: if(!(*s2))
541: return true;
542: else
543: return !strict;
544: } else if(!(*s2))
545: return !strict;
546: if(isalpha(*s1)) {
547: if(tolower(*s1) !=tolower(*s2))
548: return false;
549: } else if((*s1) !=(*s2))
550: return false;
551: s1++;
552: s2++;
553: }
1.57 parser 554: }
555:
1.84 paf 556: static bool isLeap(int year) {
1.57 parser 557: return !(
558: (year % 4) || ((year % 400) && !(year % 100))
559: );
560: }
561:
562: int getMonthDays(int year, int month) {
563: int monthDays[]={
564: 31,
565: isLeap(year) ? 29 : 28,
566: 31,
567: 30,
568: 31,
569: 30,
570: 31,
571: 31,
572: 30,
573: 31,
574: 30,
575: 31
576: };
577: return monthDays[month];
1.41 paf 578: }
1.69 parser 579:
580: void remove_crlf(char *start, char *end) {
581: for(char *p=start; p<end; p++)
582: switch(*p) {
583: case '\n': *p='|'; break;
584: case '\r': *p=' '; break;
585: }
1.91 paf 586: }
587:
588:
589: /// must be last in this file
590: #undef vsnprintf
591: int __vsnprintf(char *b, size_t s, const char *f, va_list l) {
592: if(!s)
593: return 0;
594:
595: int r;
596: // note: on win32& maybe somewhere else
597: // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing
598: --s;
599: #if _MSC_VER
600: /*
601: win32:
602: mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm
603:
604: if the number of bytes to write exceeds buffer, then count bytes are written and –1 is returned
605: */
606: r=_vsnprintf(b, s, f, l);
607: if(r<0)
608: r=s;
609: #else
610: r=vsnprintf(b, s, f, l);
611: /*
612: solaris:
613: man vsnprintf
614:
615: The snprintf() function returns the number of characters
616: formatted, that is, the number of characters that would have
617: been written to the buffer if it were large enough. If the
618: value of n is 0 on a call to snprintf(), an unspecified
619: value less than 1 is returned.
620: */
621:
622: if(r<0)
623: r=0;
624: else if(r>s)
625: r=s;
626: #endif
627: b[r]=0;
628: return r;
629: }
630:
631: int __snprintf(char *b, size_t s, const char *f, ...) {
632: va_list l;
633: va_start(l, f);
634: int r=__vsnprintf(b, s, f, l);
635: va_end(l);
636: return r;
1.98 paf 637: }
638:
639: int pa_sleep(unsigned long secs, unsigned long usecs) {
640: for (; usecs >= 1000000; ++secs, usecs -= 1000000);
641:
642: #ifdef WIN32
643: Sleep(secs * 1000 + usecs / 1000);
644: return 0;
645: #else
646: struct timeval t;
647: t.tv_sec = secs;
648: t.tv_usec = usecs;
649: return (select(0, NULL, NULL, NULL, &t) == -1 ? errno : 0);
650: #endif
1.74 parser 651: }
E-mail: