version 1.14, 2015/02/06 08:53:01 |
version 1.15, 2015/02/06 09:10:55 |
|
|
|
|
#define DEFLINEWIDTH 80 |
#define DEFLINEWIDTH 80 |
|
|
static void fold(int); |
static void fold(unsigned int); |
static int new_column_position(int, int); |
static unsigned int new_column_position(unsigned int, int); |
static __dead void usage(void); |
static __dead void usage(void); |
int count_bytes = 0; |
int count_bytes = 0; |
int split_words = 0; |
int split_words = 0; |
|
|
int |
int |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
{ |
{ |
int ch, lastch, newarg, prevoptind, width; |
int ch, lastch, newarg, prevoptind; |
|
unsigned int width; |
const char *errstr; |
const char *errstr; |
|
|
width = 0; |
width = 0; |
|
|
split_words = 1; |
split_words = 1; |
break; |
break; |
case 'w': |
case 'w': |
width = strtonum(optarg, 1, INT_MAX, &errstr); |
width = strtonum(optarg, 1, UINT_MAX, &errstr); |
if (errstr != NULL) |
if (errstr != NULL) |
errx(1, "illegal width value, %s: %s", errstr, |
errx(1, "illegal width value, %s: %s", errstr, |
optarg); |
optarg); |
|
|
width = 0; |
width = 0; |
else if (!isdigit(lastch)) |
else if (!isdigit(lastch)) |
usage(); |
usage(); |
if (width > INT_MAX / 10 - 1) |
if (width > UINT_MAX / 10 - 1) |
errx(1, "illegal width value, too large"); |
errx(1, "illegal width value, too large"); |
width = (width * 10) + (ch - '0'); |
width = (width * 10) + (ch - '0'); |
if (width < 1) |
if (width < 1) |
|
|
* returns embedded in the input stream. |
* returns embedded in the input stream. |
*/ |
*/ |
static void |
static void |
fold(int width) |
fold(unsigned int width) |
{ |
{ |
static char *buf = NULL; |
static char *buf = NULL; |
static int buf_max = 0; |
static int buf_max = 0; |
int ch, col; |
int ch; |
int indx; |
unsigned int col, indx; |
|
|
col = indx = 0; |
col = indx = 0; |
while ((ch = getchar()) != EOF) { |
while ((ch = getchar()) != EOF) { |
|
|
|
|
col = new_column_position(col, ch); |
col = new_column_position(col, ch); |
if (col > width) { |
if (col > width) { |
int i, last_space; |
unsigned int i, last_space; |
|
|
if (split_words) { |
if (split_words) { |
for (i = 0, last_space = -1; i < indx; i++) |
for (i = 0, last_space = -1; i < indx; i++) |
|
|
/* |
/* |
* calculate the column position |
* calculate the column position |
*/ |
*/ |
static int |
static unsigned int |
new_column_position(int col, int ch) |
new_column_position(unsigned int col, int ch) |
{ |
{ |
if (!count_bytes) { |
if (!count_bytes) { |
switch (ch) { |
switch (ch) { |