date and time created 82/10/25 01:14:49 by mckusick
[unix-history] / usr / src / bin / pwd / pwd.c
CommitLineData
f4d092e2 1static char *sccsid = "@(#)pwd.c 4.3 (Berkeley) %G%";
1b66014a
BJ
2/*
3 * Print working (current) directory
4 */
5#include <stdio.h>
6#include <sys/param.h>
7#include <sys/stat.h>
f4d092e2 8#include <dir.h>
1b66014a
BJ
9
10char dot[] = ".";
11char dotdot[] = "..";
12char name[BUFSIZ];
24f62f00 13DIR *file;
1b66014a
BJ
14int off = -1;
15struct stat d, dd;
24f62f00 16struct direct *dir;
1b66014a
BJ
17
18main()
19{
20 int rdev, rino;
21
22 stat("/", &d);
23 rdev = d.st_dev;
24 rino = d.st_ino;
25 for (;;) {
26 stat(dot, &d);
27 if (d.st_ino==rino && d.st_dev==rdev)
28 prname();
24f62f00 29 if ((file = opendir(dotdot)) == NULL) {
1b66014a
BJ
30 fprintf(stderr,"pwd: cannot open ..\n");
31 exit(1);
32 }
24f62f00 33 fstat(file->dd_fd, &dd);
1b66014a
BJ
34 chdir(dotdot);
35 if(d.st_dev == dd.st_dev) {
36 if(d.st_ino == dd.st_ino)
37 prname();
38 do
24f62f00 39 if ((dir = readdir(file)) == NULL) {
1b66014a
BJ
40 fprintf(stderr,"read error in ..\n");
41 exit(1);
42 }
24f62f00
KM
43 while (dir->d_ino != d.st_ino);
44 } else
45 do {
46 if ((dir = readdir(file)) == NULL) {
1b66014a
BJ
47 fprintf(stderr,"read error in ..\n");
48 exit(1);
49 }
24f62f00 50 stat(dir->d_name, &dd);
1b66014a 51 } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
24f62f00 52 closedir(file);
1b66014a
BJ
53 cat();
54 }
55}
56
57prname()
58{
59 write(1, "/", 1);
60 if (off<0)
61 off = 0;
62 name[off] = '\n';
63 write(1, name, off+1);
64 exit(0);
65}
66
67cat()
68{
69 register i, j;
70
24f62f00
KM
71 i = dir->d_namlen;
72 if ((off + dir->d_namlen + 2) > BUFSIZ-1)
1b66014a
BJ
73 prname();
74 for(j=off+1; j>=0; --j)
75 name[j+i+1] = name[j];
76 off=i+off+1;
77 name[i] = '/';
78 for(--i; i>=0; --i)
24f62f00 79 name[i] = dir->d_name[i];
1b66014a 80}