source: vis_dev/glu-2.3/src/util/pathsearch.c @ 13

Last change on this file since 13 was 13, checked in by cecile, 13 years ago

library glu 2.3

File size: 2.6 KB
Line 
1/*
2 * Revision Control Information
3 *
4 * $Id: pathsearch.c,v 1.8 2009/01/05 21:04:04 fabio Exp $
5 *
6 */
7/* LINTLIBRARY */
8
9#if HAVE_SYS_FILE_H
10#  include <sys/file.h>
11#endif
12
13#if HAVE_SYS_STAT_H
14#  include <sys/stat.h>
15#endif
16
17#include "util.h"
18
19/**Function********************************************************************
20
21  Synopsis           [ Check that a given file is present and accessible ]
22
23  SideEffects        [none]
24******************************************************************************/
25int
26util_check_file(char const *filename, char const *mode)
27{
28#if defined(HAVE_SYS_STAT_H)
29    struct stat stat_rec;
30    int access_char = mode[0];
31    int access_mode = R_OK;
32
33    /* First check that the file is a regular file. */
34
35    if (stat(filename,&stat_rec) == 0 && S_ISREG(stat_rec.st_mode)) {
36        if (access_char == 'w') {
37            access_mode = W_OK;
38        } else if (access_char == 'x') {
39            access_mode = X_OK;
40        }
41        return access(filename,access_mode) == 0;
42    }
43    return 0;
44
45#else
46
47    FILE *fp;
48    int got_file;
49
50    if (strcmp(mode, "x") == 0) {
51        mode = "r";
52    }
53    fp = fopen(filename, mode);
54    got_file = (fp != 0);
55    if (fp != 0) {
56        (void) fclose(fp);
57    }
58    return got_file;
59
60#endif
61}
62
63/**Function********************************************************************
64
65  Synopsis           [ Search for a program in all possible paths ]
66
67  SideEffects        [none]
68
69******************************************************************************/
70char *
71util_path_search(char const *prog)
72{
73#ifdef HAVE_GETENV
74    return util_file_search(prog, getenv("PATH"), "x");
75#else
76    return util_file_search(prog, NIL(char), "x");
77#endif
78}
79
80char *
81util_file_search(
82  char const *file      /* file we're looking for */,
83  char *path            /* search path, colon separated */,
84  char const *mode      /* "r", "w", or "x" */)
85{
86    int quit;
87    char *buffer, *filename, *save_path, *cp;
88
89    if (path == 0 || strcmp(path, "") == 0) {
90        path = ".";             /* just look in the current directory */
91    }
92
93    save_path = path = util_strsav(path);
94    quit = 0;
95    do {
96        cp = strchr(path, ':');
97        if (cp != 0) {
98            *cp = '\0';
99        } else {
100            quit = 1;
101        }
102
103        /* cons up the filename out of the path and file name */
104        if (strcmp(path, ".") == 0) {
105            buffer = util_strsav(file);
106        } else {
107            buffer = ALLOC(char, strlen(path) + strlen(file) + 4);
108            (void) sprintf(buffer, "%s/%s", path, file);
109        }
110        filename = util_tilde_expand(buffer);
111        FREE(buffer);
112
113        /* see if we can access it */
114        if (util_check_file(filename, mode)) {
115            FREE(save_path);
116            return filename;
117        }
118        FREE(filename);
119        path = ++cp;
120    } while (! quit); 
121
122    FREE(save_path);
123    return 0;
124}
Note: See TracBrowser for help on using the repository browser.