I've been studying UNIX and there's a example code on the book,
but this code seems not working. When I compile the code, returns segmentation error.
#include<dirent.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int my_double_ls(const char *name)
{
struct dirent *d;
DIR *dp;
if ((dp=opendir(name)) == NULL)
return (-1);
while (d=readdir(dp)) {
if (d->d_ino != 0)
printf("%s\n", d->d_name);
}
rewinddir(dp);
while (d = readdir(dp)) {
if (d->d_ino != 0)
printf("%s\n", d->d_name);
}
closedir(dp);
return(0);
}
int main(int argc, char **argv){
my_double_ls(argv[1]);
return(0);
}
You probably invoked the program without command line arguments:
Try this:
...
int main(int argc, char **argv){
if (argc < 2)
{
printf("You need to specify the directory.\n");
exit(1);
}
my_double_ls(argv[1]);
return 0;
}
The my_double_ls
function looks more or less correct to me.