123243Smckusick /* 229310Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 323243Smckusick * All rights reserved. The Berkeley software License Agreement 423243Smckusick * specifies the terms and conditions for redistribution. 523243Smckusick * 6*40500Smckusick * @(#)sys.c 7.9 (Berkeley) 03/15/90 723243Smckusick */ 8327Sbill 940498Sroot #include "sys/param.h" 10*40500Smckusick #include "ufs/dinode.h" 1140498Sroot #include "ufs/fs.h" 1240498Sroot #include "ufs/dir.h" 1340498Sroot #include "sys/reboot.h" 14327Sbill #include "saio.h" 15327Sbill 1634942Sbostic #define isdigit(c) ((u_int)((c) - '0') <= 9) 1734463Sbostic #define isspace(c) ((c) == ' ' || (c) == '\t') 1834942Sbostic #define isupper(c) ((u_int)((c) - 'A') <= 'Z' - 'A') 1934463Sbostic #define tolower(c) ((c) - 'A' + 'a') 2034463Sbostic 21327Sbill ino_t dlook(); 22327Sbill 236068Smckusic struct dirstuff { 246068Smckusic int loc; 256068Smckusic struct iob *io; 266068Smckusic }; 276068Smckusic 2834463Sbostic struct iob iob[NFILES]; 2934463Sbostic 30327Sbill static 3110022Ssam openi(n, io) 326068Smckusic register struct iob *io; 33327Sbill { 34327Sbill register struct dinode *dp; 3511083Ssam int cc; 36327Sbill 37327Sbill io->i_offset = 0; 386068Smckusic io->i_bn = fsbtodb(&io->i_fs, itod(&io->i_fs, n)) + io->i_boff; 396068Smckusic io->i_cc = io->i_fs.fs_bsize; 40327Sbill io->i_ma = io->i_buf; 4111083Ssam cc = devread(io); 42327Sbill dp = (struct dinode *)io->i_buf; 43*40500Smckusick io->i_ino = dp[itoo(&io->i_fs, n)]; 4411083Ssam return (cc); 45327Sbill } 46327Sbill 47327Sbill static 48327Sbill find(path, file) 496068Smckusic register char *path; 506068Smckusic struct iob *file; 51327Sbill { 52327Sbill register char *q; 5334463Sbostic char *dir, c; 54327Sbill int n; 55327Sbill 5634463Sbostic if (path == NULL || *path == '\0') { 57327Sbill printf("null path\n"); 5810022Ssam return (0); 59327Sbill } 60327Sbill 6111083Ssam if (openi((ino_t) ROOTINO, file) < 0) { 6211083Ssam printf("can't read root inode\n"); 6311083Ssam return (0); 6411083Ssam } 6530768Skarels dir = path; 66327Sbill while (*path) { 67327Sbill while (*path == '/') 68327Sbill path++; 69327Sbill q = path; 70327Sbill while(*q != '/' && *q != '\0') 71327Sbill q++; 72327Sbill c = *q; 73327Sbill *q = '\0'; 7425166Skarels if (q == path) path = "." ; /* "/" means "/." */ 75327Sbill 7630768Skarels if ((n = dlook(path, file, dir)) != 0) { 7711083Ssam if (c == '\0') 78327Sbill break; 7911083Ssam if (openi(n, file) < 0) 8011083Ssam return (0); 81327Sbill *q = c; 82327Sbill path = q; 83327Sbill continue; 84327Sbill } else { 8525166Skarels printf("%s: not found\n", path); 8610022Ssam return (0); 87327Sbill } 88327Sbill } 8910022Ssam return (n); 90327Sbill } 91327Sbill 9234463Sbostic #define NBUFS 4 9334463Sbostic static char b[NBUFS][MAXBSIZE]; 9434463Sbostic static daddr_t blknos[NBUFS]; 9534463Sbostic 96327Sbill static daddr_t 97327Sbill sbmap(io, bn) 986068Smckusic register struct iob *io; 996068Smckusic daddr_t bn; 100327Sbill { 101*40500Smckusick register struct dinode *ip; 1026068Smckusic int i, j, sh; 103327Sbill daddr_t nb, *bap; 104327Sbill 105327Sbill ip = &io->i_ino; 1066068Smckusic if (bn < 0) { 107327Sbill printf("bn negative\n"); 10810022Ssam return ((daddr_t)0); 109327Sbill } 110327Sbill 111327Sbill /* 1126068Smckusic * blocks 0..NDADDR are direct blocks 113327Sbill */ 1146068Smckusic if(bn < NDADDR) { 115*40500Smckusick nb = ip->di_db[bn]; 11610022Ssam return (nb); 117327Sbill } 118327Sbill 119327Sbill /* 1206068Smckusic * addresses NIADDR have single and double indirect blocks. 1216068Smckusic * the first step is to determine how many levels of indirection. 122327Sbill */ 1236068Smckusic sh = 1; 1246068Smckusic bn -= NDADDR; 1256068Smckusic for (j = NIADDR; j > 0; j--) { 1266068Smckusic sh *= NINDIR(&io->i_fs); 1276068Smckusic if (bn < sh) 128327Sbill break; 1296068Smckusic bn -= sh; 130327Sbill } 1316068Smckusic if (j == 0) { 1326068Smckusic printf("bn ovf %D\n", bn); 1336068Smckusic return ((daddr_t)0); 134327Sbill } 135327Sbill 136327Sbill /* 1376068Smckusic * fetch the first indirect block address from the inode 138327Sbill */ 139*40500Smckusick nb = ip->di_ib[NIADDR - j]; 1406068Smckusic if (nb == 0) { 141327Sbill printf("bn void %D\n",bn); 14210022Ssam return ((daddr_t)0); 143327Sbill } 144327Sbill 145327Sbill /* 146327Sbill * fetch through the indirect blocks 147327Sbill */ 1486068Smckusic for (; j <= NIADDR; j++) { 149327Sbill if (blknos[j] != nb) { 1506068Smckusic io->i_bn = fsbtodb(&io->i_fs, nb) + io->i_boff; 151327Sbill io->i_ma = b[j]; 1526068Smckusic io->i_cc = io->i_fs.fs_bsize; 15311083Ssam if (devread(io) != io->i_fs.fs_bsize) { 15411083Ssam if (io->i_error) 15511083Ssam errno = io->i_error; 15611083Ssam printf("bn %D: read error\n", io->i_bn); 15711083Ssam return ((daddr_t)0); 15811083Ssam } 159327Sbill blknos[j] = nb; 160327Sbill } 161327Sbill bap = (daddr_t *)b[j]; 1626068Smckusic sh /= NINDIR(&io->i_fs); 1636068Smckusic i = (bn / sh) % NINDIR(&io->i_fs); 164327Sbill nb = bap[i]; 165327Sbill if(nb == 0) { 166327Sbill printf("bn void %D\n",bn); 16710022Ssam return ((daddr_t)0); 168327Sbill } 169327Sbill } 17010022Ssam return (nb); 171327Sbill } 172327Sbill 1736068Smckusic /* 1746068Smckusic * get next entry in a directory. 1756068Smckusic */ 1766068Smckusic struct direct * 1776068Smckusic readdir(dirp) 1786068Smckusic register struct dirstuff *dirp; 179327Sbill { 1806068Smckusic register struct direct *dp; 1816068Smckusic register struct iob *io; 1826068Smckusic daddr_t lbn, d; 1836068Smckusic int off; 184327Sbill 1856068Smckusic io = dirp->io; 1866068Smckusic for(;;) { 187*40500Smckusick if (dirp->loc >= io->i_ino.di_size) 18811083Ssam return (NULL); 1896068Smckusic off = blkoff(&io->i_fs, dirp->loc); 1906068Smckusic if (off == 0) { 1916068Smckusic lbn = lblkno(&io->i_fs, dirp->loc); 1926068Smckusic d = sbmap(io, lbn); 1936068Smckusic if(d == 0) 19434463Sbostic return (NULL); 1956068Smckusic io->i_bn = fsbtodb(&io->i_fs, d) + io->i_boff; 1966068Smckusic io->i_ma = io->i_buf; 197*40500Smckusick io->i_cc = dblksize(&io->i_fs, &io->i_ino, lbn); 19811083Ssam if (devread(io) < 0) { 19911083Ssam errno = io->i_error; 20025166Skarels printf("bn %D: directory read error\n", 20125166Skarels io->i_bn); 20211083Ssam return (NULL); 20311083Ssam } 2046068Smckusic } 2056068Smckusic dp = (struct direct *)(io->i_buf + off); 2066068Smckusic dirp->loc += dp->d_reclen; 2076068Smckusic if (dp->d_ino == 0) 2086068Smckusic continue; 2096068Smckusic return (dp); 210327Sbill } 211327Sbill } 212327Sbill 213*40500Smckusick static ino_t 214*40500Smckusick dlook(s, io, dir) 215*40500Smckusick char *s; 216*40500Smckusick register struct iob *io; 217*40500Smckusick char *dir; 218*40500Smckusick { 219*40500Smckusick register struct direct *dp; 220*40500Smckusick register struct dinode *ip; 221*40500Smckusick struct dirstuff dirp; 222*40500Smckusick int len; 223*40500Smckusick 224*40500Smckusick if (s == NULL || *s == '\0') 225*40500Smckusick return (0); 226*40500Smckusick ip = &io->i_ino; 227*40500Smckusick if ((ip->di_mode&IFMT) != IFDIR) { 228*40500Smckusick printf("%s: not a directory\n", dir); 229*40500Smckusick return (0); 230*40500Smckusick } 231*40500Smckusick if (ip->di_size == 0) { 232*40500Smckusick printf("%s: zero length directory\n", dir); 233*40500Smckusick return (0); 234*40500Smckusick } 235*40500Smckusick len = strlen(s); 236*40500Smckusick dirp.loc = 0; 237*40500Smckusick dirp.io = io; 238*40500Smckusick for (dp = readdir(&dirp); dp != NULL; dp = readdir(&dirp)) { 239*40500Smckusick if(dp->d_ino == 0) 240*40500Smckusick continue; 241*40500Smckusick if (dp->d_namlen == len && !strcmp(s, dp->d_name)) 242*40500Smckusick return (dp->d_ino); 243*40500Smckusick } 244*40500Smckusick return (0); 245*40500Smckusick } 246*40500Smckusick 247327Sbill lseek(fdesc, addr, ptr) 24810022Ssam int fdesc, ptr; 24910022Ssam off_t addr; 250327Sbill { 251327Sbill register struct iob *io; 252327Sbill 25334463Sbostic #ifndef SMALL 25434463Sbostic if (ptr != L_SET) { 255327Sbill printf("Seek not from beginning of file\n"); 25610022Ssam errno = EOFFSET; 25710022Ssam return (-1); 258327Sbill } 25934463Sbostic #endif 260327Sbill fdesc -= 3; 2616068Smckusic if (fdesc < 0 || fdesc >= NFILES || 26210022Ssam ((io = &iob[fdesc])->i_flgs & F_ALLOC) == 0) { 26310022Ssam errno = EBADF; 26410022Ssam return (-1); 26510022Ssam } 266327Sbill io->i_offset = addr; 2676068Smckusic io->i_bn = addr / DEV_BSIZE; 268327Sbill io->i_cc = 0; 26910022Ssam return (0); 270327Sbill } 271327Sbill 272327Sbill getc(fdesc) 27310022Ssam int fdesc; 274327Sbill { 275327Sbill register struct iob *io; 2766068Smckusic register struct fs *fs; 277327Sbill register char *p; 2786068Smckusic int c, lbn, off, size, diff; 279327Sbill 280327Sbill 281327Sbill if (fdesc >= 0 && fdesc <= 2) 28210022Ssam return (getchar()); 283327Sbill fdesc -= 3; 2846068Smckusic if (fdesc < 0 || fdesc >= NFILES || 28510022Ssam ((io = &iob[fdesc])->i_flgs&F_ALLOC) == 0) { 28610022Ssam errno = EBADF; 28710022Ssam return (-1); 28810022Ssam } 289327Sbill p = io->i_ma; 290327Sbill if (io->i_cc <= 0) { 2916068Smckusic if ((io->i_flgs & F_FILE) != 0) { 292*40500Smckusick diff = io->i_ino.di_size - io->i_offset; 2936068Smckusic if (diff <= 0) 2946068Smckusic return (-1); 2956068Smckusic fs = &io->i_fs; 2966068Smckusic lbn = lblkno(fs, io->i_offset); 2976068Smckusic io->i_bn = fsbtodb(fs, sbmap(io, lbn)) + io->i_boff; 2986068Smckusic off = blkoff(fs, io->i_offset); 299*40500Smckusick size = dblksize(fs, &io->i_ino, lbn); 3006068Smckusic } else { 3016068Smckusic io->i_bn = io->i_offset / DEV_BSIZE; 3026068Smckusic off = 0; 3036068Smckusic size = DEV_BSIZE; 3046068Smckusic } 305327Sbill io->i_ma = io->i_buf; 3066068Smckusic io->i_cc = size; 30711083Ssam if (devread(io) < 0) { 30811083Ssam errno = io->i_error; 30911083Ssam return (-1); 31011083Ssam } 3116068Smckusic if ((io->i_flgs & F_FILE) != 0) { 312*40500Smckusick if (io->i_offset - off + size >= io->i_ino.di_size) 3136068Smckusic io->i_cc = diff + off; 314327Sbill io->i_cc -= off; 3156068Smckusic } 316327Sbill p = &io->i_buf[off]; 317327Sbill } 318327Sbill io->i_cc--; 319327Sbill io->i_offset++; 320327Sbill c = (unsigned)*p++; 321327Sbill io->i_ma = p; 32210022Ssam return (c); 323327Sbill } 3246068Smckusic 32510022Ssam int errno; 326327Sbill 327327Sbill read(fdesc, buf, count) 32810022Ssam int fdesc, count; 32910022Ssam char *buf; 330327Sbill { 33125166Skarels register i, size; 332327Sbill register struct iob *file; 33325166Skarels register struct fs *fs; 33425166Skarels int lbn, off; 335327Sbill 33610022Ssam errno = 0; 337327Sbill if (fdesc >= 0 & fdesc <= 2) { 338327Sbill i = count; 339327Sbill do { 340327Sbill *buf = getchar(); 341327Sbill } while (--i && *buf++ != '\n'); 34210022Ssam return (count - i); 343327Sbill } 344327Sbill fdesc -= 3; 3456068Smckusic if (fdesc < 0 || fdesc >= NFILES || 34610022Ssam ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) { 34710022Ssam errno = EBADF; 34810022Ssam return (-1); 34910022Ssam } 35010022Ssam if ((file->i_flgs&F_READ) == 0) { 35110022Ssam errno = EBADF; 35210022Ssam return (-1); 35310022Ssam } 35434463Sbostic #ifndef SMALL 3556068Smckusic if ((file->i_flgs & F_FILE) == 0) { 356327Sbill file->i_cc = count; 357327Sbill file->i_ma = buf; 3587446Sroot file->i_bn = file->i_boff + (file->i_offset / DEV_BSIZE); 359327Sbill i = devread(file); 36010022Ssam if (i < 0) 36110022Ssam errno = file->i_error; 36230547Skarels else 36330547Skarels file->i_offset += i; 36410022Ssam return (i); 365327Sbill } 36634463Sbostic #endif 367*40500Smckusick if (file->i_offset+count > file->i_ino.di_size) 368*40500Smckusick count = file->i_ino.di_size - file->i_offset; 36925166Skarels if ((i = count) <= 0) 37025166Skarels return (0); 37125166Skarels /* 37225166Skarels * While reading full blocks, do I/O into user buffer. 37325166Skarels * Anything else uses getc(). 37425166Skarels */ 37525166Skarels fs = &file->i_fs; 37625166Skarels while (i) { 37725166Skarels off = blkoff(fs, file->i_offset); 37825166Skarels lbn = lblkno(fs, file->i_offset); 379*40500Smckusick size = dblksize(fs, &file->i_ino, lbn); 38025166Skarels if (off == 0 && size <= i) { 38125166Skarels file->i_bn = fsbtodb(fs, sbmap(file, lbn)) + 38225166Skarels file->i_boff; 38325166Skarels file->i_cc = size; 38425166Skarels file->i_ma = buf; 38525166Skarels if (devread(file) < 0) { 38625166Skarels errno = file->i_error; 38725166Skarels return (-1); 38825166Skarels } 38925166Skarels file->i_offset += size; 39025166Skarels file->i_cc = 0; 39125166Skarels buf += size; 39225166Skarels i -= size; 39325166Skarels } else { 39425166Skarels size -= off; 39525166Skarels if (size > i) 39625166Skarels size = i; 39725166Skarels i -= size; 39825166Skarels do { 39925166Skarels *buf++ = getc(fdesc+3); 40025166Skarels } while (--size); 40125166Skarels } 40225166Skarels } 40325166Skarels return (count); 404327Sbill } 405327Sbill 40634463Sbostic #ifndef SMALL 407327Sbill write(fdesc, buf, count) 40810022Ssam int fdesc, count; 40910022Ssam char *buf; 410327Sbill { 411327Sbill register i; 412327Sbill register struct iob *file; 413327Sbill 41410022Ssam errno = 0; 415327Sbill if (fdesc >= 0 && fdesc <= 2) { 416327Sbill i = count; 417327Sbill while (i--) 418327Sbill putchar(*buf++); 41910022Ssam return (count); 420327Sbill } 421327Sbill fdesc -= 3; 4226068Smckusic if (fdesc < 0 || fdesc >= NFILES || 42310022Ssam ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) { 42410022Ssam errno = EBADF; 42510022Ssam return (-1); 42610022Ssam } 42710022Ssam if ((file->i_flgs&F_WRITE) == 0) { 42810022Ssam errno = EBADF; 42910022Ssam return (-1); 43010022Ssam } 431327Sbill file->i_cc = count; 432327Sbill file->i_ma = buf; 4337446Sroot file->i_bn = file->i_boff + (file->i_offset / DEV_BSIZE); 434327Sbill i = devwrite(file); 4357446Sroot file->i_offset += count; 43610022Ssam if (i < 0) 43710022Ssam errno = file->i_error; 43810022Ssam return (i); 439327Sbill } 44034463Sbostic #endif 441327Sbill 4423349Swnj int openfirst = 1; 44334463Sbostic u_int opendev; /* last device opened */ 44434463Sbostic extern u_int bootdev; 4453349Swnj 446327Sbill open(str, how) 4476068Smckusic char *str; 44810022Ssam int how; 449327Sbill { 45034463Sbostic register char *t; 45134463Sbostic register int cnt; 452327Sbill register struct iob *file; 45334463Sbostic int fdesc, args[8], *argp; 454327Sbill 4553349Swnj if (openfirst) { 45634463Sbostic for (cnt = 0; cnt < NFILES; cnt++) 45734463Sbostic iob[cnt].i_flgs = 0; 4583349Swnj openfirst = 0; 459327Sbill } 460327Sbill 46134463Sbostic for (fdesc = 0;; fdesc++) { 46234463Sbostic if (fdesc == NFILES) 46334463Sbostic _stop("No more file slots"); 46434463Sbostic if (iob[fdesc].i_flgs == 0) { 46534463Sbostic file = &iob[fdesc]; 46634463Sbostic file->i_flgs |= F_ALLOC; 46734463Sbostic file->i_adapt = file->i_ctlr = file->i_unit = 46834463Sbostic file->i_part = 0; 46934463Sbostic break; 47034463Sbostic } 47134463Sbostic } 472327Sbill 47334463Sbostic for (cnt = 0; cnt < sizeof(args)/sizeof(args[0]); args[cnt++] = 0); 47434463Sbostic #ifndef SMALL 47534463Sbostic for (t = str; *t && *t != '/' && *t != ':' && *t != '('; ++t) 47634463Sbostic if (isupper(*t)) 47734463Sbostic *t = tolower(*t); 47834463Sbostic switch(*t) { 47934463Sbostic case '(': /* type(adapt, ctlr, drive, partition)file */ 480*40500Smckusick if ((file->i_dev = getdev(str, t - str)) == -1) 48130547Skarels goto bad; 48234463Sbostic for (argp = args + 4, cnt = 0; *t != ')'; ++cnt) { 48334463Sbostic for (++t; isspace(*t); ++t); 48434463Sbostic if (*t == ')') 48534463Sbostic break; 48634463Sbostic if (!isdigit(*t)) 48730547Skarels goto badspec; 48834463Sbostic *argp++ = atoi(t); 48934463Sbostic for (++t; isdigit(*t); ++t); 49034463Sbostic if (*t != ',' && *t != ')' || cnt == 4) 49134463Sbostic goto badspec; 49230547Skarels } 49334463Sbostic for (++t; isspace(*t); ++t); 49434463Sbostic argp -= 4; 49534463Sbostic file->i_adapt = *argp++; 49634463Sbostic file->i_ctlr = *argp++; 49734463Sbostic file->i_unit = *argp++; 49834463Sbostic file->i_part = *argp; 49934463Sbostic break; 50034463Sbostic case ':': /* [A-Za-z]*[0-9]*[A-Za-z]:file */ 50134463Sbostic for (t = str; *t != ':' && !isdigit(*t); ++t); 502*40500Smckusick if ((file->i_dev = getdev(str, t - str)) == -1) 50330547Skarels goto bad; 50434463Sbostic if ((file->i_unit = getunit(t)) == -1) 50530547Skarels goto bad; 50634463Sbostic for (; isdigit(*t); ++t); 50734463Sbostic if (*t >= 'a' && *t <= 'h') 50834463Sbostic file->i_part = *t++ - 'a'; 50934463Sbostic if (*t != ':') { 51025166Skarels errno = EOFFSET; 51130547Skarels goto badspec; 51225166Skarels } 51334463Sbostic for (++t; isspace(*t); ++t); 51434463Sbostic break; 51534463Sbostic case '/': 51634463Sbostic default: /* default bootstrap unit and device */ 51734463Sbostic #else 51834463Sbostic { 51934463Sbostic #endif /* SMALL */ 520*40500Smckusick file->i_dev = B_TYPE(bootdev); 52134463Sbostic file->i_adapt = B_ADAPTOR(bootdev); 52234463Sbostic file->i_ctlr = B_CONTROLLER(bootdev); 52334463Sbostic file->i_unit = B_UNIT(bootdev); 52434463Sbostic file->i_part = B_PARTITION(bootdev); 52534463Sbostic t = str; 52625166Skarels } 52734463Sbostic 528*40500Smckusick opendev = MAKEBOOTDEV(file->i_dev, file->i_adapt, file->i_ctlr, 52934463Sbostic file->i_unit, file->i_part); 53034463Sbostic 53130547Skarels if (errno = devopen(file)) 53230547Skarels goto bad; 53334463Sbostic 53434463Sbostic if (*t == '\0') { 53534463Sbostic file->i_flgs |= how + 1; 53625166Skarels file->i_cc = 0; 53725166Skarels file->i_offset = 0; 53825166Skarels return (fdesc+3); 53925166Skarels } 54034463Sbostic #ifndef SMALL 54134463Sbostic else if (how != 0) { 54234463Sbostic printf("Can't write files yet.. Sorry\n"); 54334463Sbostic errno = EIO; 54434463Sbostic goto bad; 54534463Sbostic } 54633408Skarels #endif 5476068Smckusic file->i_ma = (char *)(&file->i_fs); 5486068Smckusic file->i_cc = SBSIZE; 54930768Skarels file->i_bn = SBOFF / DEV_BSIZE + file->i_boff; 5506068Smckusic file->i_offset = 0; 55111083Ssam if (devread(file) < 0) { 55211083Ssam errno = file->i_error; 55311083Ssam printf("super block read error\n"); 55430547Skarels goto bad; 55511083Ssam } 55634463Sbostic if ((cnt = find(t, file)) == 0) { 55710022Ssam errno = ESRCH; 55830547Skarels goto bad; 559327Sbill } 56034463Sbostic if (openi(cnt, file) < 0) { 56111083Ssam errno = file->i_error; 56230547Skarels goto bad; 56311083Ssam } 564327Sbill file->i_offset = 0; 565327Sbill file->i_cc = 0; 566327Sbill file->i_flgs |= F_FILE | (how+1); 56710022Ssam return (fdesc+3); 56830547Skarels 56933408Skarels #ifndef SMALL 57030547Skarels badspec: 57134463Sbostic printf("malformed device specification\nusage: device(adaptor, controller, drive, partition)file\n"); 57233408Skarels #endif 57330547Skarels bad: 57430547Skarels file->i_flgs = 0; 57530547Skarels return (-1); 576327Sbill } 577327Sbill 57830924Skarels #ifndef SMALL 57930547Skarels static 58030547Skarels getdev(str, len) 58133408Skarels register char *str; 58230547Skarels int len; 58330547Skarels { 58430547Skarels register struct devsw *dp; 58533408Skarels register int i; 58634463Sbostic char savedch = str[len]; 58730547Skarels 58834463Sbostic str[len] = '\0'; 58933408Skarels for (dp = devsw, i = 0; i < ndevs; dp++, i++) 59033408Skarels if (dp->dv_name && strcmp(str, dp->dv_name) == 0) { 59134463Sbostic str[len] = savedch; 59233408Skarels return (i); 59333408Skarels } 59433408Skarels printf("Unknown device\nKnown devices are:\n"); 59533408Skarels for (dp = devsw, i = 0; i < ndevs; dp++, i++) 59633408Skarels if (dp->dv_name) 59733408Skarels printf(" %s", dp->dv_name); 59834463Sbostic printf("\n"); 59930547Skarels errno = ENXIO; 60030547Skarels return (-1); 60130547Skarels } 60230547Skarels 60330547Skarels static 60430547Skarels getunit(cp) 60530547Skarels register char *cp; 60630547Skarels { 60734463Sbostic int unit; 60830547Skarels 60934463Sbostic unit = atoi(cp); 61034463Sbostic if ((u_int)unit > 255) { 61130547Skarels printf("minor device number out of range (0-255)\n"); 61230547Skarels errno = EUNIT; 61334463Sbostic return (-1); 61430547Skarels } 61534463Sbostic return (unit); 61630547Skarels } 61734463Sbostic #endif /* SMALL */ 61830547Skarels 619327Sbill close(fdesc) 62010022Ssam int fdesc; 621327Sbill { 622327Sbill struct iob *file; 623327Sbill 624327Sbill fdesc -= 3; 6256068Smckusic if (fdesc < 0 || fdesc >= NFILES || 62610022Ssam ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) { 62710022Ssam errno = EBADF; 62810022Ssam return (-1); 62910022Ssam } 630327Sbill if ((file->i_flgs&F_FILE) == 0) 631327Sbill devclose(file); 632327Sbill file->i_flgs = 0; 63310022Ssam return (0); 634327Sbill } 635327Sbill 63634463Sbostic #ifndef SMALL 63710022Ssam ioctl(fdesc, cmd, arg) 63810022Ssam int fdesc, cmd; 63910022Ssam char *arg; 64010022Ssam { 64110022Ssam register struct iob *file; 64210022Ssam int error = 0; 64310022Ssam 64410331Shelge fdesc -= 3; 64510022Ssam if (fdesc < 0 || fdesc >= NFILES || 64610022Ssam ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) { 64710022Ssam errno = EBADF; 64810022Ssam return (-1); 64910022Ssam } 65010022Ssam switch (cmd) { 65110022Ssam 65210022Ssam case SAIOHDR: 65310022Ssam file->i_flgs |= F_HDR; 65410022Ssam break; 65510022Ssam 65610022Ssam case SAIOCHECK: 65710022Ssam file->i_flgs |= F_CHECK; 65810022Ssam break; 65910022Ssam 66010022Ssam case SAIOHCHECK: 66110022Ssam file->i_flgs |= F_HCHECK; 66210022Ssam break; 66310022Ssam 66410331Shelge case SAIONOBAD: 66510331Shelge file->i_flgs |= F_NBSF; 66610331Shelge break; 66710331Shelge 66810331Shelge case SAIODOBAD: 66910331Shelge file->i_flgs &= ~F_NBSF; 67010331Shelge break; 67110331Shelge 67210022Ssam default: 67310022Ssam error = devioctl(file, cmd, arg); 67410022Ssam break; 67510022Ssam } 67610022Ssam if (error < 0) 67710022Ssam errno = file->i_error; 67810022Ssam return (error); 67910022Ssam } 68034463Sbostic #endif /* SMALL */ 68110022Ssam 682327Sbill exit() 683327Sbill { 684327Sbill _stop("Exit called"); 685327Sbill } 686327Sbill 687327Sbill _stop(s) 68810022Ssam char *s; 689327Sbill { 6902391Stoy int i; 6912391Stoy 6922391Stoy for (i = 0; i < NFILES; i++) 6932391Stoy if (iob[i].i_flgs != 0) 6942391Stoy close(i); 695327Sbill printf("%s\n", s); 696327Sbill _rtt(); 697327Sbill } 698