blob: 4b853c698643b77476b99e8767b3fa6234103ce3 [file] [log] [blame]
Glenn L McGrathefc6bf62004-07-23 06:43:29 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath82364bb2004-01-27 09:22:20 +00002/*
Glenn L McGrathefc6bf62004-07-23 06:43:29 +00003 * seq implementation for busybox
4 *
Rob Landley73a20f32006-02-23 19:54:48 +00005 * Copyright (C) 2004, Glenn McGrath
Glenn L McGrath82364bb2004-01-27 09:22:20 +00006 *
Rob Landley73a20f32006-02-23 19:54:48 +00007 * Licensed under the GPL v2, see the file LICENSE in this tarball.
Glenn L McGrath82364bb2004-01-27 09:22:20 +00008 */
9
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Glenn L McGrath82364bb2004-01-27 09:22:20 +000011
Denis Vlasenko99912ca2007-04-10 15:43:37 +000012/* This is a NOFORK applet. Be very careful! */
13
14
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000015int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000016int seq_main(int argc, char **argv)
Glenn L McGrath82364bb2004-01-27 09:22:20 +000017{
Denis Vlasenko7753ea42008-11-12 21:37:19 +000018 enum {
19 OPT_w = (1 << 0),
20 OPT_s = (1 << 1),
21 };
Denis Vlasenko99912ca2007-04-10 15:43:37 +000022 double last, increment, i;
Denis Vlasenko7753ea42008-11-12 21:37:19 +000023 const char *sep, *opt_s = "\n";
24 unsigned opt = getopt32(argv, "+ws:", &opt_s);
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000025 unsigned width = 0;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000026
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000027 argc -= optind;
28 argv += optind;
Denis Vlasenko99912ca2007-04-10 15:43:37 +000029 i = increment = 1;
Rob Landley73a20f32006-02-23 19:54:48 +000030 switch (argc) {
Rob Landley73a20f32006-02-23 19:54:48 +000031 case 3:
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000032 increment = atof(argv[1]);
Rob Landley73a20f32006-02-23 19:54:48 +000033 case 2:
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000034 i = atof(*argv);
35 case 1:
Denis Vlasenkoce979602006-09-27 23:31:08 +000036 last = atof(argv[argc-1]);
Rob Landley73a20f32006-02-23 19:54:48 +000037 break;
38 default:
39 bb_show_usage();
Glenn L McGrath82364bb2004-01-27 09:22:20 +000040 }
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000041 if (opt & OPT_w) /* Pad to length of start or last */
42 width = MAX(strlen(*argv), strlen(argv[argc-1]));
Glenn L McGrath82364bb2004-01-27 09:22:20 +000043
Glenn L McGrathefc6bf62004-07-23 06:43:29 +000044 /* You should note that this is pos-5.0.91 semantics, -- FK. */
Denis Vlasenko7753ea42008-11-12 21:37:19 +000045 sep = "";
Denis Vlasenko99912ca2007-04-10 15:43:37 +000046 while ((increment > 0 && i <= last) || (increment < 0 && i >= last)) {
Denis Vlasenko7753ea42008-11-12 21:37:19 +000047 printf("%s%0*g", sep, width, i);
48 sep = opt_s;
Denis Vlasenko99912ca2007-04-10 15:43:37 +000049 i += increment;
Glenn L McGrath82364bb2004-01-27 09:22:20 +000050 }
Bernhard Reutner-Fischerc021cb02008-11-12 13:22:24 +000051 bb_putchar('\n');
Denis Vlasenko99912ca2007-04-10 15:43:37 +000052 return fflush(stdout);
Glenn L McGrath82364bb2004-01-27 09:22:20 +000053}