1 /* A stupid little spinning wheel designed to make it look like useful work
4 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
27 #include <unistd.h> /* for isatty */
36 /* "alarm" is not available on mingw32, and the SIGALRM constant is not
37 defined. Don't bother with a spinner in this case. */
38 #if ! HAVE_ALARM || ! defined (SIGALRM)
39 #define alarm(n) abort()
40 #define signal(sig,func) SIG_ERR
44 /* An application can update this to get a count printed with the spinner.
45 If left at 0, no count is printed. */
47 unsigned long spinner_count = 0;
50 int spinner_wanted = -1; /* -1 uninitialized, 1 wanted, 0 not */
51 int spinner_tick = 1; /* 1 ready to print, 0 not */
56 spinner_signal (int signum)
60 if (signal (SIGALRM, spinner_signal) == SIG_ERR)
62 printf ("spinner_signal(): Oops, cannot reinstall SIGALRM\n");
69 /* Initialize the spinner.
71 This is done the first time spinner() is called, so an application
72 doesn't need to call this directly.
74 The spinner is only wanted if the output is a tty. */
76 #define SPINNER_WANTED_INIT() \
77 if (spinner_wanted < 0) spinner_init ()
82 spinner_wanted = isatty (fileno (stdout));
83 if (spinner_wanted == -1)
89 if (signal (SIGALRM, spinner_signal) == SIG_ERR)
91 printf ("(no spinner)\r");
97 /* unbufferred output so the spinner will show up */
98 setbuf (stdout, NULL);
105 static const char data[] = { '|', '/', '-', '\\' };
110 SPINNER_WANTED_INIT ();
115 pos = (pos + 1) % numberof (data);
118 if (spinner_count != 0)
120 sprintf (buf+1, " %lu\r", spinner_count);