Changeset 903 for trunk/src/gmakenew/misc.c
- Timestamp:
- May 23, 2007, 7:31:19 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gmakenew/misc.c
r549 r903 172 172 173 173 174 /* Return a newly-allocated string whose contents175 concatenate those of s1, s2, s3. */174 /* Return a string whose contents concatenate those of s1, s2, s3. 175 This string lives in static, re-used memory. */ 176 176 177 177 char * … … 179 179 { 180 180 unsigned int len1, len2, len3; 181 char *result; 182 183 len1 = *s1 != '\0' ? strlen (s1) : 0; 184 len2 = *s2 != '\0' ? strlen (s2) : 0; 185 len3 = *s3 != '\0' ? strlen (s3) : 0; 186 187 result = (char *) xmalloc (len1 + len2 + len3 + 1); 188 189 if (*s1 != '\0') 190 bcopy (s1, result, len1); 191 if (*s2 != '\0') 192 bcopy (s2, result + len1, len2); 193 if (*s3 != '\0') 194 bcopy (s3, result + len1 + len2, len3); 195 *(result + len1 + len2 + len3) = '\0'; 181 static unsigned int rlen = 0; 182 static char *result = NULL; 183 184 len1 = (s1 && *s1 != '\0') ? strlen (s1) : 0; 185 len2 = (s2 && *s2 != '\0') ? strlen (s2) : 0; 186 len3 = (s3 && *s3 != '\0') ? strlen (s3) : 0; 187 188 if (len1 + len2 + len3 + 1 > rlen) 189 result = xrealloc (result, (rlen = len1 + len2 + len3 + 10)); 190 191 if (len1) 192 memcpy (result, s1, len1); 193 if (len2) 194 memcpy (result + len1, s2, len2); 195 if (len3) 196 memcpy (result + len1 + len2, s3, len3); 197 198 result[len1+len2+len3] = '\0'; 196 199 197 200 return result; … … 351 354 #undef xstrdup 352 355 353 char*356 void * 354 357 xmalloc (unsigned int size) 355 358 { 356 359 /* Make sure we don't allocate 0, for pre-ANSI libraries. */ 357 char *result = (char *)malloc (size ? size : 1);360 void *result = malloc (size ? size : 1); 358 361 if (result == 0) 359 362 fatal (NILF, _("virtual memory exhausted")); … … 362 365 363 366 364 char*365 xrealloc ( char*ptr, unsigned int size)366 { 367 char*result;367 void * 368 xrealloc (void *ptr, unsigned int size) 369 { 370 void *result; 368 371 369 372 /* Some older implementations of realloc() don't conform to ANSI. */ … … 385 388 result = strdup (ptr); 386 389 #else 387 result = (char *)malloc (strlen (ptr) + 1);390 result = malloc (strlen (ptr) + 1); 388 391 #endif 389 392 … … 394 397 return result; 395 398 #else 396 return strcpy (result, ptr);399 return strcpy (result, ptr); 397 400 #endif 398 401 } … … 403 406 savestring (const char *str, unsigned int length) 404 407 { 405 register char *out = (char *)xmalloc (length + 1);408 char *out = xmalloc (length + 1); 406 409 if (length > 0) 407 bcopy (str, out, length);410 memcpy (out, str, length); 408 411 out[length] = '\0'; 409 412 return out; … … 448 451 */ 449 452 char * 450 end_of_token_w32 (c har *s, char stopchar)451 { 452 registerchar *p = s;453 registerint backslash = 0;453 end_of_token_w32 (const char *s, char stopchar) 454 { 455 const char *p = s; 456 int backslash = 0; 454 457 455 458 while (*p != '\0' && *p != stopchar … … 469 472 } 470 473 471 return p;474 return (char *)p; 472 475 } 473 476 #endif … … 483 486 } 484 487 485 /* Find the next token in PTR; return the address of it, and store the 486 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */ 488 /* Find the next token in PTR; return the address of it, and store the length 489 of the token into *LENGTHPTR if LENGTHPTR is not nil. Set *PTR to the end 490 of the token, so this function can be called repeatedly in a loop. */ 487 491 488 492 char * 489 find_next_token (char **ptr, unsigned int *lengthptr) 490 { 491 char *p = next_token (*ptr); 492 char *end; 493 find_next_token (const char **ptr, unsigned int *lengthptr) 494 { 495 const char *p = next_token (*ptr); 493 496 494 497 if (*p == '\0') 495 498 return 0; 496 499 497 *ptr = end = end_of_token (p);500 *ptr = end_of_token (p); 498 501 if (lengthptr != 0) 499 *lengthptr = end - p; 500 return p; 502 *lengthptr = *ptr - p; 503 504 return (char *)p; 501 505 } 502 506 … … 508 512 alloc_dep () 509 513 { 510 struct dep *d = (struct dep *)xmalloc (sizeof (struct dep));511 bzero ((char *) d, sizeof (struct dep));514 struct dep *d = xmalloc (sizeof (struct dep)); 515 memset (d, '\0', sizeof (struct dep)); 512 516 return d; 513 517 } … … 519 523 free_dep (struct dep *d) 520 524 { 521 if (d->name != 0) 522 free (d->name); 523 524 if (d->stem != 0) 525 free (d->stem); 526 527 free ((char *)d); 525 free (d); 528 526 } 529 527 … … 534 532 copy_dep_chain (const struct dep *d) 535 533 { 536 register struct dep *c;537 534 struct dep *firstnew = 0; 538 535 struct dep *lastnew = 0; … … 540 537 while (d != 0) 541 538 { 542 c = (struct dep *) xmalloc (sizeof (struct dep)); 543 bcopy ((char *) d, (char *) c, sizeof (struct dep)); 544 545 if (c->name != 0) 546 c->name = xstrdup (c->name); 547 if (c->stem != 0) 548 c->stem = xstrdup (c->stem); 539 struct dep *c = xmalloc (sizeof (struct dep)); 540 memcpy (c, d, sizeof (struct dep)); 549 541 550 542 c->next = 0; … … 573 565 } 574 566 575 576 /* Free a chain of `struct nameseq'. Each nameseq->name is freed 577 as well. For `struct dep' chains use free_dep_chain. */ 578 579 void 580 free_ns_chain (struct nameseq *n) 581 { 582 register struct nameseq *tmp; 583 584 while (n != 0) 585 { 586 if (n->name != 0) 587 free (n->name); 588 589 tmp = n; 590 591 n = n->next; 592 593 free (tmp); 594 } 595 596 } 597 598 #ifdef iAPX286 599 /* The losing compiler on this machine can't handle this macro. */ 600 601 char * 602 dep_name (struct dep *dep) 603 { 604 return dep->name == 0 ? dep->file->name : dep->name; 567 /* Free a chain of struct nameseq. 568 For struct dep chains use free_dep_chain. */ 569 570 void 571 free_ns_chain (struct nameseq *ns) 572 { 573 while (ns != 0) 574 { 575 struct nameseq *t = ns; 576 ns = ns->next; 577 free (t); 578 } 579 } 580 581 582 583 #if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI 584 585 /* If we don't have strcasecmp() (from POSIX), or anything that can substitute 586 for it, define our own version. */ 587 588 int 589 strcasecmp (const char *s1, const char *s2) 590 { 591 while (1) 592 { 593 int c1 = (int) *(s1++); 594 int c2 = (int) *(s2++); 595 596 if (isalpha (c1)) 597 c1 = tolower (c1); 598 if (isalpha (c2)) 599 c2 = tolower (c2); 600 601 if (c1 != '\0' && c1 == c2) 602 continue; 603 604 return (c1 - c2); 605 } 605 606 } 606 607 #endif
Note:
See TracChangeset
for help on using the changeset viewer.