home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / VFNAME.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  6KB  |  197 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  valid_fname.c AKA vfname.c (revised)
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "filnames.h"
  10. #include "vfname.h"
  11.  
  12.  
  13. /*
  14. **  valid_fname.c
  15. **
  16. **  Verifies whether a filename is valid or invalid without
  17. **  altering the passed filename itself.
  18. **
  19. **  Arguments: 1 - fname = a filename with no path or drive
  20. **             2 - wild_check: 0 means wildcard use okay
  21. **                             any other value means test for
  22. **                             wildcards which are not acceptable
  23. **
  24. **  Returns:   Error_   - fname is invalid
  25. **             Success_ - fname is valid
  26. **
  27. **  Side effects: none
  28. **
  29. **  Notes: Space, ASCII character 32, is a special case. Dos will
  30. **         write a filename or volume label that includes a space.
  31. **         Getting access to that file, afterwards, is not always
  32. **         easy :) For my purposes, space is an invalid filename
  33. **         token. You? You're on your own :)
  34. **
  35. **         High-bit-set or embedded control characters are not handled
  36. **         in this version.
  37. **
  38. **  Revisions: 1) Dropped str2upper after comment by David Johnson
  39. **                on 07-17-93
  40. **             2) Added [] to token list after comment by Ed
  41. **                Kowalski on 07-17-93
  42. **             3) Added lpt1-lpt3, com1-com4 and clock$ to
  43. **                invalid name list after comment by Ed
  44. **                Kowalski on 07-17-93
  45. **             4) Eliminated double exit points after my own
  46. **                comment to Bob Stout :)
  47. **             5) Major revision to clean up code and add parameterized
  48. **                filename and format validation
  49. **
  50. **  Public domain by Sid Rogers on 07/22/1993,
  51. **                   Bob Stout 11-Sep-95
  52. */
  53.  
  54. Boolean_T valid_fname(const char *fname, Boolean_T wild_check)
  55. {
  56.       /*
  57.       ** Invalid filename tokens - path separators first, wildcards last
  58.       */
  59.  
  60.       static char *invalid_tokens=
  61.             (PATH_SEP_CHARS INVALID_CHARS WILDCARD_CHARS);
  62.  
  63.       /*
  64.       ** Invalid file names -- even with extension .xxx
  65.       */
  66.  
  67.       static char *invalid_3lnam[] = DEVICE_LIST_3;
  68.       static char *invalid_4lnam[] = DEVICE_LIST_4;
  69.       static char *invalid_6lnam[] = DEVICE_LIST_6;
  70.  
  71.       int num_vals;
  72.       int fl = strlen(fname);
  73.       int i, j, k;
  74.       char *ptr;
  75.  
  76.       if (wild_check)               /* Using wildcards invalid if TRUE  */
  77.             num_vals = strlen(invalid_tokens);
  78.       else  num_vals = strlen(invalid_tokens) - strlen(WILDCARD_CHARS);
  79.  
  80.       for (i = 0; NULL != invalid_3lnam[i]; ++i)
  81.       {
  82.             if (Success_ == strncmp(invalid_3lnam[i], fname, 3))
  83.                   return Error_;
  84.       }
  85.  
  86.       for (i = 0; NULL != invalid_4lnam[i]; ++i)
  87.       {
  88.             if (Success_ == strncmp(invalid_4lnam[i], fname, 4))
  89.                   return Error_;
  90.       }
  91.  
  92.       for (i = 0; NULL != invalid_6lnam[i]; ++i)
  93.       {
  94.             if (Success_ == strncmp(invalid_6lnam[i], fname, 6))
  95.                   return Error_;
  96.       }
  97.  
  98.       /* Process filename for invalid tokens    */
  99.  
  100.       for (i = -1, j = 0; i < 0 && j <= num_vals; j++)
  101.       {
  102.             for (k = 0; k < fl; k++)
  103.             {
  104.                   if (invalid_tokens[j] == fname[k])
  105.                         i = j;
  106.             }
  107.             if (i >= 0)
  108.                   return Error_;
  109.       }
  110.  
  111.       /*
  112.       ** OK so far, check format
  113.       */
  114.  
  115.       if (fl > FNAME_MAX)
  116.             return Error_;
  117.  
  118.       for (i = 0; i < strlen(PATH_SEP_CHARS); ++i)
  119.       {
  120.             if (NULL != strchr(fname, invalid_tokens[i]))
  121.                   return Error_;
  122.       }
  123.       
  124.       if (NULL == (ptr = strchr(fname, '.')))
  125.       {
  126.             if (fl > FNAME_MAX)
  127.                   return Error_;
  128.       }
  129.       else
  130.       {
  131.             if ((ptr - (char *)fname) > FNAME_SIZE_MAX)
  132.                   return Error_;
  133.  
  134. #if FNAME_SIZE_MIN
  135.  
  136.             if ((ptr - (char *)fname) < FNAME_SIZE_MIN)
  137.                   return Error_;
  138.  
  139. #endif
  140.  
  141.             ++ptr;                              /* Skip the period      */
  142.  
  143.             if (!MULTIPLE_EXTS && NULL != strchr(ptr, '.'))
  144.                   return Error_;
  145.  
  146.             if (strlen(ptr) > FEXT_SIZE_MAX)
  147.                   return Error_;
  148.  
  149. #if FEXT_SIZE_MIN
  150.  
  151.             if (strlen(ptr) < FEXT_SIZE_MIN)
  152.                   return Error_;
  153. #endif
  154.  
  155.       }
  156.  
  157.       return Success_;
  158. }
  159.  
  160. #ifdef TEST
  161.  
  162. main()
  163. {
  164.       static char *name_test[]= {"aaa","aux","con","prn","nul","lpt1",
  165.                                     "lpt2","lpt3","com1","com2","com3",
  166.                                     "com4","bbbb","clock$","cccccc",
  167.                                     "dev/nul","clock$.ext",NULL};
  168.       static char *token_test[]={"00fname.","01 fname","02fname,",
  169.                                     "03fname[","04fname;","05fname:",
  170.                                     "06fname|","07fname/","08fname<",
  171.                                     "09fname>","10fname+","11fname=",
  172.                                     "12fname\\","13fname\"","14fname]",
  173.                                     "15fname*","16fname?","filename.17",
  174.                                     "filename.018","19longfilename",
  175.                                     "20.longext","21.mlt.exts","a.22",
  176.                                     ".23","fname24.",NULL};
  177.       register int i;
  178.  
  179.       puts("Names test");
  180.       for (i = 0; (name_test[i]); ++i)
  181.       {
  182.             printf("%6s is %s\n", name_test[i],
  183.                   valid_fname(name_test[i], 0) ? "INvalid" : "Valid");
  184.       }
  185.  
  186.       puts("\nTokens test");
  187.       for (i = 0; (token_test[i]); ++i)
  188.       {
  189.             printf("%s is %s\n", token_test[i],
  190.                   valid_fname(token_test[i], 1) ? "INvalid" : "Valid");
  191.       }
  192.  
  193.       return 0;
  194. }
  195.  
  196. #endif /* TEST */
  197.