strcat dumping core in SunOS

74 views Asked by At

I have a program which goes through the directory structure and concatenates the files present in the path to szFile . I have used dirent here to get the directory entries. It is dumping core in the strcat function inside the for loop only in SunOS . It goes through fine in HP and AIX machine .

#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <limits.h>
#include <sys/types.h>

int main()
{

    DIR            *pDirHand;

    char            szFile[1024];
    struct dirent   pdirent ;
    struct dirent *pResult = NULL;

    char *sDir = "fullpath"; /* fullpath can be /make/users/path */

    strncpy (szFile, sDir, sizeof(szFile)-1);
    szFile[sizeof(szFile)-1] = '\0';

    if (NULL == (pDirHand = opendir(szFile)))
    {
        return -1;
    }

    for(readdir_r(pDirHand, &pdirent, &pResult); pResult != 0;readdir_r(pDirHand, &pdirent, &pResult))
    {
        FILE *fp;
        fp=fopen("debug.log","a+");
        strcpy (szFile, sDir);

        strcat (szFile, "/");

        strcat (szFile, pdirent.d_name);
    }

    if (pDirHand) closedir (pDirHand);

    return 0;
}

I dont have any files currently in the path that I assign to sDir. It has "." and ".." directory entries in it but I get a core dump in the line

strcat (szFile, pdirent.d_name);

I had used dbx to find out the value of szFile , during the second iteration the value is exceeding the memory allocated for it . The value comes as

"fullpath/../fullpath/../fullpath/../fullpath/../fullpath/..fullpath/..fullpath/../../../../../../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/..fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/.." ...

I have tried using strlcat , but the concatinated value of szFile is not coming properly. Anybody faced this problem in SunOS or can help ?

0

There are 0 answers