8000 minix/commands/simple/df.c at R3.1.4 · blackskygg/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
executable file
·
442 lines (374 loc) · 11.3 KB

df.c

File metadata and controls

executable file
·
442 lines (374 loc) · 11.3 KB
}
}
closedir(dp);
return &unknown; /* Case 4. */
}
/* (num / tot) in percentages rounded up. */
#define percent(num, tot) ((int) ((100L * (num) + ((tot) - 1)) / (tot)))
/* One must be careful printing all these _t types. */
#define L(n) ((long) (n))
int df(const struct mtab *mt)
{
int fd;
bit_t i_count, z_count;
block_t totblocks, busyblocks;
int n, block_size;
struct v12_super_block super, *sp;
/* Don't allow Joe User to df just any device. */
seteuid(*mt->mountpoint == 0 ? ruid : euid);
setegid(*mt->mountpoint == 0 ? rgid : egid);
if ((fd = open(mt->devname, O_RDONLY)) < 0) {
fprintf(stderr, "df: %s: %s\n", mt->devname, strerror(errno));
return(1);
}
lseek(fd, (off_t) SUPER_BLOCK_BYTES, SEEK_SET); /* skip boot block */
if (read(fd, (char *) &super, sizeof(super)) != (int) sizeof(super)) {
fprintf(stderr, "df: Can't read super block of %s\n", mt->devname);
close(fd);
return(1);
}
sp = &super;
if (sp->s_magic != SUPER_V1 && sp->s_magic != SUPER_V2
&& sp->s_magic != SUPER_V3) {
fprintf(stderr, "df: %s: Not a valid file system\n", mt->devname);
close(fd);
return(1);
}
if(sp->s_magic != SUPER_V3) block_size = _STATIC_BLOCK_SIZE;
else block_size = super.s_block_size;
if(block_size < _MIN_BLOCK_SIZE || block_size > _MAX_BLOCK_SIZE) {
fprintf(stderr, "df: %s: funny block size (%d)\n",
mt->devname, block_size);
close(fd);
return(1);
}
if (sp->s_magic == SUPER_V1) sp->s_zones= sp->s_nzones;
lseek(fd, (off_t) block_size * 2L, SEEK_SET); /* skip rest of super block */
i_count = bit_count(sp->s_imap_blocks, (bit_t) (sp->s_ninodes+1),
fd, block_size);
if (i_count == -1) {
fprintf(stderr, "df: Can't find bit maps of %s\n", mt->devname);
close(fd);
return(1);
}
i_count--; /* There is no inode 0. */
/* The first bit in the zone map corresponds with zone s_firstdatazone - 1
* This means that there are s_zones - (s_firstdatazone - 1) bits in the map
*/
z_count = bit_count(sp->s_zmap_blocks,
(bit_t) (sp->s_zones - (sp->s_firstdatazone - 1)), fd, block_size);
if (z_count == -1) {
fprintf(stderr, "df: Can't find bit maps of %s\n", mt->devname);
close(fd);
return(1);
}
/* Don't forget those zones before sp->s_firstdatazone - 1 */
z_count += sp->s_firstdatazone - 1;
#ifdef __minix_vmd
totblocks = sp->s_zones;
busyblocks = z_count;
#else
totblocks = (block_t) sp->s_zones << sp->s_log_zone_size;
busyblocks = (block_t) z_count << sp->s_log_zone_size;
#endif
busyblocks = busyblocks * (block_size/512) / (unitsize/512);
totblocks = totblocks * (block_size/512) / (unitsize/512);
/* Print results. */
printf("%s", mt->devname);
n= strlen(mt->devname);
if (n > 15 && istty) { putchar('\n'); n= 0; }
while (n < 15) { putchar(' '); n++; }
if (!Pflag && !iflag) {
printf(" %9ld %9ld %9ld %3d%% %3d%% %s\n",
L(totblocks), /* Blocks */
L(totblocks - busyblocks), /* free */
L(busyblocks), /* used */
percent(busyblocks, totblocks), /* % */
percent(i_count, sp->s_ninodes), /* FUsed% */
mt->mountpoint /* Mounted on */
);
}
if (!Pflag && iflag) {
printf(" %9ld %9ld %9ld %3d%% %3d%% %s\n",
L(sp->s_ninodes), /* Files */
L(sp->s_ninodes - i_count), /* free */
L(i_count), /* used */
percent(i_count, sp->s_ninodes), /* % */
percent(busyblocks, totblocks), /* BUsed% */
mt->mountpoint /* Mounted on */
);
}
if (Pflag && !iflag) {
printf(" %9ld %9ld %9ld %4d%% %s\n",
L(totblocks), /* Blocks */
L(busyblocks), /* Used */
totblocks - busyblocks, /* Available */
percent(busyblocks, totblocks), /* Capacity */
mt->mountpoint /* Mounted on */
);
}
if (Pflag && iflag) {
printf(" %9ld %9ld %9ld %4d%% %s\n",
L(sp->s_ninodes), /* Inodes */
L(i_count), /* IUsed */
L(sp->s_ninodes - i_count), /* IAvail */
percent(i_count, sp->s_ninodes), /* Capacity */
mt->mountpoint /* Mounted on */
);
}
close(fd);
return(0);
}
bit_t bit_count(unsigned blocks, bit_t bits, int fd, int block_size)
{
char *wptr;
int i, b;
bit_t busy;
char *wlim;
static char buf[_MAX_BLOCK_SIZE];
static char bits_in_char[1 << CHAR_BIT];
/* Precalculate bitcount for each char. */
if (bits_in_char[1] != 1) {
for (b = (1 << 0); b < (1 << CHAR_BIT); b <<= 1)
for (i = 0; i < (1 << CHAR_BIT); i++)
if (i & b) bits_in_char[i]++;
}
/* Loop on blocks, reading one at a time and counting bits. */
busy = 0;
for (i = 0; i < blocks && bits != 0; i++) {
if (read(fd, buf, block_size) != block_size) return(-1);
wptr = &buf[0];
if (bits >= CHAR_BIT * block_size) {
wlim = &buf[block_size];
bits -= CHAR_BIT * block_size;
} else {
b = bits / CHAR_BIT; /* whole chars in map */
wlim = &buf[b];
bits -= b * CHAR_BIT; /* bits in last char, if any */
b = *wlim & ((1 << bits) - 1); /* bit pattern from last ch */
busy += bits_in_char[b];
bits = 0;
}
/* Loop on the chars of a block. */
while (wptr != wlim)
busy += bits_in_char[*wptr++ & ((1 << CHAR_BIT) - 1)];
}
return(busy);
}
/*
* $PchId: df.c,v 1.7 1998/07/27 18:42:17 philip Exp $
*/
0