ls: do not follow links with -s (closes bug 33),
[oweals/busybox.git] / coreutils / seq.c
index 8a2a80c144264215fb4694390fcde0780d396e2d..4b853c698643b77476b99e8767b3fa6234103ce3 100644 (file)
@@ -1,44 +1,53 @@
+/* vi: set sw=4 ts=4: */
 /*
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of version 2 of the GNU General Public License as
- *  published by the Free Software Foundation.
+ * seq implementation for busybox
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU Library General Public License for more details.
+ * Copyright (C) 2004, Glenn McGrath
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Licensed under the GPL v2, see the file LICENSE in this tarball.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include "busybox.h"
+#include "libbb.h"
 
-extern int seq_main(int argc, char **argv)
+/* This is a NOFORK applet. Be very careful! */
+
+
+int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int seq_main(int argc, char **argv)
 {
-       double last;
-       double first = 1;
-       double increment = 1;
-       double i;
+       enum {
+               OPT_w = (1 << 0),
+               OPT_s = (1 << 1),
+       };
+       double last, increment, i;
+       const char *sep, *opt_s = "\n";
+       unsigned opt = getopt32(argv, "+ws:", &opt_s);
+       unsigned width = 0;
 
-       if (argc == 4) {
-               first = atof(argv[1]);
-               increment = atof(argv[2]);
-       }
-       else if (argc == 3) {
-               first = atof(argv[1]);
+       argc -= optind;
+       argv += optind;
+       i = increment = 1;
+       switch (argc) {
+               case 3:
+                       increment = atof(argv[1]);
+               case 2:
+                       i = atof(*argv);
+               case 1:
+                       last = atof(argv[argc-1]);
+                       break;
+               default:
+                       bb_show_usage();
        }
-       else if (argc != 2) {
-               bb_show_usage();
-       }
-       last = atof(argv[argc - 1]);
+       if (opt & OPT_w) /* Pad to length of start or last */
+               width = MAX(strlen(*argv), strlen(argv[argc-1]));
 
-       for (i = first; ((first <= last) ? (i <= last): (i >= last));i += increment) {
-               printf("%g\n", i);
+       /* You should note that this is pos-5.0.91 semantics, -- FK. */
+       sep = "";
+       while ((increment > 0 && i <= last) || (increment < 0 && i >= last)) {
+               printf("%s%0*g", sep, width, i);
+               sep = opt_s;
+               i += increment;
        }
-
-       return(EXIT_SUCCESS);
+       bb_putchar('\n');
+       return fflush(stdout);
 }