Ignore:
Timestamp:
May 23, 2007, 7:31:19 AM (18 years ago)
Author:
bird
Message:

Merged with the 2007-05-23 CVS. Added rsort and fixed a couple of windows build issues.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gmakenew/misc.c

    r549 r903  
    172172
    173173
    174 /* Return a newly-allocated string whose contents
    175    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.  */
    176176
    177177char *
     
    179179{
    180180  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';
    196199
    197200  return result;
     
    351354#undef xstrdup
    352355
    353 char *
     356void *
    354357xmalloc (unsigned int size)
    355358{
    356359  /* 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);
    358361  if (result == 0)
    359362    fatal (NILF, _("virtual memory exhausted"));
     
    362365
    363366
    364 char *
    365 xrealloc (char *ptr, unsigned int size)
    366 {
    367   char *result;
     367void *
     368xrealloc (void *ptr, unsigned int size)
     369{
     370  void *result;
    368371
    369372  /* Some older implementations of realloc() don't conform to ANSI.  */
     
    385388  result = strdup (ptr);
    386389#else
    387   result = (char *) malloc (strlen (ptr) + 1);
     390  result = malloc (strlen (ptr) + 1);
    388391#endif
    389392
     
    394397  return result;
    395398#else
    396   return strcpy(result, ptr);
     399  return strcpy (result, ptr);
    397400#endif
    398401}
     
    403406savestring (const char *str, unsigned int length)
    404407{
    405   register char *out = (char *) xmalloc (length + 1);
     408  char *out = xmalloc (length + 1);
    406409  if (length > 0)
    407     bcopy (str, out, length);
     410    memcpy (out, str, length);
    408411  out[length] = '\0';
    409412  return out;
     
    448451 */
    449452char *
    450 end_of_token_w32 (char *s, char stopchar)
    451 {
    452   register char *p = s;
    453   register int backslash = 0;
     453end_of_token_w32 (const char *s, char stopchar)
     454{
     455  const char *p = s;
     456  int backslash = 0;
    454457
    455458  while (*p != '\0' && *p != stopchar
     
    469472    }
    470473
    471   return p;
     474  return (char *)p;
    472475}
    473476#endif
     
    483486}
    484487
    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.  */
    487491
    488492char *
    489 find_next_token (char **ptr, unsigned int *lengthptr)
    490 {
    491   char *p = next_token (*ptr);
    492   char *end;
     493find_next_token (const char **ptr, unsigned int *lengthptr)
     494{
     495  const char *p = next_token (*ptr);
    493496
    494497  if (*p == '\0')
    495498    return 0;
    496499
    497   *ptr = end = end_of_token (p);
     500  *ptr = end_of_token (p);
    498501  if (lengthptr != 0)
    499     *lengthptr = end - p;
    500   return p;
     502    *lengthptr = *ptr - p;
     503
     504  return (char *)p;
    501505}
    502506
     
    508512alloc_dep ()
    509513{
    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));
    512516  return d;
    513517}
     
    519523free_dep (struct dep *d)
    520524{
    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);
    528526}
    529527
     
    534532copy_dep_chain (const struct dep *d)
    535533{
    536   register struct dep *c;
    537534  struct dep *firstnew = 0;
    538535  struct dep *lastnew = 0;
     
    540537  while (d != 0)
    541538    {
    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));
    549541
    550542      c->next = 0;
     
    573565}
    574566
    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
     570void
     571free_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
     588int
     589strcasecmp (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    }
    605606}
    606607#endif
Note: See TracChangeset for help on using the changeset viewer.