#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    12

    pass adresses to recursive functions


    Can i pass address to a recursive function?

    below code finds all files in a dir and store it in a pointer array with files full path.. But in the directory if a subdirectory exists, to list the subdir's files i have to send the subdir path to open_mydir function again. I have to use here recursion.. But i coulndt handle it.. Any idea.. Thanx for help..

    char ** open_mydir(char dir[]) {

    struct dirent *dptr;
    struct stat st;
    char *buff;
    char **excldir;
    DIR *dirp;

    myarr = (char **) malloc(100 * sizeof(char **));
    subdir = (char **) malloc(100 * sizeof(char **));


    int i=0,p=0;

    if( (dirp = opendir(dir)) == NULL )
    {
    fprintf(stderr,"Error opening %s ",buff);
    perror("dirlist");
    exit(1);
    }

    while(dptr = readdir(dirp)) {
    if ((strcmp(dptr->d_name, ".") == 0 || strcmp(dptr->d_name, "..") == 0)) {
    continue;
    }

    if (stat (dptr->d_name, &st) != -1 && S_ISDIR (st.st_mode)) {

    if (NULL == (subdir[p] = (char *) malloc(strlen(dptr->d_name) + strlen(dir) + 1))) {
    fprintf(stderr, "Unable to allocate memory.\n");
    exit(1);
    }
    sprintf(subdir[p],"%s%s",strdup(dir),strdup(dptr->d_name));
    p++;
    continue;
    }

    if (NULL == (myarr[i] = (char *) malloc(strlen(dptr->d_name) + strlen(dir) + 1))) {
    fprintf(stderr, "Unable to allocate memory.\n");
    exit(1);
    }
    sprintf(myarr[i],"%s%s",strdup(dir),strdup(dptr->d_name));
    i++;

    }

    /*here i have to use recursion in my dir 2 subdir exist for ex..
    if i uncomment below it blows up..*/

    /* int k;
    for(k=0; k<1; k++) {
    open_mydir(subdir[k]);
    }*/

    closedir(dirp);

    return myarr;
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    12

    SOLVED


    okay i solved the problem.. with declaring 2 global variable and calling the open_mydir function recursivly in main() ,the problem solved ..

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <time.h>

    /*------------------------------------Prototypes */

    char ** open_mydir(char dir[]);
    char *file_modified_time(char * filename);


    //-------------------------------Global variables declerations */
    char stbuff[10];
    char dir[] = "/home/jazzy/cfiles/";
    char **myarr;
    char **subdir;

    int f_i=0,s_i=0;

    int main() {

    char *file_st[100];
    char **ptr_files;
    char *output = NULL;
    char **loop;
    int i=0;

    ptr_files = open_mydir(dir);

    while(*subdir != NULL) {
    open_mydir(*subdir);
    *subdir++;
    }

    while(*ptr_files != NULL) {
    printf("%s\n",ptr_files[i]);
    *ptr_files++;
    }


    return 0;
    }


    char ** open_mydir(char dir[]) {

    struct dirent *dptr;
    struct stat st;
    char *buff;
    char **excldir;
    DIR *dirp;

    if (myarr == NULL ) {
    myarr = (char **) malloc(100 * sizeof(char **));
    subdir = (char **) malloc(100 * sizeof(char **));
    }


    if( (dirp = opendir(dir)) == NULL )
    {
    fprintf(stderr,"Error opening %s ",buff);
    perror("dirlist");
    exit(1);
    }

    while(dptr = readdir(dirp)) {
    if ((strcmp(dptr->d_name, ".") == 0 || strcmp(dptr->d_name, "..") == 0)) {
    continue;
    }

    if (stat (dptr->d_name, &st) != -1 && S_ISDIR (st.st_mode)) {

    if (NULL == (subdir[s_i] = (char *) malloc(strlen(dptr->d_name) + strlen(dir) + 1))) {
    fprintf(stderr, "Unable to allocate memory.\n");
    exit(1);
    }
    sprintf(subdir[s_i],"%s%s",strdup(dir),strdup(dptr->d_name));
    s_i++;
    continue;
    }

    if (NULL == (myarr[f_i] = (char *) malloc(strlen(dptr->d_name) + strlen(dir) + 1))) {
    fprintf(stderr, "Unable to allocate memory.\n");
    exit(1);
    }
    sprintf(myarr[f_i],"%s%s",strdup(dir),strdup(dptr->d_name));
    f_i++;

    }


    closedir(dirp);

    return myarr;

    }

IMN logo majestic logo threadwatch logo seochat tools logo