fold.c (24360) | fold.c (27270) |
---|---|
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 --- 21 unchanged lines hidden (view full) --- 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint | 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 --- 21 unchanged lines hidden (view full) --- 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint |
38static char copyright[] = | 38static const char copyright[] = |
39"@(#) Copyright (c) 1990, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint | 39"@(#) Copyright (c) 1990, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint |
44#if 0 |
|
44static char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93"; | 45static char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93"; |
46#endif 47static const char rcsid[] = 48 "$Id$"; |
|
45#endif /* not lint */ 46 | 49#endif /* not lint */ 50 |
51#include <err.h> |
|
47#include <stdio.h> | 52#include <stdio.h> |
53#include <stdlib.h> |
|
48#include <string.h> | 54#include <string.h> |
55#include <unistd.h> |
|
49 50#define DEFLINEWIDTH 80 51 | 56 57#define DEFLINEWIDTH 80 58 |
59void fold __P((int)); 60static void usage __P((void)); 61 62int |
|
52main(argc, argv) 53 int argc; 54 char **argv; 55{ | 63main(argc, argv) 64 int argc; 65 char **argv; 66{ |
56 extern int errno, optind; | 67 extern int optind; |
57 extern char *optarg; 58 register int ch; 59 int width; 60 char *p; 61 62 width = -1; 63 while ((ch = getopt(argc, argv, "0123456789w:")) != -1) 64 switch (ch) { 65 case 'w': 66 if ((width = atoi(optarg)) <= 0) { | 68 extern char *optarg; 69 register int ch; 70 int width; 71 char *p; 72 73 width = -1; 74 while ((ch = getopt(argc, argv, "0123456789w:")) != -1) 75 switch (ch) { 76 case 'w': 77 if ((width = atoi(optarg)) <= 0) { |
67 (void)fprintf(stderr, 68 "fold: illegal width value.\n"); 69 exit(1); | 78 errx(1, "illegal width value"); |
70 } 71 break; 72 case '0': case '1': case '2': case '3': case '4': 73 case '5': case '6': case '7': case '8': case '9': 74 if (width == -1) { 75 p = argv[optind - 1]; 76 if (p[0] == '-' && p[1] == ch && !p[2]) 77 width = atoi(++p); 78 else 79 width = atoi(argv[optind] + 1); 80 } 81 break; 82 default: | 79 } 80 break; 81 case '0': case '1': case '2': case '3': case '4': 82 case '5': case '6': case '7': case '8': case '9': 83 if (width == -1) { 84 p = argv[optind - 1]; 85 if (p[0] == '-' && p[1] == ch && !p[2]) 86 width = atoi(++p); 87 else 88 width = atoi(argv[optind] + 1); 89 } 90 break; 91 default: |
83 (void)fprintf(stderr, 84 "usage: fold [-w width] [file ...]\n"); 85 exit(1); | 92 usage(); |
86 } 87 argv += optind; 88 argc -= optind; 89 90 if (width == -1) 91 width = DEFLINEWIDTH; 92 if (!*argv) 93 fold(width); 94 else for (; *argv; ++argv) 95 if (!freopen(*argv, "r", stdin)) { | 93 } 94 argv += optind; 95 argc -= optind; 96 97 if (width == -1) 98 width = DEFLINEWIDTH; 99 if (!*argv) 100 fold(width); 101 else for (; *argv; ++argv) 102 if (!freopen(*argv, "r", stdin)) { |
96 (void)fprintf(stderr, 97 "fold: %s: %s\n", *argv, strerror(errno)); 98 exit(1); | 103 err(1, "%s", *argv); |
99 } else 100 fold(width); 101 exit(0); 102} 103 | 104 } else 105 fold(width); 106 exit(0); 107} 108 |
109static void 110usage() 111{ 112 (void)fprintf(stderr, "usage: fold [-w width] [file ...]\n"); 113 exit(1); 114} 115 116void |
|
104fold(width) 105 register int width; 106{ 107 register int ch, col, new; 108 109 for (col = 0;;) { 110 switch (ch = getchar()) { 111 case EOF: --- 41 unchanged lines hidden --- | 117fold(width) 118 register int width; 119{ 120 register int ch, col, new; 121 122 for (col = 0;;) { 123 switch (ch = getchar()) { 124 case EOF: --- 41 unchanged lines hidden --- |