static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out,
        char* dirName)
{
    FILINFO f;
    FILINFO *dp;
    int sz;

    if (dir == NULL || ssh == NULL || out == NULL) {
        return WS_BAD_ARGUMENT;
    }
    dp = &f;

    if (f_readdir(dir, dp) != FR_OK) {
        return WS_FATAL_ERROR;
    }
    sz = (int)WSTRLEN(dp->fname);
    out->fName = (char*)WMALLOC(sz + 1, out->heap, DYNTYPE_SFTP);
    if (out->fName == NULL) {
        return WS_MEMORY_E;
    }

    WMEMCPY(out->fName, dp->fname, sz);
    out->fName[sz] = '\0';
    out->fSz = sz;

    /* attempt to get file attributes. Could be directory or have none */
    {
        char* buf;
        int   bufSz;
        int   tmpSz;

        bufSz = out->fSz + (int)WSTRLEN(dirName) + sizeof(WS_DELIM);
        buf = (char*)WMALLOC(bufSz + 1, out->heap, DYNTYPE_SFTP);
        if (buf == NULL) {
            WFREE(out->fName, out->heap, DYNTYPE_SFTP);
            return WS_MEMORY_E;
        }
        buf[0] = '\0';
        WSTRNCAT(buf, dirName, bufSz + 1);
        tmpSz = (int)WSTRLEN(buf);

        /* add delimiter between path and file/dir name */
        if (tmpSz + 1 < bufSz) {
            buf[tmpSz] = WS_DELIM;
            buf[tmpSz+1] = '\0';
        }
        WSTRNCAT(buf, out->fName, bufSz + 1);
        if (XSTRLEN(out->fName) == 0) {
            WFREE(buf, out->heap, DYNTYPE_SFTP);
            return WS_EOF;
        }
        if (wolfSSH_CleanPath(ssh, buf) < 0) {
            WFREE(buf, out->heap, DYNTYPE_SFTP);
            return WS_FATAL_ERROR;
        }

        if (SFTP_GetAttributes(ssh->fs, buf, &out->atrb, 0, ssh->ctx->heap)
                != WS_SUCCESS) {
            WLOG(WS_LOG_SFTP, "Unable to get attribute values for %s",
                    out->fName);
            WFREE(buf, out->heap, DYNTYPE_SFTP);
            return WS_FATAL_ERROR;
        }
        WFREE(buf, out->heap, DYNTYPE_SFTP);
    }

    /* Use attributes and fName to create long name */
    if (SFTP_CreateLongName(out) != WS_SUCCESS) {
        WLOG(WS_LOG_DEBUG, "Error creating long name for %s", out->fName);
        WFREE(out->fName, out->heap, DYNTYPE_SFTP);
        return WS_FATAL_ERROR;
    }
    return WS_SUCCESS;
}
static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out,
        char* dirName)
{
    FILINFO f;
    FILINFO *dp;
    int sz;

    if (dir == NULL || ssh == NULL || out == NULL) {
        return WS_BAD_ARGUMENT;
    }
    dp = &f;

    if (f_readdir(dir, dp) != FR_OK) {
        return WS_FATAL_ERROR;
    }
    sz = (int)WSTRLEN(dp->fname);
    out->fName = (char*)WMALLOC(sz + 1, out->heap, DYNTYPE_SFTP);
    if (out->fName == NULL) {
        return WS_MEMORY_E;
    }

    WMEMCPY(out->fName, dp->fname, sz);
    out->fName[sz] = '\0';
    out->fSz = sz;

     
    {
        char* buf;
        int   bufSz;
        int   tmpSz;

        bufSz = out->fSz + (int)WSTRLEN(dirName) + sizeof(WS_DELIM);
        buf = (char*)WMALLOC(bufSz + 1, out->heap, DYNTYPE_SFTP);
        if (buf == NULL) {
            WFREE(out->fName, out->heap, DYNTYPE_SFTP);
            return WS_MEMORY_E;
        }
        buf[0] = '\0';
        WSTRNCAT(buf, dirName, bufSz + 1);
        tmpSz = (int)WSTRLEN(buf);

         
        if (tmpSz + 1 < bufSz) {
            buf[tmpSz] = WS_DELIM;
            buf[tmpSz+1] = '\0';
        }
        WSTRNCAT(buf, out->fName, bufSz + 1);
        if (XSTRLEN(out->fName) == 0) {
            WFREE(buf, out->heap, DYNTYPE_SFTP);
            return WS_EOF;
        }
        if (wolfSSH_CleanPath(ssh, buf) < 0) {
            WFREE(buf, out->heap, DYNTYPE_SFTP);
            return WS_FATAL_ERROR;
        }

        if (SFTP_GetAttributes(ssh->fs, buf, &out->atrb, 0, ssh->ctx->heap)
                != WS_SUCCESS) {
            WLOG(WS_LOG_SFTP, "Unable to get attribute values for %s",
                    out->fName);
            WFREE(buf, out->heap, DYNTYPE_SFTP);
            return WS_FATAL_ERROR;
        }
        WFREE(buf, out->heap, DYNTYPE_SFTP);
    }

     
    if (SFTP_CreateLongName(out) != WS_SUCCESS) {
        WLOG(WS_LOG_DEBUG, "Error creating long name for %s", out->fName);
        WFREE(out->fName, out->heap, DYNTYPE_SFTP);
        return WS_FATAL_ERROR;
    }
    return WS_SUCCESS;
}