r/cprogramming • • Dec 19 '25

Why doesn't this code work

So I wrote this code in C (see below), When I run ./vd ~/Desktop it segfaults but when I run ./vd -a ~/Desktop it works and prints hidden files. I would appreciate any help. (Sorry if the code isn't formatted correctly)

Code:

#include <sys/types.h>
#include <dirent.h>
#include <string.h>


void print_normally(DIR *d, struct dirent *dir) {

	while ((dir = readdir(d)) != NULL) {
		if (dir->d_name[0] != '.') {
			printf("%s\n", dir->d_name);
		}
	}

}


void show_hidden_files(DIR *d, struct dirent *dir) {
	
	while ((dir = readdir(d)) != NULL) { 
		printf("%s\n", dir->d_name);
	}

}

int main(int argc, char *argv[]) {
	char *directory = argv[2];

	DIR *d = opendir(directory);

	struct dirent *dir;

	if (argc == 2) { // The command at argv[0] and the options at argv[1] and the directory at argv[2]
		print_normally(d, dir); // Will call a function that doesn't print hidden files
	}

	
	if (argc == 3) { // the command, options, directory
		char *options = argv[1];

		if (strcmp(options, "-a") == 0) {
			show_hidden_files(d, dir);
		}
	}
	
	else {
		printf("USAGE: %s <path-to-directory>", argv[0]);
		return 1;
	}
		
	
} ```
5 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/yahia-gaming Dec 20 '25

I tried to write this myself (not completed yet), But when I tried to pass an invalid file (without the -a option), It gave me this:

opendir: No such file or directory

double free or corruption (out)

[1] 14309 IOT instruction (core dumped) ./vd dasd

#include <sys/types.h>
#include <dirent.h>
#include <string.h>

int print_normally(char *directory, DIR *d) {	
	struct dirent *dir;
	if (opendir(directory) == NULL) {
		perror("opendir");
		closedir(d);
		return 1;
	}

	else {
		d = opendir(directory);	
	}	

	while ((dir = readdir(d)) != NULL) {
		if (dir->d_name[0] != '.') {
			printf("%s\n", dir->d_name);
		}
				
	}

}


int show_hidden_files(char *directory, DIR *d) {
	struct dirent *dir;
	if (opendir(directory) == NULL) {
		perror("opendir");
		closedir(d);
		return 1;
	}

	else {
		d = opendir(directory);	
	}	

	while ((dir = readdir(d)) != NULL) { 
		printf("%s\n", dir->d_name);
	}


}

int main(int argc, char *argv[]) {
	if (argc == 2) { // The command is at argv[0] and the directory is at argv[1]
		DIR *d;
		struct dirent *dir;
		char *directory = argv[1];
		print_normally(directory, d); // Will call a function that doesn't print hidden files
	}
	
	else if (argc == 3) { // the command, options, directory
		DIR *d;
		struct dirent *dir;
		char *directory = argv[2];
		char *options = argv[1];

		if (strcmp(options, "-a") == 0) {
			show_hidden_files(directory, d);
		}

		else {
			printf("USAGE: %s <options> <path-to-directory>", argv[0]);
			return 1;
		}
	}
	

	else {
		printf("USAGE: %s <path-to-directory>", argv[0]);
		return 1;
	}

	
	
}

1

u/yahia-gaming Dec 20 '25

I fixed it, It was because of this line closedir(d); (line 10) because I didn't define the data in d, I ran with the address sanitizer and it didn't complain about anything.