Hi Jim thank you for your reply!
I actually thought that what I had done was the problem but I think it is actually getopt() I will try to explain:
this first code takes care of a var that is set to either d,f or l (dir, file, or slink) the desired option in getopt().
char getFileMode(char charSetter) {
char fileType = 0; /*var to hold the value to be returned*/
charSetter=0;
fileType = charSetter;
return fileType; /*end function */
}
Then I have a function where I do all the tests to see if I can open a dir do lstat and then test for S_ISDIR, S_ISREG and S_ISLNK.
All seems to work fine in it.
This is the prototype of the funcion.
void dirTreeWalker(char fileType, char *dirName, char charSetter);
This is what it does
1.1 open dir
1.2 if dp == NULL print ERROR
2.1 else while dp != NULL
2.2 if not ".." and not "." lstat
2.3 if lstat(path, &info) < 0) print error
2.4 if ((S_ISDIR(info.st_mode))&&(fileType == 'd')) print path
else if ((S_ISREG(info.st_mode))&&( fileType == 'f')) print path
else if ((S_ISLNK(info.st_mode))&&(fileType == 'l')) print path
2.5 else print all thre options
2.6 closedir
So long for that function, I have tested it on its own and it works.
However, and here is the problem I think...
When I use getopt like I do as follows I don't get to see the directories nor the files. It is as if charSetter does not get assigned any value.
/*this set of vars for getopt*/
int c;
char *options = "t::dfl";
extern char *optarg;
extern int optind, optopt, opterr;
char dir = 'd',
file = 'f',
slink = 'l',
charSetter = 0;
/*this set of vars for the traversal and the test*/
char fileType = 0;
char *dirName = argv[argc-1];
/* char *fileName = */
while ((c = getopt(argc, argv, options)) != -1) {
switch (c) {
case 't':
optarg = options;
printf("Type is set: %c\n", charSetter);
printf("option: %c\n",c);
break;
case 'd':
printf("d is set\n");
charSetter = dir;
break;
case 'f':
printf("f is set\n");
charSetter = file;
printf("\ntype of file is f %c\n", fileType); /*test to be removed*/
break;
case 'l':
printf("l is set\n");
charSetter = slink;
printf("\ntype of file is l %c\n", fileType); /*test to be removed*/
break;
case ':':
printf("-%c without type\n", optopt);
break;
case '?':
printf("unknown arg %c\n", optopt);
fprintf(stderr, "Unrecognized option: -%c\n", optopt);
}
}
int i;
for(i=1;i<argc;i++){
printf("argv: %s\n",argv);
}
for (; optind < argc; optind++) {
fileType = getFileMode(charSetter);
dirTreeWalker(fileType, dirName, charSetter);
}