Mercurial > hg > toybox
annotate toys/seq.c @ 481:e1b9a8579ddb
mkswap helpstring and argument string
author | Elie De Brauwer <eliedebrauwer@gmail.com> |
---|---|
date | Sat, 18 Feb 2012 14:13:55 +0100 |
parents | 93223118c813 |
children | 15bad33d5ded |
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" | |
13 default y | |
14 help | |
15 usage: seq [first] [increment] last | |
16 | |
17 Count from first to last, by increment. Omitted arguments default | |
18 to 1. Two arguments are used as first and last. Arguments can be | |
19 negative or floating point. | |
20 */ | |
21 | |
22 #include "toys.h" | |
23 | |
24 void seq_main(void) | |
25 { | |
26 double first, increment, last, dd; | |
27 | |
28 // Parse command line arguments, with appropriate defaults. | |
29 // Note that any non-numeric arguments are treated as zero. | |
30 first = increment = 1; | |
31 switch (toys.optc) { | |
32 case 3: | |
33 increment = atof(toys.optargs[1]); | |
34 case 2: | |
35 first = atof(*toys.optargs); | |
36 default: | |
37 last = atof(toys.optargs[toys.optc-1]); | |
38 } | |
39 | |
40 // Yes, we're looping on a double. Yes rounding errors can accumulate if | |
41 // you use a non-integer increment. Deal with it. | |
42 for (dd=first; (increment>0 && dd<=last) || (increment <0 && dd>=last); | |
43 dd+=increment) | |
44 { | |
45 printf("%g\n", dd); | |
46 } | |
47 } |