Deleted Added
full compact
fold.c (98342) fold.c (102944)
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kevin Ruddy.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 33 unchanged lines hidden (view full) ---

42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93";
46#endif
47#endif /* not lint */
48
49#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kevin Ruddy.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 33 unchanged lines hidden (view full) ---

42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93";
46#endif
47#endif /* not lint */
48
49#include <sys/cdefs.h>
50__FBSDID("$FreeBSD: head/usr.bin/fold/fold.c 98342 2002-06-17 12:11:05Z tjr $");
50__FBSDID("$FreeBSD: head/usr.bin/fold/fold.c 102944 2002-09-04 23:29:10Z dwmalone $");
51
52#include <ctype.h>
53#include <err.h>
54#include <limits.h>
55#include <locale.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>

--- 4 unchanged lines hidden (view full) ---

63void fold(int);
64static int newpos(int, int);
65static void usage(void);
66
67int bflag; /* Count bytes, not columns */
68int sflag; /* Split on word boundaries */
69
70int
51
52#include <ctype.h>
53#include <err.h>
54#include <limits.h>
55#include <locale.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>

--- 4 unchanged lines hidden (view full) ---

63void fold(int);
64static int newpos(int, int);
65static void usage(void);
66
67int bflag; /* Count bytes, not columns */
68int sflag; /* Split on word boundaries */
69
70int
71main(argc, argv)
72 int argc;
73 char **argv;
71main(int argc, char **argv)
74{
72{
75 register int ch;
73 int ch;
76 int rval, width;
77 char *p;
78
79 (void) setlocale(LC_CTYPE, "");
80
81 width = -1;
82 while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1)
83 switch (ch) {

--- 34 unchanged lines hidden (view full) ---

118 warn("%s", *argv);
119 rval = 1;
120 } else
121 fold(width);
122 exit(rval);
123}
124
125static void
74 int rval, width;
75 char *p;
76
77 (void) setlocale(LC_CTYPE, "");
78
79 width = -1;
80 while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1)
81 switch (ch) {

--- 34 unchanged lines hidden (view full) ---

116 warn("%s", *argv);
117 rval = 1;
118 } else
119 fold(width);
120 exit(rval);
121}
122
123static void
126usage()
124usage(void)
127{
128 (void)fprintf(stderr, "usage: fold [-bs] [-w width] [file ...]\n");
129 exit(1);
130}
131
132/*
133 * Fold the contents of standard input to fit within WIDTH columns (or bytes)
134 * and write to standard output.
135 *
136 * If sflag is set, split the line at the last space character on the line.
137 * This flag necessitates storing the line in a buffer until the current
138 * column > width, or a newline or EOF is read.
139 *
140 * The buffer can grow larger than WIDTH due to backspaces and carriage
141 * returns embedded in the input stream.
142 */
143void
125{
126 (void)fprintf(stderr, "usage: fold [-bs] [-w width] [file ...]\n");
127 exit(1);
128}
129
130/*
131 * Fold the contents of standard input to fit within WIDTH columns (or bytes)
132 * and write to standard output.
133 *
134 * If sflag is set, split the line at the last space character on the line.
135 * This flag necessitates storing the line in a buffer until the current
136 * column > width, or a newline or EOF is read.
137 *
138 * The buffer can grow larger than WIDTH due to backspaces and carriage
139 * returns embedded in the input stream.
140 */
141void
144fold(width)
145 register int width;
142fold(int width)
146{
147 static char *buf;
148 static int buf_max;
149 int ch, col, i, indx, space;
150
151 col = indx = 0;
152 while ((ch = getchar()) != EOF) {
153 if (ch == '\n') {

--- 37 unchanged lines hidden (view full) ---

191 if (indx != 0)
192 fwrite(buf, 1, indx, stdout);
193}
194
195/*
196 * Update the current column position for a character.
197 */
198static int
143{
144 static char *buf;
145 static int buf_max;
146 int ch, col, i, indx, space;
147
148 col = indx = 0;
149 while ((ch = getchar()) != EOF) {
150 if (ch == '\n') {

--- 37 unchanged lines hidden (view full) ---

188 if (indx != 0)
189 fwrite(buf, 1, indx, stdout);
190}
191
192/*
193 * Update the current column position for a character.
194 */
195static int
199newpos(col, ch)
200 int col, ch;
196newpos(int col, int ch)
201{
202
203 if (bflag)
204 ++col;
205 else
206 switch (ch) {
207 case '\b':
208 if (col > 0)

--- 16 unchanged lines hidden ---
197{
198
199 if (bflag)
200 ++col;
201 else
202 switch (ch) {
203 case '\b':
204 if (col > 0)

--- 16 unchanged lines hidden ---