/* My df command - it works for me ... */ #include #include #include struct mnt_info { char mnt_dospath[256]; char mnt_cygpath[256]; }; /* Types of drives we can df */ #define ALLOW_FIXED 1 #define ALLOW_REMOTE 2 #define ALLOW_CDROM 4 #define ALLOW_RAMDISK 8 #define ALLOW_REMOVABLE 16 #define ALLOW_ALL 31 /* Types of output */ #define BYTE 1 #define KILO 2 #define MEGA 3 #define HUMAN 4 #define CLUSTER 5 #define PRINT_TYPE 6 void usage() { fprintf(stderr, "Usage: df [OPTION]...\n"); fprintf(stderr, "Show information about the filesystems\n\n"); fprintf(stderr, " -a, --all include Remote and CD-ROM filesystems\n"); fprintf(stderr, " -c, --clusters list cluster information instead of block usage\n"); fprintf(stderr, " -h, --human-readable print sizes in human readable format (e.g. 1K 234M 2G\n"); fprintf(stderr, " -i, --inodes same as -c, in for compatibility\n"); fprintf(stderr, " -k, --kilobytes use 1024-byte blocks (default)\n"); fprintf(stderr, " -m, --megabytes use 1024K-byte blocks\n"); fprintf(stderr, " -t, --type TYPE limit listing to filesystems of type TYPE\n"); fprintf(stderr, " -x, --exclude-type TYPE limit listing to filesystems not of TYPE\n"); fprintf(stderr, " -v (ignored)\n"); fprintf(stderr, " -L List available filesystem types.\n"); /* fprintf(stderr, " -P, --portability use the POSIX output format\n");*/ fprintf(stderr, " -T, --print-type print filesystem type\n"); fprintf(stderr, " --help display this help and exit\n"); fprintf(stderr, " --version output version information and exit\n"); exit(0); } void human(char *str, long l) { int a=l, b=0; if (l < 1024) sprintf(str, "%4dB", l); else if (l < 1048576) sprintf(str, "%4dK", l/1024); else sprintf(str, "%4dM", l / 1048576); } void print_info(int out_type, DWORD a, DWORD b, DWORD c, DWORD d) { char tmp[20]; switch(out_type) { case HUMAN: if (a != 0) { human(tmp, a); printf(" %s ", tmp); } human(tmp, b); printf(" %s ", tmp); human(tmp, c); printf("%s ", tmp); human(tmp, d); printf(" %s ", tmp); break; case BYTE: printf(" %5d %5d %5d %5d ", a, b, c, d); break; case KILO: if (a != 0) printf(" %7ld ", a / 1024); printf(" %7ld %7ld %7ld ", b / 1024, c / 1024, d / 1024); break; case MEGA: printf(" %4ld %4ld %4ld ", b / 1048576, c / 1048576, d / 1048576); break; } } int main(int argc, char *argv[]) { long drivelist; DWORD SecPerClus, BytePerSec, FreeClus, TotClus, ClusSize; LPDWORD spc, bps, fc, tc; int i, j, k=0, l; char tmp[20], tmp2[20]; struct mnt_info mount_list[5]; int num_mounts = 0; struct mntent *mm; /* Setup parameters */ int fs_types = ALLOW_FIXED; /* default to only fixed disks */ int out_type = KILO; /* default to 1024-byte blocks */ int out_type2 = 0; /* Cluster info or not */ if (argc > 1) { for(i=1; imnt_fsname[1] == ':') && (mm->mnt_fsname[2] == '\\') ) { strcpy(mount_list[num_mounts].mnt_dospath, mm->mnt_fsname); strcpy(mount_list[num_mounts++].mnt_cygpath, mm->mnt_dir); } mm = getmntent(NULL); } spc = &SecPerClus; bps = &BytePerSec; fc = &FreeClus; tc = &TotClus; drivelist = GetLogicalDrives(); if (out_type2 == CLUSTER) printf("Drive CSize Clusters CUsed CFree %CUsed Mounted on\n"); else if (out_type == HUMAN) { printf("Drive "); if (out_type2 == PRINT_TYPE) printf(" Type "); printf("Size Used Avail Capacity Mounted on\n"); } else { printf("Drive "); if (out_type2 == PRINT_TYPE) printf(" Type "); if (out_type == MEGA) printf("MB-blocks"); else printf("1024-blocks"); printf(" Used Available Capacity Mounted on\n"); } for(i=0; i<26; i++) { if (drivelist % 2) { SecPerClus = BytePerSec = FreeClus = TotClus = 0; sprintf(tmp, "%c:\\", i + 'A'); j = GetDriveType(tmp); if ( (j == DRIVE_FIXED && fs_types & ALLOW_FIXED) || (j == DRIVE_REMOTE && fs_types & ALLOW_REMOTE) || (j == DRIVE_CDROM && fs_types & ALLOW_CDROM) || (j == DRIVE_RAMDISK && fs_types & ALLOW_RAMDISK) || (j == DRIVE_REMOVABLE && fs_types & ALLOW_REMOVABLE) ) { printf(" %c: ", i + 'A'); if (out_type2 == PRINT_TYPE) switch(j) { case DRIVE_FIXED: printf("FIXED "); break; case DRIVE_CDROM: printf("CDROM "); break; case DRIVE_REMOTE: printf("REMOTE "); break; case DRIVE_RAMDISK: printf("RAMDISK "); break; case DRIVE_REMOVABLE: printf("REMOVABLE "); break; } GetDiskFreeSpace(tmp, spc, bps, fc, tc); ClusSize = BytePerSec * SecPerClus; switch(out_type2) { case CLUSTER: print_info(out_type, ClusSize, TotClus, TotClus - FreeClus, FreeClus); break; default: print_info(out_type, 0, TotClus * ClusSize, (TotClus - FreeClus) * ClusSize, FreeClus * ClusSize); break; } printf("%3d%% ", 100 - 100 * FreeClus / TotClus); for(l=0; l