Commit | Line | Data |
---|---|---|
31cef89c | 1 | static char *sccsid = "@(#)pwd.c 4.1 (Berkeley) 10/1/80"; |
3a54d117 BJ |
2 | /* |
3 | * Print working (current) directory | |
4 | */ | |
5 | #include <stdio.h> | |
6 | #include <sys/param.h> | |
7 | #include <sys/stat.h> | |
8 | #include <sys/dir.h> | |
9 | ||
10 | char dot[] = "."; | |
11 | char dotdot[] = ".."; | |
12 | char name[BUFSIZ]; | |
13 | int file; | |
14 | int off = -1; | |
15 | struct stat d, dd; | |
16 | struct direct dir; | |
17 | ||
18 | main() | |
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(); | |
29 | if ((file = open(dotdot,0)) < 0) { | |
30 | fprintf(stderr,"pwd: cannot open ..\n"); | |
31 | exit(1); | |
32 | } | |
33 | fstat(file, &dd); | |
34 | chdir(dotdot); | |
35 | if(d.st_dev == dd.st_dev) { | |
36 | if(d.st_ino == dd.st_ino) | |
37 | prname(); | |
38 | do | |
39 | if (read(file, (char *)&dir, sizeof(dir)) < sizeof(dir)) { | |
40 | fprintf(stderr,"read error in ..\n"); | |
41 | exit(1); | |
42 | } | |
43 | while (dir.d_ino != d.st_ino); | |
44 | } | |
45 | else do { | |
46 | if(read(file, (char *)&dir, sizeof(dir)) < sizeof(dir)) { | |
47 | fprintf(stderr,"read error in ..\n"); | |
48 | exit(1); | |
49 | } | |
50 | stat(dir.d_name, &dd); | |
51 | } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev); | |
52 | close(file); | |
53 | cat(); | |
54 | } | |
55 | } | |
56 | ||
57 | prname() | |
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 | ||
67 | cat() | |
68 | { | |
69 | register i, j; | |
70 | ||
71 | i = -1; | |
72 | while (dir.d_name[++i] != 0); | |
73 | if ((off+i+2) > BUFSIZ-1) | |
74 | prname(); | |
75 | for(j=off+1; j>=0; --j) | |
76 | name[j+i+1] = name[j]; | |
77 | off=i+off+1; | |
78 | name[i] = '/'; | |
79 | for(--i; i>=0; --i) | |
80 | name[i] = dir.d_name[i]; | |
81 | } |