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