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