Changeset a6b4c0df in rtems for cpukit/httpd/misc.c
- Timestamp:
- 09/01/00 10:57:21 (22 years ago)
- Branches:
- 4.10, 4.11, 4.8, 4.9, 5, master
- Children:
- 5f117e8
- Parents:
- 757e1661
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpukit/httpd/misc.c
r757e1661 ra6b4c0df 2 2 * misc.c -- Miscellaneous routines. 3 3 * 4 * Copyright (c) GoAhead Software Inc., 1995- 1999. All Rights Reserved.4 * Copyright (c) GoAhead Software Inc., 1995-2000. All Rights Reserved. 5 5 * 6 6 * See the file "license.txt" for usage and redistribution license requirements … … 17 17 /********************************* Defines ************************************/ 18 18 /* 19 * Sprintf buffer structure. Make the increment 8 less than64 so that19 * Sprintf buffer structure. Make the increment 64 so that 20 20 * a balloc can use a 64 byte block. 21 21 */ 22 22 23 23 #define STR_REALLOC 0x1 /* Reallocate the buffer as required */ 24 #define STR_INC 58/* Growth increment */24 #define STR_INC 64 /* Growth increment */ 25 25 26 26 typedef struct { … … 60 60 /* 61 61 * "basename" returns a pointer to the last component of a pathname 62 * LINUX , RTEMS,and LynxOS have their own basename function63 */ 64 65 #if ! LINUX & ! __rtems__ & ! LYNX66 char_t *basename(char_t *name)62 * LINUX and LynxOS have their own basename function 63 */ 64 65 #if ! LINUX && ! LYNX && ! __rtems__ 66 char_t *basename(char_t *name) 67 67 { 68 68 char_t *cp; … … 84 84 } 85 85 } 86 #endif /* ! LINUX & & ! __rtems__ &&! LYNX */86 #endif /* ! LINUX & ! LYNX */ 87 87 88 88 /******************************************************************************/ … … 92 92 */ 93 93 94 char_t *dirname(char_t * buf, char_t*name, int bufsize)95 { 96 char_t *cp;94 char_t *dirname(char_t *buf, char_t *name, int bufsize) 95 { 96 char_t *cp; 97 97 int len; 98 98 … … 131 131 } 132 132 133 133 134 /******************************************************************************/ 134 135 /* 135 136 * sprintf and vsprintf are bad, ok. You can easily clobber memory. Use 136 * gsnprintf and gvsnprintfinstead! These functions do _not_ support floating137 * fmtAlloc and fmtValloc instead! These functions do _not_ support floating 137 138 * point, like %e, %f, %g... 138 139 */ 139 140 140 int gsnprintf(char_t **s, int n, char_t *fmt, ...)141 int fmtAlloc(char_t **s, int n, char_t *fmt, ...) 141 142 { 142 143 va_list ap; … … 148 149 *s = NULL; 149 150 va_start(ap, fmt); 150 result = gvsnprintf(s, n, fmt, ap);151 result = dsnprintf(s, n, fmt, ap, 0); 151 152 va_end(ap); 152 153 return result; … … 155 156 /******************************************************************************/ 156 157 /* 158 * Support a static buffer version for small buffers only! 159 */ 160 161 int fmtStatic(char_t *s, int n, char_t *fmt, ...) 162 { 163 va_list ap; 164 int result; 165 166 a_assert(s); 167 a_assert(fmt); 168 a_assert(n <= 256); 169 170 if (n <= 0) { 171 return -1; 172 } 173 va_start(ap, fmt); 174 result = dsnprintf(&s, n, fmt, ap, 0); 175 va_end(ap); 176 return result; 177 } 178 179 /******************************************************************************/ 180 /* 157 181 * This function appends the formatted string to the supplied string, 158 182 * reallocing if required. 159 183 */ 160 184 161 int gsprintfRealloc(char_t **s, int n, int msize, char_t *fmt, ...)185 int fmtRealloc(char_t **s, int n, int msize, char_t *fmt, ...) 162 186 { 163 187 va_list ap; … … 181 205 */ 182 206 183 int gvsnprintf(char_t **s, int n, char_t *fmt, va_list arg)207 int fmtValloc(char_t **s, int n, char_t *fmt, va_list arg) 184 208 { 185 209 a_assert(s); 186 210 a_assert(fmt); 187 211 212 *s = NULL; 188 213 return dsnprintf(s, n, fmt, arg, 0); 189 214 } … … 191 216 /******************************************************************************/ 192 217 /* 193 * Dynamic sprintf implementation. Supports dynamic buffer allocation also.218 * Dynamic sprintf implementation. Supports dynamic buffer allocation. 194 219 * This function can be called multiple times to grow an existing allocated 195 220 * buffer. In this case, msize is set to the size of the previously allocated … … 253 278 c = *fmt++; 254 279 } else { 255 for ( ; gisdigit( c); c = *fmt++) {280 for ( ; gisdigit((int)c); c = *fmt++) { 256 281 width = width * 10 + (c - '0'); 257 282 } … … 264 289 c = *fmt++; 265 290 } else { 266 for (prec = 0; gisdigit( c); c = *fmt++) {291 for (prec = 0; gisdigit((int)c); c = *fmt++) { 267 292 prec = prec * 10 + (c - '0'); 268 293 } … … 403 428 static void put_char(strbuf_t *buf, char_t c) 404 429 { 405 if (buf->count >= buf->size) {430 if (buf->count >= (buf->size - 1)) { 406 431 if (! (buf->flags & STR_REALLOC)) { 407 432 return; … … 409 434 buf->size += STR_INC; 410 435 if (buf->size > buf->max && buf->size > STR_INC) { 411 a_assert(buf->size <= buf->max); 436 /* 437 * Caller should increase the size of the calling buffer 438 */ 412 439 buf->size -= STR_INC; 413 440 return; 414 441 } 415 442 if (buf->s == NULL) { 416 buf->s = balloc(B_L, buf->size * sizeof(char_t *));443 buf->s = balloc(B_L, buf->size * sizeof(char_t)); 417 444 } else { 418 buf->s = brealloc(B_L, buf->s, buf->size * sizeof(char_t *));445 buf->s = brealloc(B_L, buf->s, buf->size * sizeof(char_t)); 419 446 } 420 447 } 421 448 buf->s[buf->count] = c; 422 ++buf->count; 449 if (c != '\0') { 450 ++buf->count; 451 } 423 452 } 424 453 … … 429 458 430 459 static void put_string(strbuf_t *buf, char_t *s, int len, int width, 431 int prec, enum flag f)460 int prec, enum flag f) 432 461 { 433 462 int i; … … 459 488 460 489 static void put_ulong(strbuf_t *buf, unsigned long int value, int base, 461 int upper, char_t *prefix, int width, int prec, enum flag f)490 int upper, char_t *prefix, int width, int prec, enum flag f) 462 491 { 463 492 unsigned long x, x2; … … 476 505 } 477 506 if (!(f & flag_minus)) { 478 for (i = 0; i < width; ++i) { 479 put_char(buf, ' '); 507 if (f & flag_zero) { 508 for (i = 0; i < width; ++i) { 509 put_char(buf, '0'); 510 } 511 } else { 512 for (i = 0; i < width; ++i) { 513 put_char(buf, ' '); 514 } 480 515 } 481 516 } … … 525 560 */ 526 561 527 char *uniToAsc(char *buf, char_t *ustr, int nBytes)562 char *uniToAsc(char *buf, char_t *ustr, int nBytes) 528 563 { 529 564 #if UNICODE … … 538 573 } 539 574 540 541 575 /******************************************************************************/ 542 576 /* 543 577 * allocate (balloc) a buffer and do ascii to unicode conversion into it. 544 * cp points to the ascii string which must be NULL terminated. 545 * Return a pointer to the unicode buffer which must be bfree'd later. 546 * Return NULL on failure to get buffer. 547 */ 548 char_t *ballocAscToUni(char * cp) 549 { 550 char_t * unip; 578 * cp points to the ascii buffer. alen is the length of the buffer to be 579 * converted not including a terminating NULL. Return a pointer to the 580 * unicode buffer which must be bfree'd later. Return NULL on failure to 581 * get buffer. The buffer returned is NULL terminated. 582 */ 583 584 char_t *ballocAscToUni(char *cp, int alen) 585 { 586 char_t *unip; 551 587 int ulen; 552 588 553 ulen = ( strlen(cp)+ 1) * sizeof(char_t);589 ulen = (alen + 1) * sizeof(char_t); 554 590 if ((unip = balloc(B_L, ulen)) == NULL) { 555 591 return NULL; 556 592 } 557 593 ascToUni(unip, cp, ulen); 594 unip[alen] = 0; 558 595 return unip; 559 596 } … … 563 600 * allocate (balloc) a buffer and do unicode to ascii conversion into it. 564 601 * unip points to the unicoded string. ulen is the number of characters 565 * in the unicode string including teminating null, if there is one. 566 * Return a pointer to the ascii buffer which must be bfree'd later. 567 * Return NULL on failure to get buffer. 568 */ 569 char *ballocUniToAsc(char_t * unip, int ulen) 602 * in the unicode string not including a teminating null. Return a pointer 603 * to the ascii buffer which must be bfree'd later. Return NULL on failure 604 * to get buffer. The buffer returned is NULL terminated. 605 */ 606 607 char *ballocUniToAsc(char_t *unip, int ulen) 570 608 { 571 609 char * cp; 572 610 573 if ((cp = balloc(B_L, ulen )) == NULL) {611 if ((cp = balloc(B_L, ulen+1)) == NULL) { 574 612 return NULL; 575 613 } 576 614 uniToAsc(cp, unip, ulen); 615 cp[ulen] = '\0'; 577 616 return cp; 578 617 } 579 618 580 619 /******************************************************************************/ 581 620 /* 621 * convert a hex string to an integer. The end of the string or a non-hex 622 * character will indicate the end of the hex specification. 623 */ 624 625 unsigned int hextoi(char_t *hexstring) 626 { 627 register char_t *h; 628 register unsigned int c, v; 629 630 v = 0; 631 h = hexstring; 632 if (*h == '0' && (*(h+1) == 'x' || *(h+1) == 'X')) { 633 h += 2; 634 } 635 while ((c = (unsigned int)*h++) != 0) { 636 if (c >= '0' && c <= '9') { 637 c -= '0'; 638 } else if (c >= 'a' && c <= 'f') { 639 c = (c - 'a') + 10; 640 } else if (c >= 'A' && c <= 'F') { 641 c = (c - 'A') + 10; 642 } else { 643 break; 644 } 645 v = (v * 0x10) + c; 646 } 647 return v; 648 } 649 650 /******************************************************************************/ 651 /* 652 * convert a string to an integer. If the string starts with "0x" or "0X" 653 * a hexidecimal conversion is done. 654 */ 655 656 unsigned int gstrtoi(char_t *s) 657 { 658 if (*s == '0' && (*(s+1) == 'x' || *(s+1) == 'X')) { 659 s += 2; 660 return hextoi(s); 661 } 662 return gatoi(s); 663 } 664 665 /******************************************************************************/ 666
Note: See TracChangeset
for help on using the changeset viewer.