Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / pcc / dist / pcc-libs / libI77 / util.c
blob3da5144bc381c99236837e6bb6539000984c111d
1 /* $Id: util.c,v 1.1.1.1 2008/08/24 05:34:48 gmcgarry Exp $ */
2 /*
3 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * Redistributions of source code and documentation must retain the above
10 * copyright notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditionsand the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed or owned by Caldera
17 * International, Inc.
18 * Neither the name of Caldera International, Inc. nor the names of other
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
22 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
39 #include <fcntl.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include "fio.h"
46 static void dcat(char *a,char *b);
48 #define DIRSIZE 14
50 void
51 g_char(char *a, ftnlen alen, char *b)
53 char *x=a+alen-1,*y=b+alen-1;
54 *(y+1)=0;
55 for(;x>=a && *x==' ';x--) *y--=0;
56 for(;x>=a;*y--= *x--);
59 void
60 b_char(char *a, char *b, ftnlen blen)
62 int i;
63 for(i=0;i<blen && *a!=0;i++) *b++= *a++;
64 for(;i<blen;i++) *b++=' ';
67 int
68 inode(char *a)
70 struct stat x;
71 if(stat(a,&x)<0) return(-1);
72 return(x.st_ino);
74 #define DONE {*bufpos++=0; close(file); return;}
75 #define INTBOUND (sizeof(int)-1)
76 #define register
78 static void
79 mvgbt(int n,int len,char *a,char *b)
80 { register int num=n*len;
81 if( ((int)a&INTBOUND)==0 && ((int)b&INTBOUND)==0 && (num&INTBOUND)==0 )
82 { register int *x=(int *)a,*y=(int *)b;
83 num /= sizeof(int);
84 if(x>y) for(;num>0;num--) *y++= *x++;
85 else for(num--;num>=0;num--) *(y+num)= *(x+num);
87 else
88 { register char *x=a,*y=b;
89 if(x>y) for(;num>0;num--) *y++= *x++;
90 else for(num--;num>=0;num--) *(y+num)= *(x+num);
93 static char *
94 curdir(void)
95 { char name[256],*bufpos = name;
96 struct stat x;
97 struct dirent y;
98 int file,i;
99 *bufpos++ = 0;
100 loop: stat(".",&x);
101 if((file=open("..",0))<0) goto done;
103 { if(read(file,&y,sizeof(y))<sizeof(y)) goto done;
104 } while(y.d_ino!=x.st_ino);
105 close(file);
106 if(y.d_ino!=2)
107 { dcat(name,y.d_name);
108 chdir("..");
109 goto loop;
111 if(stat(y.d_name,&x)<0 || chdir("/")<0
112 || (file=open("/",0))<0) goto done;
113 i=x.st_dev;
115 { if(read(file,&y,sizeof(y))<sizeof(y)) goto done;
116 if(y.d_ino==0) continue;
117 if(stat(y.d_name,&x)<0) goto done;
118 } while(x.st_dev!=i || (x.st_mode&S_IFMT)!=S_IFDIR);
119 if(strcmp(".",y.d_name) || strcmp("..",y.d_name))
120 dcat(name,y.d_name);
121 dcat(name,"/");
122 done:
123 bufpos=calloc(strlen(name)+1,1);
124 strcpy(bufpos,name);
125 chdir(name);
126 close(file);
127 return(bufpos);
130 void
131 dcat(char *a,char *b)
133 int i,j;
134 i=strlen(b);
135 j=strlen(a);
136 mvgbt(1,j+1,a,a+i+1);
137 mvgbt(1,i,b,a);
138 a[i]='/';
142 fullpath(char *a,char *b, int errflag)
144 char *a1,*a2,*npart,*dpart,*p;
145 a1=curdir();
146 npart=NULL;
147 for(p=a;*p!=0;p++)
148 if(*p=='/') npart=p;
149 if(npart==NULL)
150 { dpart=NULL;
151 npart=a;
153 else
154 { dpart=a;
155 *npart++ = 0;
157 if(dpart!=NULL)
158 { chdir(dpart);
159 a2=curdir();
160 strcpy(b,a2);
162 else
163 { a2=NULL;
164 strcpy(b, a1);
166 strcat(b,npart);
167 chdir(a1);
168 if(a1!=NULL)
169 { free(a1);
170 a1=NULL;
172 if(a2!=NULL)
173 { free(a2);
175 return(0);