int open(char *str, int how) { register char *cp; register struct iob *file; int i, fdesc; #if CHECK_CAREFULLY /* iob[] is in BSS, so it is guaranteed to be zero. */ if (open_init == 0) { for (i = 0; i < NFILES; i++) iob[i].i_flgs = 0; open_init = 1; } #endif for (fdesc = 0; fdesc < NFILES; fdesc++) if (iob[fdesc].i_flgs == 0) goto gotfile; stop("Out of file descriptor slots"); gotfile: (file = &iob[fdesc])->i_flgs |= F_ALLOC; if ((cp = xx(str, file)) == (char *) -1) { close(fdesc); return -1; } if (*cp == '\0') { file->i_flgs |= how+1; file->i_cc = 0; file->i_offset = 0; return (fdesc); } file->i_cc = SBSIZE; file->i_bn = (SBLOCK / DEV_BSIZE) + file->i_boff; file->i_offset = 0; if (file->i_fs == 0) { if (fs_block == 0) { fs_block = (struct fs *)malloc(SBSIZE); } if (fs_block_valid == 0) { file->i_ma = (char *)fs_block; if (devread(file) < 0) { #ifndef SMALL error(SUPERBLOCK_ERROR, 1); #endif close(fdesc); return (-1); } byte_swap_superblock(fs_block); fs_block_valid = 1; } file->i_fs = fs_block; file->i_buf = malloc(MAXBSIZE); } #if BIG_ENDIAN_FS if (file->i_fs->fs_magic != FS_MAGIC) { error(SUPERBLOCK_ERROR, 2); close(fdesc); return (-1); } /* * The following is a gross hack to boot disks that have an actual * blocksize of 512 bytes but were written with a theoretical 1024 * byte blocksize (fsbtodb == 0). * * We can make this assumption because we can only boot disks with * a 512 byte sector size. */ if (file->i_fs->fs_fsize == 0) { error(SUPERBLOCK_ERROR,3); close(fdesc); return (-1); } file->i_fs->fs_fsbtodb = ffs(file->i_fs->fs_fsize / DEV_BSIZE) - 1; #endif BIG_ENDIAN_FS if ((i = find(cp, file)) == 0) { close(fdesc); return (-1); } #if CHECK_CAREFULLY if (how != 0) { error("Can't write files\n"); close(fdesc); return (-1); } #endif CHECK_CAREFULLY if (openi(i, file) < 0) { close(fdesc); return (-1); } file->i_offset = 0; file->i_cc = 0; file->i_flgs |= F_FILE | (how+1); return (fdesc); }
long UFSInitPartition( CICell ih ) { #if !BOOT1 long ret; #endif if (ih == gCurrentIH) { #ifdef __i386__ CacheInit(ih, gBlockSize); #endif return 0; } #if !BOOT1 verbose("UFSInitPartition: %x\n", ih); #endif gCurrentIH = 0; #ifdef __i386__ if (!gULBuf) gULBuf = (char *) malloc(UFS_LABEL_SIZE); if (!gFSBuf) gFSBuf = (char *) malloc(SBSIZE); if (!gTempName) gTempName = (char *) malloc(MAXNAMLEN + 1); if (!gTempName2) gTempName2 = (char *) malloc(MAXNAMLEN + 1); if (!gRootInodePtr) gRootInodePtr = (InodePtr) malloc(sizeof(Inode)); if (!gFileInodePtr) gFileInodePtr = (InodePtr) malloc(sizeof(Inode)); if (!gULBuf || !gFSBuf || !gTempName || !gTempName2 || !gRootInodePtr || !gFileInodePtr) return -1; #endif // Assume there is no Disk Label gPartitionBase = 0; #if !BOOT1 // read the disk label to get the UUID // (rumor has it that UFS headers can be either-endian on disk; hopefully // that isn't true for this UUID field). Seek(ih, gPartitionBase + UFS_LABEL_OFFSET); ret = Read(ih, (long)&gUFSLabel, UFS_LABEL_SIZE); if(ret != 0) bzero(&gUFSLabel, UFS_LABEL_SIZE); #endif /* !BOOT1 */ // Look for the Super Block Seek(ih, gPartitionBase + SBOFF); Read(ih, (long)gFSBuf, SBSIZE); gFS = (struct fs *)gFSBuf; byte_swap_superblock(gFS); if (gFS->fs_magic != FS_MAGIC) { return -1; } ih->modTime = gFS->fs_time; // Calculate the block size and set up the block cache. gBlockSize = gFS->fs_bsize; gFragSize = gFS->fs_fsize; gFragsPerBlock = gBlockSize / gFragSize; if (gTempBlock != 0) free(gTempBlock); gTempBlock = malloc(gBlockSize); CacheInit(ih, gBlockSize); gCurrentIH = ih; // Read the Root Inode ReadInode(ROOTINO, gRootInodePtr, 0, 0); return 0; }