|
|
1.1 ! root 1: static char Sccsid[]="@(#)curdir.c 3.4"; ! 2: ! 3: # define IROOT 2 ! 4: # include "sys/types.h" ! 5: # include "macros.h" ! 6: ! 7: #include <stdio.h> ! 8: #include "sys/param.h" ! 9: #include <mtab.h> ! 10: #include "sys/dir.h" ! 11: #include "sys/filsys.h" ! 12: /* ! 13: current directory. ! 14: Places the full pathname of the current directory in `str'. ! 15: Handles file systems not mounted on a root directory ! 16: via /etc/mtab (see mtab(V)). ! 17: NOTE: PWB systems don't use mtab(V), but they don't mount ! 18: file systems anywhere but on a root directory (so far, at least). ! 19: ! 20: returns 0 on success ! 21: < 0 on failure. ! 22: ! 23: Current directory on return: ! 24: success: same as on entry ! 25: failure: UNKNOWN! ! 26: */ ! 27: static char *curdirp; ! 28: ! 29: static char *flg[] = { ! 30: "read/write", ! 31: "read only" ! 32: }; ! 33: ! 34: static struct mtab mtab[NMOUNT], *mp; ! 35: static struct filsys super; ! 36: ! 37: ! 38: curdir(str) ! 39: char *str; ! 40: { ! 41: register int n; ! 42: ! 43: curdirp = str; ! 44: n = findir(0); ! 45: return(n+chdir(str)); ! 46: } ! 47: ! 48: ! 49: # define ADDSLASH if (flag) *curdirp++ = '/'; ! 50: # define QUIT { close(fd); return(-1); } ! 51: ! 52: findir(flag) ! 53: { ! 54: register int fd,inum; ! 55: register char *tp; ! 56: char *slashp,tmp[100]; ! 57: int dev, r; ! 58: struct direct entry; ! 59: struct stat s; ! 60: ! 61: if (stat(".",&s)<0) return(-1); ! 62: if ((inum = s.st_ino) == IROOT) { ! 63: dev = s.st_dev; ! 64: if ((fd = open("/",0))<0) return(-1); ! 65: if (fstat(fd,&s)<0) ! 66: QUIT; ! 67: if (dev == s.st_dev) { ! 68: *curdirp++ = '/'; ! 69: *curdirp = 0; ! 70: close(fd); ! 71: return(0); ! 72: } ! 73: slashp = entry.d_name; ! 74: slashp--; ! 75: while (read(fd,&entry,sizeof(entry)) == sizeof(entry)) { ! 76: if (entry.d_ino == 0) continue; ! 77: *slashp = '/'; ! 78: if (stat(slashp,&s)<0) continue; ! 79: if (s.st_dev != dev) continue; ! 80: if ((s.st_mode&S_IFMT) != S_IFDIR) continue; ! 81: for (tp = slashp; *curdirp = (*tp++); curdirp++); ! 82: ADDSLASH; ! 83: *curdirp = 0; ! 84: close(fd); ! 85: return(0); ! 86: } ! 87: close(fd); ! 88: ! 89: fd = open("/etc/mtab",0); ! 90: if(fd < 0) { ! 91: fprintf(stderr, "curdir: cannot open /etc/mtab!\n"); ! 92: return(-1); ! 93: } ! 94: read(fd, mtab, sizeof mtab); ! 95: for(mp = mtab; mp < &mtab[NMOUNT]; mp++) { ! 96: if(mp->m_dname[0]) { ! 97: sprintf(tmp,"/dev/%s",mp->m_dname); ! 98: if ( stat(tmp,&s) < 0 ) ! 99: QUIT; ! 100: if ( s.st_rdev != dev ) continue; ! 101: for ( tp = mp->m_path; *curdirp = (*tp++); curdirp++ ); ! 102: ADDSLASH; ! 103: close(fd); ! 104: /* test print ! 105: printf("%.10s on /dev/%s %s on %s", ! 106: mp->m_path, mp->m_dname); ! 107: end test print */ ! 108: return(0); ! 109: } ! 110: } ! 111: ! 112: QUIT; ! 113: } ! 114: if ((fd = open("..",0))<0) return(-1); ! 115: for (entry.d_ino = 0; entry.d_ino != inum; ) ! 116: if (read(fd,&entry,sizeof(entry)) != sizeof(entry)) ! 117: QUIT; ! 118: close(fd); ! 119: if (chdir("..")<0) return(-1); ! 120: if (findir(-1)<0) r = -1; ! 121: else r = 0; ! 122: for (tp = entry.d_name; *curdirp = (*tp++); curdirp++); ! 123: ADDSLASH; ! 124: *curdirp = 0; ! 125: return(r); ! 126: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.