cdirectorylsstatdirent.h

directory listing C (using dirent and stat)


Im making a directory listing application that prints a directory listing just like the ’ls’ and ’dir’ commands in Linux and Windows respec- tively.

my function: prints a listing of all files in the directory specified by path.

this is my code so far:

#include "ls.h"

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>

// Use this function to display the files. DO NOT CHANGE IT.
void _printLine(unsigned int size, unsigned int sizeOnDisk, const char* name)
{
    printf("%010u   %010u   %s\n", size, sizeOnDisk, name);
}

// Assume this to be the maximum length of a file name returned by readdir
#define MAX_FILE_NAME_LENGTH 255

int list(const char* path)
{
    (void) path;
    struct dirent *dent;
    struct stat s;
    DIR *dir;
    dir = opendir(".");
    if (!dir){
        perror("opendir");
        return -1;
    }

    errno = 0;
    while ((dent = readdir(dir)) != NULL){
        _printLine(s.st_size, s.st_blocks*512, dent->d_name);
    }
    closedir(dir);

    return 0;
}

Im trying to pass the "size" of the file and "size on disk" to the print function(using stat), while also passing the name of the file (using dirent). But i can't figure out how to implement this right, or if it is even possible?


Solution

  • You never called stat?

    $ gcc -Wall ls.c 
    $ ./a.out .
    0000000160   0000000000   .
    0000000608   0000000000   ..
    0000000947   0000004096   ls.c
    0000000811   0000004096   read.c
    0000049840   0000053248   a.out
    $ ls -l 
    total 120
    -rwxr-xr-x  1 anicolao  wheel  49840 Mar 18 08:39 a.out
    -rw-r--r--  1 anicolao  wheel    947 Mar 18 08:39 ls.c
    -rw-r--r--  1 anicolao  wheel    811 Mar 17 17:58 read.c
    $ cat ls.c
    
    //#include "ls.h"
    
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <dirent.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    // Use this function to display the files. DO NOT CHANGE IT.
    void _printLine(unsigned int size, unsigned int sizeOnDisk, const char* name)
    {
        printf("%010u   %010u   %s\n", size, sizeOnDisk, name);
    }
    
    // Assume this to be the maximum length of a file name returned by readdir
    #define MAX_FILE_NAME_LENGTH 255
    
    int list(const char* path)
    {
        (void) path;
        struct dirent *dent;
        struct stat s;
        DIR *dir;
        dir = opendir(".");
        if (!dir){
            perror("opendir");
            return -1;
        }
    
        errno = 0;
        while ((dent = readdir(dir)) != NULL){
            stat(dent->d_name, &s);
            _printLine(s.st_size, s.st_blocks*512, dent->d_name);
        }
        closedir(dir);
    
        return 0;
    }
    
    int main(int argc, char **argv) {
        return list(argv[1]);
    }
    

    There is also missing error checking, but I think the missing call will get you going.