Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8419711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:41:15+00:00 2026-06-10T02:41:15+00:00

int ScanDirectories(const char *dirname, struct images *imagesHeadPtr, struct filesToParse *filesHeadPtr) { // scan the

  • 0
int ScanDirectories(const char *dirname, struct images *imagesHeadPtr, struct filesToParse *filesHeadPtr)
{
    // scan the directory and store the entries in a buffer 


DIR *dir;
struct dirent *ent;
int jpgs = 0, pngs = 0;
int totalFiles = 0;
int filesToScan = 0;
char name[256];
char *tmp = malloc(sizeof(char));

if((dir = opendir(dirname)) == NULL)
{
    perror("Unable to open directory");
    DisplayFolderNotFound();
    return(0);
}

while((ent = readdir(dir)) != NULL)
{
    strcpy(name, ent->d_name);
    strncpy(tmp, name, 1);
    if(strcmp(tmp, ".") == 0)   //Not valid directories. These are dir's created by system and are hidden.
    continue;

    char dirCopy[strlen(dirname)+ strlen(name) + 1 /* for slash */  + 1 /*for null character*/];

    strcpy(dirCopy, dirname);        
    strcat(dirCopy, "/");
    strcat(dirCopy, name);

    struct stat s;

    if( stat(dirCopy, &s) == 0 )
    {
        if( s.st_mode & S_IFDIR )
        {
            //              it's a directory
            //              printf("Directory [%s]\n", dirCopy);
            ScanDirectories(dirCopy, imagesHeadPtr, filesHeadPtr); //Already inside a dir, recursively traverse it.
        }
        else if( s.st_mode & S_IFREG )
        {
            //it's a file
            //printf("File [%s]\n", name);

            ++totalFiles;
        }
        else
        {
            //something else
        }
    }
    else
    {
        //error
        return(0);
    }

    int extensionLength = 0;
    char *endP = name + strlen(name) - 1; //pointer to the last char of filename
    char *temp = endP;

    while (*temp != '.')
    {
        ++extensionLength;
        --temp;
    }

    char *extension = (char *)malloc(sizeof(char)*(extensionLength+1 /* for . */ + 1 /* for null char */));
    strncpy(extension, name+strlen(name)-extensionLength-1, extensionLength+2);

    if(strcmp(extension, ".abc")==0)
    {                
        ++pngs;

        struct images *nextPtr = imagesHeadPtr;

        while(nextPtr->next != NULL)
        nextPtr = nextPtr ->next;

        nextPtr->fileName = (char *)malloc(sizeof(char)*(strlen(name)+1));
        strcpy(nextPtr->fileName, name);
        nextPtr->filePath = (char *)malloc(sizeof(char)*(strlen(dirCopy)+1));
        strcpy(nextPtr->filePath, dirCopy);
        nextPtr->isUsed = 0;
        nextPtr->fileSize = GetFileSize(dirCopy)/1000.0;
        nextPtr->next = (struct images *)malloc(sizeof(struct images));
        nextPtr = nextPtr->next;
        nextPtr->fileName = NULL;
        nextPtr->filePath = NULL;
        nextPtr->isUsed = 0;
        nextPtr->fileSize = 0;
        nextPtr->next = NULL;
    }

    else if(strcmp(extension, ".rst")==0)
    {                
        ++jpgs;

        struct images *nextPtr = imagesHeadPtr;

        while(nextPtr->next != NULL)
        nextPtr = nextPtr ->next;

        nextPtr->fileName = (char *)malloc(sizeof(char)*(strlen(name)+1));
        strcpy(nextPtr->fileName, name);
        nextPtr->filePath = (char *)malloc(sizeof(char)*(strlen(dirCopy)+1));
        strcpy(nextPtr->filePath, dirCopy);
        nextPtr->isUsed = 0;
        nextPtr->fileSize = GetFileSize(dirCopy)/1000.0;
        nextPtr->next = (struct images *)malloc(sizeof(struct images));
        nextPtr = nextPtr->next;
        nextPtr->fileName = NULL;
        nextPtr->filePath = NULL;
        nextPtr->isUsed = 0;
        nextPtr->fileSize = 0;
        nextPtr->next = NULL;
    }

    else if(strcmp(extension, ".dig") == 0)
    {
        ++filesToScan;

        struct filesToParse *nextPtr = filesHeadPtr;

        while(nextPtr->next != NULL)
        nextPtr = nextPtr ->next;

        nextPtr->filePath = (char *)malloc(sizeof(char)*(strlen(dirCopy)+1));
        strcpy(nextPtr->filePath, dirCopy);
        nextPtr->next = (struct filesToParse *)malloc(sizeof(struct filesToParse));
        nextPtr = nextPtr->next;
        nextPtr->filePath = NULL;
        nextPtr->next = NULL;        
    }

    else if(strcmp(extension, ".x") == 0)
    {
        ++filesToScan;

        struct filesToParse *nextPtr = filesHeadPtr;

        while(nextPtr->next != NULL)
        nextPtr = nextPtr ->next;

        nextPtr->filePath = (char *)malloc(sizeof(char)*(strlen(dirCopy)+1));
        strcpy(nextPtr->filePath, dirCopy);
        nextPtr->next = (struct filesToParse *)malloc(sizeof(struct filesToParse));
        nextPtr = nextPtr->next;
        nextPtr->filePath = NULL;
        nextPtr->next = NULL;        
    }

    else if(strcmp(extension, ".cderf") == 0)
    {
        ++filesToScan;

        struct filesToParse *nextPtr = filesHeadPtr;

        while(nextPtr->next != NULL)
        nextPtr = nextPtr ->next;

        nextPtr->filePath = (char *)malloc(sizeof(char)*(strlen(dirCopy)+1));
        strcpy(nextPtr->filePath, dirCopy);
        nextPtr->next = (struct filesToParse *)malloc(sizeof(struct filesToParse));
        nextPtr = nextPtr->next;
        nextPtr->filePath = NULL;
        nextPtr->next = NULL;                        
    } 

        free(extension); //because of this line, my code runs into infinite loop saying
//unable to open dir : too many files open. If I comment this out, my code works fine but the concepts of memory management say that I should be freeing it. 

}

free(tmp);

if (closedir(dir) != 0)
perror("Unable to close directory");

return(1);

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:41:17+00:00Added an answer on June 10, 2026 at 2:41 am

    C library function strcmp() stops comparing when strings differ or \0 (end of string) is reached: http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

    if(strcmp(tmp, ".") == 0)   //Not valid directories. These are dir's created by system and are hidden.
        continue;
    

    But tmp string doesn’t have null terminator \0. So the comparison generates the infinite loop.

    Reserve 2 bytes instead of one for tmp string:

    char *tmp = malloc(2*sizeof(char));
    

    Add the terminator \0 to the string:

    tmp[1]='\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

int :: cadena calculatelenght(const cadena& a, const char* cad) { cadena c; int lenght
int gb2Utf8(const char* source, int sourceLen, void *target, int targetLen) { int result =
int main(void) { char testStr[50] = Hello, world!; char revS[50] = testStr; } I
int main() { char *p,c; for(p=Hello World;c=*p;++p) { printf(%c,c); } } In the above
int main() { int x,y; int z; char s='a'; x=10;y=4; z = x/y; printf(%d\n,s);
int a[10]; int b[10]; a = b; // illegal typedef struct { int real;
int main( ) { char str[200]; int n,tc; scanf(%d,&tc); while(tc--) { scanf(%d,&n); gets(str); puts(str);
int * const const * b What does it mean? cdecl says declare b
int main(){ char ch; fork(); cin >> c; } After calling fork() I should
int i=9999; char c=i; gives no overflow warning, While char c=9999; gives, warning C4305

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.