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