docs/cut

annotate code/cut.c__openbsd.2008-06-27 @ 21:bac481be86d7

Umlaute konvertiert
author markus schnalke <meillo@marmaro.de>
date Thu, 28 May 2015 06:41:08 +0200
parents
children
rev   line source
meillo@14 1 /* $OpenBSD: cut.c,v 1.13 2008/06/27 08:02:13 sobrado Exp $ */
meillo@14 2 /* $NetBSD: cut.c,v 1.9 1995/09/02 05:59:23 jtc Exp $ */
meillo@14 3
meillo@14 4 /*
meillo@14 5 * Copyright (c) 1989, 1993
meillo@14 6 * The Regents of the University of California. All rights reserved.
meillo@14 7 *
meillo@14 8 * This code is derived from software contributed to Berkeley by
meillo@14 9 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
meillo@14 10 *
meillo@14 11 * Redistribution and use in source and binary forms, with or without
meillo@14 12 * modification, are permitted provided that the following conditions
meillo@14 13 * are met:
meillo@14 14 * 1. Redistributions of source code must retain the above copyright
meillo@14 15 * notice, this list of conditions and the following disclaimer.
meillo@14 16 * 2. Redistributions in binary form must reproduce the above copyright
meillo@14 17 * notice, this list of conditions and the following disclaimer in the
meillo@14 18 * documentation and/or other materials provided with the distribution.
meillo@14 19 * 3. Neither the name of the University nor the names of its contributors
meillo@14 20 * may be used to endorse or promote products derived from this software
meillo@14 21 * without specific prior written permission.
meillo@14 22 *
meillo@14 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
meillo@14 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
meillo@14 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
meillo@14 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
meillo@14 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
meillo@14 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
meillo@14 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
meillo@14 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
meillo@14 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
meillo@14 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
meillo@14 33 * SUCH DAMAGE.
meillo@14 34 */
meillo@14 35
meillo@14 36 #ifndef lint
meillo@14 37 static char copyright[] =
meillo@14 38 "@(#) Copyright (c) 1989, 1993\n\
meillo@14 39 The Regents of the University of California. All rights reserved.\n";
meillo@14 40 #endif /* not lint */
meillo@14 41
meillo@14 42 #ifndef lint
meillo@14 43 #if 0
meillo@14 44 static char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95";
meillo@14 45 #endif
meillo@14 46 static char rcsid[] = "$OpenBSD: cut.c,v 1.13 2008/06/27 08:02:13 sobrado Exp $";
meillo@14 47 #endif /* not lint */
meillo@14 48
meillo@14 49 #include <ctype.h>
meillo@14 50 #include <err.h>
meillo@14 51 #include <errno.h>
meillo@14 52 #include <limits.h>
meillo@14 53 #include <locale.h>
meillo@14 54 #include <stdio.h>
meillo@14 55 #include <stdlib.h>
meillo@14 56 #include <string.h>
meillo@14 57 #include <unistd.h>
meillo@14 58
meillo@14 59 int cflag;
meillo@14 60 char dchar;
meillo@14 61 int dflag;
meillo@14 62 int fflag;
meillo@14 63 int sflag;
meillo@14 64
meillo@14 65 void c_cut(FILE *, char *);
meillo@14 66 void f_cut(FILE *, char *);
meillo@14 67 void get_list(char *);
meillo@14 68 void usage(void);
meillo@14 69
meillo@14 70 int
meillo@14 71 main(int argc, char *argv[])
meillo@14 72 {
meillo@14 73 FILE *fp;
meillo@14 74 void (*fcn)(FILE *, char *);
meillo@14 75 int ch;
meillo@14 76
meillo@14 77 setlocale (LC_ALL, "");
meillo@14 78
meillo@14 79 dchar = '\t'; /* default delimiter is \t */
meillo@14 80
meillo@14 81 /* Since we don't support multi-byte characters, the -c and -b
meillo@14 82 options are equivalent, and the -n option is meaningless. */
meillo@14 83 while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
meillo@14 84 switch(ch) {
meillo@14 85 case 'b':
meillo@14 86 case 'c':
meillo@14 87 fcn = c_cut;
meillo@14 88 get_list(optarg);
meillo@14 89 cflag = 1;
meillo@14 90 break;
meillo@14 91 case 'd':
meillo@14 92 dchar = *optarg;
meillo@14 93 dflag = 1;
meillo@14 94 break;
meillo@14 95 case 'f':
meillo@14 96 get_list(optarg);
meillo@14 97 fcn = f_cut;
meillo@14 98 fflag = 1;
meillo@14 99 break;
meillo@14 100 case 's':
meillo@14 101 sflag = 1;
meillo@14 102 break;
meillo@14 103 case 'n':
meillo@14 104 break;
meillo@14 105 case '?':
meillo@14 106 default:
meillo@14 107 usage();
meillo@14 108 }
meillo@14 109 argc -= optind;
meillo@14 110 argv += optind;
meillo@14 111
meillo@14 112 if (fflag) {
meillo@14 113 if (cflag)
meillo@14 114 usage();
meillo@14 115 } else if (!cflag || dflag || sflag)
meillo@14 116 usage();
meillo@14 117
meillo@14 118 if (*argv)
meillo@14 119 for (; *argv; ++argv) {
meillo@14 120 if (!(fp = fopen(*argv, "r")))
meillo@14 121 err(1, "%s", *argv);
meillo@14 122 fcn(fp, *argv);
meillo@14 123 (void)fclose(fp);
meillo@14 124 }
meillo@14 125 else
meillo@14 126 fcn(stdin, "stdin");
meillo@14 127 exit(0);
meillo@14 128 }
meillo@14 129
meillo@14 130 int autostart, autostop, maxval;
meillo@14 131
meillo@14 132 char positions[_POSIX2_LINE_MAX + 1];
meillo@14 133
meillo@14 134 void
meillo@14 135 get_list(char *list)
meillo@14 136 {
meillo@14 137 int setautostart, start, stop;
meillo@14 138 char *pos;
meillo@14 139 char *p;
meillo@14 140
meillo@14 141 /*
meillo@14 142 * set a byte in the positions array to indicate if a field or
meillo@14 143 * column is to be selected; use +1, it's 1-based, not 0-based.
meillo@14 144 * This parser is less restrictive than the Draft 9 POSIX spec.
meillo@14 145 * POSIX doesn't allow lists that aren't in increasing order or
meillo@14 146 * overlapping lists. We also handle "-3-5" although there's no
meillo@14 147 * real reason too.
meillo@14 148 */
meillo@14 149 while ((p = strsep(&list, ", \t"))) {
meillo@14 150 setautostart = start = stop = 0;
meillo@14 151 if (*p == '-') {
meillo@14 152 ++p;
meillo@14 153 setautostart = 1;
meillo@14 154 }
meillo@14 155 if (isdigit(*p)) {
meillo@14 156 start = stop = strtol(p, &p, 10);
meillo@14 157 if (setautostart && start > autostart)
meillo@14 158 autostart = start;
meillo@14 159 }
meillo@14 160 if (*p == '-') {
meillo@14 161 if (isdigit(p[1]))
meillo@14 162 stop = strtol(p + 1, &p, 10);
meillo@14 163 if (*p == '-') {
meillo@14 164 ++p;
meillo@14 165 if (!autostop || autostop > stop)
meillo@14 166 autostop = stop;
meillo@14 167 }
meillo@14 168 }
meillo@14 169 if (*p)
meillo@14 170 errx(1, "[-cf] list: illegal list value");
meillo@14 171 if (!stop || !start)
meillo@14 172 errx(1, "[-cf] list: values may not include zero");
meillo@14 173 if (stop > _POSIX2_LINE_MAX)
meillo@14 174 errx(1, "[-cf] list: %d too large (max %d)",
meillo@14 175 stop, _POSIX2_LINE_MAX);
meillo@14 176 if (maxval < stop)
meillo@14 177 maxval = stop;
meillo@14 178 for (pos = positions + start; start++ <= stop; *pos++ = 1)
meillo@14 179 ;
meillo@14 180 }
meillo@14 181
meillo@14 182 /* overlapping ranges */
meillo@14 183 if (autostop && maxval > autostop)
meillo@14 184 maxval = autostop;
meillo@14 185
meillo@14 186 /* set autostart */
meillo@14 187 if (autostart)
meillo@14 188 memset(positions + 1, '1', autostart);
meillo@14 189 }
meillo@14 190
meillo@14 191 /* ARGSUSED */
meillo@14 192 void
meillo@14 193 c_cut(FILE *fp, char *fname)
meillo@14 194 {
meillo@14 195 int ch, col;
meillo@14 196 char *pos;
meillo@14 197
meillo@14 198 for (;;) {
meillo@14 199 pos = positions + 1;
meillo@14 200 for (col = maxval; col; --col) {
meillo@14 201 if ((ch = getc(fp)) == EOF)
meillo@14 202 return;
meillo@14 203 if (ch == '\n')
meillo@14 204 break;
meillo@14 205 if (*pos++)
meillo@14 206 (void)putchar(ch);
meillo@14 207 }
meillo@14 208 if (ch != '\n') {
meillo@14 209 if (autostop)
meillo@14 210 while ((ch = getc(fp)) != EOF && ch != '\n')
meillo@14 211 (void)putchar(ch);
meillo@14 212 else
meillo@14 213 while ((ch = getc(fp)) != EOF && ch != '\n')
meillo@14 214 ;
meillo@14 215 }
meillo@14 216 (void)putchar('\n');
meillo@14 217 }
meillo@14 218 }
meillo@14 219
meillo@14 220 void
meillo@14 221 f_cut(FILE *fp, char *fname)
meillo@14 222 {
meillo@14 223 int ch, field, isdelim;
meillo@14 224 char *pos, *p, sep;
meillo@14 225 int output;
meillo@14 226 size_t len;
meillo@14 227 char *lbuf, *tbuf;
meillo@14 228
meillo@14 229 for (sep = dchar, tbuf = NULL; (lbuf = fgetln(fp, &len));) {
meillo@14 230 output = 0;
meillo@14 231 if (lbuf[len - 1] != '\n') {
meillo@14 232 /* no newline at the end of the last line so add one */
meillo@14 233 if ((tbuf = (char *)malloc(len + 1)) == NULL)
meillo@14 234 err(1, NULL);
meillo@14 235 memcpy(tbuf, lbuf, len);
meillo@14 236 tbuf[len] = '\n';
meillo@14 237 lbuf = tbuf;
meillo@14 238 }
meillo@14 239 for (isdelim = 0, p = lbuf;; ++p) {
meillo@14 240 ch = *p;
meillo@14 241 /* this should work if newline is delimiter */
meillo@14 242 if (ch == sep)
meillo@14 243 isdelim = 1;
meillo@14 244 if (ch == '\n') {
meillo@14 245 if (!isdelim && !sflag)
meillo@14 246 (void)fwrite(lbuf, len, 1, stdout);
meillo@14 247 break;
meillo@14 248 }
meillo@14 249 }
meillo@14 250 if (!isdelim)
meillo@14 251 continue;
meillo@14 252
meillo@14 253 pos = positions + 1;
meillo@14 254 for (field = maxval, p = lbuf; field; --field, ++pos) {
meillo@14 255 if (*pos) {
meillo@14 256 if (output++)
meillo@14 257 (void)putchar(sep);
meillo@14 258 while ((ch = *p++) != '\n' && ch != sep)
meillo@14 259 (void)putchar(ch);
meillo@14 260 } else
meillo@14 261 while ((ch = *p++) != '\n' && ch != sep)
meillo@14 262 ;
meillo@14 263 if (ch == '\n')
meillo@14 264 break;
meillo@14 265 }
meillo@14 266 if (ch != '\n') {
meillo@14 267 if (autostop) {
meillo@14 268 if (output)
meillo@14 269 (void)putchar(sep);
meillo@14 270 for (; (ch = *p) != '\n'; ++p)
meillo@14 271 (void)putchar(ch);
meillo@14 272 } else
meillo@14 273 for (; (ch = *p) != '\n'; ++p)
meillo@14 274 ;
meillo@14 275 }
meillo@14 276 (void)putchar('\n');
meillo@14 277 }
meillo@14 278 if (tbuf)
meillo@14 279 free(tbuf);
meillo@14 280 }
meillo@14 281
meillo@14 282 void
meillo@14 283 usage(void)
meillo@14 284 {
meillo@14 285 (void)fprintf(stderr,
meillo@14 286 "usage: cut -b list [-n] [file ...]\n"
meillo@14 287 " cut -c list [file ...]\n"
meillo@14 288 " cut -f list [-s] [-d delim] [file ...]\n");
meillo@14 289 exit(1);
meillo@14 290 }