home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / finger / part02 / string.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  2.7 KB  |  118 lines

  1. /*
  2.  * string.c -- common string routines for finger (December 1985)
  3.  *
  4.  * Copyright (C) 1986, 1990  Philip L. Budne
  5.  *
  6.  * This file is part of "Phil's Finger Program".
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 1, or (at your option)
  11.  * any later version.
  12.  *
  13.  */
  14.  
  15. # ifndef lint
  16. static char *rcsid = "$Id: string.c,v 3.0 90/07/06 13:11:46 budd Rel $";
  17. # endif /* lint not defined */
  18.  
  19. # include <ctype.h>            /* for isxxx */
  20. # include <stdio.h>            /* for NULL */
  21. # include <strings.h>            /* for strcpy() */
  22. # include "finger.h"
  23. # include "upper.h"            /* uppercase[] */
  24.  
  25. GLOBAL char *strip( s )
  26. register char *s;
  27. {
  28.     register char *cp;
  29.  
  30.     for( cp = s; isprint( *cp ); cp++ )    /* skip printing chars */
  31.     ;
  32.     *cp = EOS;                /* tie off after printables */
  33.  
  34.     while( cp > s )            /* backup */
  35.     if( isspace( *(cp - 1) ) )    /* is previous a space? */
  36.         cp--;            /* yes, keep going */
  37.     else
  38.         break;
  39.     *cp = EOS;                /* tie off before spaces */
  40.  
  41.     for( cp = s; isspace( *cp ); cp++ )    /* start at top */
  42.     ;                /* skipping spaces */
  43.  
  44.     return( cp );            /* return remainder */
  45. } /* strip */
  46.  
  47. GLOBAL void cleanup( s )    /* strip off leading white, trailing crud */
  48. register char *s;        /* in place */
  49. {
  50.     register char *cp;
  51.  
  52.     for( cp = s; isprint( *cp ); cp++ ) ; /* cut off any crud.. */
  53.     *cp = EOS;
  54.  
  55.     for( cp = s; *cp != EOS && isspace( *cp ); cp++ ); /* trim leading white */
  56.  
  57.     if( cp != s )        /* leading white? */
  58.     if( *cp == EOS )
  59.         *s = EOS;        /* empty string!! */
  60.     else
  61.         strcpy(s, cp);    /* copy backwards to remove leading white */
  62. } /* cleanup */
  63.  
  64. GLOBAL char *zup( s )        /* blast in place to upper */
  65. char *s;
  66. {
  67.     register char *cp;
  68.     for( cp = s; *cp != NULL; cp++ )
  69.         *cp = uppercase[ *cp & 0177 ];
  70.  
  71.     return( s );
  72. } /* zup */
  73.  
  74. GLOBAL char *strzcpy(dest, src, count)    /* non-nul terminated src, max length */
  75. register char *dest, *src;
  76. register int count;
  77. {
  78.     while( count-- > 0  &&  *src != EOS )
  79.     *dest++ = *src++;
  80.  
  81.     *dest = EOS;
  82.     return( dest );
  83. } /* strzcpy */
  84.  
  85.  
  86. GLOBAL char *strupcpy(dest, src)    /* copy and uppercasify */
  87. register char *src, *dest;
  88. {
  89.     register int c;
  90.  
  91.     do
  92.     *dest++ = uppercase[ (c = *src++ & 0177) ];
  93.     while( c != EOS );
  94.     return( dest );
  95. } /* strupcpy */
  96.  
  97. GLOBAL char *savestr( s )
  98. char *s;
  99. {
  100.     char *d;
  101.  
  102.     if( s == NULL )
  103.     return( NULL );            /* note: NOT "" (see alias copy loop) */
  104.                     /* in args.c */
  105.  
  106.     if( (d = malloc( strlen( s ) + 1 )) == NULL ) {
  107.     fprintf(stderr, "?Savestr failed for '%s'\n", s);
  108.     exit( 1 );
  109.     }
  110.     return( strcpy(d, s) );
  111. } /* savestr */
  112.  
  113. /*
  114.  * Local variables:
  115.  * comment-column: 40
  116.  * End:
  117.  */
  118.