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