can't check if file is dir

Apr 25, 2010 at 12:32am
can someone please tell me why i can't check if the file is a directory? thanks
this is system programming on UNIX console.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"minifind.c" 64 lines, 1307 characters
/*minifind.c
 *      
 *      Problem Statement:  a program that finds a file under directories.
 *      Input:  command type
 *      Output:  paths where files are
 *      Usage:  minifind dir filename
 */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
void where_filename(char *, char *);
char * mk_dirname(char *);
main(int ac, char *av[])
{

        if( ac != 3){
                fprintf( stderr, "usage: %s source destination\n", *av);
                exit(1);
        }

        where_filename(av[1],av[2]);
}
void where_filename(char * dir_name, char * file_name){
        DIR * dir_ptr;
        struct dirent * direntp;
        struct stat info;
        char * tmp_dirname;

        if( (dir_ptr = opendir(dir_name)) == NULL)
                fprintf(stderr,"minifind: cannot open %s\n", dir_name);
        else
        {
                while((direntp = readdir( dir_ptr) ) != NULL ){

                        if((!strcmp(direntp->d_name, ".") == 0) && (!strcmp(direntp->d_name, "..") == 0)){
                                stat(direntp->d_name, &info);
                                 // can't check if file is directory
                                if(S_ISDIR(info.st_mode)){ printf("%s\n", direntp->d_name);
                                        return where_filename(direntp->d_name,file_name);
                        }
                }
                        if( strcmp(direntp->d_name,file_name) == 0){
                                printf("%s/%s\n", dir_name, file_name);

                        }

                }
                closedir(dir_ptr);
        }
}

}
Apr 25, 2010 at 2:46am
I don't see why it shouldn't work.

Your indentation is horrific. I'm not sure your program is working the way you think it is. Also, do you really want to return after your recursive call on line 45?

Examples of directory handling:
http://www.cplusplus.com/forum/unices/12452/
http://www.cplusplus.com/forum/unices/3548/#msg15009

Hope this helps.
Topic archived. No new replies allowed.