Changeset 2596 for vendor/gnumake/current/misc.c
- Timestamp:
- Jun 20, 2012, 12:44:52 AM (13 years ago)
- Location:
- vendor/gnumake/current
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/gnumake/current
- Property svn:ignore deleted
-
vendor/gnumake/current/misc.c
r1989 r2596 1 1 /* Miscellaneous generic support functions for GNU Make. 2 2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software4 Foundation, Inc.3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 4 2010 Free Software Foundation, Inc. 5 5 This file is part of GNU Make. 6 6 … … 114 114 ++in; 115 115 116 /* If the newline is quoted, discard following whitespace 117 and any preceding whitespace; leave just one space. */ 116 /* If the newline is escaped, discard following whitespace leaving just 117 one space. POSIX requires that each backslash/newline/following 118 whitespace sequence be reduced to a single space. */ 118 119 if (backslash) 119 120 { 120 121 in = next_token (in); 122 /* Removing this loop will fix Savannah bug #16670: do we want to? */ 121 123 while (out > line && isblank ((unsigned char)out[-1])) 122 124 --out; … … 162 164 163 165 164 /* Return a string whose contents concatenate th ose of s1, s2, s3.166 /* Return a string whose contents concatenate the NUM strings provided 165 167 This string lives in static, re-used memory. */ 166 168 167 char * 168 concat (const char *s1, const char *s2, const char *s3) 169 { 170 unsigned int len1, len2, len3; 169 const char * 170 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H 171 concat (unsigned int num, ...) 172 #else 173 concat (num, va_alist) 174 unsigned int num; 175 va_dcl 176 #endif 177 { 171 178 static unsigned int rlen = 0; 172 179 static char *result = NULL; 173 174 len1 = (s1 && *s1 != '\0') ? strlen (s1) : 0; 175 len2 = (s2 && *s2 != '\0') ? strlen (s2) : 0; 176 len3 = (s3 && *s3 != '\0') ? strlen (s3) : 0; 177 178 if (len1 + len2 + len3 + 1 > rlen) 179 result = xrealloc (result, (rlen = len1 + len2 + len3 + 10)); 180 181 if (len1) 182 memcpy (result, s1, len1); 183 if (len2) 184 memcpy (result + len1, s2, len2); 185 if (len3) 186 memcpy (result + len1 + len2, s3, len3); 187 188 result[len1+len2+len3] = '\0'; 180 int ri = 0; 181 182 #if USE_VARIADIC 183 va_list args; 184 #endif 185 186 VA_START (args, num); 187 188 while (num-- > 0) 189 { 190 const char *s = va_arg (args, const char *); 191 unsigned int l = s ? strlen (s) : 0; 192 193 if (l == 0) 194 continue; 195 196 if (ri + l > rlen) 197 { 198 rlen = ((rlen ? rlen : 60) + l) * 2; 199 result = xrealloc (result, rlen); 200 } 201 202 memcpy (result + ri, s, l); 203 ri += l; 204 } 205 206 VA_END (args); 207 208 /* Get some more memory if we don't have enough space for the 209 terminating '\0'. */ 210 if (ri == rlen) 211 { 212 rlen = (rlen ? rlen : 60) * 2; 213 result = xrealloc (result, rlen); 214 } 215 216 result[ri] = '\0'; 189 217 190 218 return result; … … 341 369 342 370 #undef xmalloc 371 #undef xcalloc 343 372 #undef xrealloc 344 373 #undef xstrdup … … 347 376 xmalloc (unsigned int size) 348 377 { 349 /* Make sure we don't allocate 0, for pre- ANSI libraries. */378 /* Make sure we don't allocate 0, for pre-ISO implementations. */ 350 379 void *result = malloc (size ? size : 1); 351 380 if (result == 0) … … 356 385 357 386 void * 387 xcalloc (unsigned int size) 388 { 389 /* Make sure we don't allocate 0, for pre-ISO implementations. */ 390 void *result = calloc (size ? size : 1, 1); 391 if (result == 0) 392 fatal (NILF, _("virtual memory exhausted")); 393 return result; 394 } 395 396 397 void * 358 398 xrealloc (void *ptr, unsigned int size) 359 399 { 360 400 void *result; 361 401 362 /* Some older implementations of realloc() don't conform to ANSI. */402 /* Some older implementations of realloc() don't conform to ISO. */ 363 403 if (! size) 364 404 size = 1; … … 394 434 395 435 char * 396 savestring (const char *str, unsigned int length) 397 { 398 char *out = xmalloc (length + 1); 436 xstrndup (const char *str, unsigned int length) 437 { 438 char *result; 439 440 #ifdef HAVE_STRNDUP 441 result = strndup (str, length); 442 if (result == 0) 443 fatal (NILF, _("virtual memory exhausted")); 444 #else 445 result = xmalloc (length + 1); 399 446 if (length > 0) 400 memcpy (out, str, length); 401 out[length] = '\0'; 402 return out; 447 strncpy (result, str, length); 448 result[length] = '\0'; 449 #endif 450 451 return result; 403 452 } 404 453 … … 493 542 494 543 495 /* Allocate a new `struct dep' with all fields initialized to 0. */ 496 497 struct dep * 498 alloc_dep () 499 { 500 struct dep *d = xmalloc (sizeof (struct dep)); 501 memset (d, '\0', sizeof (struct dep)); 502 return d; 503 } 504 505 506 /* Free `struct dep' along with `name' and `stem'. */ 507 508 void 509 free_dep (struct dep *d) 510 { 511 free (d); 512 } 513 514 /* Copy a chain of `struct dep', making a new chain 515 with the same contents as the old one. */ 544 /* Copy a chain of `struct dep'. For 2nd expansion deps, dup the name. */ 516 545 517 546 struct dep * … … 525 554 struct dep *c = xmalloc (sizeof (struct dep)); 526 555 memcpy (c, d, sizeof (struct dep)); 556 557 if (c->need_2nd_expansion) 558 c->name = xstrdup (c->name); 527 559 528 560 c->next = 0; … … 590 622 return (c1 - c2); 591 623 } 624 } 625 #endif 626 627 #if !HAVE_STRNCASECMP && !HAVE_STRNICMP && !HAVE_STRNCMPI 628 629 /* If we don't have strncasecmp() (from POSIX), or anything that can 630 substitute for it, define our own version. */ 631 632 int 633 strncasecmp (const char *s1, const char *s2, int n) 634 { 635 while (n-- > 0) 636 { 637 int c1 = (int) *(s1++); 638 int c2 = (int) *(s2++); 639 640 if (isalpha (c1)) 641 c1 = tolower (c1); 642 if (isalpha (c2)) 643 c2 = tolower (c2); 644 645 if (c1 != '\0' && c1 == c2) 646 continue; 647 648 return (c1 - c2); 649 } 650 651 return 0; 592 652 } 593 653 #endif
Note:
See TracChangeset
for help on using the changeset viewer.