Question

I have a function that reads all files contained in a single input directory.
I wanted to make that function read not only files in "main" directory, but also ones contained in all subdirectories.

In order to do so, i wrote this code:

#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>

struct dirent *readdir(DIR *dirp);
char * percorso;
DIR *cartella;
struct dirent *elemento;



char * scrivi(char * a, char * b)
{

char *targetdir = malloc(2048);
strcpy(targetdir,a);
strcat(targetdir,"/");
strcat(targetdir,b);

printf("%s \n", targetdir); 
return targetdir;
}    

void scorriFolder(char* nome)
{
if ((cartella = opendir(nome)) == NULL)         
    perror("opendir() error");                      
else {
    printf("contents of root: \n");
    while ((elemento = readdir(cartella)) != NULL)
    {                                               
        if(elemento->d_type == DT_DIR)
        {
            if(elemento->d_name != ".." || elemento->d_name != ".")
            {
                percorso = scrivi(nome, elemento->d_name);
                scorriFolder(percorso);
            }
        }
        else 
        {
            printf("  %s\n", elemento->d_name); 
        }
    }
    closedir(cartella);                         
}
}    

main(int argc, char * argv[]) {
scorriFolder(argv[1]);
} 

But it doesn't even compile, saying:

warning: incompatible implicit declaration of built-in function ‘malloc’
warning: incompatible implicit declaration of built-in function ‘strcpy’
warning: incompatible implicit declaration of built-in function ‘strcat’

As far as I know, this issue is due to a wrong format of variables passed into malloc, strcpy and strcat functions. (elemento->d_name has type char and not char*

What can I do, in order to get this code work?

Thanks.

EDIT

This is a working while snippet:

while ((elemento = readdir(cartella)) != NULL)

{               
        if ( strcmp(elemento->d_name, ".")  == 0)
        {
            continue;
        }
        if ( strcmp(elemento->d_name, "..")  == 0)
        {
            continue;
        }
            if(elemento->d_type == DT_DIR)
            {

                {
                    percorso = scrivi(nome, elemento->d_name); 
                    scorriFolder(percorso);
                }
            }
            else 
            {
                printf("  %s\n", elemento->d_name); 
            }
        }

Once it scans a subdirectory it crashes because path it is not updated as the program exits from subdirectory. I am trying to fix it.

Was it helpful?

Solution

You need to add #include <stdlib.h> and #include <string.h> to the beginning of the file.

warning: incompatible implicit declaration of built-in function ‘malloc’

This error message is telling you that the compiler can't determine the return type and parameters to malloc. I think the compiler assumes int for the return type if it can't find one. Which is not void * which malloc actually returns.

malloc is defined in <stdlib.h>

strcpy and strcat are defined in <string.h>

To find out where these functions are defined you can read the man page by typing man malloc , man strcpy, or man strcat

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top