xref: /csrg-svn/usr.bin/f77/libU77/getcwd_.c (revision 3557)
1*3557Sdlw /** F77 NOTE: the getcwd() routine should be in libc.a ! **/
2*3557Sdlw /*
3*3557Sdlw  * return name of working (current) directory
4*3557Sdlw  */
5*3557Sdlw #include	<stdio.h>
6*3557Sdlw #include	<sys/param.h>
7*3557Sdlw #include	<sys/stat.h>
8*3557Sdlw #include	<sys/dir.h>
9*3557Sdlw 
10*3557Sdlw static char	dot[]	= ".";
11*3557Sdlw static char	dotdot[] = "..";
12*3557Sdlw static char	name[128];
13*3557Sdlw 
14*3557Sdlw char *
15*3557Sdlw getcwd()
16*3557Sdlw {
17*3557Sdlw 	int	rdev, rino;
18*3557Sdlw 	int	fd;
19*3557Sdlw 	struct	stat	d, dd;
20*3557Sdlw 	struct	direct	dir;
21*3557Sdlw 	char	*prepend();
22*3557Sdlw 	char	*namep = &name[(sizeof name)-1];
23*3557Sdlw 
24*3557Sdlw 	*namep = '\0';
25*3557Sdlw 	stat("/", &d);
26*3557Sdlw 	rdev = d.st_dev;
27*3557Sdlw 	rino = d.st_ino;
28*3557Sdlw 	for (;;)
29*3557Sdlw 	{
30*3557Sdlw 		stat(dot, &d);
31*3557Sdlw 		if (d.st_ino == rino && d.st_dev == rdev)
32*3557Sdlw 		{
33*3557Sdlw 			chdir(namep);
34*3557Sdlw 			return(namep);
35*3557Sdlw 		}
36*3557Sdlw 		if ((fd = open(dotdot,0)) < 0)
37*3557Sdlw 		{
38*3557Sdlw 			chdir(prepend(namep, dot));
39*3557Sdlw 			return((char *)0);
40*3557Sdlw 		}
41*3557Sdlw 		chdir(dotdot);
42*3557Sdlw 		fstat(fd, &dd);
43*3557Sdlw 		if(d.st_dev == dd.st_dev)
44*3557Sdlw 		{
45*3557Sdlw 			if(d.st_ino == dd.st_ino)
46*3557Sdlw 			{
47*3557Sdlw 				close(fd);
48*3557Sdlw 				chdir(namep);
49*3557Sdlw 				return(namep);
50*3557Sdlw 			}
51*3557Sdlw 			do
52*3557Sdlw 			{
53*3557Sdlw 				if (read(fd, (char *)&dir, sizeof(dir)) < sizeof(dir))
54*3557Sdlw 				{
55*3557Sdlw 					close(fd);
56*3557Sdlw 					chdir(prepend(namep, dot));
57*3557Sdlw 					return((char *)0);
58*3557Sdlw 				}
59*3557Sdlw 			} while (dir.d_ino != d.st_ino);
60*3557Sdlw 		}
61*3557Sdlw 		else do
62*3557Sdlw 		{
63*3557Sdlw 				if(read(fd, (char *)&dir, sizeof(dir)) < sizeof(dir))
64*3557Sdlw 				{
65*3557Sdlw 					close(fd);
66*3557Sdlw 					chdir(prepend(namep, dot));
67*3557Sdlw 					return((char *)0);
68*3557Sdlw 				}
69*3557Sdlw 				stat(dir.d_name, &dd);
70*3557Sdlw 			} while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
71*3557Sdlw 		close(fd);
72*3557Sdlw 		namep = prepend(prepend(namep, dir.d_name), "/");
73*3557Sdlw 	}
74*3557Sdlw }
75*3557Sdlw 
76*3557Sdlw char *
77*3557Sdlw prepend(p, n)
78*3557Sdlw char *p;
79*3557Sdlw char *n;
80*3557Sdlw {
81*3557Sdlw 	int i = 0;
82*3557Sdlw 
83*3557Sdlw 	while (i < DIRSIZ && *n)
84*3557Sdlw 	{
85*3557Sdlw 		n++; i++;
86*3557Sdlw 	}
87*3557Sdlw 	while (i--)
88*3557Sdlw 		*--p = *--n;
89*3557Sdlw 	return(p);
90*3557Sdlw }
91*3557Sdlw 
92*3557Sdlw /*
93*3557Sdlw char id_getcwd[] = "@(#)getcwd_.c	1.1";
94*3557Sdlw  * Get pathname of current working directory.
95*3557Sdlw  *
96*3557Sdlw  * calling sequence:
97*3557Sdlw  *	character*128 path
98*3557Sdlw  *	ierr = getcwd(path)
99*3557Sdlw  * where:
100*3557Sdlw  *	path will receive the pathname of the current working directory.
101*3557Sdlw  *	ierr will be 0 if successful, a system error code otherwise.
102*3557Sdlw  */
103*3557Sdlw 
104*3557Sdlw extern int errno;
105*3557Sdlw 
106*3557Sdlw long
107*3557Sdlw getcwd_(path, len)
108*3557Sdlw char *path;
109*3557Sdlw int len;
110*3557Sdlw {
111*3557Sdlw 	char *p;
112*3557Sdlw 
113*3557Sdlw 	p = getcwd();
114*3557Sdlw 	if (p)
115*3557Sdlw 	{
116*3557Sdlw 		b_char(p, path, len);
117*3557Sdlw 		return(0L);
118*3557Sdlw 	}
119*3557Sdlw 	return((long)errno);
120*3557Sdlw }
121