~ubuntu-branches/ubuntu/quantal/vice/quantal

« back to all changes in this revision

Viewing changes to src/findpath.c

  • Committer: Bazaar Package Importer
  • Author(s): Zed Pobre
  • Date: 2005-03-27 13:06:04 UTC
  • mfrom: (4 hoary)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20050327130604-zodmv0i60dbbmcik
Tags: 1.16-3
Apply patch from Andreas Jochens <aj@andaco.de> to correct building on
AMD64 and GCC 4.x (closes: #300936)

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include <stdlib.h>
35
35
#include <string.h>
36
36
 
37
 
#ifdef __IBMC__
38
 
#include <direct.h>
39
 
#endif
40
 
 
41
 
#ifdef HAVE_IO_H
42
 
#include <io.h>
43
 
#endif
44
 
#ifdef HAVE_UNISTD_H
45
 
#include <unistd.h>
46
 
#endif
47
 
 
48
37
#include "archdep.h"
49
38
#include "findpath.h"
50
 
#include "utils.h"
 
39
#include "ioutil.h"
 
40
#include "lib.h"
 
41
 
51
42
 
52
43
/*
53
44
 * This function is checked to be robust with all path 3 types possible
56
47
 * Overflow testing for internal buffer is always done.
57
48
 */
58
49
 
59
 
char * findpath(const char *cmd, const char *syspath, int mode)
 
50
char *findpath(const char *cmd, const char *syspath, int mode)
60
51
{
61
 
    char * pd = NULL;
62
 
    char *c;
63
 
    char buf[MAXPATHLEN];
 
52
    char *pd = NULL;
 
53
    char *c, *buf;
 
54
    size_t maxpathlen;
 
55
 
 
56
    maxpathlen = (size_t)ioutil_maxpathlen();
 
57
 
 
58
    buf = lib_malloc(maxpathlen);
64
59
 
65
60
    buf[0] = '\0'; /* this will (and needs to) stay '\0' */
66
61
 
70
65
        const char *ps;
71
66
 
72
67
        if (archdep_path_is_relative(cmd)) {
73
 
            if (getcwd(buf + 1, sizeof(buf) - 128) == (int)NULL)
 
68
            if (ioutil_getcwd(buf + 1, (int)maxpathlen - 128) == NULL)
74
69
                goto fail;
75
70
 
76
71
            l = strlen(buf + 1);
78
73
            l = 0;
79
74
        }
80
75
 
81
 
        if (l + strlen(cmd) >= sizeof(buf) - 5)
 
76
        if (l + strlen(cmd) >= maxpathlen - 5)
82
77
            goto fail;
83
78
 
84
79
        ps = cmd;
94
89
        state = 1;
95
90
 
96
91
        /* delete extra `/./', '/../' and '//':s from the path */
97
 
        while (*ps)
98
 
        {
99
 
            switch (state)
100
 
            {
101
 
            case 0: if (*ps == '/') state = 1; else state = 0; break;
102
 
            case 1:
103
 
                if (*ps == '.') { state = 2; break; }
104
 
                if (*ps == '/') pd--; else state = 0; break;
105
 
            case 2:
106
 
                if (*ps == '/') { state = 1; pd -= 2; break; }
107
 
                if (*ps == '.') state = 3; else state = 0; break;
108
 
            case 3:
109
 
                if (*ps != '/') { state = 0; break; }
 
92
        while (*ps) {
 
93
            switch (state) {
 
94
              case 0:
 
95
                if (*ps == '/')
 
96
                    state = 1;
 
97
                else
 
98
                    state = 0;
 
99
                break;
 
100
              case 1:
 
101
                if (*ps == '.') {
 
102
                    state = 2;
 
103
                    break;
 
104
                }
 
105
                if (*ps == '/')
 
106
                    pd--;
 
107
                else
 
108
                    state = 0;
 
109
                break;
 
110
              case 2:
 
111
                if (*ps == '/') {
 
112
                    state = 1;
 
113
                    pd -= 2;
 
114
                    break;
 
115
                }
 
116
                if (*ps == '.')
 
117
                    state = 3;
 
118
                else
 
119
                    state = 0;
 
120
                break;
 
121
              case 3:
 
122
                if (*ps != '/') {
 
123
                    state = 0;
 
124
                    break;
 
125
                }
110
126
                state = 1;
111
127
                pd -= 4;
112
128
                while (*pd != '/' && *pd != '\0')
113
129
                    pd--;
114
 
                if (*pd == '\0') pd++;
 
130
                if (*pd == '\0')
 
131
                    pd++;
115
132
                state = 1;
116
133
                break;
117
134
            }
121
138
 
122
139
        *pd = '\0';
123
140
        pd = buf + 1;
124
 
    }
125
 
    else
126
 
    {
127
 
        const char * path = syspath;
128
 
        const char * s;
 
141
    } else {
 
142
        const char *path = syspath;
 
143
        const char *s;
129
144
        size_t cl = strlen(cmd) + 1;
130
145
 
131
 
        for (s = path; s; path = s + 1)
132
 
        {
 
146
        for (s = path; s; path = s + 1) {
133
147
            char * p;
134
148
            int l;
135
149
 
136
 
            s = strchr(path, FINDPATH_SEPARATOR_CHAR);
137
 
            l = s? (s - path): strlen(path);
 
150
            s = strchr(path, ARCHDEP_FINDPATH_SEPARATOR_CHAR);
 
151
            l = s ? (s - path) : (int)strlen(path);
138
152
 
139
 
            if (l + cl > sizeof buf - 5)
 
153
            if (l + cl > maxpathlen - 5)
140
154
                continue;
141
155
 
142
156
            memcpy(buf + 1, path, l);
148
162
 
149
163
            memcpy(p, cmd, cl);
150
164
 
151
 
            for(c= buf + 1; *c !='\0'; c++)
 
165
            for(c = buf + 1; *c != '\0'; c++)
152
166
#if defined (__MSDOS__) || defined (WIN32) || defined (__OS2__)
153
 
                if(*c=='/') *c='\\';
 
167
                if(*c == '/')
 
168
                    *c = '\\';
154
169
#else
155
 
                if(*c=='\\') *c='/';
 
170
                if(*c == '\\')
 
171
                    *c ='/';
156
172
#endif
157
 
            if (access(buf + 1, mode) == 0)
158
 
            {
 
173
            if (ioutil_access(buf + 1, mode) == 0) {
159
174
                pd = p /* + cl*/ ;
160
175
                break;
161
176
            }
163
178
    }
164
179
 
165
180
 
166
 
    if (pd)
167
 
    {
 
181
    if (pd) {
 
182
        char *tmpbuf;
168
183
#if 0
169
184
        do pd--;
170
185
        while (*pd != '/'); /* there is at least one '/' */
174
189
        *pd = '\0';
175
190
#endif
176
191
 
177
 
        return stralloc(buf + 1);
 
192
        tmpbuf = lib_stralloc(buf + 1);
 
193
        lib_free(buf);
 
194
        return tmpbuf;
178
195
    }
179
196
 fail:
 
197
    lib_free(buf);
180
198
    return NULL;
181
199
}
182
200