#include <stdio.h>
#include <windows.h>

#include "libssh2.h"
#include "libssh2_sftp.h"

int main(int argc, char **argv)
{
	int s, rc;
	char buf[1024];
	struct sockaddr_in addr;
	void *xfer, *dir, *sess = libssh2_session_init_ex(NULL, NULL, NULL, NULL);
	LIBSSH2_SFTP_ATTRIBUTES attr;
	WSADATA wsa;

	WSAStartup(MAKEWORD(2, 0), &wsa);
	s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = PF_INET;
	addr.sin_addr.s_addr = htonl(0xc0a80503); // 192.168.5.3
	addr.sin_port = htons(22);
	printf("connect: %d\t%d\n", connect(s, (struct sockaddr *)&addr, sizeof(addr)), WSAGetLastError());
	printf("startup: %d\n", libssh2_session_startup(sess, s));
	libssh2_session_set_blocking(sess, 0);
	while ((rc = libssh2_userauth_publickey_fromfile_ex(sess, "sftp", 4, NULL, "D:\\Avco\\WinLogSFTP\\privatekey", "")) == LIBSSH2_ERROR_EAGAIN);
	printf("auth: %d %d\n", rc, libssh2_session_last_error(sess, NULL, NULL, 0));
	while ((xfer = libssh2_sftp_init(sess)) == NULL && libssh2_session_last_error(sess, NULL, NULL, 0) == LIBSSH2_ERROR_EAGAIN);
	while ((dir = libssh2_sftp_open_ex(xfer, "/logs", 5, 0, 0, 1)) == NULL && libssh2_session_last_error(sess, NULL, NULL, 0) == LIBSSH2_ERROR_EAGAIN);
	
	for (rc = 1; rc > 0;)
	{
		memset(buf, 0, sizeof(buf));
		while ((rc = libssh2_sftp_readdir_ex(dir, buf, sizeof(buf), NULL, 0, &attr)) == LIBSSH2_ERROR_EAGAIN);
		printf("%d: %s\n", rc, buf);
	}

	getch();
	return(0);
}

