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