home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / src / wildcards.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-10  |  3.4 KB  |  129 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: wildcards.c,v 5.5 1992/12/11 01:45:04 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.5 $   $State: Exp $
  6.  *
  7.  *            Copyright (c) 1988-1992 USENET Community Trust
  8.  *            Copyright (c) 1986,1987 Dave Taylor
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log: wildcards.c,v $
  17.  * Revision 5.5  1992/12/11  01:45:04  syd
  18.  * remove sys/types.h include, it is now included by defs.h
  19.  * and this routine includes defs.h or indirectly includes defs.h
  20.  * From: Syd
  21.  *
  22.  * Revision 5.4  1992/12/07  05:00:39  syd
  23.  * Add include of sys/types.h for time_t
  24.  * From: Syd
  25.  *
  26.  * Revision 5.3  1992/10/31  18:59:24  syd
  27.  * Prevent index underflow when wildchar is in first three chars of string
  28.  * From: Syd via note from gwh@dogmatix.inmos.co.uk
  29.  *
  30.  * Revision 5.2  1992/10/11  01:18:23  syd
  31.  * Fix where a SHELL=sh would cause a segv due to no / in
  32.  * the shell name.
  33.  * From: Syd
  34.  *
  35.  * Revision 5.1  1992/10/03  22:58:40  syd
  36.  * Initial checkin as of 2.4 Release at PL0
  37.  *
  38.  *
  39.  ******************************************************************************/
  40.  
  41. /*
  42.  * Wildcard handling module for elm.
  43.  */
  44.  
  45. #include <stdio.h>
  46. #include "defs.h"
  47.  
  48. /*
  49.  * Common wildcards.  Note that we count space as a wildcard, for
  50.  * concatenation of individual unwildcarded file names.
  51.  */
  52.  
  53. static char    sh_wildcards[] =   "[]*? " ;
  54. static char    csh_wildcards[] =  "[]*?{} " ;
  55.  
  56. /* List of known shells and their wildcards.  Expand as needed. */
  57.  
  58. static struct    wildcard_list
  59. {
  60.     char    *shell_name ;
  61.     char    *wildcard_chars ;
  62. } known_shells[] = {
  63.     {  "csh", csh_wildcards    },
  64.     {  "sh", sh_wildcards },
  65.     {  "ksh", sh_wildcards },
  66.     {  "tcsh", csh_wildcards },
  67.     {  "bash", sh_wildcards },
  68.     {  "zsh", sh_wildcards },
  69.     {  NULL, sh_wildcards }
  70. } ;
  71.  
  72.  
  73. /*
  74.  * Function: has_wildcards.
  75.  *
  76.  * Scans the incoming file name for wildcard characters, returns
  77.  * true if they are there and false otherwise.  Watch for escapes
  78.  * (ie, preceeding backslash).
  79.  *
  80.  * One tricky part here -- different shells have different wildcarding
  81.  * characters (yuck).  We need to examine the shell used, and set up
  82.  * the right list.  If the shell isn't one we're familiar with, assume
  83.  * Bourne shell.
  84.  */
  85.  
  86.  
  87. int
  88. has_wildcards( name )
  89. char *name ;
  90. {
  91.     static char        *user_wildcards = NULL ;
  92.     char            *wildchar ;
  93.     struct wildcard_list     *wptr ;
  94.     char            *user_shell ;
  95.     char            *sh_name ;
  96.  
  97.     if ( NULL == user_wildcards )    /* Determine wildcards only once */
  98.     {
  99.         if ( NULL == ( user_shell = getenv( "SHELL" ) ) )
  100.             user_wildcards = sh_wildcards ;
  101.         else
  102.         {
  103.             if ( NULL != ( sh_name = rindex( user_shell, '/' ) ) )
  104.                 sh_name++ ;
  105.             else
  106.                 sh_name = user_shell;
  107.  
  108.             for ( wptr = known_shells ; ( wptr->shell_name != NULL ) ; wptr++ )
  109.                 if ( 0 == strcmp( wptr->shell_name, sh_name ) )
  110.                     break ;
  111.             user_wildcards = wptr->wildcard_chars ;
  112.         }
  113.     }
  114.  
  115.     /* If unescaped wildcard chars found, return true */
  116.  
  117.     for ( wildchar = name ; NULL != ( wildchar = strpbrk( wildchar, user_wildcards ) ) ; wildchar++ )
  118.     {
  119.         if ( *( wildchar - 1 ) != '\\' ) {
  120.             if (wildchar - name < 3)
  121.                 return TRUE;
  122.             /* elm predoubles the backslashes */
  123.             if ( *( wildchar - 3 ) != '\\' )
  124.                 return TRUE ;
  125.         }
  126.     }
  127.     return FALSE ;
  128. }
  129.