Thread: ls -l help

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    25

    ls -l help

    I currently have this code:

    Code:
    void printdir(char *dir, int depth)
    {
        DIR *dp;
        struct dirent *entry;
        struct stat statbuf;
        
        FILE *file;  /* declare a FILE pointer  */
        file = fopen("outputs.txt", "w");
         
    	if((dp = opendir(dir)) == NULL) {
            fprintf(stderr,"cannot open directory: %s\n", dir);
            return;
        }
        chdir(dir);
        
    while((entry = readdir(dp)) != NULL) {
            if(S_ISDIR(statbuf.st_mode)) {
                /* Found a directory, but ignore . and .. */
                if(strcmp(".",entry->d_name) == 0 ||
                    strcmp("..",entry->d_name) == 0)
                    continue;
                printf("%*s%s/\n",depth,"",entry->d_name);
    	    fprintf(file,"%*s%s/\n",depth,"",entry->d_name);
    	    
                /* Recurse at a new indent level */
                printdir(entry->d_name,depth+4);
            }
            else printf("%*s%s\n",depth,"",entry->d_name);
    	fprintf(file,"%*s%s\n",depth,"",entry->d_name);
        }
        chdir("..");
        closedir(dp);
    }
    For some reason it doesn't seem to enter directories and print out there contents as intended.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well reopening the output file in each recursive instance won't be good.

    > else printf("%*s%s\n",depth,"",entry->d_name);
    > fprintf(file,"%*s%s\n",depth,"",entry->d_name);
    Always use braces - one of these statements is not part of the else
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    25

    Thanx

    Thanx I think I will go with to directory's any chance of help in trying to display file size, permissions etc

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > void printdir(char *dir, int depth)
    Well I would change this to
    void printdir(char *dir, int depth, FILE *fp)

    That is, you pass in the opened file where you want all the output to go.

    Then you can do
    Code:
    FILE *fp = fopen( "file.txt", "w" );
    printdir( ".", 0, fp );  // to file
    printdir( ".", 0, stdout ); // to screen
    > /* Recurse at a new indent level */
    > printdir(entry->d_name,depth+4);
    I think I would just put the two chdir() calls around this, since you may need to chdir into several sub-directories within a given directory.

    BTW, this is in the FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1044780608
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To Make A Quick Sort For LS
    By chris Ray in forum C Programming
    Replies: 13
    Last Post: 11-20-2008, 05:25 PM
  2. version of ls -l bug, only takes cwd :(
    By chl12 in forum C Programming
    Replies: 2
    Last Post: 06-12-2007, 02:44 AM
  3. Turtle CVS and sourceforge, no 'ls'
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 03:19 AM
  4. Re-writing ls -F in C++
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 03-21-2002, 08:39 AM
  5. FTP 'ls' one screen at a time? Or how to?
    By rainbow in forum Linux Programming
    Replies: 2
    Last Post: 02-23-2002, 05:39 PM