]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
examples: prefer `return` over `exit()` (cont.)
authorViktor Szakats <commit@vsz.me>
Fri, 28 Feb 2025 16:44:55 +0000 (17:44 +0100)
committerViktor Szakats <commit@vsz.me>
Sat, 1 Mar 2025 01:09:02 +0000 (02:09 +0100)
Some of these calls were not in callbacks. These examples may leak
handles.

Also fix some whitespace.

Follow-up to 08c7c937dc0dbd1f92f73360e5d8b2bb2ee6afa8 #16507
Closes #16524

docs/examples/ephiperfifo.c
docs/examples/evhiperfifo.c
docs/examples/ghiper.c
docs/examples/hiperfifo.c

index 2104a6b887906f95fa5d24502708735d3ba7d91a..5a25429f764e4fe2828311bc10ad8e279248ef8c 100644 (file)
@@ -418,22 +418,22 @@ static int init_fifo(GlobalInfo *g)
   struct epoll_event epev;
 
   fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
-  if(lstat (fifo, &st) == 0) {
+  if(lstat(fifo, &st) == 0) {
     if((st.st_mode & S_IFMT) == S_IFREG) {
       errno = EEXIST;
       perror("lstat");
-      exit(1);
+      return 1;
     }
   }
   unlink(fifo);
-  if(mkfifo (fifo, 0600) == -1) {
+  if(mkfifo(fifo, 0600) == -1) {
     perror("mkfifo");
-    exit(1);
+    return 1;
   }
   sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
   if(sockfd == -1) {
     perror("open");
-    exit(1);
+    return 1;
   }
 
   g->fifofd = sockfd;
@@ -449,9 +449,9 @@ static int init_fifo(GlobalInfo *g)
 
 static void clean_fifo(GlobalInfo *g)
 {
-    epoll_ctl(g->epfd, EPOLL_CTL_DEL, g->fifofd, NULL);
-    fclose(g->input);
-    unlink(fifo);
+  epoll_ctl(g->epfd, EPOLL_CTL_DEL, g->fifofd, NULL);
+  fclose(g->input);
+  unlink(fifo);
 }
 
 
@@ -478,13 +478,13 @@ int main(int argc, char **argv)
   g.epfd = epoll_create1(EPOLL_CLOEXEC);
   if(g.epfd == -1) {
     perror("epoll_create1 failed");
-    exit(1);
+    return 1;
   }
 
   g.tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
   if(g.tfd == -1) {
     perror("timerfd_create failed");
-    exit(1);
+    return 1;
   }
 
   memset(&its, 0, sizeof(struct itimerspec));
@@ -496,7 +496,8 @@ int main(int argc, char **argv)
   ev.data.fd = g.tfd;
   epoll_ctl(g.epfd, EPOLL_CTL_ADD, g.tfd, &ev);
 
-  init_fifo(&g);
+  if(init_fifo(&g))
+    return 1;
   g.multi = curl_multi_init();
 
   /* setup the generic multi interface options we want */
@@ -521,7 +522,7 @@ int main(int argc, char **argv)
       }
       else {
         perror("epoll_wait");
-        exit(1);
+        return 1;
       }
     }
 
index a96687e8388120ede7f4fde69883dd3cef85c609..76a7cde469fe951ca25b08e03e960fec9b513e1a 100644 (file)
@@ -402,22 +402,22 @@ static int init_fifo(GlobalInfo *g)
   curl_socket_t sockfd;
 
   fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
-  if(lstat (fifo, &st) == 0) {
+  if(lstat(fifo, &st) == 0) {
     if((st.st_mode & S_IFMT) == S_IFREG) {
       errno = EEXIST;
       perror("lstat");
-      exit(1);
+      return 1;
     }
   }
   unlink(fifo);
-  if(mkfifo (fifo, 0600) == -1) {
+  if(mkfifo(fifo, 0600) == -1) {
     perror("mkfifo");
-    exit(1);
+    return 1;
   }
   sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
   if(sockfd == -1) {
     perror("open");
-    exit(1);
+    return 1;
   }
   g->input = fdopen(sockfd, "r");
 
@@ -436,7 +436,8 @@ int main(int argc, char **argv)
   memset(&g, 0, sizeof(GlobalInfo));
   g.loop = ev_default_loop(0);
 
-  init_fifo(&g);
+  if(init_fifo(&g))
+    return 1;
   g.multi = curl_multi_init();
 
   ev_timer_init(&g.timer_event, timer_cb, 0., 0.);
index 8e8300763c740ad4528683721c1753d07beb3177..5066ff5adc72c5fc10aa9cdc56235325995e988f 100644 (file)
@@ -392,21 +392,21 @@ int init_fifo(void)
     if((st.st_mode & S_IFMT) == S_IFREG) {
       errno = EEXIST;
       perror("lstat");
-      exit(1);
+      return CURL_SOCKET_BAD;
     }
   }
 
   unlink(fifo);
-  if(mkfifo (fifo, 0600) == -1) {
+  if(mkfifo(fifo, 0600) == -1) {
     perror("mkfifo");
-    exit(1);
+    return CURL_SOCKET_BAD;
   }
 
   socket = open(fifo, O_RDWR | O_NONBLOCK, 0);
 
-  if(socket == -1) {
+  if(socket == CURL_SOCKET_BAD) {
     perror("open");
-    exit(1);
+    return socket;
   }
   MSG_OUT("Now, pipe some URL's into > %s\n", fifo);
 
@@ -421,6 +421,8 @@ int main(void)
   GIOChannel* ch;
 
   fd = init_fifo();
+  if(fd == CURL_SOCKET_BAD)
+    return 1;
   ch = g_io_channel_unix_new(fd);
   g_io_add_watch(ch, G_IO_IN, fifo_cb, g);
   gmain = g_main_loop_new(NULL, FALSE);
index d9c00b811d7f819bac9f97d8ce298017ceafe03e..6e96794b607e2c155c0e8f2de6aff5c6ae013303 100644 (file)
@@ -399,22 +399,22 @@ static int init_fifo(GlobalInfo *g)
   curl_socket_t sockfd;
 
   fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
-  if(lstat (fifo, &st) == 0) {
+  if(lstat(fifo, &st) == 0) {
     if((st.st_mode & S_IFMT) == S_IFREG) {
       errno = EEXIST;
       perror("lstat");
-      exit(1);
+      return 1;
     }
   }
   unlink(fifo);
   if(mkfifo (fifo, 0600) == -1) {
     perror("mkfifo");
-    exit(1);
+    return 1;
   }
   sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
   if(sockfd == -1) {
     perror("open");
-    exit(1);
+    return 1;
   }
   g->input = fdopen(sockfd, "r");
 
@@ -440,7 +440,8 @@ int main(int argc, char **argv)
 
   memset(&g, 0, sizeof(GlobalInfo));
   g.evbase = event_base_new();
-  init_fifo(&g);
+  if(init_fifo(&g))
+    return 1;
   g.multi = curl_multi_init();
   evtimer_assign(&g.timer_event, g.evbase, timer_cb, &g);