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