Another cleanup patch that's been in my tree for a while. Again I think it's

originally from Bernhard Fischer...
This commit is contained in:
Rob Landley 2005-12-02 17:55:45 +00:00
parent b5ca9e0c4e
commit 746cfc8bf0

View File

@ -6,19 +6,7 @@
Modified for busybox based on coreutils v 5.0
Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
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 General Public License for more details.
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 or later, see the file LICENSE in this tarball.
*/
#include <ctype.h>
@ -26,13 +14,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/types.h>
#include <unistd.h>
#include "busybox.h"
/* If nonzero, count bytes, not column positions. */
static int count_bytes;
static unsigned long flags;
#define FLAG_COUNT_BYTES 1
#define FLAG_BREAK_SPACES 2
#define FLAG_WIDTH 4
/* Assuming the current column is COLUMN, return the column that
printing C will move the cursor to.
@ -40,7 +30,7 @@ static int count_bytes;
static int adjust_column(int column, char c)
{
if (!count_bytes) {
if (!(flags & FLAG_COUNT_BYTES)) {
if (c == '\b') {
if (column > 0)
column--;
@ -57,18 +47,14 @@ static int adjust_column(int column, char c)
extern int fold_main(int argc, char **argv)
{
/* If nonzero, try to break on whitespace. */
int break_spaces;
/* If nonzero, at least one of the files we read was standard input. */
int have_read_stdin;
int have_read_stdin = 0;
int width = 80;
int i;
int optc;
int errs = 0;
break_spaces = count_bytes = have_read_stdin = 0;
have_read_stdin = 0;
/* Turn any numeric options into -w options. */
for (i = 1; i < argc; i++) {
@ -88,22 +74,10 @@ extern int fold_main(int argc, char **argv)
}
}
while ((optc = getopt(argc, argv, "bsw:")) > 0) {
switch (optc) {
case 'b': /* Count bytes rather than columns. */
count_bytes = 1;
break;
case 's': /* Break at word boundaries. */
break_spaces = 1;
break;
case 'w': { /* Line width. */
width = bb_xgetlarg(optarg, 10, 1, 10000);
break;
}
default:
bb_show_usage();
}
}
char *w_opt;
flags = bb_getopt_ulflags(argc, argv, "bsw:", &w_opt);
if (flags & 4)
width = bb_xgetlarg(w_opt, 10, 1, 10000);
argv += optind;
if (!*argv) {
@ -139,7 +113,7 @@ rescan:
/* This character would make the line too long.
Print the line plus a newline, and make this character
start the next line. */
if (break_spaces) {
if (flags & FLAG_BREAK_SPACES) {
/* Look for the last blank. */
int logical_end;