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 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 |
71main(int argc, char **argv) |
72{ |
73 int ch; |
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 |
124usage(void) |
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 |
142fold(int width) |
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 |
196newpos(int col, int ch) |
197{ 198 199 if (bflag) 200 ++col; 201 else 202 switch (ch) { 203 case '\b': 204 if (col > 0) --- 16 unchanged lines hidden --- |