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 the ftime 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 and argv).
  • 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:

  1. Directory Path:
    • If provided, specifies the directory to list files from.
    • Example: program.exe C:\Users\Username\Documents
  2. 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: