Instructions
Requirements and Specifications
- (4pts.)
- (3pts.)
- (3pts.)
Parse the dirent struct to see if an entry is a directory or a file. If it is a directory, prepend "./" to the filename before printing it out. Include a main that does this below.
Open a file handle to each file, use lseek to determine the file's size in bytes and print out the file's size in bytes next to its name. Include a main that does this below.
Source Code
Program 0
#include <stdio.h>
#include <dirent.h>
int main()
{
DIR *dir;
struct dirent *dirent;
dir = opendir("."); /* open current directory */
if (dir == NULL)
{
printf("Error opening current directory\n");
return 1;
}
printf("Filenames:\n\n");
/* read all files in directory */
while ((dirent = readdir(dir)) != NULL)
{
printf("%s\n", dirent->d_name);
}
printf("\n");
closedir(dir);
return 0;
}
Program 1
#include <stdio.h>
#include <dirent.h>
int main()
{
DIR *dir;
struct dirent *dirent;
dir = opendir("."); /* open current directory */
if (dir == NULL)
{
printf("Error opening current directory\n");
return 1;
}
printf("Filenames:\n\n");
/* read all files in directory */
while ((dirent = readdir(dir)) != NULL)
{
if (dirent->d_type == DT_DIR)
printf("./"); /* append ./ to directories */
printf("%s\n", dirent->d_name);
}
printf("\n");
closedir(dir);
return 0;
}
Program 2
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
DIR *dir;
struct dirent *dirent;
int fd;
off_t filesize;
dir = opendir("."); /* open current directory */
if (dir == NULL)
{
printf("Error opening current directory\n");
return 1;
}
printf("Filename\tSize\n\n");
/* read all files in directory */
while ((dirent = readdir(dir)) != NULL)
{
/* print only files and directories */
if (dirent->d_type == DT_DIR)
printf("./"); /* append ./ to directories */
printf("%s", dirent->d_name); /* always print name */
if (dirent->d_type == DT_REG) {
fd = open(dirent->d_name, O_RDONLY); /* open read only */
if (fd != -1)
{
filesize = lseek(fd, 0, SEEK_END); /* move pointer to end of file */
close(fd);
printf("\t%ld", filesize);
}
}
printf("\n");
}
printf("\n");
closedir(dir);
return 0;
}
Program 3
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
int main()
{
DIR *dir;
struct dirent *entry;
int fd;
off_t filesize;
struct stat st;
dir = opendir("."); /* open current directory */
if (dir == NULL)
{
printf("Error opening current directory\n");
return 1;
}
printf("Filename\tSize\n\n");
/* read all files in directory */
while ((entry = readdir(dir)) != NULL)
{
/* print only files and directories */
if (stat(entry->d_name, &st) != -1) /* read file information */
{
switch (st.st_mode & S_IFMT) /* get file type */
{
case S_IFDIR:
printf("\x1B[34m"); /* set to light blue */
break;
case S_IFREG:
if (st.st_mode & S_IXUSR) /* if executable */
printf("\x1B[1;31m"); /* set to bold red */
else
printf("\x1B[92m"); /* set to light green */
break;
default:
printf("\x1B[37m"); /* set to white */
}
}
else /* unable to get stat */
printf("\x1B[37m"); /* set to white */
if (entry->d_type == DT_DIR)
printf("./"); /* append ./ to directories */
printf("%s", entry->d_name); /* always print name */
if (entry->d_type == DT_REG) {
fd = open(entry->d_name, O_RDONLY); /* open read only */
if (fd != -1)
{
filesize = lseek(fd, 0, SEEK_END); /* move pointer to end of file */
close(fd);
printf("\t%ld", filesize);
}
}
printf("\x1B[0m"); /* reset color */
printf("\n");
}
printf("\n");
closedir(dir);
return 0;
}