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