Home | History | Annotate | Line # | Download | only in fsdb
fsdbutil.c revision 1.6
      1 /*	$NetBSD: fsdbutil.c,v 1.6 1997/07/31 00:21:59 jtc 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 #ifndef lint
     40 static char rcsid[] = "$NetBSD: fsdbutil.c,v 1.6 1997/07/31 00:21:59 jtc Exp $";
     41 #endif /* not lint */
     42 
     43 #include <sys/types.h>
     44 #include <sys/stat.h>
     45 #include <sys/param.h>
     46 #include <sys/time.h>
     47 #include <sys/mount.h>
     48 #include <ctype.h>
     49 #include <fcntl.h>
     50 #include <grp.h>
     51 #include <pwd.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 #include <err.h>
     57 
     58 #include <ufs/ufs/dinode.h>
     59 #include <ufs/ffs/fs.h>
     60 
     61 #include "fsdb.h"
     62 #include "fsck.h"
     63 
     64 char **
     65 crack(line, argc)
     66 	char *line;
     67 	int *argc;
     68 {
     69     static char *argv[8];
     70     int i;
     71     char *p, *val;
     72     for (p = line, i = 0; p != NULL && i < 8; i++) {
     73 	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
     74 	    /**/;
     75 	if (val)
     76 	    argv[i] = val;
     77 	else
     78 	    break;
     79     }
     80     *argc = i;
     81     return argv;
     82 }
     83 
     84 int
     85 argcount(cmdp, argc, argv)
     86 	struct cmdtable *cmdp;
     87 	int argc;
     88 	char *argv[];
     89 {
     90     if (cmdp->minargc == cmdp->maxargc)
     91 	warnx("command `%s' takes %u arguments", cmdp->cmd, cmdp->minargc-1);
     92     else
     93 	warnx("command `%s' takes from %u to %u arguments",
     94 	      cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
     95 
     96     warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
     97     return 1;
     98 }
     99 
    100 void
    101 printstat(cp, inum, dp)
    102 	const char *cp;
    103 	ino_t inum;
    104 	struct dinode *dp;
    105 {
    106     struct group *grp;
    107     struct passwd *pw;
    108     time_t t;
    109     char *p;
    110 
    111     printf("%s: ", cp);
    112     switch (dp->di_mode & IFMT) {
    113     case IFDIR:
    114 	puts("directory");
    115 	break;
    116     case IFREG:
    117 	puts("regular file");
    118 	break;
    119     case IFBLK:
    120 	printf("block special (%d,%d)",
    121 	       major(dp->di_rdev), minor(dp->di_rdev));
    122 	break;
    123     case IFCHR:
    124 	printf("character special (%d,%d)",
    125 	       major(dp->di_rdev), minor(dp->di_rdev));
    126 	break;
    127     case IFLNK:
    128 	fputs("symlink",stdout);
    129 	if (dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
    130 	    dp->di_blocks == 0)
    131 	    printf(" to `%.*s'\n", (int) dp->di_size, (char *)dp->di_shortlink);
    132 	else
    133 		putchar('\n');
    134 	break;
    135     case IFSOCK:
    136 	puts("socket");
    137 	break;
    138     case IFIFO:
    139 	puts("fifo");
    140 	break;
    141     }
    142     printf("I=%u MODE=%o SIZE=%qu", inum, dp->di_mode, dp->di_size);
    143     t = dp->di_mtime;
    144     p = ctime(&t);
    145     printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
    146 	   dp->di_mtimensec);
    147     t = dp->di_ctime;
    148     p = ctime(&t);
    149     printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
    150 	   dp->di_ctimensec);
    151     t = dp->di_atime;
    152     p = ctime(&t);
    153     printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
    154 	   dp->di_atimensec);
    155 
    156     if ((pw = getpwuid(dp->di_uid)) != NULL)
    157 	printf("OWNER=%s ", pw->pw_name);
    158     else
    159 	printf("OWNUID=%u ", dp->di_uid);
    160     if ((grp = getgrgid(dp->di_gid)) != NULL)
    161 	printf("GRP=%s ", grp->gr_name);
    162     else
    163 	printf("GID=%u ", dp->di_gid);
    164 
    165     printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", dp->di_nlink, dp->di_flags,
    166 	   dp->di_blocks, dp->di_gen);
    167 }
    168 
    169 int
    170 checkactive()
    171 {
    172     if (!curinode) {
    173 	warnx("no current inode\n");
    174 	return 0;
    175     }
    176     return 1;
    177 }
    178 
    179 int
    180 checkactivedir()
    181 {
    182     if (!curinode) {
    183 	warnx("no current inode\n");
    184 	return 0;
    185     }
    186     if ((curinode->di_mode & IFMT) != IFDIR) {
    187 	warnx("inode %d not a directory", curinum);
    188 	return 0;
    189     }
    190     return 1;
    191 }
    192 
    193 int
    194 printactive()
    195 {
    196     if (!checkactive())
    197 	return 1;
    198     switch (curinode->di_mode & IFMT) {
    199     case IFDIR:
    200     case IFREG:
    201     case IFBLK:
    202     case IFCHR:
    203     case IFLNK:
    204     case IFSOCK:
    205     case IFIFO:
    206 	printstat("current inode", curinum, curinode);
    207 	break;
    208     case 0:
    209 	printf("current inode %d: unallocated inode\n", curinum);
    210 	break;
    211     default:
    212 	printf("current inode %d: screwy itype 0%o (mode 0%o)?\n",
    213 	       curinum, curinode->di_mode & IFMT, curinode->di_mode);
    214 	break;
    215     }
    216     return 0;
    217 }
    218