From 77311f420a541a0de5b3014e0e40ff8b4205d4af Mon Sep 17 00:00:00 2001 From: Marc Hoersken Date: Tue, 14 Dec 2021 07:52:26 +0100 Subject: docs/examples: workaround broken -Wno-pedantic-ms-format Avoid CURL_FORMAT_CURL_OFF_T by using unsigned long instead. Improve size_t to long conversion in imap-append.c example. Ref: https://github.com/curl/curl/issues/6079 Ref: https://github.com/curl/curl/pull/6082 Assisted-by: Jay Satiro Reviewed-by: Daniel Stenberg Preparation of #7922 --- docs/examples/anyauthput.c | 10 +++++----- docs/examples/chkspeed.c | 18 +++++++++--------- docs/examples/fileupload.c | 10 ++++------ docs/examples/ftpupload.c | 15 ++++++++------- docs/examples/httpput.c | 10 +++++----- docs/examples/imap-append.c | 7 +++++-- docs/examples/progressfunc.c | 12 ++++++------ docs/examples/sendrecv.c | 6 ++---- docs/examples/sftpuploadresume.c | 2 +- 9 files changed, 45 insertions(+), 45 deletions(-) diff --git a/docs/examples/anyauthput.c b/docs/examples/anyauthput.c index cbb963390..f5db702f5 100644 --- a/docs/examples/anyauthput.c +++ b/docs/examples/anyauthput.c @@ -81,17 +81,17 @@ static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp) static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream) { ssize_t retcode; - curl_off_t nread; + unsigned long nread; int *fdp = (int *)stream; int fd = *fdp; retcode = read(fd, ptr, (READ_3RD_ARG)(size * nmemb)); - nread = (curl_off_t)retcode; - - fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T - " bytes from file\n", nread); + if(retcode > 0) { + nread = (unsigned long)retcode; + fprintf(stderr, "*** We read %lu bytes from file\n", nread); + } return retcode; } diff --git a/docs/examples/chkspeed.c b/docs/examples/chkspeed.c index bc5387d77..4c7136bf5 100644 --- a/docs/examples/chkspeed.c +++ b/docs/examples/chkspeed.c @@ -170,32 +170,32 @@ int main(int argc, char *argv[]) /* check for bytes downloaded */ res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD_T, &val); if((CURLE_OK == res) && (val>0)) - printf("Data downloaded: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", val); + printf("Data downloaded: %lu bytes.\n", (unsigned long)val); /* check for total download time */ res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME_T, &val); if((CURLE_OK == res) && (val>0)) - printf("Total download time: %" CURL_FORMAT_CURL_OFF_T ".%06ld sec.\n", - (val / 1000000), (long)(val % 1000000)); + printf("Total download time: %lu.%06lu sec.\n", + (unsigned long)(val / 1000000), (unsigned long)(val % 1000000)); /* check for average download speed */ res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD_T, &val); if((CURLE_OK == res) && (val>0)) - printf("Average download speed: %" CURL_FORMAT_CURL_OFF_T - " kbyte/sec.\n", val / 1024); + printf("Average download speed: %lu kbyte/sec.\n", + (unsigned long)(val / 1024)); if(prtall) { /* check for name resolution time */ res = curl_easy_getinfo(curl_handle, CURLINFO_NAMELOOKUP_TIME_T, &val); if((CURLE_OK == res) && (val>0)) - printf("Name lookup time: %" CURL_FORMAT_CURL_OFF_T ".%06ld sec.\n", - (val / 1000000), (long)(val % 1000000)); + printf("Name lookup time: %lu.%06lu sec.\n", + (unsigned long)(val / 1000000), (unsigned long)(val % 1000000)); /* check for connect time */ res = curl_easy_getinfo(curl_handle, CURLINFO_CONNECT_TIME_T, &val); if((CURLE_OK == res) && (val>0)) - printf("Connect time: %" CURL_FORMAT_CURL_OFF_T ".%06ld sec.\n", - (val / 1000000), (long)(val % 1000000)); + printf("Connect time: %lu.%06lu sec.\n", + (unsigned long)(val / 1000000), (unsigned long)(val % 1000000)); } } else { diff --git a/docs/examples/fileupload.c b/docs/examples/fileupload.c index afea64316..81914b2c9 100644 --- a/docs/examples/fileupload.c +++ b/docs/examples/fileupload.c @@ -68,18 +68,16 @@ int main(void) if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); - } else { /* now extract transfer info */ curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD_T, &speed_upload); curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &total_time); - fprintf(stderr, "Speed: %" CURL_FORMAT_CURL_OFF_T " bytes/sec during %" - CURL_FORMAT_CURL_OFF_T ".%06ld seconds\n", - speed_upload, - (total_time / 1000000), (long)(total_time % 1000000)); - + fprintf(stderr, "Speed: %lu bytes/sec during %lu.%06lu seconds\n", + (unsigned long)speed_upload, + (unsigned long)(total_time / 1000000), + (unsigned long)(total_time % 1000000)); } /* always cleanup */ curl_easy_cleanup(curl); diff --git a/docs/examples/ftpupload.c b/docs/examples/ftpupload.c index 1b7bbf26a..75356d86a 100644 --- a/docs/examples/ftpupload.c +++ b/docs/examples/ftpupload.c @@ -50,16 +50,17 @@ variable's memory when passed in to it from an app like this. */ static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream) { - curl_off_t nread; + unsigned long nread; /* in real-world cases, this would probably get this data differently as this fread() stuff is exactly what the library already would do by default internally */ size_t retcode = fread(ptr, size, nmemb, stream); - nread = (curl_off_t)retcode; + if(retcode > 0) { + nread = (unsigned long)retcode; + fprintf(stderr, "*** We read %lu bytes from file\n", nread); + } - fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T - " bytes from file\n", nread); return retcode; } @@ -69,7 +70,7 @@ int main(void) CURLcode res; FILE *hd_src; struct stat file_info; - curl_off_t fsize; + unsigned long fsize; struct curl_slist *headerlist = NULL; static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS; @@ -80,9 +81,9 @@ int main(void) printf("Couldn't open '%s': %s\n", LOCAL_FILE, strerror(errno)); return 1; } - fsize = (curl_off_t)file_info.st_size; + fsize = (unsigned long)file_info.st_size; - printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize); + printf("Local file size: %lu bytes.\n", fsize); /* get a FILE * of the same file */ hd_src = fopen(LOCAL_FILE, "rb"); diff --git a/docs/examples/httpput.c b/docs/examples/httpput.c index 90749da2d..7fd027819 100644 --- a/docs/examples/httpput.c +++ b/docs/examples/httpput.c @@ -41,17 +41,17 @@ static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream) { size_t retcode; - curl_off_t nread; + unsigned long nread; /* in real-world cases, this would probably get this data differently as this fread() stuff is exactly what the library already would do by default internally */ retcode = fread(ptr, size, nmemb, stream); - nread = (curl_off_t)retcode; - - fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T - " bytes from file\n", nread); + if(retcode > 0) { + nread = (unsigned long)retcode; + fprintf(stderr, "*** We read %lu bytes from file\n", nread); + } return retcode; } diff --git a/docs/examples/imap-append.c b/docs/examples/imap-append.c index 78ecc8d01..e36cb10c0 100644 --- a/docs/examples/imap-append.c +++ b/docs/examples/imap-append.c @@ -89,7 +89,8 @@ int main(void) curl = curl_easy_init(); if(curl) { - long infilesize; + size_t filesize; + long infilesize = LONG_MAX; struct upload_status upload_ctx = { 0 }; /* Set username and password */ @@ -108,7 +109,9 @@ int main(void) curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); - infilesize = strlen(payload_text); + filesize = strlen(payload_text); + if(filesize <= LONG_MAX) + infilesize = (long)filesize; curl_easy_setopt(curl, CURLOPT_INFILESIZE, infilesize); /* Perform the append */ diff --git a/docs/examples/progressfunc.c b/docs/examples/progressfunc.c index df783119e..56889df66 100644 --- a/docs/examples/progressfunc.c +++ b/docs/examples/progressfunc.c @@ -51,14 +51,14 @@ static int xferinfo(void *p, be used */ if((curtime - myp->lastruntime) >= MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL) { myp->lastruntime = curtime; - fprintf(stderr, "TOTAL TIME: %" CURL_FORMAT_CURL_OFF_T ".%06ld\r\n", - (curtime / 1000000), (long)(curtime % 1000000)); + fprintf(stderr, "TOTAL TIME: %lu.%06lu\r\n", + (unsigned long)(curtime / 1000000), + (unsigned long)(curtime % 1000000)); } - fprintf(stderr, "UP: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T - " DOWN: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T - "\r\n", - ulnow, ultotal, dlnow, dltotal); + fprintf(stderr, "UP: %lu of %lu DOWN: %lu of %lu\r\n", + (unsigned long)ulnow, (unsigned long)ultotal, + (unsigned long)dlnow, (unsigned long)dltotal); if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES) return 1; diff --git a/docs/examples/sendrecv.c b/docs/examples/sendrecv.c index 44741aeba..d8158466b 100644 --- a/docs/examples/sendrecv.c +++ b/docs/examples/sendrecv.c @@ -120,8 +120,7 @@ int main(void) return 1; } - printf("Sent %" CURL_FORMAT_CURL_OFF_T " bytes.\n", - (curl_off_t)nsent); + printf("Sent %lu bytes.\n", (unsigned long)nsent); } while(nsent_total < request_len); @@ -151,8 +150,7 @@ int main(void) break; } - printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n", - (curl_off_t)nread); + printf("Received %lu bytes.\n", (unsigned long)nread); } /* always cleanup */ diff --git a/docs/examples/sftpuploadresume.c b/docs/examples/sftpuploadresume.c index e2aa37392..da2a4102c 100644 --- a/docs/examples/sftpuploadresume.c +++ b/docs/examples/sftpuploadresume.c @@ -66,7 +66,7 @@ static curl_off_t sftpGetRemoteFileSize(const char *i_remoteFile) &remoteFileSizeByte); if(result) return -1; - printf("filesize: %" CURL_FORMAT_CURL_OFF_T "\n", remoteFileSizeByte); + printf("filesize: %lu\n", (unsigned long)remoteFileSizeByte); } curl_easy_cleanup(curlHandlePtr); -- cgit v1.2.1