Mercurial > hg > toybox
annotate toys/seq.c @ 543:60b97ba66a70
Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 12 Mar 2012 23:00:28 -0500 |
parents | 15bad33d5ded |
children |
rev | line source |
---|---|
296 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * seq.c - Count from first to last, by increment. | |
4 * | |
5 * Copyright 2006 Rob Landley <rob@landley.net> | |
6 * | |
7 * Not in SUSv3. (Don't ask me why not.) | |
8 | |
304
93223118c813
Option parsing: stopearly is now a ^ prefix (not +), and an option string with
Rob Landley <rob@landley.net>
parents:
296
diff
changeset
|
9 USE_SEQ(NEWTOY(seq, "<1>3?", TOYFLAG_USR|TOYFLAG_BIN)) |
296 | 10 |
11 config SEQ | |
12 bool "seq" | |
511 | 13 depends on TOYBOX_FLOAT |
296 | 14 default y |
15 help | |
16 usage: seq [first] [increment] last | |
17 | |
18 Count from first to last, by increment. Omitted arguments default | |
19 to 1. Two arguments are used as first and last. Arguments can be | |
20 negative or floating point. | |
21 */ | |
22 | |
23 #include "toys.h" | |
24 | |
25 void seq_main(void) | |
26 { | |
27 double first, increment, last, dd; | |
28 | |
29 // Parse command line arguments, with appropriate defaults. | |
30 // Note that any non-numeric arguments are treated as zero. | |
31 first = increment = 1; | |
32 switch (toys.optc) { | |
33 case 3: | |
34 increment = atof(toys.optargs[1]); | |
35 case 2: | |
36 first = atof(*toys.optargs); | |
37 default: | |
38 last = atof(toys.optargs[toys.optc-1]); | |
39 } | |
40 | |
41 // Yes, we're looping on a double. Yes rounding errors can accumulate if | |
42 // you use a non-integer increment. Deal with it. | |
43 for (dd=first; (increment>0 && dd<=last) || (increment <0 && dd>=last); | |
44 dd+=increment) | |
45 { | |
46 printf("%g\n", dd); | |
47 } | |
48 } |