1: #include <sys/param.h> 2: #include <sys/ino.h> 3: #include <sys/inode.h> 4: #include <sys/filsys.h> 5: #include <sys/dir.h> 6: #include "saio.h" 7: 8: int segflag = 0; 9: 10: 11: static 12: openi(n,io) 13: register struct iob *io; 14: { 15: register struct dinode *dp; 16: 17: io->i_offset = 0; 18: io->i_bn = (daddr_t)((n+15)/INOPB) + io->i_boff; 19: io->i_cc = 512; 20: io->i_ma = io->i_buf; 21: devread(io); 22: 23: dp = io->i_buf; 24: dp = &dp[(n-1)%INOPB]; 25: io->i_ino.i_number = n; 26: io->i_ino.i_mode = dp->di_mode; 27: io->i_ino.i_size = dp->di_size; 28: l3tol((char *)io->i_ino.i_un.i_addr,(char *)dp->di_addr,NADDR); 29: } 30: 31: 32: static 33: find(path, file) 34: register char *path; 35: struct iob *file; 36: { 37: register char *q; 38: char c; 39: int n; 40: 41: if (path==NULL || *path=='\0') { 42: printf("null path\n"); 43: return(0); 44: } 45: 46: openi((ino_t) 2, file); 47: while (*path) { 48: while (*path == '/') 49: path++; 50: q = path; 51: while(*q != '/' && *q != '\0') 52: q++; 53: c = *q; 54: *q = '\0'; 55: 56: if ((n=dlook(path, file))!=0) { 57: if (c=='\0') 58: break; 59: openi(n, file); 60: *q = c; 61: path = q; 62: continue; 63: } else { 64: printf("%s not found\n",path); 65: return(0); 66: } 67: } 68: return(n); 69: } 70: 71: 72: static daddr_t 73: sbmap(io, bn) 74: register struct iob *io; 75: daddr_t bn; 76: { 77: register i; 78: register struct inode *ip; 79: int j, sh; 80: daddr_t nb, *bap; 81: 82: ip = &io->i_ino;; 83: if(bn < 0) { 84: printf("bn negative\n"); 85: return((daddr_t)0); 86: } 87: 88: /* 89: * blocks 0..NADDR-4 are direct blocks 90: */ 91: if(bn < NADDR-3) { 92: i = bn; 93: nb = ip->i_un.i_addr[i]; 94: return(nb); 95: } 96: 97: /* 98: * addresses NADDR-3, NADDR-2, and NADDR-1 99: * have single, double, triple indirect blocks. 100: * the first step is to determine 101: * how many levels of indirection. 102: */ 103: sh = 0; 104: nb = 1; 105: bn -= NADDR-3; 106: for(j=3; j>0; j--) { 107: sh += NSHIFT; 108: nb <<= NSHIFT; 109: if(bn < nb) 110: break; 111: bn -= nb; 112: } 113: if(j == 0) { 114: printf("bn ovf %D\n",bn); 115: return((daddr_t)0); 116: } 117: 118: /* 119: * fetch the address from the inode 120: */ 121: nb = ip->i_un.i_addr[NADDR-j]; 122: if(nb == 0) { 123: printf("bn void %D\n",bn); 124: return((daddr_t)0); 125: } 126: 127: /* 128: * fetch through the indirect blocks 129: */ 130: for(; j<=3; j++) { 131: if (blknos[j] != nb) { 132: io->i_bn = nb + io->i_boff; 133: io->i_ma = b[j]; 134: io->i_cc = 512; 135: devread(io); 136: blknos[j] = nb; 137: } 138: bap = b[j]; 139: sh -= NSHIFT; 140: i = (bn>>sh) & NMASK; 141: nb = bap[i]; 142: if(nb == 0) { 143: printf("bn void %D\n",bn); 144: return((daddr_t)0); 145: } 146: } 147: 148: return(nb); 149: } 150: 151: static ino_t 152: dlook(s, io) 153: char *s; 154: register struct iob *io; 155: { 156: register struct direct *dp; 157: register struct inode *ip; 158: daddr_t bn; 159: int n,dc; 160: 161: if (s==NULL || *s=='\0') 162: return(0); 163: ip = &io->i_ino; 164: if ((ip->i_mode&IFMT)!=IFDIR) { 165: printf("not a directory\n"); 166: return(0); 167: } 168: 169: n = ip->i_size/sizeof(struct direct); 170: 171: if (n==0) { 172: printf("zero length directory\n"); 173: return(0); 174: } 175: 176: dc = 512; 177: bn = (daddr_t)0; 178: while(n--) { 179: if (++dc >= 512/sizeof(struct direct)) { 180: io->i_bn = sbmap(io, bn++) + io->i_boff; 181: io->i_ma = io->i_buf; 182: io->i_cc = 512; 183: devread(io); 184: dp = io->i_buf; 185: dc = 0; 186: } 187: 188: if (match(s, dp->d_name)) 189: return(dp->d_ino); 190: dp++; 191: } 192: return(0); 193: } 194: 195: static 196: match(s1,s2) 197: register char *s1,*s2; 198: { 199: register cc; 200: 201: cc = DIRSIZ; 202: while (cc--) { 203: if (*s1 != *s2) 204: return(0); 205: if (*s1++ && *s2++) 206: continue; else 207: return(1); 208: } 209: return(1); 210: } 211: 212: lseek(fdesc, addr, ptr) 213: int fdesc; 214: off_t addr; 215: int ptr; 216: { 217: register struct iob *io; 218: 219: if (ptr != 0) { 220: printf("Seek not from beginning of file\n"); 221: return(-1); 222: } 223: fdesc -= 3; 224: if (fdesc < 0 || fdesc >= NFILES || ((io = &iob[fdesc])->i_flgs&F_ALLOC) == 0) 225: return(-1); 226: io->i_offset = addr; 227: io->i_bn = addr/512 + io->i_boff; 228: io->i_cc = 0; 229: return(0); 230: } 231: 232: getc(fdesc) 233: int fdesc; 234: { 235: register struct iob *io; 236: register char *p; 237: register c; 238: int off; 239: 240: 241: if (fdesc >= 0 && fdesc <= 2) 242: return(getchar()); 243: fdesc -= 3; 244: if (fdesc < 0 || fdesc >= NFILES || ((io = &iob[fdesc])->i_flgs&F_ALLOC) == 0) 245: return(-1); 246: p = io->i_ma; 247: if (io->i_cc <= 0) { 248: io->i_bn = io->i_offset/(off_t)512; 249: if (io->i_flgs&F_FILE) 250: io->i_bn = sbmap(io, io->i_bn) + io->i_boff; 251: io->i_ma = io->i_buf; 252: io->i_cc = 512; 253: devread(io); 254: if (io->i_flgs&F_FILE) { 255: off = io->i_offset % (off_t)512; 256: if (io->i_offset+(512-off) >= io->i_ino.i_size) 257: io->i_cc = io->i_ino.i_size - io->i_offset + off; 258: io->i_cc -= off; 259: if (io->i_cc <= 0) 260: return(-1); 261: } else 262: off = 0; 263: p = &io->i_buf[off]; 264: } 265: io->i_cc--; 266: io->i_offset++; 267: c = (unsigned)*p++; 268: io->i_ma = p; 269: return(c); 270: } 271: getw(fdesc) 272: int fdesc; 273: { 274: register w,i; 275: register char *cp; 276: int val; 277: 278: for (i = 0, val = 0, cp = &val; i < sizeof(val); i++) { 279: w = getc(fdesc); 280: if (w < 0) { 281: if (i == 0) 282: return(-1); 283: else 284: return(val); 285: } 286: *cp++ = w; 287: } 288: return(val); 289: } 290: 291: read(fdesc, buf, count) 292: int fdesc; 293: char *buf; 294: int count; 295: { 296: register i; 297: register struct iob *file; 298: 299: if (fdesc >= 0 & fdesc <= 2) { 300: i = count; 301: do { 302: *buf = getchar(); 303: } while (--i && *buf++ != '\n'); 304: return(count - i); 305: } 306: fdesc -= 3; 307: if (fdesc < 0 || fdesc >= NFILES || ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) 308: return(-1); 309: if ((file->i_flgs&F_READ) == 0) 310: return(-1); 311: if ((file->i_flgs&F_FILE) == 0) { 312: file->i_cc = count; 313: file->i_ma = buf; 314: i = devread(file); 315: file->i_bn++; 316: return(i); 317: } 318: else { 319: if (file->i_offset+count > file->i_ino.i_size) 320: count = file->i_ino.i_size - file->i_offset; 321: if ((i = count) <= 0) 322: return(0); 323: do { 324: *buf++ = getc(fdesc+3); 325: } while (--i); 326: return(count); 327: } 328: } 329: 330: write(fdesc, buf, count) 331: int fdesc; 332: char *buf; 333: int count; 334: { 335: register i; 336: register struct iob *file; 337: 338: if (fdesc >= 0 && fdesc <= 2) { 339: i = count; 340: while (i--) 341: putchar(*buf++); 342: return(count); 343: } 344: fdesc -= 3; 345: if (fdesc < 0 || fdesc >= NFILES || ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) 346: return(-1); 347: if ((file->i_flgs&F_WRITE) == 0) 348: return(-1); 349: file->i_cc = count; 350: file->i_ma = buf; 351: i = devwrite(file); 352: file->i_bn++; 353: return(i); 354: } 355: 356: open(str, how) 357: char *str; 358: int how; 359: { 360: register char *cp; 361: int i; 362: register struct iob *file; 363: register struct devsw *dp; 364: int fdesc; 365: static first = 1; 366: long atol(); 367: 368: if (first) { 369: for (i = 0; i < NFILES; i++) 370: iob[i].i_flgs = 0; 371: first = 0; 372: } 373: 374: for (fdesc = 0; fdesc < NFILES; fdesc++) 375: if (iob[fdesc].i_flgs == 0) 376: goto gotfile; 377: _stop("No more file slots"); 378: gotfile: 379: (file = &iob[fdesc])->i_flgs |= F_ALLOC; 380: 381: for (cp = str; *cp && *cp != '('; cp++) 382: ; 383: if (*cp != '(') { 384: printf("Bad device\n"); 385: file->i_flgs = 0; 386: return(-1); 387: } 388: *cp++ = '\0'; 389: for (dp = devsw; dp->dv_name; dp++) { 390: if (match(str, dp->dv_name)) 391: goto gotdev; 392: } 393: printf("Unknown device\n"); 394: file->i_flgs = 0; 395: return(-1); 396: gotdev: 397: *(cp-1) = '('; 398: file->i_ino.i_dev = dp-devsw; 399: file->i_unit = *cp++ - '0'; 400: if (file->i_unit < 0 || file->i_unit > 7) { 401: printf("Bad unit specifier\n"); 402: file->i_flgs = 0; 403: return(-1); 404: } 405: if (*cp++ != ',') { 406: badoff: 407: printf("Missing offset specification\n"); 408: file->i_flgs = 0; 409: return(-1); 410: } 411: file->i_boff = atol(cp); 412: for (;;) { 413: if (*cp == ')') 414: break; 415: if (*cp++) 416: continue; 417: goto badoff; 418: } 419: devopen(file); 420: if (*++cp == '\0') { 421: file->i_flgs |= how+1; 422: file->i_cc = 0; 423: file->i_offset = 0; 424: return(fdesc+3); 425: } 426: if ((i = find(cp, file)) == 0) { 427: file->i_flgs = 0; 428: return(-1); 429: } 430: if (how != 0) { 431: printf("Can't write files yet.. Sorry\n"); 432: file->i_flgs = 0; 433: return(-1); 434: } 435: openi(i, file); 436: file->i_offset = 0; 437: file->i_cc = 0; 438: file->i_flgs |= F_FILE | (how+1); 439: return(fdesc+3); 440: } 441: 442: close(fdesc) 443: int fdesc; 444: { 445: struct iob *file; 446: 447: fdesc -= 3; 448: if (fdesc < 0 || fdesc >= NFILES || ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) 449: return(-1); 450: if ((file->i_flgs&F_FILE) == 0) 451: devclose(file); 452: file->i_flgs = 0; 453: return(0); 454: } 455: 456: exit() 457: { 458: _stop("Exit called"); 459: } 460: 461: _stop(s) 462: char *s; 463: { 464: printf("%s\n", s); 465: _rtt(); 466: } 467: 468: trap(ps) 469: int ps; 470: { 471: printf("Trap %o\n", ps); 472: for (;;) 473: ; 474: }