home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / sendmail-5.65c+IDA-1.4.4.1 / uiuc / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  2.2 KB  |  124 lines

  1. #include <ctype.h>
  2. #include <stdio.h>
  3.  
  4. static char RCSid[] = "$Header: misc.c,v 2.2 85/09/18 21:03:08 essick Exp $";
  5.  
  6. /*
  7.  *    random routines that I find helpful every now and
  8.  *    then.
  9.  */
  10.  
  11. /*
  12.  *    save a copy of a string somewhere using malloc().
  13.  */
  14. char   *strsave (p)
  15. char   *p;
  16. {
  17.     register char  *hold;
  18.     register int    len;
  19.     extern char *malloc ();
  20.  
  21.     if (p == (char *) NULL)
  22.     return (char *) NULL;
  23.  
  24.     hold = malloc ((len = strlen (p) + 1));        /* include null */
  25.     if (hold == (char *) NULL)
  26.     {
  27.     fprintf (stderr, "Unable to malloc %d bytes.\n",
  28.         strlen (p) + 1);
  29.     exit (1);
  30.     }
  31.     strncpy (hold, p, len);
  32.     return (hold);
  33. }
  34.  
  35. /*
  36.  * case insensitive string comparison
  37.  *    == 0 if true. <0 if first "less", else > 0
  38.  */
  39.  
  40. int     strsame (a, b)
  41. register char  *a;
  42. register char  *b;
  43. {
  44.     register char   aa;
  45.     register char   bb;
  46.  
  47.     if (a == (char *) NULL)
  48.     return (-1);
  49.     if (b == (char *) NULL)
  50.     return (1);
  51.     while (*a && *b)                    /* got strings left */
  52.     {
  53.     aa = isupper (*a) ? tolower (*a) : *a;
  54.     bb = isupper (*b) ? tolower (*b) : *b;
  55.     if (aa < bb)
  56.         return (-1);
  57.     if (aa > bb)
  58.         return (1);
  59.     a++;
  60.     b++;                        /* next ones */
  61.     }
  62.     if (*a)
  63.     return (1);
  64.     if (*b)
  65.     return (-1);
  66.     return (0);
  67. }
  68.  
  69. /* 
  70.  * one which limits the length
  71.  */
  72. int     strnsame (a, b, length)
  73. register char  *a;
  74. register char  *b;
  75. int     length;
  76. {
  77.     register char   aa;
  78.     register char   bb;
  79.  
  80.     if (a == (char *) NULL)
  81.     return (-1);
  82.     if (b == (char *) NULL)
  83.     return (1);
  84.     while (*a && *b)                    /* got strings left */
  85.     {
  86.     aa = isupper (*a) ? tolower (*a) : *a;
  87.     bb = isupper (*b) ? tolower (*b) : *b;
  88.     if (aa < bb)
  89.         return (-1);
  90.     if (aa > bb)
  91.         return (1);
  92.     a++;
  93.     b++;                        /* next ones */
  94.     if (--length <= 0)
  95.         return (0);                    /* same */
  96.     }
  97.     if (*a)
  98.     return (1);
  99.     if (*b)
  100.     return (-1);
  101.     return (0);
  102. }
  103.  
  104. /*
  105.  *    strlower(p)
  106.  *
  107.  *    turn a given string into all lower case letters.
  108.  *    convert it in place.
  109.  */
  110.  
  111. int     strlower (p)
  112. char   *p;
  113. {
  114.     register char  *pp = p;
  115.  
  116.     while (*pp)
  117.     {
  118.     if (isascii (*pp) && isupper (*pp))
  119.         *pp = tolower (*pp);            /* make lower case */
  120.     pp++;                        /* and to next */
  121.     }
  122.     return (0);                        /* all done */
  123. }
  124.