File: path.c

package info (click to toggle)
joe 2.8-10
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,080 kB
  • ctags: 1,764
  • sloc: ansic: 18,800; asm: 224; makefile: 122; sh: 9
file content (387 lines) | stat: -rw-r--r-- 6,559 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* Directory and path functions
   Copyright (C) 1992 Joseph H. Allen

This file is part of JOE (Joe's Own Editor)

JOE is free software; you can redistribute it and/or modify it under the 
terms of the GNU General Public License as published by the Free Software 
Foundation; either version 1, or (at your option) any later version.  

JOE is distributed in the hope that it will be useful, but WITHOUT ANY 
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
details.  

You should have received a copy of the GNU General Public License along with 
JOE; see the file COPYING.  If not, write to the Free Software Foundation, 
675 Mass Ave, Cambridge, MA 02139, USA.  */ 

#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include "config.h"
#include "zstr.h"
#include "vs.h"
#include "va.h"
#include "tty.h"
#include "path.h"

#ifdef DIRENT
#include <dirent.h>
#else
#ifdef SYSDIRENT
#include <sys/dirent.h>
#else
#ifdef SYSDIR
#include <sys/dir.h>
#else
#ifdef BSDSYSDIR
#include <bsd/sys/dir.h>
#else
#ifndef __MSDOS__
#include "dir.c"
#endif
#endif
#endif
#endif
#endif

#ifdef junk
char *abspth(path)
char *path;
 {
 char *s=0;
 int x=0;
 int y;
 if(path[0]=='/')
  {
  s=vsadd(s,'/');
  while(path[x]=='/') ++x;
  y=1;
  }
 else
  {
  if(!(s=pwd())) return 0;
  s=vsncpy(NULL,0,sz(s));
  if(s[1]) s=vsadd(s,'/');
  y=sLEN(s);
  }
 while(path[x])
  {
  if(path[x]=='.' && (path[x+1]==0 || path[x+1]=='/'))
   {
   x+=1;
   while(path[x]=='/') ++x;
   continue;
   }
  if(path[x]=='.' && path[x+1]=='.' && (path[x+2]==0 || path[x+2]=='/'))
   {
   x+=2;
   while(path[x]=='/') ++x;
   if(y!=1)
    {
    --y;
    while(s[y-1]!='/') --y;
    }
   continue;
   }
  do
   s=vsset(s,y,path[x]), ++y, ++x;
   while(path[x] && path[x]!='/');
  s=vsset(s,y,'/'), ++y;
  while(path[x]=='/') x++;
  }
 if(y!=1 && s[y-1]=='/') --y;
 s=vstrunc(s,y);
 return s;
 }
#endif

char *ossep(path)
char *path;
 {
 int x;
 for(x=0;path[x];++x)
#ifdef __MSDOS__
  if(path[x]=='/') path[x]='\\';
#else
  if(path[x]=='\\') path[x]='/';
#endif
 return path;
 }

char *joesep(path)
char *path;
 {
 int x;
 for(x=0;path[x];++x) if(path[x]=='\\') path[x]='/';
 return path;
 }

char *namprt(path)
char *path;
 {
 char *z;
#ifdef __MSDOS__
 if(path[0] && path[1]==':') path+=2;
#endif
 z=path+slen(path);
 while(z!=path && z[-1]!='/') --z;
 return vsncpy(NULL,0,sz(z));
 }

char *namepart(tmp,path)
char *tmp;
char *path;
 {
 char *z;
#ifdef __MSDOS__
 if(path[0] && path[1]==':') path+=2;
#endif
 z=path+zlen(path);
 while(z!=path && z[-1]!='/') --z;
 return zcpy(tmp,z);
 }

char *dirprt(path)
char *path;
 {
 char *b=path;
 char *z=path+slen(path);
#ifdef __MSDOS__
 if(b[0] && b[1]==':') b+=2;
#endif
 while(z!=b && z[-1]!='/') --z;
 return vsncpy(NULL,0,path,z-path);
 }

char *begprt(path)
char *path;
 {
 char *z=path+slen(path);
 int drv=0;
#ifdef __MSDOS__
 if(path[0] && path[1]==':') drv=2;
#endif
 while(z!=path+drv && z[-1]=='/') --z;
 if(z==path+drv) return vsncpy(NULL,0,sz(path));
 else
  {
  while(z!=path+drv && z[-1]!='/') --z;
  return vsncpy(NULL,0,path,z-path);
  }
 }

char *endprt(path)
char *path;
 {
 char *z=path+slen(path);
 int drv=0;
#ifdef __MSDOS__
 if(path[0] && path[1]==':') drv=2;
#endif
 while(z!=path+drv && z[-1]=='/') --z;
 if(z==path+drv) return vsncpy(NULL,0,sc(""));
 else
  {
  while(z!=path+drv && z[-1]!='/') --z;
  return vsncpy(NULL,0,sz(z));
  }
 }

int mkpath(path)
char *path;
 {
 char *s;
 if(path[0]=='/')
  {
  if(chddir("/")) return 1;
  s=path;
  goto in;
  }
 while(path[0])
  {
  int c;
  for(s=path;*s && *s!='/';s++);
  c= *s; *s=0;
  if(chddir(path))
   {
   if(mkdir(path,0777)) return 1;
   if(chddir(path)) return 1;
   }
  *s=c;
  in:
  while(*s=='/') ++s;
  path=s;
  }
 return 0;
 }

/* Create a temporary file */

char *mktmp(where)
char *where;
 {
 static int seq=0;
 char *name;
 int fd;
 if(!where) where=getenv("TEMP");
#ifdef __MSDOS__
 if(!where) where="";
#else
 if(!where) where="/tmp";
#endif
 name=(char *)malloc(zlen(where)+16);
 loop:
 sprintf(name,"%s/J%d%d.tmp",where,seq= ++seq%1000,(unsigned)time(NULL)%1000);
 ossep(name);
 if((fd=open(name,O_RDONLY))!= -1)
  {
  close(fd);
  goto loop;
  }
 if((fd=creat(name,0666))== -1) return 0;
 else close(fd);
 return name;
 }

int rmatch(a,b)
char *a, *b;
 {
 int flag, inv, c;
 for(;;)
  switch(*a)
   {
  case '*': ++a;
            do if(rmatch(a,b)) return 1; while(*b++);
            return 0;

  case '[': ++a;
            flag=0;
            if(*a=='^') ++a, inv=1; else inv=0;
            if(*a==']') if(*b==*a++) flag=1;
            while(*a && (c= *a++)!=']')
             if(c=='-' && a[-2]!='[' && *a)
              { if(*b>=a[-2] && *b<=*a) flag=1; }
             else if(*b==c) flag=1;
            if((!flag && !inv) || (flag && inv) || !*b) return 0;
            ++b;
            break;

  case '?': ++a;
            if(!*b) return 0;
            ++b;
            break;

  case 0:   if(!*b) return 1;
            else return 0;

  default:  if(*a++!=*b++) return 0;
   }
 }

int isreg(s)
char *s;
 {
 int x;
 for(x=0;s[x];++x) if(s[x]=='*' || s[x]=='?' || s[x]=='[') return 1;
 return 0;
 }

#ifdef __MSDOS__

#include <dos.h>
#include <dir.h>

struct direct
 {
 char d_name[16];
 } direc;
int dirstate=0;
struct ffblk ffblk;
char *dirpath=0;

void *opendir(path)
char *path;
 {
 dirstate=0;
 return &direc;
 }

void closedir()
 {
 }

struct direct *readdir()
 {
 int x;
 if(dirstate)
  {
  if(findnext(&ffblk)) return 0;
  }
 else
  {
  if(findfirst("*.*",&ffblk,FA_DIREC))
   return 0;
  dirstate=1;
  }
 zcpy(direc.d_name,ffblk.ff_name);
 for(x=0;direc.d_name[x];++x) direc.d_name[x]=todn(direc.d_name[x]);
 return &direc;
 }

#endif

char **rexpnd(word)
char *word;
 {
 void *dir;
 char **lst=0;
#ifdef DIRENT
 struct dirent *de;
#else
#ifdef SYSDIRENT
 struct dirent *de;
#else
 struct direct *de;
#endif
#endif
 dir=opendir(".");
 if(dir)
  {
  while(de=readdir(dir))
   if(zcmp(".",de->d_name))
    if(rmatch(word,de->d_name))
     lst=vaadd(lst,vsncpy(NULL,0,sz(de->d_name)));
  closedir(dir);
  }
 return lst;
 }

int chpwd(path)
char *path;
 {
#ifdef __MSDOS__
 char buf[256];
 int x;
 if(!path) return 0;
 if(path[0] && path[1]==':')
  {
  if(_chdrive(path[0]&0x1F)) return -1;
  path+=2;
  }
 if(!path[0]) return 0;
 zcpy(buf,path);
 x=zlen(buf);
 while(x>1)
  {
  --x;
  if(buf[x]=='/' || buf[x]=='\\') buf[x]=0;
  else break;
  }
 return chdir(buf);
#else
 if(!path || !path[0]) return 0;
 return chdir(path);
#endif
 }