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