xref: /csrg-svn/sbin/clri/clri.c (revision 38503)
1 static char sccsid[] = "@(#)clri.c 2.5 07/30/89";
2 
3 /* static char *sccsid = "@(#)clri.c	4.1 (Berkeley) 10/1/80"; */
4 
5 /*
6  * clri filsys inumber ...
7  */
8 
9 #include <sys/param.h>
10 #include <sys/time.h>
11 #include <sys/vnode.h>
12 #include <ufs/inode.h>
13 #include <ufs/fs.h>
14 
15 #define ISIZE	(sizeof(struct dinode))
16 #define	NI	(MAXBSIZE/ISIZE)
17 struct	ino {
18 	char	junk[ISIZE];
19 };
20 struct	ino	buf[NI];
21 
22 union {
23 	char		dummy[SBSIZE];
24 	struct fs	sblk;
25 } sb_un;
26 #define sblock sb_un.sblk
27 
28 int	status;
29 long	dev_bsize = 1;
30 
31 main(argc, argv)
32 	int argc;
33 	char *argv[];
34 {
35 	register i, f;
36 	unsigned n;
37 	int j, k;
38 	long off;
39 
40 	if (argc < 3) {
41 		printf("usage: clri filsys inumber ...\n");
42 		exit(4);
43 	}
44 	f = open(argv[1], 2);
45 	if (f < 0) {
46 		printf("cannot open %s\n", argv[1]);
47 		exit(4);
48 	}
49 	lseek(f, SBOFF, 0);
50 	if (read(f, &sblock, SBSIZE) != SBSIZE) {
51 		printf("cannot read %s\n", argv[1]);
52 		exit(4);
53 	}
54 	if (sblock.fs_magic != FS_MAGIC) {
55 		printf("bad super block magic number\n");
56 		exit(4);
57 	}
58 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
59 	for (i = 2; i < argc; i++) {
60 		if (!isnumber(argv[i])) {
61 			printf("%s: is not a number\n", argv[i]);
62 			status = 1;
63 			continue;
64 		}
65 		n = atoi(argv[i]);
66 		if (n == 0) {
67 			printf("%s: is zero\n", argv[i]);
68 			status = 1;
69 			continue;
70 		}
71 		off = fsbtodb(&sblock, itod(&sblock, n)) * dev_bsize;
72 		lseek(f, off, 0);
73 		if (read(f, (char *)buf, sblock.fs_bsize) != sblock.fs_bsize) {
74 			printf("%s: read error\n", argv[i]);
75 			status = 1;
76 		}
77 	}
78 	if (status)
79 		exit(status);
80 	for (i = 2; i < argc; i++) {
81 		n = atoi(argv[i]);
82 		printf("clearing %u\n", n);
83 		off = fsbtodb(&sblock, itod(&sblock, n)) * dev_bsize;
84 		lseek(f, off, 0);
85 		read(f, (char *)buf, sblock.fs_bsize);
86 		j = itoo(&sblock, n);
87 		for (k = 0; k < ISIZE; k++)
88 			buf[j].junk[k] = 0;
89 		lseek(f, off, 0);
90 		write(f, (char *)buf, sblock.fs_bsize);
91 	}
92 	exit(status);
93 }
94 
95 isnumber(s)
96 	char *s;
97 {
98 	register c;
99 
100 	while(c = *s++)
101 		if (c < '0' || c > '9')
102 			return(0);
103 	return(1);
104 }
105