Plz help me to debug my program

This is a discussion on Plz help me to debug my program within the Linux Programming forums, part of the Platform Specific Boards category; I write a program to search file or directory from current directory by user's input. Below is my code. Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    1

    Plz help me to debug my program

    I write a program to search file or directory from current directory by user's input.
    Below is my code.
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <string.h>
    
    static int cnt = 0;
    
    void in_dir(char *path, char *find) {
       DIR *dirp;
       struct dirent *dp;
       struct stat sbuf;
       char *name;
       char *new_path;
       
       dirp = opendir(path);
       if (!dirp) {
          perror("opendir()");
          return;
       }
       
       path = strcat(path, "/");
       new_path = path;
       
       while ((dp = readdir(dirp)) != NULL) {
          if (strstr(dp->d_name, find) == NULL) {
             if (S_ISDIR(sbuf.st_mode)) {
                in_dir(new_path, find);
             }
             continue;
          }
          
          if (stat(dp->d_name, &sbuf) == -1) {
             perror("stat()");                   /* Has problem here */
             return;
          }
          
          name = dp->d_name;
          if (S_ISDIR(sbuf.st_mode)) {
          	 new_path = strcat(new_path, name);
             printf("\t%s\n", new_path);
             in_dir(new_path, find);
          }
          else
             printf("\t%s%s\n", path, name);
          
          ++cnt;
       }
    }
    
    int main(int argc, char *argv[]) {
       char *cur_path;
       char buf[100];
       char input[10];
       
       printf("File or directory name you want to search: ");
       /*fgets(input, 10, stdin);*/
       scanf("%s", &input);
       
       /*printf("You wanna to search '%s'\n", input);*/
       /* Start to search from current directory. */
       cur_path = getcwd(buf, 100);
       in_dir(cur_path, input);
       
       printf("%d matches.\n", cnt);
       return(0);
    }
    If files are located in current directory, my program works well.
    However, if files are located in sub-directory, my program goes to wrong.
    The error message is :
    stat(): No such file or directory
    But I am sure the filename it passed to is exist.
    I have no idea how to debug it.
    Could anyone help me? Very thanks!!!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why not print out the filename of what you're actually stat'ing.
    Code:
          if (stat(dp->d_name, &sbuf) == -1) {
             perror(dp->d_name);
             return;
          }
    Also, this
    scanf("%s", &input);
    should be
    scanf("%s", input);
    or even better, use fgets() but make sure you get rid of the newline character that fgets() puts into the buffer for you.

    For sample code, see the FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1044780608
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please debug this program
    By TheDeveloper in forum C Programming
    Replies: 9
    Last Post: 01-07-2009, 10:00 AM
  2. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21