|
|
1.1.1.2 ! root 1: /* sys.c 4.4 81/03/22 */ 1.1 root 2: 1.1.1.2 ! root 3: #include <sys/param.h> ! 4: #include <sys/ino.h> ! 5: #include <sys/inode.h> ! 6: #include <sys/filsys.h> ! 7: #include <sys/dir.h> 1.1 root 8: #include "saio.h" 9: 10: ino_t dlook(); 11: 12: static 13: openi(n,io) 1.1.1.2 ! root 14: register struct iob *io; 1.1 root 15: { 16: register struct dinode *dp; 17: 18: io->i_offset = 0; 1.1.1.2 ! root 19: io->i_bn = fsbtodb(io->i_blksz, itod(io->i_blksz, n)); ! 20: io->i_cc = BSIZE(io->i_blksz); 1.1 root 21: io->i_ma = io->i_buf; 1.1.1.2 ! root 22: devread(io); ! 23: 1.1 root 24: dp = (struct dinode *)io->i_buf; 1.1.1.2 ! root 25: dp = &dp[itoo(io->i_blksz, n)]; ! 26: io->i_ino.i_number = n; ! 27: io->i_ino.i_mode = dp->di_mode; ! 28: io->i_ino.i_size = dp->di_size; ! 29: l3tol((char *)io->i_ino.i_un.i_addr, (char *)dp->di_addr, NADDR); 1.1 root 30: } 31: 1.1.1.2 ! root 32: static 1.1 root 33: find(path, file) 1.1.1.2 ! root 34: register char *path; ! 35: struct iob *file; 1.1 root 36: { 37: register char *q; 38: char c; 1.1.1.2 ! root 39: int n; 1.1 root 40: 41: if (path==NULL || *path=='\0') { 42: printf("null path\n"); 43: return(0); 44: } 45: 46: openi((ino_t) ROOTINO, 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: static daddr_t 72: sbmap(io, bn) 1.1.1.2 ! root 73: register struct iob *io; ! 74: daddr_t bn; 1.1 root 75: { 1.1.1.2 ! root 76: register i; 1.1 root 77: register struct inode *ip; 1.1.1.2 ! root 78: int j, sh; ! 79: daddr_t nb, *bap; ! 80: int ibn = bn; 1.1 root 81: 82: ip = &io->i_ino; 1.1.1.2 ! root 83: if(bn < 0) { ! 84: printf("bn negative\n"); ! 85: return((daddr_t)0); ! 86: } 1.1 root 87: 88: /* 1.1.1.2 ! root 89: * blocks 0..NADDR-4 are direct blocks 1.1 root 90: */ 1.1.1.2 ! root 91: if(bn < NADDR-3) { ! 92: i = bn; ! 93: nb = ip->i_un.i_addr[i]; 1.1 root 94: return(nb); 95: } 96: 97: /* 1.1.1.2 ! root 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. 1.1 root 102: */ 1.1.1.2 ! root 103: sh = 0; ! 104: nb = 1; ! 105: bn -= NADDR-3; ! 106: for(j=3; j>0; j--) { ! 107: sh += NSHIFT(io->i_blksz); ! 108: nb <<= NSHIFT(io->i_blksz); ! 109: if(bn < nb) 1.1 root 110: break; 1.1.1.2 ! root 111: bn -= nb; 1.1 root 112: } 1.1.1.2 ! root 113: if(j == 0) { ! 114: printf("bn ovf %D\n",bn); ! 115: return((daddr_t)0); 1.1 root 116: } 117: 118: /* 1.1.1.2 ! root 119: * fetch the address from the inode 1.1 root 120: */ 1.1.1.2 ! root 121: nb = ip->i_un.i_addr[NADDR-j]; ! 122: if(nb == 0) { 1.1 root 123: printf("bn void %D\n",bn); 124: return((daddr_t)0); 125: } 126: 127: /* 128: * fetch through the indirect blocks 129: */ 1.1.1.2 ! root 130: for(; j<=3; j++) { 1.1 root 131: if (blknos[j] != nb) { 1.1.1.2 ! root 132: io->i_bn = fsbtodb(io->i_blksz, nb); 1.1 root 133: io->i_ma = b[j]; 1.1.1.2 ! root 134: io->i_cc = BSIZE(io->i_blksz); ! 135: devread(io); ! 136: bap = (daddr_t *)b[j]; 1.1 root 137: blknos[j] = nb; 138: } 139: bap = (daddr_t *)b[j]; 1.1.1.2 ! root 140: sh -= NSHIFT(io->i_blksz); ! 141: i = (bn>>sh) & NMASK(io->i_blksz); 1.1 root 142: nb = bap[i]; 143: if(nb == 0) { 144: printf("bn void %D\n",bn); 145: return((daddr_t)0); 146: } 147: } 148: return(nb); 149: } 150: 151: static ino_t 152: dlook(s, io) 1.1.1.2 ! root 153: char *s; ! 154: register struct iob *io; 1.1 root 155: { 156: register struct direct *dp; 157: register struct inode *ip; 1.1.1.2 ! root 158: daddr_t bn; ! 159: int n,dc; 1.1 root 160: 1.1.1.2 ! root 161: if (s==NULL || *s=='\0') 1.1 root 162: return(0); 1.1.1.2 ! root 163: ip = &io->i_ino; ! 164: if ((ip->i_mode&IFMT)!=IFDIR) { 1.1 root 165: printf("not a directory\n"); 166: return(0); 167: } 1.1.1.2 ! root 168: ! 169: n = ip->i_size/sizeof(struct direct); ! 170: ! 171: if (n==0) { 1.1 root 172: printf("zero length directory\n"); 173: return(0); 174: } 1.1.1.2 ! root 175: ! 176: dc = BSIZE(io->i_blksz); ! 177: bn = (daddr_t)0; ! 178: while(n--) { ! 179: if (++dc >= BSIZE(io->i_blksz)/sizeof(struct direct)) { ! 180: io->i_bn = fsbtodb(io->i_blksz, sbmap(io, bn++)); ! 181: io->i_ma = io->i_buf; ! 182: io->i_cc = BSIZE(io->i_blksz); ! 183: devread(io); ! 184: dp = (struct direct *)io->i_buf; ! 185: dc = 0; ! 186: } ! 187: ! 188: if (match(s, dp->d_name)) 1.1 root 189: return(dp->d_ino); 1.1.1.2 ! root 190: dp++; 1.1 root 191: } 192: return(0); 193: } 194: 1.1.1.2 ! root 195: static ! 196: match(s1,s2) ! 197: register char *s1,*s2; 1.1 root 198: { 1.1.1.2 ! root 199: register cc; 1.1 root 200: 1.1.1.2 ! root 201: cc = DIRSIZ; ! 202: while (cc--) { ! 203: if (*s1 != *s2) ! 204: return(0); ! 205: if (*s1++ && *s2++) ! 206: continue; else ! 207: return(1); 1.1 root 208: } 1.1.1.2 ! root 209: return(1); 1.1 root 210: } 211: 212: lseek(fdesc, addr, ptr) 1.1.1.2 ! root 213: int fdesc; ! 214: off_t addr; ! 215: int ptr; 1.1 root 216: { 217: register struct iob *io; 218: 1.1.1.2 ! root 219: if (ptr != 0) { ! 220: printf("Seek not from beginning of file\n"); ! 221: return(-1); ! 222: } 1.1 root 223: fdesc -= 3; 1.1.1.2 ! root 224: if (fdesc < 0 || fdesc >= NFILES || ((io = &iob[fdesc])->i_flgs&F_ALLOC) == 0) 1.1 root 225: return(-1); 226: io->i_si.si_flgs &= ~F_EOF; 227: io->i_offset = addr; 1.1.1.2 ! root 228: io->i_bn = fsbtodb(io->i_blksz, addr/BSIZE(io->i_blksz)); 1.1 root 229: io->i_cc = 0; 230: return(0); 231: } 232: 233: getc(fdesc) 1.1.1.2 ! root 234: int fdesc; 1.1 root 235: { 236: register struct iob *io; 237: register char *p; 1.1.1.2 ! root 238: register c; ! 239: int off; 1.1 root 240: 241: 242: if (fdesc >= 0 && fdesc <= 2) 243: return(getchar()); 244: fdesc -= 3; 1.1.1.2 ! root 245: if (fdesc < 0 || fdesc >= NFILES || ((io = &iob[fdesc])->i_flgs&F_ALLOC) == 0) 1.1 root 246: return(-1); 247: p = io->i_ma; 248: if (io->i_cc <= 0) { 1.1.1.2 ! root 249: io->i_bn = fsbtodb(io->i_blksz, ! 250: io->i_offset/(off_t)BSIZE(io->i_blksz)); ! 251: if (io->i_flgs&F_FILE) ! 252: io->i_bn = fsbtodb(io->i_blksz, ! 253: sbmap(io, dbtofsb(io->i_blksz, io->i_bn))); 1.1 root 254: io->i_ma = io->i_buf; 1.1.1.2 ! root 255: io->i_cc = BSIZE(io->i_blksz); ! 256: devread(io); ! 257: if (io->i_flgs&F_FILE) { ! 258: off = io->i_offset % (off_t)BSIZE(io->i_blksz); ! 259: if (io->i_offset+(BSIZE(io->i_blksz)-off) >= io->i_ino.i_size) ! 260: io->i_cc = io->i_ino.i_size - io->i_offset + off; 1.1 root 261: io->i_cc -= off; 1.1.1.2 ! root 262: if (io->i_cc <= 0) ! 263: return(-1); ! 264: } else ! 265: off = 0; 1.1 root 266: p = &io->i_buf[off]; 267: } 268: io->i_cc--; 269: io->i_offset++; 270: c = (unsigned)*p++; 271: io->i_ma = p; 272: return(c); 273: } 274: 275: 276: read(fdesc, buf, count) 1.1.1.2 ! root 277: int fdesc; ! 278: char *buf; ! 279: int count; 1.1 root 280: { 1.1.1.2 ! root 281: register i; 1.1 root 282: register struct iob *file; 283: 284: if (fdesc >= 0 && fdesc <= 2) { 285: i = count; 286: do { 287: *buf = getchar(); 288: } while (--i && *buf++ != '\n'); 289: return(count - i); 290: } 291: fdesc -= 3; 1.1.1.2 ! root 292: if (fdesc < 0 || fdesc >= NFILES || ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) 1.1 root 293: return(-1); 294: if ((file->i_flgs&F_READ) == 0) 295: return(-1); 1.1.1.2 ! root 296: if ((file->i_flgs&F_FILE) == 0) { 1.1 root 297: file->i_cc = count; 298: file->i_ma = buf; 1.1.1.2 ! root 299: i = devread(file); ! 300: file->i_bn += CLSIZE; 1.1 root 301: return(i); 1.1.1.2 ! root 302: } ! 303: else { 1.1 root 304: if (file->i_offset+count > file->i_ino.i_size) 305: count = file->i_ino.i_size - file->i_offset; 306: if ((i = count) <= 0) 307: return(0); 308: do { 1.1.1.2 ! root 309: *buf++ = getc(fdesc+3); 1.1 root 310: } while (--i); 311: return(count); 312: } 313: } 314: 315: write(fdesc, buf, count) 1.1.1.2 ! root 316: int fdesc; ! 317: char *buf; ! 318: int count; 1.1 root 319: { 320: register i; 321: register struct iob *file; 322: 323: if (fdesc >= 0 && fdesc <= 2) { 324: i = count; 325: while (i--) 326: putchar(*buf++); 327: return(count); 328: } 329: fdesc -= 3; 1.1.1.2 ! root 330: if (fdesc < 0 || fdesc >= NFILES || ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) 1.1 root 331: if ((file->i_flgs&F_WRITE) == 0) 332: return(-1); 333: file->i_cc = count; 334: file->i_ma = buf; 1.1.1.2 ! root 335: i = devwrite(file); ! 336: file->i_bn += CLSIZE; 1.1 root 337: return(i); 338: } 339: 340: /* 341: * Open a file on a physical device, or open the device itself. 342: * 343: * The device is identified by a struct bootparam which gives pointers 344: * to the struct boottab, containing the device open, strategy and close 345: * routines; and the device parameters such as controller #, unit #, etc. 346: * 347: * If *str == 0, the device is opened, else the named file in the file 348: * system on the device is opened. 349: */ 350: int 351: physopen(bp, str, how) 1.1.1.2 ! root 352: register struct bootparam *bp; ! 353: char *str; ! 354: int how; 1.1 root 355: { 356: int fdesc; 357: register struct iob *file; 358: 359: fdesc = getiob(); /* Allocate an IOB */ 360: file = &iob[fdesc]; 361: 362: file->i_boottab = bp->bp_boottab; /* Record pointer to boot table */ 363: file->i_ctlr = bp->bp_ctlr; 364: file->i_unit = bp->bp_unit; 365: file->i_boff = bp->bp_part; 366: file->i_ino.i_dev = 0; /* Call it device 0 for chuckles */ 367: 368: return(openfile(fdesc, file, str, how)); 369: } 370: 371: int openfirst = 1; 372: 373: /* 374: * Allocate an IOB for a newly opened file. 375: */ 376: int 377: getiob() 378: { 379: register int fdesc; 380: 381: if (openfirst) { 382: openfirst = 0; 383: for (fdesc = 0; fdesc < NFILES; fdesc++) 384: iob[fdesc].i_flgs = 0; 385: } 386: 387: for (fdesc = 0; fdesc < NFILES; fdesc++) 388: if (iob[fdesc].i_flgs == 0) 389: goto gotfile; 390: _stop("No more file slots"); 391: gotfile: 392: (&iob[fdesc])->i_flgs |= F_ALLOC; 393: return fdesc; 394: } 395: 396: /* Hex string scanner for open() */ 397: static char * 398: openhex(p, ip) 399: register char *p; 400: int *ip; 401: { 402: register int ac = 0; 403: 404: while (*p) { 405: if (*p >= '0' && *p <= '9') 406: ac = (ac<<4) + (*p - '0'); 407: else if (*p >= 'a' && *p <= 'f') 408: ac = (ac<<4) + (*p - 'a' + 10); 409: else if (*p >= 'A' && *p <= 'F') 410: ac = (ac<<4) + (*p - 'A' + 10); 411: else 412: break; 413: p++; 414: } 415: if (*p == ',') 416: p++; 417: *ip = ac; 418: return (p); 419: } 420: 421: /* 422: * Open a device or file-in-file-system-on-device. 423: */ 424: int 425: open(str, how) 1.1.1.2 ! root 426: char *str; ! 427: int how; 1.1 root 428: { 429: register char *cp; 430: register struct iob *file; 431: register struct boottab *dp; 432: register struct boottab **tablep; 433: int fdesc; 434: long atol(); 435: 436: extern struct boottab *(devsw[]); 437: 438: fdesc = getiob(); /* Allocate an IOB */ 439: file = &iob[fdesc]; 440: 441: for (cp = str; *cp && *cp != '('; cp++) 442: ; 443: if (*cp++ != '(') { 444: file->i_flgs = 0; 445: goto badsyntax; 446: } 447: for (tablep = devsw; 0 != (dp = *tablep); tablep++) { 448: if (str[0] == dp->b_dev[0] && str[1] == dp->b_dev[1]) 449: goto gotdev; 450: } 451: printf("Unknown device: %c%c; known devices:\n", str[0], str[1]); 452: for (tablep = devsw; 0 != (dp = *tablep); tablep++) 453: printf(" %s\n", dp->b_desc); 454: file->i_flgs = 0; 455: return(-1); 456: gotdev: 457: file->i_boottab = dp; /* Record pointer to boot table */ 458: file->i_ino.i_dev = tablep-devsw; 459: cp = openhex(cp, &file->i_ctlr); 460: cp = openhex(cp, &file->i_unit); 461: cp = openhex(cp, (int *)&file->i_boff); 462: if (*cp == '\0') goto doit; 463: if (*cp != ')') { 464: badsyntax: 465: printf("%s: bad syntax, try dev(ctlr,unit,part)name\n", str); 466: return -1; 467: } 468: do 469: cp++; 470: while (*cp == ' '); 471: 472: doit: 473: return(openfile(fdesc, file, cp, how)); 474: } 475: 476: 477: /* 478: * File processing for open call 479: */ 480: openfile(fdesc, file, cp, how) 1.1.1.2 ! root 481: int fdesc; ! 482: struct iob *file; ! 483: char *cp; ! 484: int how; 1.1 root 485: { 1.1.1.2 ! root 486: int i; 1.1 root 487: 1.1.1.2 ! root 488: if (devopen(file)) { 1.1 root 489: file->i_flgs = 0; 490: return(-1); /* if devopen fails, open fails */ 491: } 492: 493: if (*cp == '\0') { /* Opening a device */ 494: file->i_flgs |= how+1; 495: file->i_cc = 0; 496: file->i_offset = 0; 497: return(fdesc+3); 498: } 1.1.1.2 ! root 499: if ((i = find(cp, file)) == 0) { 1.1 root 500: file->i_flgs = 0; 501: return(-1); 502: } 503: if (how != 0) { 504: printf("Can't write files yet.. Sorry\n"); 505: file->i_flgs = 0; 506: return(-1); 507: } 1.1.1.2 ! root 508: openi(i, file); 1.1 root 509: file->i_offset = 0; 510: file->i_cc = 0; 511: file->i_flgs |= F_FILE | (how+1); 512: return(fdesc+3); 513: } 514: 515: 516: close(fdesc) 1.1.1.2 ! root 517: int fdesc; 1.1 root 518: { 1.1.1.2 ! root 519: struct iob *file; 1.1 root 520: 521: fdesc -= 3; 1.1.1.2 ! root 522: if (fdesc < 0 || fdesc >= NFILES || ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) 1.1 root 523: return(-1); 1.1.1.2 ! root 524: devclose(file); 1.1 root 525: file->i_flgs = 0; 526: return(0); 527: } 528: 529: exit() 530: { 1.1.1.2 ! root 531: _stop("Exit called"); 1.1 root 532: } 533: 534: _stop(s) 1.1.1.2 ! root 535: char *s; 1.1 root 536: { 1.1.1.2 ! root 537: int i; 1.1 root 538: 539: for (i = 0; i < NFILES; i++) 540: if (iob[i].i_flgs != 0) 541: close(i); 1.1.1.2 ! root 542: printf("%s\n", s); 1.1 root 543: _exit(); 544: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.