]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/worm.c
ncurses 6.4 - patch 20240330
[ncurses.git] / test / worm.c
index 4888a519c8a256ac2b3a5040a5857d9e5b59d0bc..7c4842e4004f3bc772080e9dd9f2607402691e60 100644 (file)
@@ -53,7 +53,7 @@
   traces will be dumped.  The program stops and waits for one character of
   input at the beginning and end of the interval.
 
-  $Id: worm.c,v 1.84 2022/07/23 17:06:16 tom Exp $
+  $Id: worm.c,v 1.89 2022/12/24 20:46:49 tom Exp $
 */
 
 #include <test.priv.h>
@@ -368,7 +368,11 @@ start_worm(void *arg)
     unsigned long compare = 0;
     Trace(("start_worm"));
     while (!quit_worm((int) (((struct worm *) arg) - worm))) {
-       while (compare < sequence) {
+       for (;;) {
+           bool done = FALSE;
+           Locked(done = (compare >= sequence));
+           if (done)
+               break;
            ++compare;
            USING_WINDOW2(stdscr, draw_worm, arg);
        }
@@ -447,12 +451,13 @@ update_refs(WINDOW *win, void *data)
 #endif
 
 static void
-usage(void)
+usage(int ok)
 {
     static const char *msg[] =
     {
        "Usage: worm [options]"
        ,""
+       ,USAGE_COMMON
        ,"Options:"
 #if HAVE_USE_DEFAULT_COLORS
        ," -d       invoke use_default_colors"
@@ -471,8 +476,11 @@ usage(void)
     for (n = 0; n < SIZEOF(msg); n++)
        fprintf(stderr, "%s\n", msg[n]);
 
-    ExitProgram(EXIT_FAILURE);
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
 
 int
 main(int argc, char *argv[])
@@ -489,7 +497,7 @@ main(int argc, char *argv[])
 
     setlocale(LC_ALL, "");
 
-    while ((ch = getopt(argc, argv, "dfl:n:tT:N")) != -1) {
+    while ((ch = getopt(argc, argv, OPTS_COMMON "dfl:n:tT:N")) != -1) {
        switch (ch) {
 #if HAVE_USE_DEFAULT_COLORS
        case 'd':
@@ -502,13 +510,13 @@ main(int argc, char *argv[])
        case 'l':
            if ((length = atoi(optarg)) < 2 || length > MAX_LENGTH) {
                fprintf(stderr, "%s: Invalid length\n", *argv);
-               usage();
+               usage(FALSE);
            }
            break;
        case 'n':
            if ((number = atoi(optarg)) < 1 || number > MAX_WORMS) {
                fprintf(stderr, "%s: Invalid number of worms\n", *argv);
-               usage();
+               usage(FALSE);
            }
            break;
        case 't':
@@ -517,19 +525,22 @@ main(int argc, char *argv[])
 #ifdef TRACE
        case 'T':
            if (sscanf(optarg, "%d,%d", &trace_start, &trace_end) != 2)
-               usage();
+               usage(FALSE);
            break;
        case 'N':
            _nc_optimize_enable ^= OPTIMIZE_ALL;        /* declared by ncurses */
            break;
 #endif /* TRACE */
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
        default:
-           usage();
+           usage(ch == OPTS_USAGE);
            /* NOTREACHED */
        }
     }
     if (optind < argc)
-       usage();
+       usage(FALSE);
 
     signal(SIGINT, onsig);
     initscr();
@@ -663,6 +674,15 @@ main(int argc, char *argv[])
 
     Trace(("Cleanup"));
     cleanup();
+#ifdef USE_PTHREADS
+    /*
+     * Do this just in case one of the threads did not really exit.
+     */
+    Trace(("join all threads"));
+    for (n = 0; n < number; n++) {
+       pthread_join(worm[n].thread, NULL);
+    }
+#endif
 #if NO_LEAKS
     for (y = 0; y < max_refs; y++) {
        free(refs[y]);
@@ -672,15 +692,6 @@ main(int argc, char *argv[])
        free(w->xpos);
        free(w->ypos);
     }
-#endif
-#ifdef USE_PTHREADS
-    /*
-     * Do this just in case one of the threads did not really exit.
-     */
-    Trace(("join all threads"));
-    for (n = 0; n < number; n++) {
-       pthread_join(worm[n].thread, NULL);
-    }
 #endif
     ExitProgram(EXIT_SUCCESS);
 }