static int SFTP_CreateLongName(WS_SFTPNAME* name)
{
    char perm[11];
    int linkCount = 1; /* @TODO set to correct value */
#if defined(XGMTIME) && defined(XSNPRINTF)
    char date[WS_DATE_SIZE + 1]; /* +1 for null terminator */
    struct tm* localTime = NULL;
#endif
    WS_SFTP_FILEATRB* atr;
    int i;

    int totalSz = 0;

    if (name == NULL) {
        return WS_BAD_ARGUMENT;
    }

#if defined(XGMTIME) && defined(XSNPRINTF)
    atr = &name->atrb;

    /* get date as calendar date */
    localTime = XGMTIME((const time_t*)&atr->mtime, &localTime);
    if (localTime == NULL) {
        return WS_MEMORY_E;
    }
    getDate(date, sizeof(date), localTime);
    totalSz += WS_DATE_SIZE;

    /* set permissions */
    for (i = 0; i < 10; i++) {
        perm[i] = '-';
    }

    if (atr->flags & WOLFSSH_FILEATRB_PERM) {
        word32 tmp = atr->per;

        i = 0;
        perm[i++] = (tmp & 0x4000)?'d':'-';
        perm[i++] = (tmp & 0x100)?'r':'-';
        perm[i++] = (tmp & 0x080)?'w':'-';
        perm[i++] = (tmp & 0x040)?'x':'-';

        perm[i++] = (tmp & 0x020)?'r':'-';
        perm[i++] = (tmp & 0x010)?'w':'-';
        perm[i++] = (tmp & 0x008)?'x':'-';

        perm[i++] = (tmp & 0x004)?'r':'-';
        perm[i++] = (tmp & 0x002)?'w':'-';
        perm[i++] = (tmp & 0x001)?'x':'-';
    }
    totalSz += i;
    perm[i] = '\0';

    totalSz += name->fSz; /* size of file name */
    totalSz += 7; /* for all ' ' spaces */
    totalSz += 3 + 8 + 8 + 8; /* linkCount + uid + gid + size */
#else
    totalSz = name->fSz;
#endif

    name->lName = (char*)WMALLOC(totalSz + 1, name->heap, DYNTYPE_SFTP);
    if (name->lName == NULL) {
        WFREE(name->lName, name->heap, DYNTYPE_SFTP);
        return WS_MEMORY_E;
    }
    name->lSz = totalSz;
    name->lName[totalSz] = '\0';

#if defined(XGMTIME) && defined(XSNPRINTF)
    WSNPRINTF(name->lName, totalSz, "%s %3d %8d %8d %8d %s %s",
            perm, linkCount, atr->uid, atr->gid, atr->sz[0], date, name->fName);
#else
    WMEMCPY(name->lName, name->fName, totalSz);
#endif

    return WS_SUCCESS;
}
static int SFTP_CreateLongName(WS_SFTPNAME* name)
{
    char perm[11];
    int linkCount = 1;  
#if defined(XGMTIME) && defined(XSNPRINTF)
    char date[WS_DATE_SIZE + 1];  
    struct tm* localTime = NULL;
#endif
    WS_SFTP_FILEATRB* atr;
    int i;

    int totalSz = 0;

    if (name == NULL) {
        return WS_BAD_ARGUMENT;
    }

#if defined(XGMTIME) && defined(XSNPRINTF)
    atr = &name->atrb;

     
    localTime = XGMTIME((const time_t*)&atr->mtime, &localTime);
    if (localTime == NULL) {
        return WS_MEMORY_E;
    }
    getDate(date, sizeof(date), localTime);
    totalSz += WS_DATE_SIZE;

     
    for (i = 0; i < 10; i++) {
        perm[i] = '-';
    }

    if (atr->flags & WOLFSSH_FILEATRB_PERM) {
        word32 tmp = atr->per;

        i = 0;
        perm[i++] = (tmp & 0x4000)?'d':'-';
        perm[i++] = (tmp & 0x100)?'r':'-';
        perm[i++] = (tmp & 0x080)?'w':'-';
        perm[i++] = (tmp & 0x040)?'x':'-';

        perm[i++] = (tmp & 0x020)?'r':'-';
        perm[i++] = (tmp & 0x010)?'w':'-';
        perm[i++] = (tmp & 0x008)?'x':'-';

        perm[i++] = (tmp & 0x004)?'r':'-';
        perm[i++] = (tmp & 0x002)?'w':'-';
        perm[i++] = (tmp & 0x001)?'x':'-';
    }
    totalSz += i;
    perm[i] = '\0';

    totalSz += name->fSz;  
    totalSz += 7;  
    totalSz += 3 + 8 + 8 + 8;  
#else
    totalSz = name->fSz;
#endif

    name->lName = (char*)WMALLOC(totalSz + 1, name->heap, DYNTYPE_SFTP);
    if (name->lName == NULL) {
        WFREE(name->lName, name->heap, DYNTYPE_SFTP);
        return WS_MEMORY_E;
    }
    name->lSz = totalSz;
    name->lName[totalSz] = '\0';

#if defined(XGMTIME) && defined(XSNPRINTF)
    WSNPRINTF(name->lName, totalSz, "%s %3d %8d %8d %8d %s %s",
            perm, linkCount, atr->uid, atr->gid, atr->sz[0], date, name->fName);
#else
    WMEMCPY(name->lName, name->fName, totalSz);
#endif

    return WS_SUCCESS;
}