1: /*
2: * Print working (current) directory
3: */
4: #include <stdio.h>
5: #include <sys/param.h>
6: #include <sys/stat.h>
7: #include <sys/dir.h>
8: #include <sys/file.h>
9:
10: char dot[] = ".";
11: char dotdot[] = "..";
12: char name[512];
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, FATT_RDONLY)) < 0) {
30: perror(dotdot);
31: exit(1);
32: }
33: fstat(file, &dd);
34: if (chdir(dotdot) < 0) {
35: perror(dotdot);
36: exit(1);
37: }
38: if(d.st_dev == dd.st_dev) {
39: if(d.st_ino == dd.st_ino)
40: prname();
41: do
42: if (read(file, (char *)&dir, sizeof(dir)) < sizeof(dir)) {
43: perror("read: ..: ");
44: exit(1);
45: }
46: while (dir.d_ino != d.st_ino);
47: }
48: else do {
49: if(read(file, (char *)&dir, sizeof(dir)) < sizeof(dir)) {
50: perror("read: ..: ");
51: exit(1);
52: }
53: stat(dir.d_name, &dd);
54: } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
55: close(file);
56: cat();
57: }
58: }
59:
60: prname()
61: {
62: write(1, "/", 1);
63: if (off<0)
64: off = 0;
65: name[off] = '\n';
66: write(1, name, off+1);
67: exit(0);
68: }
69:
70: cat()
71: {
72: register i, j;
73:
74: i = -1;
75: while (dir.d_name[++i] != 0);
76: if ((off+i+2) > sizeof (name) - 1)
77: prname();
78: for(j=off+1; j>=0; --j)
79: name[j+i+1] = name[j];
80: off=i+off+1;
81: name[i] = '/';
82: for(--i; i>=0; --i)
83: name[i] = dir.d_name[i];
84: }
Defined functions
cat
defined in line
70; used 1 times
main
defined in line
18;
never used
Defined variables
d
defined in line
15; used 11 times
dd
defined in line
15; used 6 times
dir
defined in line
16; used 10 times
dot
defined in line
10; used 1 times
file
defined in line
13; used 5 times
name
defined in line
12; used 7 times
off
defined in line
14; used 8 times