The C program is a file listing utility that displays information about files in a specified directory. It uses command line arguments to display the listing in a tree style. The program iterates through the files in the specified directory and shows all the files and folders in specific directory with the file size, last modified date and other file attributes.
This program works like the dir /p command of the Windows operating system or ls command in Unix.
Key Components of the Program:
Header Files:
<stdio.h>
: Standard input/output functions.<stdlib.h>
: General utility functions, including memory allocation.<string.h>
: String manipulation functions.<ctype.h>
: Functions for character classification.<direct.h>
: Functions for directory manipulation.
Structures and Union:
struct ftime
: Represents the timestamp of a file, breaking down the time and date components.union f
: A union of an array and theftime
structure, used for extracting file timestamp information.
Functions:
getdiskno(const char a[])
: Determines the current disk number based on the provided drive letter or the current working drive.ret(int a)
: Retrieves information about available space on a disk.countdig(long int a)
: Counts the number of digits in a given number.
Main Function:
- Accepts command-line arguments (
argc
andargv
). - Processes command line arguments to determine the directory to list and optional filtering based on file attributes.
- It uses
_findfirst
and_findnext
functions to iterate through files in the specified directory. - Prints details such as filename, size, timestamp, etc.
- Pauses and clears the screen after every 20 files for better readability.
Command-line Parameters:
The program accepts the following command line parameters:
- Directory Path:
- If provided, specifies the directory to list files from.
- Example:
program.exe C:\Users\Username\Documents
- Filter Mode:
- Optional parameter to filter files based on attributes.
- Options:
r
: Read-only files.h
: Hidden files.d
: Directories.s
: System files.a
: Archive files.l
: Volume labels.
- Example:
program.exe C:\Users\Username\Documents r
Source code of the Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <direct.h> #include <io.h> struct ftime { unsigned ft_tsec : 5; unsigned ft_min : 6; unsigned ft_hour : 5; unsigned ft_day : 5; unsigned ft_month : 4; unsigned ft_year : 7; }; union f { int a[2]; struct ftime b; } c; int getdiskno(const char a[]) { if (a[1] == ':') { return(tolower(a[0]) - 'a' + 1); } return (_getdrive() + 1); } long int ret(int a) { struct _diskfree_t b; _getdiskfree(a, &b); return (long int)((long int)(b.avail_clusters) * (long int)(b.sectors_per_cluster) * (long int)(b.bytes_per_sector)); } int countdig(long int a) { int count = 0; if (a == 0) return 1; while (a != 0) { a = a / 10; count++; } return count; } int main(int argc, char* argv[]) { int count = 0, mode = 0, i; long int totsize = 0; struct _finddata_t a; intptr_t handle; char string[15]; if (argc >= 2) { strcpy(string, argv[1]); } else { strcpy(string, "*.*"); } if (argc >= 3) { if (strcmp(argv[2], "r") == 0) mode = _A_RDONLY; if (strcmp(argv[2], "h") == 0) mode = _A_HIDDEN; if (strcmp(argv[2], "d") == 0) mode = _A_SUBDIR; if (strcmp(argv[2], "s") == 0) mode = _A_SYSTEM; if (strcmp(argv[2], "a") == 0) mode = _A_ARCH; } if ((handle = _findfirst(string, &a)) == -1) { printf("\nCould not find a file to match your criterion\n"); exit(0); } count++; printf("\nListing files...\n"); totsize += a.size; printf("\n%s", a.name); for (i = 0; i < (15 - strlen(a.name)); i++) printf(" "); printf("%ld", a.size); c.a[0] = a.time_access; for (i = 0; i < (10 - countdig(a.size)); i++) printf(" "); if (c.b.ft_day < 10) printf("0"); printf("%u-", c.b.ft_day); if (c.b.ft_month < 10) printf("0"); printf("%u-%u", c.b.ft_month, c.b.ft_year + 1980); printf(" "); if (c.b.ft_hour < 10) printf("0"); printf("%u:", c.b.ft_hour); if (c.b.ft_min < 10) printf("0"); printf("%u:", c.b.ft_min); if (c.b.ft_tsec < 10) printf("0"); printf("%u", c.b.ft_tsec); while (_findnext(handle, &a) == 0) { count++; printf("\n%s", a.name); totsize += a.size; for (i = 0; i < (15 - strlen(a.name)); i++) printf(" "); printf("%ld", a.size); c.a[0] = a.time_access; for (i = 0; i < (10 - countdig(a.size)); i++) printf(" "); if (c.b.ft_day < 10) printf("0"); printf("%u-", c.b.ft_day); if (c.b.ft_month < 10) printf("0"); printf("%u-%u", c.b.ft_month, c.b.ft_year + 1980); printf(" "); if (c.b.ft_hour < 10) printf("0"); printf("%u:", c.b.ft_hour); if (c.b.ft_min < 10) printf("0"); printf("%u:", c.b.ft_min); if (c.b.ft_tsec < 10) printf("0"); printf("%u", c.b.ft_tsec); if (count % 20 == 0) { if (count != 20) printf("\n"); printf("\n\nListed %d files", count); printf("\nPress a key for the next page...."); getch(); system("cls"); } } _findclose(handle); printf("\n\nListed %d files\n%ld bytes in all\n", count, totsize); printf("\n%ld bytes available in drive %c:", ret(getdiskno(string)), 'a' + getdiskno(string) - 1); return 0; } |