1 /* $NetBSD: fsdbutil.c,v 1.12 2002/07/20 08:36:26 grant Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by John T. Kohl. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 __RCSID("$NetBSD: fsdbutil.c,v 1.12 2002/07/20 08:36:26 grant Exp $"); 42 #endif /* not lint */ 43 44 #include <sys/types.h> 45 #include <sys/stat.h> 46 #include <sys/param.h> 47 #include <sys/time.h> 48 #include <sys/mount.h> 49 #include <ctype.h> 50 #include <fcntl.h> 51 #include <grp.h> 52 #include <pwd.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <time.h> 57 #include <unistd.h> 58 #include <err.h> 59 60 #include <ufs/ufs/dinode.h> 61 #include <ufs/ffs/fs.h> 62 63 #include "fsdb.h" 64 #include "fsck.h" 65 66 char ** 67 crack(line, argc) 68 char *line; 69 int *argc; 70 { 71 static char *argv[8]; 72 int i; 73 char *p, *val; 74 for (p = line, i = 0; p != NULL && i < 8; i++) { 75 while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0') 76 /**/ ; 77 if (val) 78 argv[i] = val; 79 else 80 break; 81 } 82 *argc = i; 83 return argv; 84 } 85 86 int 87 argcount(cmdp, argc, argv) 88 struct cmdtable *cmdp; 89 int argc; 90 char *argv[]; 91 { 92 if (cmdp->minargc == cmdp->maxargc) 93 warnx("command `%s' takes %u arguments", cmdp->cmd, 94 cmdp->minargc - 1); 95 else 96 warnx("command `%s' takes from %u to %u arguments", 97 cmdp->cmd, cmdp->minargc - 1, cmdp->maxargc - 1); 98 99 warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt); 100 return 1; 101 } 102 103 void 104 printstat(cp, inum, dp) 105 const char *cp; 106 ino_t inum; 107 struct dinode *dp; 108 { 109 struct group *grp; 110 struct passwd *pw; 111 time_t t; 112 char *p; 113 114 printf("%s: ", cp); 115 switch (iswap16(dp->di_mode) & IFMT) { 116 case IFDIR: 117 puts("directory"); 118 break; 119 case IFREG: 120 puts("regular file"); 121 break; 122 case IFBLK: 123 printf("block special (%d,%d)", 124 major(iswap32(dp->di_rdev)), minor(iswap32(dp->di_rdev))); 125 break; 126 case IFCHR: 127 printf("character special (%d,%d)", 128 major(iswap32(dp->di_rdev)), minor(iswap32(dp->di_rdev))); 129 break; 130 case IFLNK: 131 fputs("symlink", stdout); 132 if (iswap64(dp->di_size) > 0 && iswap64(dp->di_size) < MAXSYMLINKLEN && 133 dp->di_blocks == 0) 134 printf(" to `%.*s'\n", (int)iswap64(dp->di_size), 135 (char *)dp->di_shortlink); 136 else 137 putchar('\n'); 138 break; 139 case IFSOCK: 140 puts("socket"); 141 break; 142 case IFIFO: 143 puts("fifo"); 144 break; 145 } 146 printf("I=%u MODE=%o SIZE=%llu", inum, iswap16(dp->di_mode), 147 (unsigned long long)iswap64(dp->di_size)); 148 t = iswap32(dp->di_mtime); 149 p = ctime(&t); 150 printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20], 151 iswap32(dp->di_mtimensec)); 152 t = iswap32(dp->di_ctime); 153 p = ctime(&t); 154 printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20], 155 iswap32(dp->di_ctimensec)); 156 t = iswap32(dp->di_atime); 157 p = ctime(&t); 158 printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20], 159 iswap32(dp->di_atimensec)); 160 161 if ((pw = getpwuid(iswap32(dp->di_uid))) != NULL) 162 printf("OWNER=%s ", pw->pw_name); 163 else 164 printf("OWNUID=%u ", iswap32(dp->di_uid)); 165 if ((grp = getgrgid(iswap32(dp->di_gid))) != NULL) 166 printf("GRP=%s ", grp->gr_name); 167 else 168 printf("GID=%u ", iswap32(dp->di_gid)); 169 170 printf("LINKCNT=%hd FLAGS=0x%#x BLKCNT=0x%x GEN=0x%x\n", 171 iswap16(dp->di_nlink), 172 iswap32(dp->di_flags), iswap32(dp->di_blocks), 173 iswap32(dp->di_gen)); 174 } 175 176 int 177 checkactive() 178 { 179 if (!curinode) { 180 warnx("no current inode"); 181 return 0; 182 } 183 return 1; 184 } 185 186 int 187 checkactivedir() 188 { 189 if (!curinode) { 190 warnx("no current inode"); 191 return 0; 192 } 193 if ((iswap16(curinode->di_mode) & IFMT) != IFDIR) { 194 warnx("inode %d not a directory", curinum); 195 return 0; 196 } 197 return 1; 198 } 199 200 int 201 printactive() 202 { 203 if (!checkactive()) 204 return 1; 205 switch (iswap16(curinode->di_mode) & IFMT) { 206 case IFDIR: 207 case IFREG: 208 case IFBLK: 209 case IFCHR: 210 case IFLNK: 211 case IFSOCK: 212 case IFIFO: 213 printstat("current inode", curinum, curinode); 214 break; 215 case 0: 216 printf("current inode %d: unallocated inode\n", curinum); 217 break; 218 default: 219 printf("current inode %d: screwy itype 0%o (mode 0%o)?\n", 220 curinum, iswap16(curinode->di_mode) & IFMT, 221 iswap16(curinode->di_mode)); 222 break; 223 } 224 return 0; 225 } 226