home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / tcpipsrc / FTP / c / pathent < prev   
Text File  |  1995-01-16  |  19KB  |  816 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #include "bbc.h"
  7. #include "os.h"
  8. #include "swis.h"
  9.  
  10. #include "pathent.h"
  11.  
  12. #define PATH_OFF  ((pathent_str *)0)
  13. #define PATH_DEF  ((pathent_str *)1)
  14. #define PATH_TYPE ((pathent_str *)2)
  15.  
  16. #define u_int unsigned int
  17.  
  18. /* option priorities 
  19.  */
  20. #define cmd_DEFAULT  1
  21. #define cmd_END      2
  22.  
  23. #define str_EXTS     0
  24. #define str_CURRENT  1
  25. #define str_DEFAULT  2
  26. #define str_ROOT     3
  27. #define str_END      4
  28.  
  29. #define flg_NOCASE     (1<<0)
  30. #define flg_TRUNCDIR   (1<<1)
  31. #define flg_TRUNCATE   (1<<2)
  32. #define flg_KEEPEXT    (1<<3)
  33. #define flg_NOEXT      (1<<4)
  34. #define flg_REVEXT     (1<<5)
  35. #define flg_REV1EXT    (1<<6)
  36. #define flg_NOPATH     (1<<7)
  37. #define flg_UNIQUE     (1<<8)
  38. #define flg_CONVNFS    (1<<9)
  39. #define flg_TYPEEXT    (1<<10)
  40. #define flg_NODIREXT   (1<<11)
  41. #define flg_CONVDIREXT (1<<12)
  42. #define flg_OFF        (1U<<31U)  /* opt off */
  43.  
  44. #define dir_ROOT   1
  45. #define dir_IN     2
  46. #define dir_TYPE   3
  47.  
  48. #define ftpcliopt_truncdir   (opts & flg_TRUNCDIR)    /* Truncate dir elements */
  49. #define ftpcliopt_truncname  (opts & flg_TRUNCATE)    /* Truncate name element */
  50. #define ftpcliopt_revext     (opts & flg_REVEXT)      /* Reverse extensions */
  51. #define ftpcliopt_keepext    (opts & flg_KEEPEXT)     /* Keep extensions as "/" */
  52. #define ftpcliopt_noext      (opts & flg_NOEXT)       /* Ditch extensions */
  53. #define ftpcliopt_nopath     (opts & flg_NOPATH)      /* Ditch path */
  54. #define ftpcliopt_typeext    (opts & flg_TYPEEXT)     /* Loose last extension */
  55. #define ftpcliopt_convdirext (opts & flg_CONVDIREXT)  /* Convert dir extentions */
  56. #define ftpcliopt_nodirext   (opts & flg_NODIREXT)    /* Loose dir extentions */
  57. #define ftpcliopt_convnfs    (opts & flg_CONVNFS)     /* Convert NFS extensions */
  58. #define ftpcliopt_rev1ext    (opts & flg_REV1EXT)     /* Reverse 1st extension */
  59. #define ftpcliopt_nocase     (opts & flg_NOCASE)      /* Reverse 1st extension */
  60.  
  61. typedef struct optcmd_str
  62. {
  63.   char *cmd;
  64.   u_int opt;
  65. } optcmd_str;
  66.  
  67. static optcmd_str flgopts[] = 
  68. {
  69.   "nocase",     flg_NOCASE,
  70.   "truncdir",   flg_TRUNCDIR,
  71.   "truncate",   flg_TRUNCATE,
  72.   "keepext",    flg_KEEPEXT,
  73.   "noext",      flg_NOEXT,
  74.   "revext",     flg_REVEXT,
  75.   "rev1ext",    flg_REV1EXT,
  76.   "nopath",     flg_NOPATH,
  77.   "unique",     flg_UNIQUE,
  78.   "nfsext",     flg_CONVNFS,
  79.   "nodirext",   flg_NODIREXT,
  80.   "convdirext", flg_CONVDIREXT,
  81.   NULL, 0   
  82. };
  83.  
  84. static optcmd_str cmdopts[] = 
  85. {
  86.   "default",  cmd_DEFAULT,
  87.   "end",      cmd_END,
  88.   NULL, 0
  89. };
  90.  
  91. static optcmd_str diropts[] = 
  92. {
  93.   "root",     dir_ROOT,
  94.   "in",       dir_IN,
  95.   "type",     dir_TYPE,
  96.   NULL, 0
  97. };
  98.  
  99. static char *strdup(char *s);
  100. static int star(char *s, char *p, char **argv, int i);
  101. static int pe_wildmat(char *s, char *p, char **argv, int i);
  102. static u_int getoptcmd(optcmd_str *opts, char *s);
  103.  
  104. static pathent_str *pathhead = NULL;
  105. static pathent_str *pathtail = NULL;
  106.  
  107. /*
  108.  * This array is designed for mapping upper and lower case letter
  109.  * together for a case independent comparison.  The mappings are
  110.  * based upon ascii character sequences.
  111.  */
  112. static char charmap[] =
  113. {
  114.   '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
  115.   '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
  116.   '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
  117.   '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
  118.   '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
  119.   '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
  120.   '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
  121.   '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
  122.   '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
  123.   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
  124.   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
  125.   '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
  126.   '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
  127.   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
  128.   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
  129.   '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
  130.   '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
  131.   '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
  132.   '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
  133.   '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
  134.   '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
  135.   '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
  136.   '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
  137.   '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
  138.   '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
  139.   '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
  140.   '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
  141.   '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
  142.   '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
  143.   '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
  144.   '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  145.   '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
  146. };
  147.  
  148. #define fixc(a, b) ((b)?charmap[a]:a)
  149.  
  150. static char *strdup(char *s)
  151. {
  152.   char *p;
  153.   if (s==NULL)
  154.     return NULL;
  155.  
  156.   if (p=(char *)malloc(strlen(s)+1), p==NULL)
  157.     return NULL;
  158.  
  159.   strcpy(p, s);
  160.   return p;
  161. }
  162.  
  163. static int star(char *s, char *p, char **argv, int i)
  164. {
  165.   char *cp = s;
  166.  
  167.   while (pe_wildmat(cp, p, argv, i) == FALSE)
  168.   {
  169.     if (*++cp == '\0')
  170.       return -1;
  171.   }
  172.   return ((int) (cp - s));
  173. }
  174.  
  175. static int pe_wildmat(char *s, char *p, char **argv, int i)
  176. {
  177.   int last;
  178.   int matched;
  179.   int reverse;
  180.   int cnt;
  181.  
  182.   for (; *p; s++,p++)
  183.   {
  184.     switch(*p)
  185.     {
  186.       case '\\':
  187.         /* Literal match with following character; fall through. */
  188.         p++;
  189.       default:
  190.         if (fixc(*s,i) != fixc(*p,i))
  191.           return FALSE;
  192.         continue;
  193.       case '?':
  194.         /* Match anything. */
  195.         if (*s == '\0')
  196.           return FALSE;
  197.         continue;
  198.       case '*':
  199.         /* Trailing star matches everything. */
  200.         if (argv == NULL)
  201.           return *++p ? 1 + star(s, p, NULL, i) : TRUE;
  202.         if (*++p == '\0')
  203.         {
  204.           cnt = strlen(s);
  205.         }
  206.         else
  207.         {
  208.           if ((cnt = star(s, p, argv+1, i)) == -1)
  209.             return FALSE;
  210.         }
  211.         *argv = malloc(cnt+1);
  212.         strncpy(*argv,s,cnt);
  213.         *(*argv + cnt) = '\0';
  214.         return TRUE;
  215.       case '[':
  216.         /* [^....] means inverse character class. */
  217.         reverse = (p[1] == '^') ? TRUE : FALSE;
  218.         if (reverse)
  219.           p++;
  220.         for (last = 0400, matched = FALSE; *++p && *p != ']'; last = *p)
  221.         {
  222.           /* This next line requires a good C compiler. */
  223.           if ( (*p=='-') ? 
  224.                fixc(*s, i) <= fixc(*++p, i) && fixc(*s, i) >= fixc(last, i) : 
  225.                fixc(*s, i) == fixc(*p, i))
  226.             matched = TRUE;
  227.         }
  228.         if (matched == reverse)
  229.           return FALSE;
  230.         continue;
  231.     }
  232.   }
  233.   /* For "tar" use, matches that end at a slash also work. --hoptoad!gnu */
  234.   return *s == '\0' || *s == '/';
  235. }
  236.  
  237. /*
  238.  * Scan command table, and return matching flag value
  239.  * If an option if prefixed with '!', return flag value
  240.  * with flg_OFF set.
  241.  */
  242. static u_int getoptcmd(optcmd_str *opts, char *s)
  243. {
  244.   u_int off;
  245.   int i = 0;
  246.  
  247.   if (*s=='!')
  248.   {
  249.     s++;
  250.     off = flg_OFF;
  251.   }
  252.   else
  253.     off = 0;
  254.     
  255.   while (opts[i].cmd!=NULL && strcmp(opts[i].cmd, s))
  256.     i++;
  257.  
  258.   return (opts[i].cmd==NULL) ? 0 : opts[i].opt | off;
  259. }
  260.  
  261.  
  262. pathent_str *getpathent(char *s)
  263. {
  264.   char *p;
  265.   char *list = NULL;
  266.   u_int n;
  267.   u_int on = 0;
  268.   u_int off = 0;
  269.   int type = -1;
  270.   int str;
  271.   char *path = NULL;
  272.   char *exts[32];
  273.   int nexts = 0;
  274.   pathent_str *pathent = NULL;
  275.  
  276.   /* $ prefixes options line, so no extensions info present */
  277.   if (*s!='$')
  278.   {
  279.     /* Get list of extentions */
  280.     if (list = strtok(s, " \t\n"), list==NULL)
  281.       return NULL;
  282.     
  283.     s = NULL;
  284.  
  285.     /* Get associated type */
  286.     if (p = strtok(s, " \t\n"), p==NULL)
  287.       return NULL;
  288.  
  289.     type = atoft(p);
  290.     str = s