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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /************************************************************************/
  4. /*                                                                      */
  5. /*  EXISTSX.C                                                           */
  6. /*                                                                      */
  7. /*  POSIX-compliant functions to verify the existance of files in the   */
  8. /*  PATH or in other environment variables.                             */
  9. /*                                                                      */
  10. /*  Original Copyright 1990-93 by Robert B. Stout as part of            */
  11. /*  the MicroFirm Function Library (MFL)                                */
  12. /*                                                                      */
  13. /*  The user is granted a free limited license to use this source file  */
  14. /*  to create royalty-free programs, subject to the terms of the        */
  15. /*  license restrictions specified in the LICENSE.MFL file.             */
  16. /*                                                                      */
  17. /************************************************************************/
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #if defined(MSDOS) || defined(__MSDOS__)
  23.  #include "unistd.h"
  24. #else
  25.  #include <unistd.h>
  26. #endif
  27. #include "snipfile.h"
  28. #include "snip_str.h"
  29.  
  30. /*
  31. **  exists()
  32. **
  33. **  See if a file exists.
  34. **
  35. **  Parameters: 1 - File name
  36. **
  37. **  Returns: True_ if found, else False_.
  38. **
  39. */
  40.  
  41. Boolean_T exists(char *name)
  42. {
  43.       return(Success_ == access(name, F_OK));
  44. }
  45.  
  46. /*
  47. **  dexists()
  48. **
  49. **  See if a file exists in an environment variable.
  50. **
  51. **  Parameters: 1 - File name
  52. **              2 - Environment variable name
  53. **
  54. **  Returns: The pathname of the file which was found or NULL if the file
  55. **           was not found.
  56. **
  57. **  Side effects: The returned path name is a static string. Subesequent
  58. **                calls to any of these [d/p/g]exists() functions will
  59. **                destroy the previous contents.
  60. **
  61. **  Notes: Uses stptok() and dos2unix(), also in SNIPPETS. May require
  62. **         strupr() from SNIPPETS if it's not in your standard library.
  63. */
  64.  
  65. char *dexists(char *name, char *envar)
  66. {
  67.       char *path, *ptr;
  68.       static char newname[FILENAME_MAX];
  69.       int i, j;
  70.       char chr;
  71.  
  72.       if (exists(name))
  73.             return(name);
  74.  
  75.       if (NULL == (path = getenv(strupr(envar))))
  76.             return(NULL);                       /* no environment var.  */
  77.  
  78.       dos2unix(path);
  79.       
  80.       do
  81.       {
  82.             path = stptok(path, newname, FILENAME_MAX, ";");
  83.             if (LAST_CHAR(newname) != '/')
  84.                   strcat(newname, "/");
  85.             strcat(newname, name);
  86.             if (exists(newname))
  87.                   return(newname);
  88.       } while (path && *path);
  89.  
  90.       return NULL;
  91. }
  92.  
  93. /*
  94. **  pexists()
  95. **
  96. **  See if a file exists in the PATH.
  97. **
  98. **  Parameters: 1 - File name
  99. **
  100. **  Returns: The pathname of the file which was found or NULL if the file
  101. **           was not found.
  102. **
  103. **  Side effects: The returned path name is a static string. Subesequent
  104. **                calls to any of these [d/p/g]exists() functions will
  105. **                destroy the previous contents.
  106. */
  107.  
  108. char *pexists(char *name)
  109. {
  110.       return dexists(name, "PATH");
  111. }
  112.  
  113. /*
  114. **  gexists()
  115. **
  116. **  See if a file exists in the PATH or in an environment variable.
  117. **
  118. **  Parameters: 1 - File name
  119. **              2 - Environment variable name
  120. **
  121. **  Returns: The pathname of the file which was found or NULL if the file
  122. **           was not found.
  123. **
  124. **  Side effects: The returned path name is a static string. Subesequent
  125. **                calls to any of these [d/p/g]exists() functions will
  126. **                destroy the previous contents.
  127. */
  128.  
  129. char *gexists(char *name, char *envar)
  130. {
  131.       char *tmp;
  132.  
  133.       if (exists(name))
  134.             return(name);
  135.       if (NULL != (tmp = dexists(name, envar)))
  136.             return(tmp);
  137.       return(pexists(name));
  138. }
  139.  
  140. #ifdef TEST
  141.  
  142. main(int argc, char *argv[])
  143. {
  144.       char *fname;
  145.  
  146.       switch (argc)
  147.       {
  148.       case 2:
  149.             fname = pexists(argv[1]);
  150.             if (fname)
  151.                   printf("%s found in PATH\n", fname);
  152.             else  printf("%s not found in PATH\n", argv[1]);
  153.             break;
  154.       case 3:
  155.             fname = gexists(argv[1], argv[2]);
  156.             if (fname)
  157.                   printf("%s found in PATH or %s\n", fname, argv[2]);
  158.             else  printf("%s not found in PATH or %s\n",
  159.                         argv[1], argv[2]);
  160.             break;
  161.       default:
  162.             puts("\aUsage: EXISTSX fname [envar]");
  163.             return EXIT_FAILURE;
  164.       }
  165.  
  166.       return EXIT_SUCCESS;
  167. }
  168.  
  169. #endif /* TEST */
  170.