1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-07-31 00:03:08 +03:00

example, tests: address compiler warnings

Fix or silence all C compiler warnings discovered with (or without)
`PICKY_COMPILER=ON` (in CMake). This means all warnings showing up in
CI (gcc, clang, MSVS 2013/2015), in local tests on macOS (clang 14) and
Windows cross-builds using gcc (12) and llvm/clang (14/15).

Also fix the expression `nread -= nread` in `sftp_RW_nonblock.c`.

Cherry-picked from: #846
Closes #861
This commit is contained in:
Viktor Szakats
2023-03-20 15:46:12 +00:00
parent ec0a51db1f
commit b13936bd6a
47 changed files with 343 additions and 262 deletions

View File

@ -43,6 +43,10 @@
#pragma warning(disable:4127)
#endif
#ifdef WIN32
#define write(f, b, c) write((f), (b), (unsigned int)(c))
#endif
#define STORAGE "/tmp/sftp-storage" /* this is the local file name this
example uses to store the downloaded
file in */
@ -72,7 +76,7 @@ static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
writefd = &fd;
rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
rc = select((int)(socket_fd + 1), readfd, writefd, NULL, &timeout);
return rc;
}
@ -237,21 +241,22 @@ int main(int argc, char *argv[])
fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
do {
ssize_t nread;
do {
/* read in a loop until we block */
rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
nread = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
fprintf(stderr, "libssh2_sftp_read returned %d\n",
rc);
(int)nread);
if(rc > 0) {
if(nread > 0) {
/* write to stderr */
write(2, mem, rc);
write(2, mem, nread);
/* write to temporary storage area */
fwrite(mem, rc, 1, tempstorage);
fwrite(mem, nread, 1, tempstorage);
}
} while(rc > 0);
} while(nread > 0);
if(rc != LIBSSH2_ERROR_EAGAIN) {
if(nread != LIBSSH2_ERROR_EAGAIN) {
/* error or end of file */
break;
}
@ -265,7 +270,7 @@ int main(int argc, char *argv[])
FD_SET(sock, &fd2);
/* wait for readable or writeable */
rc = select(sock + 1, &fd, &fd2, NULL, &timeout);
rc = select((int)(sock + 1), &fd, &fd2, NULL, &timeout);
if(rc <= 0) {
/* negative is error
0 is timeout */
@ -296,6 +301,7 @@ int main(int argc, char *argv[])
size_t nread;
char *ptr;
do {
ssize_t nwritten;
nread = fread(mem, 1, sizeof(mem), tempstorage);
if(nread <= 0) {
/* end of file */
@ -305,13 +311,13 @@ int main(int argc, char *argv[])
do {
/* write data in a loop until we block */
rc = libssh2_sftp_write(sftp_handle, ptr,
nread);
ptr += rc;
nread -= nread;
} while(rc >= 0);
nwritten = libssh2_sftp_write(sftp_handle, ptr,
nread);
ptr += nwritten;
nread -= nwritten;
} while(nwritten >= 0);
if(rc != LIBSSH2_ERROR_EAGAIN) {
if(nwritten != LIBSSH2_ERROR_EAGAIN) {
/* error or end of file */
break;
}
@ -325,7 +331,7 @@ int main(int argc, char *argv[])
FD_SET(sock, &fd2);
/* wait for readable or writeable */
rc = select(sock + 1, &fd, &fd2, NULL, &timeout);
rc = select((int)(sock + 1), &fd, &fd2, NULL, &timeout);
if(rc <= 0) {
/* negative is error
0 is timeout */