checkfilename.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*====================================================================*
  2. *
  3. * bool checkfilename (char const * pathname);
  4. *
  5. * files.h
  6. *
  7. * confirm that the filename portion of a pathname string contains
  8. * only letters, digits, periods, underscores and hyphens;
  9. *
  10. * this prevents users from entering an Ethernet address where a
  11. * filename should appear on the command line; Ethernet addresses
  12. * are also valid filenames;
  13. *
  14. * Motley Tools by Charles Maier;
  15. * Copyright (c) 2001-2006 by Charles Maier Associates;
  16. * Licensed under the Internet Software Consortium License;
  17. *
  18. *--------------------------------------------------------------------*/
  19. #ifndef CHECKFILENAME_SOURCE
  20. #define CHECKFILENAME_SOURCE
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include "../tools/files.h"
  24. bool checkfilename (char const * pathname)
  25. {
  26. char const * filename = pathname;
  27. while (*pathname)
  28. {
  29. if ((*pathname == '/') || (*pathname == '\\'))
  30. {
  31. filename = pathname + 1;
  32. }
  33. pathname++;
  34. }
  35. while (isalnum (*filename) || (*filename == '.') || (*filename == '_') || (*filename == '-'))
  36. {
  37. filename++;
  38. }
  39. return (*filename == (char) (0));
  40. }
  41. #endif