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?