Commit | Line | Data |
---|---|---|
8662e665 PW |
1 | #include <sys/types.h> |
2 | #include <sys/stat.h> | |
3 | #include <sys/dir.h> | |
4 | #include "fio.h" | |
5 | #define DIRSIZE 14 | |
6 | g_char(a,alen,b) char *a,*b; ftnlen alen; | |
7 | { char *x=a+alen-1,*y=b+alen-1; | |
8 | *(y+1)=0; | |
9 | for(;x>=a && *x==' ';x--) *y--=0; | |
10 | for(;x>=a;*y--= *x--); | |
11 | } | |
12 | b_char(a,b,blen) char *a,*b; ftnlen blen; | |
13 | { int i; | |
14 | for(i=0;i<blen && *a!=0;i++) *b++= *a++; | |
15 | for(;i<blen;i++) *b++=' '; | |
16 | } | |
17 | inode(a) char *a; | |
18 | { struct stat x; | |
19 | if(stat(a,&x)<0) return(-1); | |
20 | return(x.st_ino); | |
21 | } | |
22 | #define DONE {*bufpos++=0; close(file); return;} | |
23 | #define INTBOUND sizeof(int)-1 | |
24 | #define register | |
25 | mvgbt(n,len,a,b) char *a,*b; | |
26 | { register int num=n*len; | |
27 | if( ((int)a&INTBOUND)==0 && ((int)b&INTBOUND)==0 && (num&INTBOUND)==0 ) | |
28 | { register int *x=(int *)a,*y=(int *)b; | |
29 | num /= sizeof(int); | |
30 | if(x>y) for(;num>0;num--) *y++= *x++; | |
31 | else for(num--;num>=0;num--) *(y+num)= *(x+num); | |
32 | } | |
33 | else | |
34 | { register char *x=a,*y=b; | |
35 | if(x>y) for(;num>0;num--) *y++= *x++; | |
36 | else for(num--;num>=0;num--) *(y+num)= *(x+num); | |
37 | } | |
38 | } | |
39 | char *curdir() | |
40 | { char name[256],*bufpos = name; | |
41 | struct stat x; | |
42 | struct direct y; | |
43 | int file,i; | |
44 | *bufpos++ = 0; | |
45 | loop: stat(".",&x); | |
46 | if((file=open("..",0))<0) goto done; | |
47 | do | |
48 | { if(read(file,&y,sizeof(y))<sizeof(y)) goto done; | |
49 | } while(y.d_ino!=x.st_ino); | |
50 | close(file); | |
51 | if(y.d_ino!=2) | |
52 | { dcat(name,y.d_name); | |
53 | chdir(".."); | |
54 | goto loop; | |
55 | } | |
56 | if(stat(y.d_name,&x)<0 || chdir("/")<0 | |
57 | || (file=open("/",0))<0) goto done; | |
58 | i=x.st_dev; | |
59 | do | |
60 | { if(read(file,&y,sizeof(y))<sizeof(y)) goto done; | |
61 | if(y.d_ino==0) continue; | |
62 | if(stat(y.d_name,&x)<0) goto done; | |
63 | } while(x.st_dev!=i || (x.st_mode&S_IFMT)!=S_IFDIR); | |
64 | if(strcmp(".",y.d_name) || strcmp("..",y.d_name)) | |
65 | dcat(name,y.d_name); | |
66 | dcat(name,"/"); | |
67 | done: | |
68 | bufpos=calloc(strlen(name)+1,1); | |
69 | strcpy(bufpos,name); | |
70 | chdir(name); | |
71 | close(file); | |
72 | return(bufpos); | |
73 | } | |
74 | dcat(a,b) char *a,*b; | |
75 | { | |
76 | int i,j; | |
77 | i=strlen(b); | |
78 | j=strlen(a); | |
79 | mvgbt(1,j+1,a,a+i+1); | |
80 | mvgbt(1,i,b,a); | |
81 | a[i]='/'; | |
82 | } | |
83 | fullpath(a,b,errflag) char *a,*b; | |
84 | { | |
85 | char *a1,*a2,*npart,*dpart,*p; | |
86 | a1=curdir(); | |
87 | npart=NULL; | |
88 | for(p=a;*p!=0;p++) | |
89 | if(*p=='/') npart=p; | |
90 | if(npart==NULL) | |
91 | { dpart=NULL; | |
92 | npart=a; | |
93 | } | |
94 | else | |
95 | { dpart=a; | |
96 | *npart++ = 0; | |
97 | } | |
98 | if(dpart!=NULL) | |
99 | { chdir(dpart); | |
100 | a2=curdir(); | |
101 | strcpy(b,a2); | |
102 | } | |
103 | else | |
104 | { a2=NULL; | |
105 | strcpy(b, a1); | |
106 | } | |
107 | strcat(b,npart); | |
108 | chdir(a1); | |
109 | if(a1!=NULL) | |
110 | { free(a1); | |
111 | a1=NULL; | |
112 | } | |
113 | if(a2!=NULL) | |
114 | { free(a2); | |
115 | } | |
116 | return(0); | |
117 | } |