Thread: Fixing two errors in my code

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    4

    Fixing two errors in my code

    I'm writing a program that gets a pointer to the current directory stream and lists its contents as well as the contents for any child directories.

    My plan was to use two structs; one that contains information on the directory size in bytes and a ponter to the current open directory. The other struct's members are all the entries and a stat variable for the file size.

    I want to add all the entries to the entries array. As the child directories are read, I'll allocate memory to the dir_struct array. This happens in the collect_entries array but there are two errors. Dereferencing pointer to incomplete type & invalid use of incomplete type

    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <dirent.h>
    #include <unistd.h>
    #include <errno.h>
    #include <stdlib.h>
    
    
    typedef struct dir_struct {
        DIR* stream;              //will point to the directory stream
        off_t total_size;
    
    }directory_stream;
    
    typedef struct dirent_struct {
        struct dirent *entries;    //holds the dir enries in an entriesay & name of entry
        struct stat buf;              //struct type with the file stats
        
    }directory_entries;
    
    
    
    int dir_size = 0;
    int dir_memalloc = 3;
    int dirent_size = 0;
    int dirent_memalloc = 3;
    
    
    directory_entries* entries = NULL;
    directory_stream* stream = NULL;
    
    void collect_entries(DIR* stream) {
    
        if(dirent_size == dirent_memalloc || dirent_size == 0)
            //allocate memory to the dirent array
    
        while(  (entries[dirent_size].entries = readdir(stream[dir_size].stream)) 
        != NULL) {
            
        }
    
    }
    
    
    void mem_alloc(struct dirent* entries) {
    
        
    
    }
    
    
    int main() {
    
        stream[dir_size].stream = opendir(".");
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Start with the basics first, like
    - read the entries in the current directory
    - determine whether they're files or directories
    - make the function recursive on the directory entries
    - add the storage using malloc

    BTW, there's a FAQ entry to do just that.
    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
    Jul 2017
    Posts
    4
    Quote Originally Posted by Salem View Post
    Start with the basics first, like
    - read the entries in the current directory
    - determine whether they're files or directories
    - make the function recursive on the directory entries
    - add the storage using malloc

    BTW, there's a FAQ entry to do just that.
    I've managed to do what you mentioned in your previous post but now there is a problem where I'll get a segmentation fault error. I'm sure it has to do with the [code]mem_alloc()[\code] method. Can anybody point out the problem because I don't see it.

    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <dirent.h>
    #include <unistd.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <string.h>    
    
    DIR* str;
    struct dirent **dir_entries;
    
    off_t total_size;
    int str_size = 0;
    int str_memalloc = 0;
    
    int dir_size = 0;
    int dir_memalloc = 0;
    
    void collect_entries();
    void mem_alloc();
    void print_entries();
    
    
    
    void print_entries() {
    
        for(int i = 0; i < dir_size; i++) {
    
            printf("%s\n",dir_entries[i] -> d_name);
        }
    }
    
    
    
    void collect_entries(const char* p_name) {
        DIR* temp_dir = opendir(p_name);
        struct dirent* temp_ent;
        struct stat temp_buf;
    
        while(1) {
            if( (temp_ent = readdir(temp_dir)) == NULL) {
                return;
            }
    
            stat(temp_ent -> d_name, &temp_buf);
            if( strcmp(temp_ent -> d_name, "..") == 0
            || strcmp(temp_ent -> d_name, ".") == 0) {
    
                continue;
            }
    
            else if(S_ISDIR(temp_buf.st_mode) != 0) {
                mem_alloc(temp_ent);
                char path[256];
                strcpy(path, temp_ent -> d_name);
                printf("%s\n", path);
                collect_entries(path);
            }
                
    
            else {
                printf("%s\n",temp_ent->d_name);
                mem_alloc(temp_ent);
            }
            
        }
        
        closedir(str);
    
    }
    
    
    
    void mem_alloc(struct dirent* temp_ent) {
        if(dir_size == dir_memalloc) {
    
            if(dir_memalloc == 0) {
            
                dir_memalloc = 3;        
            }
            else 
                dir_memalloc += 8;
    
            void** temp = realloc(dir_entries, dir_memalloc *
            sizeof(struct dirent*));
    
            if(!temp) {
                perror("Memory allocation failed errno: ");
            }
            dir_entries = (struct dirent**) temp;
            
        }
        
        dir_entries[dir_size++] = temp_ent;
    }
    
    
    
    int main() {
        collect_entries(".");
        print_entries();
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2015, 02:45 AM
  2. Assistance in fixing errors
    By llind212 in forum C++ Programming
    Replies: 1
    Last Post: 04-13-2011, 01:16 PM
  3. Fixing Errors
    By brietje698 in forum Networking/Device Communication
    Replies: 9
    Last Post: 12-10-2007, 11:17 PM
  4. Errors i'm having trouble fixing
    By yaniv89 in forum Windows Programming
    Replies: 5
    Last Post: 08-26-2005, 02:35 PM
  5. Fixing errors!
    By Zophixan in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2002, 06:18 PM

Tags for this Thread