docs/cut
diff code/cut.c__netbsd.2014-02-03 @ 14:21ad1c1548c4
Code ausgewaehlter Implementierungen eingefuegt
Das Datum entspricht dem Dateiaenderungsdatum.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Tue, 12 May 2015 06:46:59 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/code/cut.c__netbsd.2014-02-03 Tue May 12 06:46:59 2015 +0200 1.3 @@ -0,0 +1,306 @@ 1.4 +/* $NetBSD: cut.c,v 1.29 2014/02/03 20:22:19 wiz Exp $ */ 1.5 + 1.6 +/* 1.7 + * Copyright (c) 1989, 1993 1.8 + * The Regents of the University of California. All rights reserved. 1.9 + * 1.10 + * This code is derived from software contributed to Berkeley by 1.11 + * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue. 1.12 + * 1.13 + * Redistribution and use in source and binary forms, with or without 1.14 + * modification, are permitted provided that the following conditions 1.15 + * are met: 1.16 + * 1. Redistributions of source code must retain the above copyright 1.17 + * notice, this list of conditions and the following disclaimer. 1.18 + * 2. Redistributions in binary form must reproduce the above copyright 1.19 + * notice, this list of conditions and the following disclaimer in the 1.20 + * documentation and/or other materials provided with the distribution. 1.21 + * 3. Neither the name of the University nor the names of its contributors 1.22 + * may be used to endorse or promote products derived from this software 1.23 + * without specific prior written permission. 1.24 + * 1.25 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1.26 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.27 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.28 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 1.29 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1.30 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1.31 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.32 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 1.33 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 1.34 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 1.35 + * SUCH DAMAGE. 1.36 + */ 1.37 + 1.38 +#include <sys/cdefs.h> 1.39 +#ifndef lint 1.40 +__COPYRIGHT("@(#) Copyright (c) 1989, 1993\ 1.41 + The Regents of the University of California. All rights reserved."); 1.42 +#endif /* not lint */ 1.43 + 1.44 +#ifndef lint 1.45 +#if 0 1.46 +static char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95"; 1.47 +#endif 1.48 +__RCSID("$NetBSD: cut.c,v 1.29 2014/02/03 20:22:19 wiz Exp $"); 1.49 +#endif /* not lint */ 1.50 + 1.51 +#include <ctype.h> 1.52 +#include <err.h> 1.53 +#include <errno.h> 1.54 +#include <limits.h> 1.55 +#include <locale.h> 1.56 +#include <stdio.h> 1.57 +#include <stdlib.h> 1.58 +#include <string.h> 1.59 +#include <unistd.h> 1.60 +#include <util.h> 1.61 +#include <wchar.h> 1.62 +#include <sys/param.h> 1.63 + 1.64 +static int bflag; 1.65 +static int cflag; 1.66 +static char dchar; 1.67 +static int dflag; 1.68 +static int fflag; 1.69 +static int sflag; 1.70 + 1.71 +static void b_cut(FILE *, const char *); 1.72 +static void c_cut(FILE *, const char *); 1.73 +static void f_cut(FILE *, const char *); 1.74 +static void get_list(char *); 1.75 +static void usage(void) __dead; 1.76 + 1.77 +int 1.78 +main(int argc, char *argv[]) 1.79 +{ 1.80 + FILE *fp; 1.81 + void (*fcn)(FILE *, const char *); 1.82 + int ch, rval; 1.83 + 1.84 + fcn = NULL; 1.85 + (void)setlocale(LC_ALL, ""); 1.86 + 1.87 + dchar = '\t'; /* default delimiter is \t */ 1.88 + 1.89 + /* Since we don't support multi-byte characters, the -c and -b 1.90 + options are equivalent, and the -n option is meaningless. */ 1.91 + while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1) 1.92 + switch(ch) { 1.93 + case 'b': 1.94 + fcn = b_cut; 1.95 + get_list(optarg); 1.96 + bflag = 1; 1.97 + break; 1.98 + case 'c': 1.99 + fcn = c_cut; 1.100 + get_list(optarg); 1.101 + cflag = 1; 1.102 + break; 1.103 + case 'd': 1.104 + dchar = *optarg; 1.105 + dflag = 1; 1.106 + break; 1.107 + case 'f': 1.108 + get_list(optarg); 1.109 + fcn = f_cut; 1.110 + fflag = 1; 1.111 + break; 1.112 + case 's': 1.113 + sflag = 1; 1.114 + break; 1.115 + case 'n': 1.116 + break; 1.117 + case '?': 1.118 + default: 1.119 + usage(); 1.120 + } 1.121 + argc -= optind; 1.122 + argv += optind; 1.123 + 1.124 + if (fflag) { 1.125 + if (cflag || bflag) 1.126 + usage(); 1.127 + } else if ((!cflag && !bflag) || dflag || sflag) 1.128 + usage(); 1.129 + else if (bflag && cflag) 1.130 + usage(); 1.131 + 1.132 + rval = 0; 1.133 + if (*argv) 1.134 + for (; *argv; ++argv) { 1.135 + if (strcmp(*argv, "-") == 0) 1.136 + fcn(stdin, "stdin"); 1.137 + else { 1.138 + if ((fp = fopen(*argv, "r"))) { 1.139 + fcn(fp, *argv); 1.140 + (void)fclose(fp); 1.141 + } else { 1.142 + rval = 1; 1.143 + warn("%s", *argv); 1.144 + } 1.145 + } 1.146 + } 1.147 + else 1.148 + fcn(stdin, "stdin"); 1.149 + return(rval); 1.150 +} 1.151 + 1.152 +static size_t autostart, autostop, maxval; 1.153 + 1.154 +static char *positions = NULL; 1.155 +static size_t numpositions = 0; 1.156 +#define ALLOC_CHUNK _POSIX2_LINE_MAX /* malloc granularity */ 1.157 + 1.158 +static void 1.159 +get_list(char *list) 1.160 +{ 1.161 + size_t setautostart, start, stop; 1.162 + char *pos; 1.163 + char *p; 1.164 + 1.165 + if (positions == NULL) { 1.166 + numpositions = ALLOC_CHUNK; 1.167 + positions = ecalloc(numpositions, sizeof(*positions)); 1.168 + } 1.169 + 1.170 + /* 1.171 + * set a byte in the positions array to indicate if a field or 1.172 + * column is to be selected; use +1, it's 1-based, not 0-based. 1.173 + * This parser is less restrictive than the Draft 9 POSIX spec. 1.174 + * POSIX doesn't allow lists that aren't in increasing order or 1.175 + * overlapping lists. We also handle "-3-5" although there's no 1.176 + * real reason to. 1.177 + */ 1.178 + for (; (p = strtok(list, ", \t")) != NULL; list = NULL) { 1.179 + setautostart = start = stop = 0; 1.180 + if (*p == '-') { 1.181 + ++p; 1.182 + setautostart = 1; 1.183 + } 1.184 + if (isdigit((unsigned char)*p)) { 1.185 + start = stop = strtol(p, &p, 10); 1.186 + if (setautostart && start > autostart) 1.187 + autostart = start; 1.188 + } 1.189 + if (*p == '-') { 1.190 + if (isdigit((unsigned char)p[1])) 1.191 + stop = strtol(p + 1, &p, 10); 1.192 + if (*p == '-') { 1.193 + ++p; 1.194 + if (!autostop || autostop > stop) 1.195 + autostop = stop; 1.196 + } 1.197 + } 1.198 + if (*p) 1.199 + errx(1, "[-bcf] list: illegal list value"); 1.200 + if (!stop || !start) 1.201 + errx(1, "[-bcf] list: values may not include zero"); 1.202 + if (stop + 1 > numpositions) { 1.203 + size_t newsize; 1.204 + newsize = roundup(stop + 1, ALLOC_CHUNK); 1.205 + positions = erealloc(positions, newsize); 1.206 + (void)memset(positions + numpositions, 0, 1.207 + newsize - numpositions); 1.208 + numpositions = newsize; 1.209 + } 1.210 + if (maxval < stop) 1.211 + maxval = stop; 1.212 + for (pos = positions + start; start++ <= stop; pos++) 1.213 + *pos = 1; 1.214 + } 1.215 + 1.216 + /* overlapping ranges */ 1.217 + if (autostop && maxval > autostop) 1.218 + maxval = autostop; 1.219 + 1.220 + /* set autostart */ 1.221 + if (autostart) 1.222 + (void)memset(positions + 1, '1', autostart); 1.223 +} 1.224 + 1.225 +static void 1.226 +/*ARGSUSED*/ 1.227 +f_cut(FILE *fp, const char *fname __unused) 1.228 +{ 1.229 + int ch, field, isdelim; 1.230 + char *pos, *p, sep; 1.231 + int output; 1.232 + size_t len; 1.233 + char *lbuf, *tbuf; 1.234 + 1.235 + for (sep = dchar, tbuf = NULL; (lbuf = fgetln(fp, &len)) != NULL;) { 1.236 + output = 0; 1.237 + if (lbuf[len - 1] != '\n') { 1.238 + /* no newline at the end of the last line so add one */ 1.239 + if ((tbuf = (char *)malloc(len + 1)) == NULL) 1.240 + err(1, NULL); 1.241 + (void)memcpy(tbuf, lbuf, len); 1.242 + tbuf[len++] = '\n'; 1.243 + lbuf = tbuf; 1.244 + } 1.245 + for (isdelim = 0, p = lbuf;; ++p) { 1.246 + ch = *p; 1.247 + /* this should work if newline is delimiter */ 1.248 + if (ch == sep) 1.249 + isdelim = 1; 1.250 + if (ch == '\n') { 1.251 + if (!isdelim && !sflag) 1.252 + (void)fwrite(lbuf, len, 1, stdout); 1.253 + break; 1.254 + } 1.255 + } 1.256 + if (!isdelim) 1.257 + continue; 1.258 + 1.259 + pos = positions + 1; 1.260 + for (field = maxval, p = lbuf; field; --field, ++pos) { 1.261 + if (*pos) { 1.262 + if (output++) 1.263 + (void)putchar(sep); 1.264 + while ((ch = *p++) != '\n' && ch != sep) 1.265 + (void)putchar(ch); 1.266 + } else { 1.267 + while ((ch = *p++) != '\n' && ch != sep) 1.268 + continue; 1.269 + } 1.270 + if (ch == '\n') 1.271 + break; 1.272 + } 1.273 + if (ch != '\n') { 1.274 + if (autostop) { 1.275 + if (output) 1.276 + (void)putchar(sep); 1.277 + for (; (ch = *p) != '\n'; ++p) 1.278 + (void)putchar(ch); 1.279 + } else 1.280 + for (; (ch = *p) != '\n'; ++p); 1.281 + } 1.282 + (void)putchar('\n'); 1.283 + if (tbuf) { 1.284 + free(tbuf); 1.285 + tbuf = NULL; 1.286 + } 1.287 + } 1.288 + if (tbuf) 1.289 + free(tbuf); 1.290 +} 1.291 + 1.292 +static void 1.293 +usage(void) 1.294 +{ 1.295 + (void)fprintf(stderr, "usage:\tcut -b list [-n] [file ...]\n" 1.296 + "\tcut -c list [file ...]\n" 1.297 + "\tcut -f list [-d string] [-s] [file ...]\n"); 1.298 + exit(1); 1.299 +} 1.300 + 1.301 +/* make b_put(): */ 1.302 +#define CUT_BYTE 1 1.303 +#include "x_cut.c" 1.304 +#undef CUT_BYTE 1.305 + 1.306 +/* make c_put(): */ 1.307 +#define CUT_BYTE 0 1.308 +#include "x_cut.c" 1.309 +#undef CUT_BYTE