speed-ups and clean-ups
[unix-history] / usr / src / sbin / clri / clri.c
CommitLineData
87ad4e58 1static char sccsid[] = "@(#)clri.c 2.2 %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
KM
32int status;
33
34main(argc, argv)
e335baf5
KM
35 int argc;
36 char *argv[];
be79e615
KM
37{
38 register i, f;
39 unsigned n;
40 int j, k;
41 long off;
42
e335baf5 43 if (argc < 3) {
be79e615
KM
44 printf("usage: clri filsys inumber ...\n");
45 exit(4);
46 }
47 f = open(argv[1], 2);
e335baf5 48 if (f < 0) {
be79e615
KM
49 printf("cannot open %s\n", argv[1]);
50 exit(4);
51 }
e335baf5
KM
52 lseek(f, SBLOCK * DEV_BSIZE, 0);
53 if (read(f, &sblock, SBSIZE) != SBSIZE) {
54 printf("cannot read %s\n", argv[1]);
55 exit(4);
56 }
87ad4e58
KM
57 if (sblock.fs_magic != FS_MAGIC) {
58 printf("bad super block magic number\n");
59 exit(4);
60 }
e335baf5
KM
61 for (i = 2; i < argc; i++) {
62 if (!isnumber(argv[i])) {
be79e615
KM
63 printf("%s: is not a number\n", argv[i]);
64 status = 1;
65 continue;
66 }
67 n = atoi(argv[i]);
e335baf5 68 if (n == 0) {
be79e615
KM
69 printf("%s: is zero\n", argv[i]);
70 status = 1;
71 continue;
72 }
6994bf5d 73 off = fsbtodb(&sblock, itod(&sblock, n)) * DEV_BSIZE;
be79e615 74 lseek(f, off, 0);
e335baf5 75 if (read(f, (char *)buf, sblock.fs_bsize) != sblock.fs_bsize) {
be79e615
KM
76 printf("%s: read error\n", argv[i]);
77 status = 1;
78 }
79 }
e335baf5 80 if (status)
be79e615 81 exit(status);
e335baf5 82 for (i = 2; i < argc; i++) {
be79e615
KM
83 n = atoi(argv[i]);
84 printf("clearing %u\n", n);
6994bf5d 85 off = fsbtodb(&sblock, itod(&sblock, n)) * DEV_BSIZE;
be79e615 86 lseek(f, off, 0);
e335baf5 87 read(f, (char *)buf, sblock.fs_bsize);
6994bf5d 88 j = itoo(&sblock, n);
e335baf5 89 for (k = 0; k < ISIZE; k++)
be79e615
KM
90 buf[j].junk[k] = 0;
91 lseek(f, off, 0);
e335baf5 92 write(f, (char *)buf, sblock.fs_bsize);
be79e615
KM
93 }
94 exit(status);
95}
96
97isnumber(s)
e335baf5 98 char *s;
be79e615
KM
99{
100 register c;
101
102 while(c = *s++)
e335baf5 103 if (c < '0' || c > '9')
be79e615
KM
104 return(0);
105 return(1);
106}