claenups for previous commit
[oweals/busybox.git] / coreutils / cut.c
index 53f343a335b501cc9fcb147f21ec4dee82ee2aba..cdd90ab44c01b9a1d4524ca4a6cb78592ff545dc 100644 (file)
@@ -6,8 +6,35 @@
  * Written by Mark Whitley <markw@codepoet.org>
  * debloated by Bernhard Reutner-Fischer
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
+//config:config CUT
+//config:      bool "cut (5.3 kb)"
+//config:      default y
+//config:      help
+//config:      cut is used to print selected parts of lines from
+//config:      each file to stdout.
+
+//applet:IF_CUT(APPLET_NOEXEC(cut, cut, BB_DIR_USR_BIN, BB_SUID_DROP, cut))
+
+//kbuild:lib-$(CONFIG_CUT) += cut.o
+
+//usage:#define cut_trivial_usage
+//usage:       "[OPTIONS] [FILE]..."
+//usage:#define cut_full_usage "\n\n"
+//usage:       "Print selected fields from each input FILE to stdout\n"
+//usage:     "\n       -b LIST Output only bytes from LIST"
+//usage:     "\n       -c LIST Output only characters from LIST"
+//usage:     "\n       -d CHAR Use CHAR instead of tab as the field delimiter"
+//usage:     "\n       -s      Output only the lines containing delimiter"
+//usage:     "\n       -f N    Print only these fields"
+//usage:     "\n       -n      Ignored"
+//usage:
+//usage:#define cut_example_usage
+//usage:       "$ echo \"Hello world\" | cut -f 1 -d ' '\n"
+//usage:       "Hello\n"
+//usage:       "$ echo \"Hello world\" | cut -f 2 -d ' '\n"
+//usage:       "world\n"
 
 #include "libbb.h"
 
@@ -15,7 +42,7 @@
 
 
 /* option vars */
-static const char optstring[] ALIGN1 = "b:c:f:d:sn";
+#define OPT_STR "b:c:f:d:sn"
 #define CUT_OPT_BYTE_FLGS     (1 << 0)
 #define CUT_OPT_CHAR_FLGS     (1 << 1)
 #define CUT_OPT_FIELDS_FLGS   (1 << 2)
@@ -37,7 +64,6 @@ static int cmpfunc(const void *a, const void *b)
 {
        return (((struct cut_list *) a)->startpos -
                        ((struct cut_list *) b)->startpos);
-
 }
 
 static void cut_file(FILE *file, char delim, const struct cut_list *cut_lists, unsigned nlists)
@@ -175,8 +201,11 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
        char *sopt, *ltok;
        unsigned opt;
 
-       opt_complementary = "b--bcf:c--bcf:f--bcf";
-       opt = getopt32(argv, optstring, &sopt, &sopt, &sopt, &ltok);
+       opt = getopt32(argv, "^"
+                       OPT_STR
+                       "\0" "b--bcf:c--bcf:f--bcf",
+                       &sopt, &sopt, &sopt, &ltok
+       );
 //     argc -= optind;
        argv += optind;
        if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS)))
@@ -196,7 +225,7 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
                if (opt & CUT_OPT_SUPPRESS_FLGS) {
                        bb_error_msg_and_die
                                ("suppressing non-delimited lines makes sense%s",
-                                _op_on_field);
+                               _op_on_field);
                }
                if (delim != '\t') {
                        bb_error_msg_and_die
@@ -225,7 +254,7 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
                        if (!ntok[0]) {
                                s = BOL;
                        } else {
-                               s = xatoi_u(ntok);
+                               s = xatoi_positive(ntok);
                                /* account for the fact that arrays are zero based, while
                                 * the user expects the first char on the line to be char #1 */
                                if (s != 0)
@@ -238,7 +267,7 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
                        } else if (!ltok[0]) {
                                e = EOL;
                        } else {
-                               e = xatoi_u(ltok);
+                               e = xatoi_positive(ltok);
                                /* if the user specified and end position of 0,
                                 * that means "til the end of the line" */
                                if (e == 0)