~ubuntu-branches/ubuntu/jaunty/graphviz/jaunty

« back to all changes in this revision

Viewing changes to tools/ast/pathfind.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#pragma prototyped
 
2
/*
 
3
 * Glenn Fowler
 
4
 * AT&T Research
 
5
 *
 
6
 * include style search support
 
7
 */
 
8
 
 
9
#include <ast.h>
 
10
#include <unistd.h>
 
11
#include <strings.h>
 
12
#include <string.h>
 
13
 
 
14
typedef struct Dir_s                    /* directory list element       */
 
15
{
 
16
        struct Dir_s*   next;           /* next in list                 */
 
17
        char            dir[1];         /* directory path               */
 
18
} Dir_t;
 
19
 
 
20
static struct                           /* directory list state         */
 
21
{
 
22
        Dir_t*          head;           /* directory list head          */
 
23
        Dir_t*          tail;           /* directory list tail          */
 
24
} state;
 
25
 
 
26
/*
 
27
 * append dir to pathfind() include list
 
28
 */
 
29
 
 
30
int
 
31
pathinclude(const char* dir)
 
32
{
 
33
        register Dir_t* dp;
 
34
 
 
35
        if (dir && *dir && !streq(dir, "."))
 
36
        {
 
37
                if (!(dp = oldof(0, Dir_t, 1, strlen(dir))))
 
38
                        return -1;
 
39
                strcpy(dp->dir, dir);
 
40
                if (state.tail)
 
41
                        state.tail = state.tail->next = dp;
 
42
                else
 
43
                        state.head = state.tail = dp;
 
44
        }
 
45
        return 0;
 
46
}
 
47
 
 
48
/*
 
49
 * return path to name using pathinclude() list
 
50
 * path placed in <buf,size>
 
51
 * if lib!=0 then pathpath() attempted after include search
 
52
 * if type!=0 and name has no '.' then file.type also attempted
 
53
 * any *: prefix in lib is ignored (discipline library dictionary support)
 
54
 */
 
55
 
 
56
char*
 
57
pathfind(const char* name, const char* lib, const char* type, char* buf, size_t size)
 
58
{
 
59
        register Dir_t*         dp;
 
60
        register char*          s;
 
61
        char                    tmp[PATH_MAX];
 
62
 
 
63
        if (access(name, R_OK) >= 0)
 
64
                return strncpy(buf, name, size);
 
65
        if (type)
 
66
        {
 
67
                sfsprintf(buf, size, "%s.%s", name, type);
 
68
                if (access(buf, R_OK) >= 0)
 
69
                        return buf;
 
70
        }
 
71
        if (*name != '/')
 
72
        {
 
73
                if (strchr(name, '.'))
 
74
                        type = 0;
 
75
                for (dp = state.head; dp; dp = dp->next)
 
76
                {
 
77
                        sfsprintf(tmp, sizeof(tmp), "%s/%s", dp->dir, name);
 
78
                        if (pathpath(buf, tmp, "", PATH_REGULAR))
 
79
                                return buf;
 
80
                        if (type)
 
81
                        {
 
82
                                sfsprintf(tmp, sizeof(tmp), "%s/%s.%s", dp->dir, name, type);
 
83
                                if (pathpath(buf, tmp, "", PATH_REGULAR))
 
84
                                        return buf;
 
85
                        }
 
86
                }
 
87
                if (lib)
 
88
                {
 
89
                        if (s = strrchr((char*)lib, ':'))
 
90
                                lib = (const char*)s + 1;
 
91
                        sfsprintf(tmp, sizeof(tmp), "lib/%s/%s", lib, name);
 
92
                        if (pathpath(buf, tmp, "", PATH_REGULAR))
 
93
                                return buf;
 
94
                        if (type)
 
95
                        {
 
96
                                sfsprintf(tmp, sizeof(tmp), "lib/%s/%s.%s", lib, name, type);
 
97
                                if (pathpath(buf, tmp, "", PATH_REGULAR))
 
98
                                        return buf;
 
99
                        }
 
100
                }
 
101
        }
 
102
        return 0;
 
103
}