home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / misc.1.2.2 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  3.3 KB  |  195 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: misc.c,v 2.1 1992/10/01 00:38:29 panos Exp $" ;
  8. static char misc_version[] = VERSION ;
  9.  
  10. #include <varargs.h>
  11.  
  12. /*
  13.  * MISCELLANEOUS FUNCTIONS
  14.  */
  15.  
  16. #include "misc.h"
  17.  
  18. #ifndef NULL
  19. #define NULL        0
  20. #endif
  21.  
  22. char *strncpy() ;
  23. char *strrchr() ;
  24.  
  25.  
  26. /*
  27.  * Create a new argv array,
  28.  * copy the original to the new one,
  29.  * and clear the old one
  30.  */
  31. char **argv_copy_and_clear( org_argv, start, count )
  32.     char **org_argv ;                            /* original argv */
  33.     int start ;                                    /* from where to start copy/clear */
  34.     int count ;                                    /* how many entries to copy/clear */
  35. {
  36.     char **new_argv ;
  37.     char *p ;
  38.     int i ;
  39.     int j ;
  40.     char *malloc() ;
  41.  
  42.     new_argv = (char **) malloc( count * sizeof( char * ) ) ;
  43.     if ( new_argv == NULL )
  44.         return( NULL ) ;
  45.  
  46.     for ( i = 0 ; i < count ; i++ )
  47.     {
  48.         new_argv[ i ] = make_string( 1, org_argv[ start+i ] ) ;
  49.         if ( new_argv[ i ] == NULL )
  50.         {
  51.             for ( j = i-1 ; j >= 0 ; j-- )
  52.                 free( new_argv[ j ] ) ;
  53.             free( (char *) new_argv ) ;
  54.             return( NULL ) ;
  55.         }
  56.         for ( p = org_argv[ start+i ] ; *p ; p++ )
  57.             *p = ' ' ;
  58.     }
  59.     return( new_argv ) ;
  60. }
  61.  
  62.  
  63. /*
  64.  * We always return a pointer in pathname
  65.  */
  66. char *basename( pathname )
  67.     char *pathname ;
  68. {
  69.     char *s = strrchr( pathname, '/' ) ;
  70.  
  71.     if ( s == NULL )
  72.         return( pathname ) ;
  73.     else
  74.         return( s+1 ) ;
  75. }
  76.  
  77.  
  78. /*
  79.  * We always return a malloced string
  80.  *
  81.  * There are 2 special cases:
  82.  *
  83.  *        1) pathname == "/"
  84.  *                In this case we return "/"
  85.  *        2) pathname does not contain a '/'
  86.  *                In this case we return "."
  87.  */
  88. char *dirname( pathname )
  89.     char *pathname ;
  90. {
  91.     int len ;
  92.     char *s = strrchr( pathname, '/' ) ;
  93.     char *p ;
  94.     char *malloc() ;
  95.  
  96.     if ( s == NULL )
  97.         return( make_string( 1, "." ) ) ;
  98.     else
  99.     {
  100.         len = s - pathname ;
  101.         if ( len == 0 )
  102.             return( make_string( 1, "/" ) ) ;
  103.     }
  104.  
  105.     p = malloc( len+1 ) ;
  106.     if ( p == NULL )
  107.         return( NULL ) ;
  108.     else
  109.     {
  110.         strncpy( p, pathname, len )[ len ] = '\0' ;
  111.         return( p ) ;
  112.     }
  113. }
  114.  
  115.  
  116. char *make_string( count, va_alist )
  117.     register unsigned count ;
  118.     va_dcl
  119. {
  120.     va_list ap ;
  121.     register unsigned i ;
  122.     register unsigned len = 0 ;
  123.     register char *s, *p ;
  124.     char *new_string ;
  125.  
  126.     if ( count == 0 )
  127.         return( NULL ) ;
  128.  
  129.     va_start( ap ) ;
  130.     for ( i = 0 ; i < count ; i++ )
  131.     {
  132.         s = va_arg( ap, char * ) ;
  133.         if ( s == NULL )
  134.             continue ;
  135.         len += strlen( s ) ;
  136.     }
  137.     va_end( ap ) ;
  138.  
  139.     new_string = malloc( len + 1 ) ;
  140.     if ( new_string == NULL )
  141.         return( NULL ) ;
  142.  
  143.     p = new_string ;
  144.     va_start( ap ) ;
  145.     for ( i = 0 ; i < count ; i++ )
  146.     {
  147.         s = va_arg( ap, char * ) ;
  148.         if ( s == NULL )
  149.             continue ;
  150.         while ( *p++ = *s++ ) ;
  151.         p-- ;
  152.     }
  153.     va_end( ap ) ;
  154.     return( new_string ) ;
  155. }
  156.  
  157.  
  158. char *make_pathname( count, va_alist )
  159.     register unsigned count ;
  160.     va_dcl
  161. {
  162.     va_list ap ;
  163.     register unsigned i ;
  164.     register unsigned len = 0 ;
  165.     register char *s, *p ;
  166.     char *pathname ;
  167.  
  168.     if ( count == 0 )
  169.         return( NULL ) ;
  170.  
  171.     va_start( ap ) ;
  172.     for ( i = 0 ; i < count ; i++ )
  173.     {
  174.         s = va_arg( ap, char * ) ;
  175.         len += strlen( s ) ;
  176.     }
  177.     va_end( ap ) ;
  178.  
  179.     pathname = malloc( len + count ) ;
  180.     if ( pathname == NULL )
  181.         return( NULL ) ;
  182.     
  183.     p = pathname ;
  184.     va_start( ap ) ;
  185.     for ( i = 0 ; i < count ; i++ )
  186.     {
  187.         s = va_arg( ap, char * ) ;
  188.         while ( *p++ = *s++ ) ;
  189.         *(p-1) = '/' ;            /* change '\0' to '/' */
  190.     }
  191.     *(p-1) = '\0' ;
  192.     return( pathname ) ;
  193. }
  194.  
  195.