]> granicus.if.org Git - curl/commitdiff
examples: reduce variable scopes
authorMarcel Raad <Marcel.Raad@teamviewer.com>
Tue, 21 May 2019 08:44:16 +0000 (10:44 +0200)
committerMarcel Raad <Marcel.Raad@teamviewer.com>
Wed, 22 May 2019 08:06:21 +0000 (10:06 +0200)
Closes https://github.com/curl/curl/pull/3919

docs/examples/cacertinmem.c
docs/examples/curlgtk.c
docs/examples/ephiperfifo.c
docs/examples/htmltidy.c
docs/examples/imap-append.c
docs/examples/multi-app.c
docs/examples/sendrecv.c
docs/examples/shared-connection-cache.c
docs/examples/smooth-gtk-thread.c
docs/examples/smtp-mime.c
docs/examples/synctime.c

index 0710317076c9c50f3a5ce79e3a4afa97de91448d..11cc06bee70895786e6173759cd2f8b3ac8b8d2d 100644 (file)
@@ -85,7 +85,6 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
 
   BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem));
   X509_STORE  *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
-  X509_INFO *itmp;
   int i;
   STACK_OF(X509_INFO) *inf;
   (void)curl;
@@ -103,7 +102,7 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
   }
 
   for(i = 0; i < sk_X509_INFO_num(inf); i++) {
-    itmp = sk_X509_INFO_value(inf, i);
+    X509_INFO *itmp = sk_X509_INFO_value(inf, i);
     if(itmp->x509) {
       X509_STORE_add_cert(cts, itmp->x509);
     }
index 35b60da635f402f706418173291b463fbd1fa416..4083c8f95f30d04cd7b58c1c30c96603ce53e052 100644 (file)
@@ -45,13 +45,12 @@ int my_progress_func(GtkWidget *bar,
 void *my_thread(void *ptr)
 {
   CURL *curl;
-  FILE *outfile;
-  gchar *url = ptr;
 
   curl = curl_easy_init();
   if(curl) {
+    gchar *url = ptr;
     const char *filename = "test.curl";
-    outfile = fopen(filename, "wb");
+    FILE *outfile = fopen(filename, "wb");
 
     curl_easy_setopt(curl, CURLOPT_URL, url);
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
index a4b90fe4ff22414da6404dff9678bf0a5512274a..2b8f4cc6efd86320c4886ea6c5d4bf091313efd0 100644 (file)
@@ -472,8 +472,6 @@ void SignalHandler(int signo)
 int main(int argc _Unused, char **argv _Unused)
 {
   GlobalInfo g;
-  int err;
-  int idx;
   struct itimerspec its;
   struct epoll_event ev;
   struct epoll_event events[10];
@@ -518,8 +516,9 @@ int main(int argc _Unused, char **argv _Unused)
   fprintf(MSG_OUT, "Entering wait loop\n");
   fflush(MSG_OUT);
   while(!g_should_exit_) {
-    err = epoll_wait(g.epfd, events, sizeof(events)/sizeof(struct epoll_event),
-                     10000);
+    int idx;
+    int err = epoll_wait(g.epfd, events,
+                         sizeof(events)/sizeof(struct epoll_event), 10000);
     if(err == -1) {
       if(errno == EINTR) {
         fprintf(MSG_OUT, "note: wait interrupted\n");
index 2f4500f51c155c998f407eff799afc6d0ec11b06..1b48e0a2e43a76001c151a8d8c4c991295ffd82e 100644 (file)
@@ -74,13 +74,14 @@ void dumpNode(TidyDoc doc, TidyNode tnod, int indent)
 
 int main(int argc, char **argv)
 {
-  CURL *curl;
-  char curl_errbuf[CURL_ERROR_SIZE];
-  TidyDoc tdoc;
-  TidyBuffer docbuf = {0};
-  TidyBuffer tidy_errbuf = {0};
-  int err;
   if(argc == 2) {
+    CURL *curl;
+    char curl_errbuf[CURL_ERROR_SIZE];
+    TidyDoc tdoc;
+    TidyBuffer docbuf = {0};
+    TidyBuffer tidy_errbuf = {0};
+    int err;
+
     curl = curl_easy_init();
     curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
     curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);
index bbf9fe436d8f75ed2626fdefc1041b1a0e9587fc..157d5749a5e85f048a844364ebb00f08ba032a34 100644 (file)
@@ -85,14 +85,15 @@ int main(void)
 {
   CURL *curl;
   CURLcode res = CURLE_OK;
-  const char **p;
-  long infilesize;
-  struct upload_status upload_ctx;
-
-  upload_ctx.lines_read = 0;
 
   curl = curl_easy_init();
   if(curl) {
+    const char **p;
+    long infilesize;
+    struct upload_status upload_ctx;
+
+    upload_ctx.lines_read = 0;
+
     /* Set username and password */
     curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
     curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
index 78867d835ec1d01c823991339ab7af4c4317f7fd..b98a25161df4f8a8c859905d23dc286ed2d2d0ed 100644 (file)
@@ -147,11 +147,11 @@ int main(void)
   /* See how the transfers went */
   while((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
     if(msg->msg == CURLMSG_DONE) {
-      int idx, found = 0;
+      int idx;
 
       /* Find out which handle this message is about */
       for(idx = 0; idx<HANDLECOUNT; idx++) {
-        found = (msg->easy_handle == handles[idx]);
+        int found = (msg->easy_handle == handles[idx]);
         if(found)
           break;
       }
index cf764be4308543a77ca79f3d970bfcdd66be1d1c..5660a795560ce300ef54f7a063d0694331e0454a 100644 (file)
@@ -59,12 +59,9 @@ static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
 int main(void)
 {
   CURL *curl;
-  CURLcode res;
   /* Minimalistic http request */
   const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
   size_t request_len = strlen(request);
-  curl_socket_t sockfd;
-  size_t nsent_total = 0;
 
   /* A general note of caution here: if you're using curl_easy_recv() or
      curl_easy_send() to implement HTTP or _any_ other protocol libcurl
@@ -76,6 +73,10 @@ int main(void)
 
   curl = curl_easy_init();
   if(curl) {
+    CURLcode res;
+    curl_socket_t sockfd;
+    size_t nsent_total = 0;
+
     curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
     /* Do not do the transfer - only connect to host */
     curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
index 91864d9b29ee3e8b152748d84b34715b0fc0e89a..639ad9c54bf88a9edfd35c525877ca74a2038f19 100644 (file)
@@ -46,8 +46,6 @@ static void my_unlock(CURL *handle, curl_lock_data data, void *useptr)
 
 int main(void)
 {
-  CURL *curl;
-  CURLcode res;
   CURLSH *share;
   int i;
 
@@ -61,8 +59,10 @@ int main(void)
      still reuse connections since the pool is in the shared object! */
 
   for(i = 0; i < 3; i++) {
-    curl = curl_easy_init();
+    CURL *curl = curl_easy_init();
     if(curl) {
+      CURLcode res;
+
       curl_easy_setopt(curl, CURLOPT_URL, "https://curl.haxx.se/");
 
       /* use the share object */
index 66d8c10b5edec854f0cefe922829c25c16142acf..b64c48610c0484534cb1404e4f1e5b3132137993 100644 (file)
@@ -67,13 +67,12 @@ size_t write_file(void *ptr, size_t size, size_t nmemb, FILE *stream)
 /* https://weather.com/weather/today/l/46214?cc=*&dayf=5&unit=i */
 void *pull_one_url(void *NaN)
 {
-  CURL *curl;
-  gchar *http;
-  FILE *outfile;
-
   /* Stop threads from entering unless j is incremented */
   pthread_mutex_lock(&lock);
   while(j < num_urls) {
+    CURL *curl;
+    gchar *http;
+
     printf("j = %d\n", j);
 
     http =
@@ -85,7 +84,7 @@ void *pull_one_url(void *NaN)
     curl = curl_easy_init();
     if(curl) {
 
-      outfile = fopen(urls[j], "wb");
+      FILE *outfile = fopen(urls[j], "wb");
 
       /* Set the URL and transfer type */
       curl_easy_setopt(curl, CURLOPT_URL, http);
index 35997fa0ed19605c99bb339d922afbbcf349929e..4f3fbfd53edf44eee8b2d7d43d3e905886ee3d4b 100644 (file)
@@ -70,16 +70,17 @@ int main(void)
 {
   CURL *curl;
   CURLcode res = CURLE_OK;
-  struct curl_slist *headers = NULL;
-  struct curl_slist *recipients = NULL;
-  struct curl_slist *slist = NULL;
-  curl_mime *mime;
-  curl_mime *alt;
-  curl_mimepart *part;
-  const char **cpp;
 
   curl = curl_easy_init();
   if(curl) {
+    struct curl_slist *headers = NULL;
+    struct curl_slist *recipients = NULL;
+    struct curl_slist *slist = NULL;
+    curl_mime *mime;
+    curl_mime *alt;
+    curl_mimepart *part;
+    const char **cpp;
+
     /* This is the URL for your mailserver */
     curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
 
index d84cea91384ac043779d09eddd09d0588c4e8587..905f51189d07c2f24c1340ca2173c91257acc05a 100644 (file)
@@ -257,25 +257,15 @@ int main(int argc, char *argv[])
 {
   CURL    *curl;
   conf_t  conf[1];
-  int     OptionIndex;
-  struct  tm *lt;
-  struct  tm *gmt;
-  time_t  tt;
-  time_t  tt_local;
-  time_t  tt_gmt;
-  double  tzonediffFloat;
-  int     tzonediffWord;
-  char    timeBuf[61];
-  char    tzoneBuf[16];
   int     RetValue;
 
-  OptionIndex     = 0;
   ShowAllHeader   = 0;    /* Do not show HTTP Header */
   AutoSyncTime    = 0;    /* Do not synchronise computer clock */
   RetValue        = 0;    /* Successful Exit */
   conf_init(conf);
 
   if(argc > 1) {
+    int OptionIndex = 0;
     while(OptionIndex < argc) {
       if(strncmp(argv[OptionIndex], "--server=", 9) == 0)
         snprintf(conf->timeserver, MAX_STRING, "%s", &argv[OptionIndex][9]);
@@ -308,6 +298,16 @@ int main(int argc, char *argv[])
   curl_global_init(CURL_GLOBAL_ALL);
   curl = curl_easy_init();
   if(curl) {
+    struct tm *lt;
+    struct tm *gmt;
+    time_t tt;
+    time_t tt_local;
+    time_t tt_gmt;
+    double tzonediffFloat;
+    int tzonediffWord;
+    char timeBuf[61];
+    char tzoneBuf[16];
+
     SyncTime_CURL_Init(curl, conf->http_proxy, conf->proxy_user);
 
     /* Calculating time diff between GMT and localtime */