Style facelift.

- Reduce the number of global variables
- Make global objects static
- Use bool consistently
- Sort getopt arguments and their processing
- Add function comments
- Change notlast != 0 into !last
This commit is contained in:
Diomidis Spinellis 2006-11-06 15:58:35 +00:00
parent df449c3551
commit 3241f274f1

View File

@ -62,11 +62,13 @@ __FBSDID("$FreeBSD$");
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
/* Defaults */
#define REPS_DEF 100 #define REPS_DEF 100
#define BEGIN_DEF 1 #define BEGIN_DEF 1
#define ENDER_DEF 100 #define ENDER_DEF 100
#define STEP_DEF 1 #define STEP_DEF 1
/* Flags of options that have been set */
#define HAVE_STEP 1 #define HAVE_STEP 1
#define HAVE_ENDER 2 #define HAVE_ENDER 2
#define HAVE_BEGIN 4 #define HAVE_BEGIN 4
@ -74,52 +76,43 @@ __FBSDID("$FreeBSD$");
#define is_default(s) (*(s) == 0 || strcmp((s), "-") == 0) #define is_default(s) (*(s) == 0 || strcmp((s), "-") == 0)
double begin; static bool boring;
double ender; static int prec;
double s; static bool longdata;
long reps; static bool intdata;
int randomize; static bool chardata;
int infinity; static bool nosign;
int boring; static const char *sepstring = "\n";
int prec; static char format[BUFSIZ];
int longdata;
int intdata;
int chardata;
int nosign;
int nofinalnl;
const char *sepstring = "\n";
char format[BUFSIZ];
void getformat(void); static void getformat(void);
int getprec(char *); static int getprec(const char *);
int putdata(double, long); static int putdata(double, bool);
static void usage(void); static void usage(void);
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
bool have_format = false;
bool infinity = false;
bool nofinalnl = false;
bool randomize = false;
bool use_random = false;
int ch;
int mask = 0;
int n = 0;
double begin;
double divisor;
double ender;
double s;
double x, y; double x, y;
long i; long i;
unsigned int mask = 0; long reps;
int n = 0;
int ch;
bool use_random = false;
bool have_format = false;
double divisor;
while ((ch = getopt(argc, argv, "rb:w:cs:np:")) != -1) while ((ch = getopt(argc, argv, "b:cnp:rs:w:")) != -1)
switch (ch) { switch (ch) {
case 'r':
randomize = 1;
break;
case 'c':
chardata = 1;
break;
case 'n':
nofinalnl = 1;
break;
case 'b': case 'b':
boring = 1; boring = true;
/* FALLTHROUGH */ /* FALLTHROUGH */
case 'w': case 'w':
if (strlcpy(format, optarg, sizeof(format)) >= if (strlcpy(format, optarg, sizeof(format)) >=
@ -127,8 +120,11 @@ main(int argc, char **argv)
errx(1, "-%c word too long", ch); errx(1, "-%c word too long", ch);
have_format = true; have_format = true;
break; break;
case 's': case 'c':
sepstring = optarg; chardata = true;
break;
case 'n':
nofinalnl = true;
break; break;
case 'p': case 'p':
prec = atoi(optarg); prec = atoi(optarg);
@ -136,6 +132,12 @@ main(int argc, char **argv)
errx(1, "bad precision value"); errx(1, "bad precision value");
have_format = true; have_format = true;
break; break;
case 'r':
randomize = true;
break;
case 's':
sepstring = optarg;
break;
default: default:
usage(); usage();
} }
@ -263,7 +265,7 @@ main(int argc, char **argv)
errx(1, "bad mask"); errx(1, "bad mask");
} }
if (reps == 0) if (reps == 0)
infinity = 1; infinity = true;
if (randomize) { if (randomize) {
if (use_random) { if (use_random) {
srandom((unsigned long)s); srandom((unsigned long)s);
@ -283,8 +285,8 @@ main(int argc, char **argv)
begin >= 0 && begin < divisor && begin >= 0 && begin < divisor &&
ender >= 0 && ender < divisor) { ender >= 0 && ender < divisor) {
ender += 1; ender += 1;
nosign = 1; nosign = true;
intdata = 1; intdata = true;
(void)strlcpy(format, (void)strlcpy(format,
chardata ? "%c" : "%u", sizeof(format)); chardata ? "%c" : "%u", sizeof(format));
} }
@ -294,20 +296,26 @@ main(int argc, char **argv)
y = random() / divisor; y = random() / divisor;
else else
y = arc4random() / divisor; y = arc4random() / divisor;
if (putdata(y * x + begin, reps - i)) if (putdata(y * x + begin, !(reps - i)))
errx(1, "range error in conversion"); errx(1, "range error in conversion");
} }
} else } else
for (i = 1, x = begin; i <= reps || infinity; i++, x += s) for (i = 1, x = begin; i <= reps || infinity; i++, x += s)
if (putdata(x, reps - i)) if (putdata(x, !(reps - i)))
errx(1, "range error in conversion"); errx(1, "range error in conversion");
if (!nofinalnl) if (!nofinalnl)
putchar('\n'); putchar('\n');
exit(0); exit(0);
} }
int /*
putdata(double x, long int notlast) * Send x to stdout using the specified format.
* Last is true if this is the set's last value.
* Return 0 if OK, or a positive number if the number passed was
* outside the range specified by the various flags.
*/
static int
putdata(double x, bool last)
{ {
if (boring) if (boring)
@ -335,7 +343,7 @@ putdata(double x, long int notlast)
} else } else
printf(format, x); printf(format, x);
if (notlast != 0) if (!last)
fputs(sepstring, stdout); fputs(sepstring, stdout);
return (0); return (0);
@ -350,11 +358,15 @@ usage(void)
exit(1); exit(1);
} }
int /*
getprec(char *str) * Return the number of digits following the number's decimal point.
* Return 0 if no decimal point is found.
*/
static int
getprec(const char *str)
{ {
char *p; const char *p;
char *q; const char *q;
for (p = str; *p; p++) for (p = str; *p; p++)
if (*p == '.') if (*p == '.')
@ -367,7 +379,11 @@ getprec(char *str)
return (p - q); return (p - q);
} }
void /*
* Set format, intdata, chardata, longdata, and nosign
* based on the command line arguments.
*/
static void
getformat(void) getformat(void)
{ {
char *p, *p2; char *p, *p2;
@ -386,7 +402,7 @@ getformat(void)
} else if (!*p && chardata) { } else if (!*p && chardata) {
if (strlcpy(p, "%c", sz) >= sz) if (strlcpy(p, "%c", sz) >= sz)
errx(1, "-w word too long"); errx(1, "-w word too long");
intdata = 1; intdata = true;
} else if (!*(p+1)) { } else if (!*(p+1)) {
if (sz <= 0) if (sz <= 0)
errx(1, "-w word too long"); errx(1, "-w word too long");
@ -413,7 +429,7 @@ getformat(void)
goto fmt_broken; goto fmt_broken;
} }
if (*p == 'l') { if (*p == 'l') {
longdata = 1; longdata = true;
if (*++p == 'l') { if (*++p == 'l') {
if (p[1] != '\0') if (p[1] != '\0')
p++; p++;
@ -422,24 +438,24 @@ getformat(void)
} }
switch (*p) { switch (*p) {
case 'o': case 'u': case 'x': case 'X': case 'o': case 'u': case 'x': case 'X':
intdata = nosign = 1; intdata = nosign = true;
break; break;
case 'd': case 'i': case 'd': case 'i':
intdata = 1; intdata = true;
break; break;
case 'D': case 'D':
if (!longdata) { if (!longdata) {
intdata = 1; intdata = true;
break; break;
} }
case 'O': case 'U': case 'O': case 'U':
if (!longdata) { if (!longdata) {
intdata = nosign = 1; intdata = nosign = true;
break; break;
} }
case 'c': case 'c':
if (!(intdata | longdata)) { if (!(intdata | longdata)) {
chardata = 1; chardata = true;
break; break;
} }
case 'h': case 'n': case 'p': case 'q': case 's': case 'L': case 'h': case 'n': case 'p': case 'q': case 's': case 'L':