*** util-linux-2.7/text-utils/ul.c Sun Jul 6 16:13:23 1997 --- util-linux-2.7/text-utils/ul.c.s-max Fri Nov 27 14:39:30 1998 *************** *** 50,55 **** --- 50,57 ---- #include /* for getopt(), isatty() */ #include /* for bzero() */ #include /* for setupterm() */ + #include + #include /* for INT_MAX */ void filter(FILE *f); void flushln(void); *************** *** 61,66 **** --- 63,69 ---- void initinfo(void); void outc(int c); void setmode(int newmode); + void setcol(int newcol); #define IESC '\033' #define SO '\016' *************** *** 68,74 **** #define HFWD '9' #define HREV '8' #define FREV '7' - #define MAXBUF 512 #define NORMAL 000 #define ALTSET 001 /* Reverse */ --- 71,76 ---- *************** *** 76,81 **** --- 78,84 ---- #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, *************** *** 87,93 **** char c_char; } ; ! struct CHAR obuf[MAXBUF]; int col, maxcol; int mode; int halfpos; --- 90,97 ---- char c_char; } ; ! struct CHAR *obuf; ! int obuflen; /* Tracks number of elements in obuf. */ int col, maxcol; int mode; int halfpos; *************** *** 103,109 **** int c, ret; char *termtype; FILE *f; - char *getenv(), *strcpy(); termtype = getenv("TERM"); if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1))) --- 107,112 ---- *************** *** 165,182 **** while ((c = getc(f)) != EOF) switch(c) { case '\b': ! if (col > 0) ! col--; continue; case '\t': ! col = (col+8) & ~07; ! if (col > maxcol) ! maxcol = col; continue; case '\r': ! col = 0; continue; case SO: --- 168,182 ---- while ((c = getc(f)) != EOF) switch(c) { case '\b': ! setcol(col - 1); continue; case '\t': ! setcol((col+8) & ~07); continue; case '\r': ! setcol(0); continue; case SO: *************** *** 234,242 **** else obuf[col].c_char = '_'; case ' ': ! col++; ! if (col > maxcol) ! maxcol = col; continue; case '\n': --- 234,240 ---- else obuf[col].c_char = '_'; case ' ': ! setcol(col + 1); continue; case '\n': *************** *** 261,269 **** obuf[col].c_mode |= BOLD|mode; else obuf[col].c_mode = mode; ! col++; ! if (col > maxcol) ! maxcol = col; continue; } if (maxcol) --- 259,265 ---- obuf[col].c_mode |= BOLD|mode; else obuf[col].c_mode = mode; ! setcol(col + 1); continue; } if (maxcol) *************** *** 371,379 **** void initbuf() { ! bzero((char *)obuf, sizeof (obuf)); /* depends on NORMAL == 0 */ ! col = 0; maxcol = 0; mode &= ALTSET; } --- 367,384 ---- 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); + } + } ! /* assumes NORMAL == 0 */ ! bzero((char *)obuf, sizeof(struct CHAR) * obuflen); ! setcol(0); maxcol = 0; mode &= ALTSET; } *************** *** 385,391 **** oldcol = col; oldmax = maxcol; flushln(); ! col = oldcol; maxcol = oldmax; } --- 390,396 ---- oldcol = col; oldmax = maxcol; flushln(); ! setcol(oldcol); maxcol = oldmax; } *************** *** 504,507 **** --- 509,549 ---- } } 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); + } + } + } }