Hello and thanks for reading.
I am trying to build a program in C which is run in the Linux Shell, that accepts an argument (directory) and lists all the files in that directory, as well as whether they are a regular file, link file or directory. I need to use OpenDir()/ReadDir() and stat. Here is my code so far: (I'm a bit stuck) at the moment it only reads a file and outputs the type. How do I allow my program to read ALL files in a directory, and output their name AND what type of file they are?
#define _BSD_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>
int
main(int argc, char *argv[])
{
struct stat sb;
/*
if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(EXIT_FAILURE);
}*/
if (stat(argv[1], &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("dir "); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("lnk "); break;
case S_IFREG: printf("reg "); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
printf("%s\n", argv[1]);
exit(EXIT_SUCCESS);
}
So I rewrote everything and now I am receiving some output on whether it's a link reg or dir file, however I am only ever receiving that a file is a Directory, even when it is clearly not (a text file or a .c file etc). here is the new code:
#define _BSD_SOURCE
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
void typeOfFile(struct stat info){
if (S_ISREG(info.st_mode)) {
printf("reg");
}
if (S_ISDIR(info.st_mode)) {
printf("dir");
}
if (S_ISLNK(info.st_mode)) {
printf("lnk");
}
}
int main(int argc, char *argv[]) {
DIR *dirp;
struct dirent* dent;
struct stat info;
typeOfFile(info);
//If no args
if(argc == 1){
argv[1] = ".";
dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
do {
dent = readdir(dirp);
if (dent)
{
//////////////////////////////////////////////////////////////////////////
if (stat(argv[1], &info) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
switch (info.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("dir "); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("lnk "); break;
case S_IFREG: printf("reg "); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
//////////////////////////////////////////////////////////////////////////
printf("%s \n", dent->d_name);
}
} while (dent);
closedir(dirp);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//If specified directory
if(argc > 1){
dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
do {
dent = readdir(dirp);
if (dent)
{
/////////////////////////////////////////////////////////////////////////////////////
if (stat(argv[1], &info) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
switch (info.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("dir "); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("lnk "); break;
case S_IFREG: printf("reg "); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
//////////////////////////////////////////////////////////////////////////////////////
// printf("%s\n", argv[1]);
printf("%s \n", dent->d_name);
}
} while (dent);
closedir(dirp);
}
return 0;
}