sllul/drives.c

82 lines
2.4 KiB
C
Raw Normal View History

// This is NEW CODE to demonstrate the drvinf syscall by printing convenient
// information about drives to the console.
#include <stdio.h>
#include "../kernel/syscdefs.h"
#define DRIVE_SEARCH_MAX 1000
// Inline definition of the syscall
int drvinf(int drivenumber, struct __syscdefs_driveinfo* structure);
#define SIZEOUTPUTLENGTH 40
#define KB 1024ULL
#define MB (KB*1024)
#define GB (MB*1024)
#define TB (GB*1024)
void formatsizevalue(char* output, long long value) {
int ntb = value/TB;
value -= ntb*TB;
int ngb = value/GB;
value -= ngb*GB;
int nmb = value/MB;
value -= nmb*MB;
int nkb = value/KB;
value -= nkb*KB;
int nb = (int) value; // The remainder is any leftover bytes
if (ntb > 0) {
if (nb > 0) {
snprintf(output, SIZEOUTPUTLENGTH, "%dTB %dGB %dMB %dKB %d bytes", ntb, ngb, nmb, nkb, nb);
} else {
snprintf(output, SIZEOUTPUTLENGTH, "%dTB %dGB %dMB %dKB", ntb, ngb, nmb, nkb);
}
} else if (ngb > 0) {
if (nb > 0) {
snprintf(output, SIZEOUTPUTLENGTH, "%dGB %dMB %dKB %d bytes", ngb, nmb, nkb, nb);
} else {
snprintf(output, SIZEOUTPUTLENGTH, "%dGB %dMB %dKB", ngb, nmb, nkb);
}
} else if (nmb > 0) {
if (nb > 0) {
snprintf(output, SIZEOUTPUTLENGTH, "%dMB %dKB %d bytes", nmb, nkb, nb);
} else {
snprintf(output, SIZEOUTPUTLENGTH, "%dMB %dKB", nmb, nkb);
}
} else if (nkb > 0) {
if (nb > 0) {
snprintf(output, SIZEOUTPUTLENGTH, "%dKB %d bytes", nkb, nb);
} else {
snprintf(output, SIZEOUTPUTLENGTH, "%dKB", nkb);
}
} else {
snprintf(output, SIZEOUTPUTLENGTH, "%d bytes", nb);
}
}
char totalbuf[SIZEOUTPUTLENGTH];
char freebuf[SIZEOUTPUTLENGTH];
void printinfo(struct __syscdefs_driveinfo* info) {
//printf("[#%d] %s: %d * %lld, %lld free [%s]\n", info->drivenumber, info->name, info->blocksize, info->totalblocks, info->freedatablocks, info->fsname);
formatsizevalue(totalbuf, info->blocksize * info->totalblocks);
formatsizevalue(freebuf, info->blocksize * info->freedatablocks);
printf("[#%d] %s:\t%s total\t%s free\t[%s]\n", info->drivenumber, info->name, totalbuf, freebuf, info->fsname);
}
int main(int argc, char** argv) {
struct __syscdefs_driveinfo info;
//int isfirst = 1;
for (int i = 0; i < DRIVE_SEARCH_MAX; i++) {
int result = drvinf(i, &info);
if (result >= 0) {
/*if (isfirst) {
isfirst = 0;
} else {
printf("\n");
}*/
printinfo(&info);
}
}
}