Listing files in a user input specified directory

This is a discussion on Listing files in a user input specified directory within the C Programming forums, part of the General Programming Boards category; I am trying to list all the files in a directory specified by the user. I want the user to ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    12

    Listing files in a user input specified directory

    I am trying to list all the files in a directory specified by the user. I want the user to input "dir <directory>", where <directory> is obviously the specified folder. Currently if I input something like "dir /home" the directory cant be found. However if I input something like "dir /home sdlgjhsdlgsdljg" it works... lol. As long as input a 3rd string to be tokenized it works. Not sure why it does this.

    Code:
    #include <dirent.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <errno.h>
    
    int main( int argc, char * argv[] )
    {
        char *prompt = getcwd(NULL, 0);
        char *cmd = NULL;
        char *cmdCopy[100];
        char * tokenPtr;
        int bytes_read = 0;
        size_t nbytes = 100;
        int cmdCount = 0;
        int i = 0;
        pid_t pid;
        int done = 0;
        DIR *dp;
        struct dirent *ep;
        char ch;
        
        
        while(!done)
        {
            //take input
                while( bytes_read <= 1)
                {
                    printf("[%s ~]$ " , prompt);
                    bytes_read = getline(&cmd, &nbytes, stdin);
                }
        
            //tokenize
                tokenPtr = strtok(cmd, " \n");
                while(tokenPtr != NULL)
                {
                cmdCopy[cmdCount] = tokenPtr;
                ++cmdCount;
                tokenPtr = strtok(NULL, " ");
                }
            cmdCopy[cmdCount] = NULL;
            
            //display cmdCopy[]
            int l = 0;
            for( l = 0; l <= cmdCount; ++l )
            {
                printf("cmdCopy[%d] = %s\n", l, cmdCopy[l]);
            }    
            
            //determine if first arg is dir   
            if( strcmp(cmdCopy[0], "dir") == 0)                //dir
            {
                //make sure a second arg was supplied        
                if( cmdCopy[1] == NULL )
                {
                    printf("<Direcotry not specified.\n");
                }    
                else
                {
                    //display contents
                    dp = opendir( cmdCopy[1] );
                    printf("Getting files in cwd\n");
                    if( dp != NULL )
                    {
                        while( ep = readdir( dp ))
                            puts ( ep->d_name);
                        
                        (void) closedir(dp);
                    }
                    else
                        perror("Couldnt open directory");
                }
            }    
            done = 1;
        }
        return 1;
    }
    Last edited by BigLe2e; 07-20-2013 at 12:28 PM.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    826
    Modify line 64 to e.g.
    Code:
                    printf("Contents of directory '%s'\n", cmdCopy[1]);
    so you can verify which directory you're trying to list.

    If the directory is correct (I doubt that), the error is very weird. However, if the directory is not correct, and I don't think you've parsed it correctly, the error is in your command parsing. The way the directory is incorrect will give you a hint where the problem lies.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You should include \n as well as space here:
    tokenPtr = strtok(NULL, " ");

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    826
    ^ spoiler!

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    12
    Quote Originally Posted by Nominal Animal View Post
    Modify line 64 to e.g.
    Code:
                    printf("Contents of directory '%s'\n", cmdCopy[1]);
    so you can verify which directory you're trying to list.

    If the directory is correct (I doubt that), the error is very weird. However, if the directory is not correct, and I don't think you've parsed it correctly, the error is in your command parsing. The way the directory is incorrect will give you a hint where the problem lies.
    I have a similar printf to display the contents of cmdCopy[1] in the complete code I'm compiling but removed it to make this post a little shorter and easier to read, but the way I had it wasnt showing that the second strtok was leaving out \n.

    I think you know this is the snippet of my custom shell I posted in another thread you helped me with. I really appreciate your help on both threads! Im just about done with the assignment, I just need to finished the "cd" command and program execution on fork() and I'm done, yay! Been working on this all week and silly errors like forgetting \n and ; have been eating me alive lol. Thanks again, wouldnt have made it this far without your support.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    12
    Quote Originally Posted by oogabooga View Post
    You should include \n as well as space here:
    tokenPtr = strtok(NULL, " ");
    Yep, I made that mistake in my other code post on my first thread too. I fixed it there but like all my errors, theyre reoccurring lol. Thanks for pointing it out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2008, 09:02 AM
  2. Listing specific files in a directory
    By knirirr in forum C Programming
    Replies: 14
    Last Post: 01-29-2008, 04:42 AM
  3. Listing files in a directory.
    By Devil Panther in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2006, 08:39 PM
  4. directory listing
    By warney_out in forum C Programming
    Replies: 3
    Last Post: 02-02-2005, 12:12 AM
  5. Directory Listing
    By Angelus in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2002, 09:32 AM

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