• 0

[C (UNIX)] Directories


Question

Hello. I'm having some problems with this code, that shall become some kind of a "locate" copy (it's a college assignment).

I'm guessing that the issue resides in the file type testing macros (S_ISLNK(), S_ISDIR(), S_ISREG()). To run do program you shall type a directory (full path, ex. /usr) as the first (and only) argument, then, it is supposed to list all the directories, regular files and links but it lists some links as directories and other directories aren't even being listed.

Here's the code.

#include "sys/types.h"
#include "sys/stat.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include "dirent.h"
#include "stdio.h"

#define MAX_SIZE 2048

int main(int argc, char* argv[]) {	
	DIR* teste;
	char* actual = (char*) malloc(MAX_SIZE);

	struct stat file;
	struct dirent* info = (struct dirent*) malloc(MAX_SIZE);

	teste = opendir(argv[1]);
	info = readdir(teste);


	while (readdir(teste)!=NULL) {
		stat(actual,&file);

		info = readdir(teste);
		actual = info->d_name;

		if (strcmp(actual,"..")==0 ||
			 strcmp(actual,".")==0) continue;

		if (S_ISLNK(file.st_mode)) {
			printf("%s is a link\n",actual);
			continue;
		}

		else if (S_ISREG(file.st_mode)) { 
			printf("%s is a file\n",actual); 
		}

		else if (S_ISDIR(file.st_mode)) {
			printf("%s is a folder\n",actual);
		}
	}
	closedir(teste);
	return 0;
}

Running example:

(...)$ ./ldir /usr

only prints:

lib is a folder
sbin is a folder

in spite of /usr containing seven other folders.

Another running example:

(...)$ ./ldir /

prints

initrd.img.old is a folder
usr is a folder
dev is a folder
opt is a folder
etc is a folder
lib is a folder
tmp is a folder
selinux is a folder
boot is a folder
sys is a folder

in spite of initrd.img.old being a link.

I can't see what's wrong with it (the code) so, if anyone could help I'd be very thankful.

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I think the problem is that inside the while loop you're reassigning the info pointer by re-reading readdir. This is already done by the while loop.

I tried commenting out this line and testing it in one of my folders and it seemed to be giving the correct result.

Also, you're malloc'ing two separate blocks of memory and you're not freeing them when the program finishes. In order to prevent free() from crashing you will need to store the pointer that's returned when the malloc functions returns. This will not be the same value of the pointer at the end of the program.

Link to comment
Share on other sites

  • 0
I think the problem is that inside the while loop you're reassigning the info pointer by re-reading readdir. This is already done by the while loop.

I tried commenting out this line and testing it in one of my folders and it seemed to be giving the correct result.

Also, you're malloc'ing two separate blocks of memory and you're not freeing them when the program finishes. In order to prevent free() from crashing you will need to store the pointer that's returned when the malloc functions returns. This will not be the same value of the pointer at the end of the program.

thanks ViZioN, that while thing helped. i totally forgot about freeing the pointers :s

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.