annotate toys/seq.c @ 587:82ffae226c40

Convert another realpath use to xrealpath().
author Rob Landley <rob@landley.net>
date Fri, 01 Jun 2012 17:59:11 -0500
parents 15bad33d5ded
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
296
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * seq.c - Count from first to last, by increment.
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2006 Rob Landley <rob@landley.net>
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * Not in SUSv3. (Don't ask me why not.)
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config SEQ
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "seq"
511
15bad33d5ded This uses floating point.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
13 depends on TOYBOX_FLOAT
296
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 default y
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 help
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 usage: seq [first] [increment] last
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
17
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 Count from first to last, by increment. Omitted arguments default
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 to 1. Two arguments are used as first and last. Arguments can be
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 negative or floating point.
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 */
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
22
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 #include "toys.h"
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 void seq_main(void)
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 {
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 double first, increment, last, dd;
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
28
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 // Parse command line arguments, with appropriate defaults.
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 // Note that any non-numeric arguments are treated as zero.
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 first = increment = 1;
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 switch (toys.optc) {
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 case 3:
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 increment = atof(toys.optargs[1]);
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 case 2:
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 first = atof(*toys.optargs);
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 default:
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 last = atof(toys.optargs[toys.optc-1]);
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 }
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
40
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 // Yes, we're looping on a double. Yes rounding errors can accumulate if
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 // you use a non-integer increment. Deal with it.
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 for (dd=first; (increment>0 && dd<=last) || (increment <0 && dd>=last);
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 dd+=increment)
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 {
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 printf("%g\n", dd);
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 }
8350d13fba01 Add seq, and tests for it.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 }