From 5c36a0eb7cdb0360f9afd5d747c321f423b35984 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 7 Dec 2006 00:25:37 +0100 Subject: Imported from util-linux-2.9i tarball. --- text-utils/Makefile | 19 ++--- text-utils/colcrt.c | 7 +- text-utils/colrm.c | 5 +- text-utils/column.c | 1 - text-utils/more.c | 129 ++++++++++++++++++------------- text-utils/strings.1 | 96 ----------------------- text-utils/strings.c | 212 --------------------------------------------------- text-utils/ul.c | 90 ++++++++++++++++------ 8 files changed, 153 insertions(+), 406 deletions(-) delete mode 100644 text-utils/strings.1 delete mode 100644 text-utils/strings.c (limited to 'text-utils') diff --git a/text-utils/Makefile b/text-utils/Makefile index c657be72b..686418d61 100644 --- a/text-utils/Makefile +++ b/text-utils/Makefile @@ -11,10 +11,6 @@ include ../MCONFIG MAN1= col.1 colcrt.1 colrm.1 column.1 hexdump.1 more.1 rev.1 \ ul.1 -ifeq "$(HAVE_STRINGS)" "no" -MAN1:=$(MAN1) strings.1 -endif - # Where to put binaries? # See the "install" rule for the links. . . @@ -22,10 +18,6 @@ BIN= more USRBIN= col colcrt colrm column hexdump rev ul -ifeq "$(HAVE_STRINGS)" "no" -USRBIN:=$(USRBIN) strings -endif - # Where to put datebase files? USRLIB= more.help @@ -45,8 +37,7 @@ endif # Rules for hexdump -hexdump: hexdump.o conv.o display.o hexsyntax.o odsyntax.o parse.o \ - $(BSD)/getopt.o +hexdump: hexdump.o conv.o display.o hexsyntax.o odsyntax.o parse.o hexdump.o: hexdump.c hexdump.h conv.o: conv.c hexdump.h display.o: display.c hexdump.h @@ -56,13 +47,13 @@ parse.o: parse.c hexdump.h # Rules for everything else -col: col.o $(BSD)/getopt.o +col: col.o colcrt: colcrt.o colrm: colrm.o -column: column.o $(BSD)/err.o -more.o: more.c $(BSD)/pathnames.h +column: column.o $(ERR_O) +more.o: more.c $(LIB)/pathnames.h rev: rev.o -strings: strings.o $(BSD)/getopt.o + ifeq "$(HAVE_NCURSES)" "yes" more: more.o ul: ul.o diff --git a/text-utils/colcrt.c b/text-utils/colcrt.c index a4d19bb5a..f21f76927 100644 --- a/text-utils/colcrt.c +++ b/text-utils/colcrt.c @@ -64,10 +64,11 @@ char printall; char *progname; FILE *f; -int main(int argc, char **argv) +int +main(int argc, char **argv) { - register c; - register char *cp, *dp; + int c; + char *cp, *dp; argc--; progname = *argv++; diff --git a/text-utils/colrm.c b/text-utils/colrm.c index 3051c866c..f189e25d2 100644 --- a/text-utils/colrm.c +++ b/text-utils/colrm.c @@ -40,9 +40,10 @@ COLRM removes unwanted columns from a file int getn(char *ap); -int main(int argc, char **argv) +int +main(int argc, char **argv) { - register c, ct, first, last; + register int c, ct, first, last; first = 0; last = 0; diff --git a/text-utils/column.c b/text-utils/column.c index 09f52ebba..4ae9522ef 100644 --- a/text-utils/column.c +++ b/text-utils/column.c @@ -164,7 +164,6 @@ r_columnate() int base, chcnt, cnt, col, endcol, numcols, numrows, row; maxlength = (maxlength + TAB) & ~(TAB - 1); - /* This could be 0 */ numcols = termwidth / maxlength; if (!numcols) numcols = 1; diff --git a/text-utils/more.c b/text-utils/more.c index 6e0763169..24045472d 100644 --- a/text-utils/more.c +++ b/text-utils/more.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1980 Regents Of the University of California. + * Copyright (C) 1980 The Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms are permitted @@ -49,6 +49,7 @@ #include #endif #include +#include #define HELPFILE "/usr/lib/more.help" #define VI "/usr/bin/vi" @@ -93,6 +94,8 @@ int get_line(register FILE *f, int *length); void prbuf (register char *s, register int n); int xprintf (char *fmt, ...); void execute (char *filename, char *cmd, ...); +void errwrite (char *txt); +void errwrite1 (char *sym); #define TBUFSIZ 1024 #define LINSIZ 256 @@ -149,7 +152,7 @@ int soglitch; /* terminal has standout mode glitch */ int ulglitch; /* terminal has underline mode glitch */ int pstate = 0; /* current UL state */ char *getenv(); -static magic(); +static int magic(); struct { long chrctr, line; } context, screen_start; @@ -165,7 +168,7 @@ int main(int argc, char **argv) { FILE *f; char *s; char *p; - char ch; + int ch; int left; int prnames = 0; int initopt = 0; @@ -183,6 +186,7 @@ int main(int argc, char **argv) { nfiles = argc; fnames = argv; + setlocale(LC_ALL, ""); initterm (); nscroll = Lpp/2 - 1; if (nscroll <= 0) @@ -286,8 +290,7 @@ int main(int argc, char **argv) { if (firstf) setjmp (restore); if (firstf) { firstf = 0; - if (srchopt) - { + if (srchopt) { search (initbuf, f, 1); if (noscroll) left--; @@ -300,11 +303,12 @@ int main(int argc, char **argv) { left = command (fnames[fnum], f); } if (left != 0) { - if ((noscroll || clearit) && (file_size != LONG_MAX)) + if ((noscroll || clearit) && (file_size != LONG_MAX)) { if (clreol) home (); else doclear (); + } if (prnames) { if (bad_so) erasep (0); @@ -434,21 +438,23 @@ magic(f, fs) FILE *f; char *fs; { - struct exec ex; + char twobytes[2]; - if (fread(&ex, sizeof(ex), 1, f) == 1) - switch(ex.a_info) { - case OMAGIC: - case NMAGIC: - case ZMAGIC: + if (fread(twobytes, 2, 1, f) == 1) { + switch(twobytes[0] + (twobytes[1]<<8)) { + case OMAGIC: /* 0407 */ + case NMAGIC: /* 0410 */ + case ZMAGIC: /* 0413 */ case 0405: case 0411: case 0177545: + case 0x457f: /* simple ELF detection */ xprintf("\n******** %s: Not a text file ********\n\n", fs); (void)fclose(f); return(1); } - (void)fseek(f, 0L, L_SET); /* rewind() not necessary */ + } + (void)fseek(f, 0L, SEEK_SET); /* rewind() not necessary */ return(0); } @@ -547,7 +553,7 @@ void onquit() Pause++; } else if (!dum_opt && notell) { - write (2, "[Use q or Q to quit]", 20); + errwrite("[Use q or Q to quit]"); promptlen += 20; notell = 0; } @@ -597,7 +603,7 @@ void end_it () fflush (stdout); } else - write (2, "\n", 1); + errwrite("\n"); _exit(0); } @@ -682,7 +688,7 @@ void scanstr (int n, char *str) *sptr = '\0'; } -static char my_bell = ctrl('G'); +#define ringbell() errwrite("\007"); #ifdef undef strlen (s) @@ -750,7 +756,7 @@ char *filename; fflush(stdout); } else - write (2, &my_bell, 1); + ringbell(); inwait++; } @@ -787,7 +793,7 @@ int get_line(register FILE *f, int *length) break; } *p++ = c; - if (c == '\t') + if (c == '\t') { if (!hardtabs || (column < promptlen && !hard)) { if (hardtabs && eraseln && !dumb) { column = 1 + (column | 7); @@ -805,9 +811,9 @@ int get_line(register FILE *f, int *length) } else column = 1 + (column | 7); - else if (c == '\b' && column > 0) + } else if (c == '\b' && column > 0) { column--; - else if (c == '\r') { + } else if (c == '\r') { int next = Getc(f); if (next == '\n') { p--; @@ -816,18 +822,16 @@ int get_line(register FILE *f, int *length) } Ungetc(next,f); column = 0; - } - else if (c == '\f' && stop_opt) { + } else if (c == '\f' && stop_opt) { p[-1] = '^'; *p++ = 'L'; column += 2; Pause++; - } - else if (c == EOF) { + } else if (c == EOF) { *length = p - Line; return (column); } - else if (c >= ' ' && c != RUBOUT) + else if (isprint(c)) column++; if (column >= Mcol && fold_opt) break; c = Getc (f); @@ -988,7 +992,7 @@ int command (char *filename, register FILE *f) { register int nlines; register int retval = 0; - register char c; + register int c; char colonch; FILE *helpf; int done; @@ -1030,7 +1034,7 @@ int command (char *filename, register FILE *f) register int initline; if (no_intty) { - write(2, &my_bell, 1); + ringbell(); return (-1); } @@ -1122,7 +1126,7 @@ int command (char *filename, register FILE *f) ret (dlines); } else { - write (2, &my_bell, 1); + ringbell(); break; } case '\'': @@ -1134,7 +1138,7 @@ int command (char *filename, register FILE *f) ret (dlines); } else { - write (2, &my_bell, 1); + ringbell(); break; } case '=': @@ -1151,12 +1155,12 @@ int command (char *filename, register FILE *f) promptlen = 1; fflush (stdout); if (lastp) { - write (2,"\r", 1); + errwrite ("\r"); search (NULL, f, nlines); /* Use previous r.e. */ } else { ttyin (cmdbuf, sizeof(cmdbuf)-2, '/'); - write (2, "\r", 1); + errwrite("\r"); search (cmdbuf, f, nlines); } ret (dlines-1); @@ -1195,7 +1199,7 @@ int command (char *filename, register FILE *f) fflush (stdout); } else - write (2, &my_bell, 1); + ringbell(); break; } if (done) break; @@ -1243,7 +1247,7 @@ int colon (char *filename, int cmd, int nlines) return (0); case 'p': if (no_intty) { - write (2, &my_bell, 1); + ringbell(); return (-1); } putchar ('\r'); @@ -1259,7 +1263,7 @@ int colon (char *filename, int cmd, int nlines) case 'Q': end_it (); default: - write (2, &my_bell, 1); + ringbell(); return (-1); } } @@ -1276,7 +1280,7 @@ int number(char *cmd) i = 0; ch = otty.c_cc[VKILL]; for (;;) { ch = readch (); - if (ch >= '0' && ch <= '9') + if (isdigit(ch)) i = i*10 + ch - '0'; else if (ch == otty.c_cc[VKILL]) i = 0; @@ -1306,7 +1310,7 @@ void do_shell (char *filename) } } fflush (stdout); - write (2, "\n", 1); + errwrite("\n"); promptlen = 0; shellp = 1; execute (filename, shell, shell, "-c", shell_line, 0); @@ -1337,7 +1341,7 @@ void search(char buf[], FILE *file, register int n) line1 = Ftell (file); rdline (file); lncount++; - if ((rv = re_exec (Line)) == 1) + if ((rv = re_exec (Line)) == 1) { if (--n == 0) { if (lncount > 3 || (lncount > 1 && no_intty)) { @@ -1349,29 +1353,31 @@ void search(char buf[], FILE *file, register int n) if (!no_intty) { Currline -= (lncount >= 3 ? 3 : lncount); Fseek (file, line3); - if (noscroll) + if (noscroll) { if (clreol) { home (); cleareol (); } else doclear (); + } } else { kill_line (); - if (noscroll) + if (noscroll) { if (clreol) { home (); cleareol (); } else doclear (); + } pr (Line); putchar ('\n'); } break; } - else if (rv == -1) + } else if (rv == -1) error ("Regular expression botch"); } if (feof (file)) { @@ -1435,7 +1441,7 @@ void execute (char * filename, char * cmd, ...) va_end(argp); /* balance {}'s for some UNIX's */ execv (cmd, args); - write (2, "exec failed\n", 12); + errwrite("exec failed\n"); exit (1); } if (id > 0) { @@ -1449,7 +1455,7 @@ void execute (char * filename, char * cmd, ...) if (catch_susp) signal(SIGTSTP, onsusp); } else - write(2, "can't fork\n", 11); + errwrite("can't fork\n"); set_tty (); pr ("------------------------\n"); prompt (filename); @@ -1460,7 +1466,7 @@ void execute (char * filename, char * cmd, ...) void skiplns (register int n, register FILE *f) { - register char c; + register int c; while (n > 0) { while ((c = Getc (f)) != '\n') @@ -1636,22 +1642,23 @@ int readch () extern int errno; errno = 0; - if (read (2, &ch, 1) <= 0) + if (read (2, &ch, 1) <= 0) { if (errno != EINTR) end_it(); else ch = otty.c_cc[VKILL]; + } return (ch); } -static char BS = '\b'; +static char *BS = "\b"; static char *BSB = "\b \b"; -static char CARAT = '^'; +static char *CARAT = "^"; #define ERASEONECHAR \ if (docrterase) \ - write (2, BSB, sizeof(BSB)); \ + errwrite(BSB); \ else \ - write (2, &BS, sizeof(BS)); + errwrite(BS); void ttyin (char buf[], register int nmax, char pchar) { @@ -1698,7 +1705,7 @@ void ttyin (char buf[], register int nmax, char pchar) erasep (1); else if (docrtkill) while (promptlen-- > 1) - write (2, BSB, sizeof(BSB)); + errwrite(BSB); promptlen = 1; } sptr = buf; @@ -1714,12 +1721,12 @@ void ttyin (char buf[], register int nmax, char pchar) *sptr++ = ch; if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { ch += ch == RUBOUT ? -0100 : 0100; - write (2, &CARAT, 1); + errwrite(CARAT); promptlen++; } cbuf = ch; if (ch != '\n' && ch != ESC) { - write (2, &cbuf, 1); + errwrite1(&cbuf); promptlen++; } else @@ -1778,14 +1785,24 @@ void show (register char ch) if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { ch += ch == RUBOUT ? -0100 : 0100; - write (2, &CARAT, 1); + errwrite(CARAT); promptlen++; } cbuf = ch; - write (2, &cbuf, 1); + errwrite1(&cbuf); promptlen++; } +void errwrite (char *txt) +{ + write (fileno(stderr), txt, strlen(txt)); +} + +void errwrite1 (char *sym) +{ + write (fileno(stderr), sym, 1); +} + void error (char *mess) { if (clreol) @@ -1809,6 +1826,8 @@ void error (char *mess) void set_tty () { otty.c_lflag &= ~(ICANON|ECHO); + otty.c_cc[VMIN] = 1; /* read at least 1 char */ + otty.c_cc[VTIME] = 0; /* no timeout */ stty(fileno(stderr), &otty); } @@ -1826,12 +1845,14 @@ void reset_tty () pstate = 0; } otty.c_lflag |= ICANON|ECHO; + otty.c_cc[VMIN] = savetty0.c_cc[VMIN]; + otty.c_cc[VTIME] = savetty0.c_cc[VTIME]; stty(fileno(stderr), &savetty0); } void rdline (register FILE *f) { - register char c; + register int c; register char *p; p = Line; diff --git a/text-utils/strings.1 b/text-utils/strings.1 deleted file mode 100644 index 08dda5bfd..000000000 --- a/text-utils/strings.1 +++ /dev/null @@ -1,96 +0,0 @@ -.\" Copyright (c) 1980, 1990 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. -.\" 4. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" @(#)strings.1 6.11 (Berkeley) 5/9/91 -.\" -.Dd May 9, 1991 -.Dt STRINGS 1 -.Os BSD 3 -.Sh NAME -.Nm strings -.Nd find printable strings in a file -.Sh SYNOPSIS -.Nm strings -.Op Fl afo -.Op Fl n Ar number -.Op Ar file ... -.Sh DESCRIPTION -.Nm Strings -displays the sequences of printable characters in each of the specified -files, or in the standard input, by default. -By default, a sequence must be at least four characters in length -before being displayed. -.Pp -The options are as follows: -.Bl -tag -width Ds -.It Fl a -By default, -.Nm strings -only searches the text and data segments of object files. -The -.Fl a -option causes -.Nm strings -to search the entire object file. -.It Fl f -Each string is preceded by the name of the file -in which it was found. -.It Fl n -Specifies the minimum number of characters in a sequence to be -.Ar number , -instead of four. -.It Fl o -Each string is preceded by its decimal offset in the -file. -.El -.Pp -.Nm Strings -is useful for identifying random binaries, among other things. -.Sh SEE ALSO -.Xr hexdump 1 -.Sh BUGS -The algorithm for identifying strings is extremely primitive. -In particular, machine code instructions on certain architectures -can resemble sequences of ASCII bytes, which -will fool the algorithm. -.Sh COMPATIBILITY -Historic implementations of -.Nm -only search the initialized data portion of the object file. -This was reasonable as strings were normally stored there. -Given new compiler technology which installs strings in the -text portion of the object file, the default behavior was -changed. -.Sh HISTORY -The -.Nm -command appeared in -.Bx 3.0 . diff --git a/text-utils/strings.c b/text-utils/strings.c deleted file mode 100644 index 6c201a744..000000000 --- a/text-utils/strings.c +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Copyright (c) 1980, 1987 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * Wed Jun 22 22:22:37 1994, faith@cs.unc.edu: - * Added internationalization patches from Vitor Duarte - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define DEF_LEN 4 /* default minimum string length */ -#if 0 -#define ISSTR(ch) (isascii(ch) && (isprint(ch) || ch == '\t')) -#else -#define ISSTR(ch) (isprint(ch) || ch == '\t') -#endif - -typedef struct exec EXEC; /* struct exec cast */ - -static long foff; /* offset in the file */ -static int hcnt, /* head count */ - head_len, /* length of header */ - read_len; /* length to read */ -static u_char hbfr[sizeof(EXEC)]; /* buffer for struct exec */ - -static void usage(); - -main(argc, argv) - int argc; - char **argv; -{ - extern char *optarg; - extern int optind; - register int ch, cnt; - register u_char *C; - EXEC *head; - int exitcode, minlen; - short asdata, oflg, fflg; - u_char *bfr; - char *file, *p; - - setlocale(LC_CTYPE, ""); - - - /* - * for backward compatibility, allow '-' to specify 'a' flag; no - * longer documented in the man page or usage string. - */ - asdata = exitcode = fflg = oflg = 0; - minlen = -1; - while ((ch = getopt(argc, argv, "-0123456789an:of")) != EOF) - switch((char)ch) { - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': - /* - * kludge: strings was originally designed to take - * a number after a dash. - */ - if (minlen == -1) { - p = argv[optind - 1]; - if (p[0] == '-' && p[1] == ch && !p[2]) - minlen = atoi(++p); - else - minlen = atoi(argv[optind] + 1); - } - break; - case '-': - case 'a': - asdata = 1; - break; - case 'f': - fflg = 1; - break; - case 'n': - minlen = atoi(optarg); - break; - case 'o': - oflg = 1; - break; - case '?': - default: - usage(); - } - argc -= optind; - argv += optind; - - if (minlen == -1) - minlen = DEF_LEN; - - if (!(bfr = malloc((u_int)minlen))) { - (void)fprintf(stderr, "strings: %s\n", strerror(errno)); - exit(1); - } - bfr[minlen] = '\0'; - file = "stdin"; - do { - if (*argv) { - file = *argv++; - if (!freopen(file, "r", stdin)) { - (void)fprintf(stderr, - "strings; %s: %s\n", file, strerror(errno)); - exitcode = 1; - goto nextfile; - } - } - foff = 0; -#define DO_EVERYTHING() {read_len = -1; head_len = 0; goto start;} - read_len = -1; - if (asdata) - DO_EVERYTHING() - else { - head = (EXEC *)hbfr; - if ((head_len = - read(fileno(stdin), head, sizeof(EXEC))) == -1) - DO_EVERYTHING() - if (head_len == sizeof(EXEC) && !N_BADMAG(*head)) { - foff = N_TXTOFF(*head); - if (fseek(stdin, foff, SEEK_SET) == -1) - DO_EVERYTHING() - read_len = head->a_text + head->a_data; - head_len = 0; - } - else - hcnt = 0; - } -start: - for (cnt = 0; (ch = getch()) != EOF;) { - if (ISSTR(ch)) { - if (!cnt) - C = bfr; - *C++ = ch; - if (++cnt < minlen) - continue; - if (fflg) - printf("%s:", file); - if (oflg) - printf("%07ld %s", - foff - minlen, (char *)bfr); - else - printf("%s", bfr); - while ((ch = getch()) != EOF && ISSTR(ch)) - putchar((char)ch); - putchar('\n'); - } - cnt = 0; - } -nextfile: ; - } while (*argv); - exit(exitcode); -} - -/* - * getch -- - * get next character from wherever - */ -getch() -{ - ++foff; - if (head_len) { - if (hcnt < head_len) - return((int)hbfr[hcnt++]); - head_len = 0; - } - if (read_len == -1 || read_len-- > 0) - return(getchar()); - return(EOF); -} - -static void -usage() -{ - (void)fprintf(stderr, - "usage: strings [-afo] [-n length] [file ... ]\n"); - exit(1); -} diff --git a/text-utils/ul.c b/text-utils/ul.c index f10d22456..8da3b3f7c 100644 --- a/text-utils/ul.c +++ b/text-utils/ul.c @@ -41,6 +41,7 @@ #include /* for bzero() */ #include /* for setupterm() */ #include /* for getenv() */ +#include /* for INT_MAX */ void filter(FILE *f); void flushln(void); @@ -52,6 +53,7 @@ void reverse(void); void initinfo(void); void outc(int c); void setmode(int newmode); +void setcol(int newcol); #define IESC '\033' #define SO '\016' @@ -59,7 +61,6 @@ void setmode(int newmode); #define HFWD '9' #define HREV '8' #define FREV '7' -#define MAXBUF 512 #define NORMAL 000 #define ALTSET 001 /* Reverse */ @@ -67,6 +68,7 @@ void setmode(int newmode); #define SUBSC 004 /* Dim | Ul */ #define UNDERL 010 /* Ul */ #define BOLD 020 /* Bold */ +#define INITBUF 512 int must_use_uc, must_overstrike; char *CURS_UP, *CURS_RIGHT, *CURS_LEFT, @@ -78,7 +80,8 @@ struct CHAR { char c_char; } ; -struct CHAR obuf[MAXBUF]; +struct CHAR *obuf; +int obuflen; /* Tracks number of elements in obuf. */ int col, maxcol; int mode; int halfpos; @@ -150,23 +153,20 @@ int main(int argc, char **argv) void filter(FILE *f) { - register c; + int c; while ((c = getc(f)) != EOF) switch(c) { case '\b': - if (col > 0) - col--; + setcol(col - 1); continue; case '\t': - col = (col+8) & ~07; - if (col > maxcol) - maxcol = col; + setcol((col+8) & ~07); continue; case '\r': - col = 0; + setcol(0); continue; case SO: @@ -224,9 +224,7 @@ void filter(FILE *f) else obuf[col].c_char = '_'; case ' ': - col++; - if (col > maxcol) - maxcol = col; + setcol(col + 1); continue; case '\n': @@ -251,9 +249,7 @@ void filter(FILE *f) obuf[col].c_mode |= BOLD|mode; else obuf[col].c_mode = mode; - col++; - if (col > maxcol) - maxcol = col; + setcol(col + 1); continue; } if (maxcol) @@ -262,8 +258,8 @@ void filter(FILE *f) void flushln() { - register lastmode; - register i; + int lastmode; + int i; int hadmodes = 0; lastmode = NORMAL; @@ -274,9 +270,9 @@ void flushln() lastmode = obuf[i].c_mode; } if (obuf[i].c_char == '\0') { - if (upln) + if (upln) { PRINT(CURS_RIGHT); - else + } else outc(' '); } else outc(obuf[i].c_char); @@ -361,21 +357,30 @@ void iattr() void initbuf() { + if (obuf == NULL) { /* First time. */ + obuflen = INITBUF; + obuf = malloc(sizeof(struct CHAR) * obuflen); + if (obuf == NULL) { + fprintf(stderr, "Unable to allocate buffer.\n"); + exit(1); + } + } - bzero((char *)obuf, sizeof (obuf)); /* depends on NORMAL == 0 */ - col = 0; + /* assumes NORMAL == 0 */ + bzero((char *)obuf, sizeof(struct CHAR) * obuflen); + setcol(0); maxcol = 0; mode &= ALTSET; } void fwd() { - register oldcol, oldmax; + int oldcol, oldmax; oldcol = col; oldmax = maxcol; flushln(); - col = oldcol; + setcol(oldcol); maxcol = oldmax; } @@ -390,7 +395,7 @@ void reverse() void initinfo() { - char *getenv(), *tigetstr(); + char *tigetstr(); CURS_UP = tigetstr("cuu1"); CURS_RIGHT = tigetstr("cuf1"); @@ -495,3 +500,40 @@ void setmode(int newmode) } curmode = newmode; } + + + + +void setcol(int newcol) +{ + col = newcol; + + if (col < 0) + col = 0; + else if (col > maxcol) { + maxcol = col; + + /* If col >= obuflen, expand obuf until obuflen > col. */ + while (col >= obuflen) { + /* Paranoid check for obuflen == INT_MAX. */ + if (obuflen == INT_MAX) { + fprintf(stderr, + "Input line too long.\n"); + exit(1); + } + + /* Similar paranoia: double only up to INT_MAX. */ + obuflen = ((INT_MAX / 2) < obuflen) + ? INT_MAX + : obuflen * 2; + + /* Now we can try to expand obuf. */ + obuf = realloc(obuf, sizeof(struct CHAR) * obuflen); + if (obuf == NULL) { + fprintf(stderr, + "Out of memory when growing buffer.\n"); + exit(1); + } + } + } +} -- cgit v1.2.3-55-g7522