19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1989, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 69b50d902SRodney W. Grimes * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue. 79b50d902SRodney W. Grimes * 89b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 99b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 109b50d902SRodney W. Grimes * are met: 119b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 139b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 159b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 16*fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 179b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 189b50d902SRodney W. Grimes * without specific prior written permission. 199b50d902SRodney W. Grimes * 209b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 219b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 229b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 239b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 249b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 259b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 269b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 279b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 289b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 299b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 309b50d902SRodney W. Grimes * SUCH DAMAGE. 319b50d902SRodney W. Grimes */ 329b50d902SRodney W. Grimes 339b50d902SRodney W. Grimes #ifndef lint 34fa146c53SArchie Cobbs static const char copyright[] = 359b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 369b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 37fa146c53SArchie Cobbs static const char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95"; 389b50d902SRodney W. Grimes #endif /* not lint */ 3977c8bf7cSDavid E. O'Brien #include <sys/cdefs.h> 4077c8bf7cSDavid E. O'Brien __FBSDID("$FreeBSD$"); 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #include <ctype.h> 43812bff99SPhilippe Charnier #include <err.h> 44a5c4bafcSTim J. Robbins #include <errno.h> 459b50d902SRodney W. Grimes #include <limits.h> 46d51c6625SEivind Eklund #include <locale.h> 479b50d902SRodney W. Grimes #include <stdio.h> 489b50d902SRodney W. Grimes #include <stdlib.h> 499b50d902SRodney W. Grimes #include <string.h> 50df3f5d9dSPeter Wemm #include <unistd.h> 51d900c384STim J. Robbins #include <wchar.h> 529b50d902SRodney W. Grimes 5341b662c5SEd Schouten static int bflag; 5441b662c5SEd Schouten static int cflag; 5541b662c5SEd Schouten static wchar_t dchar; 5641b662c5SEd Schouten static char dcharmb[MB_LEN_MAX + 1]; 5741b662c5SEd Schouten static int dflag; 5841b662c5SEd Schouten static int fflag; 5941b662c5SEd Schouten static int nflag; 6041b662c5SEd Schouten static int sflag; 6118ba28c8SEitan Adler static int wflag; 629b50d902SRodney W. Grimes 6341b662c5SEd Schouten static size_t autostart, autostop, maxval; 6441b662c5SEd Schouten static char * positions; 653b90bf79STim J. Robbins 6641b662c5SEd Schouten static int b_cut(FILE *, const char *); 6741b662c5SEd Schouten static int b_n_cut(FILE *, const char *); 6841b662c5SEd Schouten static int c_cut(FILE *, const char *); 6941b662c5SEd Schouten static int f_cut(FILE *, const char *); 7041b662c5SEd Schouten static void get_list(char *); 71bd9dd0c6SAndrew Turner static int is_delim(wchar_t); 7241b662c5SEd Schouten static void needpos(size_t); 7399557a79SWill Andrews static void usage(void); 749b50d902SRodney W. Grimes 759b50d902SRodney W. Grimes int 76f4ac32deSDavid Malone main(int argc, char *argv[]) 779b50d902SRodney W. Grimes { 789b50d902SRodney W. Grimes FILE *fp; 79364d0a91STim J. Robbins int (*fcn)(FILE *, const char *); 80a6ea32c3STim J. Robbins int ch, rval; 81a5c4bafcSTim J. Robbins size_t n; 829b50d902SRodney W. Grimes 83d51c6625SEivind Eklund setlocale(LC_ALL, ""); 84d51c6625SEivind Eklund 8593738f50STim J. Robbins fcn = NULL; 869b50d902SRodney W. Grimes dchar = '\t'; /* default delimiter is \t */ 87a5c4bafcSTim J. Robbins strcpy(dcharmb, "\t"); 889b50d902SRodney W. Grimes 8918ba28c8SEitan Adler while ((ch = getopt(argc, argv, "b:c:d:f:snw")) != -1) 909b50d902SRodney W. Grimes switch(ch) { 91d51c6625SEivind Eklund case 'b': 92393cf508STim J. Robbins get_list(optarg); 93393cf508STim J. Robbins bflag = 1; 94393cf508STim J. Robbins break; 959b50d902SRodney W. Grimes case 'c': 969b50d902SRodney W. Grimes get_list(optarg); 979b50d902SRodney W. Grimes cflag = 1; 989b50d902SRodney W. Grimes break; 999b50d902SRodney W. Grimes case 'd': 100a5c4bafcSTim J. Robbins n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL); 101a5c4bafcSTim J. Robbins if (dchar == '\0' || n != strlen(optarg)) 102a5c4bafcSTim J. Robbins errx(1, "bad delimiter"); 103a5c4bafcSTim J. Robbins strcpy(dcharmb, optarg); 1049b50d902SRodney W. Grimes dflag = 1; 1059b50d902SRodney W. Grimes break; 1069b50d902SRodney W. Grimes case 'f': 1079b50d902SRodney W. Grimes get_list(optarg); 1089b50d902SRodney W. Grimes fflag = 1; 1099b50d902SRodney W. Grimes break; 1109b50d902SRodney W. Grimes case 's': 1119b50d902SRodney W. Grimes sflag = 1; 1129b50d902SRodney W. Grimes break; 113d51c6625SEivind Eklund case 'n': 114393cf508STim J. Robbins nflag = 1; 115d51c6625SEivind Eklund break; 11618ba28c8SEitan Adler case 'w': 11718ba28c8SEitan Adler wflag = 1; 11818ba28c8SEitan Adler break; 1199b50d902SRodney W. Grimes case '?': 1209b50d902SRodney W. Grimes default: 1219b50d902SRodney W. Grimes usage(); 1229b50d902SRodney W. Grimes } 1239b50d902SRodney W. Grimes argc -= optind; 1249b50d902SRodney W. Grimes argv += optind; 1259b50d902SRodney W. Grimes 1269b50d902SRodney W. Grimes if (fflag) { 12718ba28c8SEitan Adler if (bflag || cflag || nflag || (wflag && dflag)) 1289b50d902SRodney W. Grimes usage(); 12918ba28c8SEitan Adler } else if (!(bflag || cflag) || dflag || sflag || wflag) 1309b50d902SRodney W. Grimes usage(); 131393cf508STim J. Robbins else if (!bflag && nflag) 132393cf508STim J. Robbins usage(); 133393cf508STim J. Robbins 134364d0a91STim J. Robbins if (fflag) 135364d0a91STim J. Robbins fcn = f_cut; 136364d0a91STim J. Robbins else if (cflag) 137364d0a91STim J. Robbins fcn = MB_CUR_MAX > 1 ? c_cut : b_cut; 138364d0a91STim J. Robbins else if (bflag) 139364d0a91STim J. Robbins fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut; 1409b50d902SRodney W. Grimes 141a6ea32c3STim J. Robbins rval = 0; 1429b50d902SRodney W. Grimes if (*argv) 1439b50d902SRodney W. Grimes for (; *argv; ++argv) { 144204c78a1STim J. Robbins if (strcmp(*argv, "-") == 0) 145364d0a91STim J. Robbins rval |= fcn(stdin, "stdin"); 146204c78a1STim J. Robbins else { 147a6ea32c3STim J. Robbins if (!(fp = fopen(*argv, "r"))) { 148a6ea32c3STim J. Robbins warn("%s", *argv); 149a6ea32c3STim J. Robbins rval = 1; 150a6ea32c3STim J. Robbins continue; 151a6ea32c3STim J. Robbins } 1529b50d902SRodney W. Grimes fcn(fp, *argv); 1539b50d902SRodney W. Grimes (void)fclose(fp); 1549b50d902SRodney W. Grimes } 155204c78a1STim J. Robbins } 1569b50d902SRodney W. Grimes else 157364d0a91STim J. Robbins rval = fcn(stdin, "stdin"); 158a6ea32c3STim J. Robbins exit(rval); 1599b50d902SRodney W. Grimes } 1609b50d902SRodney W. Grimes 16141b662c5SEd Schouten static void 162f4ac32deSDavid Malone get_list(char *list) 1639b50d902SRodney W. Grimes { 164d8a3fbd5SWill Andrews size_t setautostart, start, stop; 1652c39ae65SEivind Eklund char *pos; 1669b50d902SRodney W. Grimes char *p; 1679b50d902SRodney W. Grimes 1689b50d902SRodney W. Grimes /* 1699b50d902SRodney W. Grimes * set a byte in the positions array to indicate if a field or 1709b50d902SRodney W. Grimes * column is to be selected; use +1, it's 1-based, not 0-based. 1710dcb7b75STim J. Robbins * Numbers and number ranges may be overlapping, repeated, and in 172d334b286SKevin Lo * any order. We handle "-3-5" although there's no real reason to. 1739b50d902SRodney W. Grimes */ 1742c39ae65SEivind Eklund for (; (p = strsep(&list, ", \t")) != NULL;) { 1759b50d902SRodney W. Grimes setautostart = start = stop = 0; 1769b50d902SRodney W. Grimes if (*p == '-') { 1779b50d902SRodney W. Grimes ++p; 1789b50d902SRodney W. Grimes setautostart = 1; 1799b50d902SRodney W. Grimes } 1802c39ae65SEivind Eklund if (isdigit((unsigned char)*p)) { 1819b50d902SRodney W. Grimes start = stop = strtol(p, &p, 10); 1829b50d902SRodney W. Grimes if (setautostart && start > autostart) 1839b50d902SRodney W. Grimes autostart = start; 1849b50d902SRodney W. Grimes } 1859b50d902SRodney W. Grimes if (*p == '-') { 1862c39ae65SEivind Eklund if (isdigit((unsigned char)p[1])) 1879b50d902SRodney W. Grimes stop = strtol(p + 1, &p, 10); 1889b50d902SRodney W. Grimes if (*p == '-') { 1899b50d902SRodney W. Grimes ++p; 1909b50d902SRodney W. Grimes if (!autostop || autostop > stop) 1919b50d902SRodney W. Grimes autostop = stop; 1929b50d902SRodney W. Grimes } 1939b50d902SRodney W. Grimes } 1949b50d902SRodney W. Grimes if (*p) 195d334b286SKevin Lo errx(1, "[-bcf] list: illegal list value"); 1969b50d902SRodney W. Grimes if (!stop || !start) 197d334b286SKevin Lo errx(1, "[-bcf] list: values may not include zero"); 198a8522a9bSTim J. Robbins if (maxval < stop) { 1999b50d902SRodney W. Grimes maxval = stop; 200a8522a9bSTim J. Robbins needpos(maxval + 1); 201a8522a9bSTim J. Robbins } 2029b50d902SRodney W. Grimes for (pos = positions + start; start++ <= stop; *pos++ = 1); 2039b50d902SRodney W. Grimes } 2049b50d902SRodney W. Grimes 2059b50d902SRodney W. Grimes /* overlapping ranges */ 206a8522a9bSTim J. Robbins if (autostop && maxval > autostop) { 2079b50d902SRodney W. Grimes maxval = autostop; 208a8522a9bSTim J. Robbins needpos(maxval + 1); 209a8522a9bSTim J. Robbins } 2109b50d902SRodney W. Grimes 2119b50d902SRodney W. Grimes /* set autostart */ 2129b50d902SRodney W. Grimes if (autostart) 2139b50d902SRodney W. Grimes memset(positions + 1, '1', autostart); 2149b50d902SRodney W. Grimes } 2159b50d902SRodney W. Grimes 21641b662c5SEd Schouten static void 217a8522a9bSTim J. Robbins needpos(size_t n) 218a8522a9bSTim J. Robbins { 219a8522a9bSTim J. Robbins static size_t npos; 220f457179aSTim J. Robbins size_t oldnpos; 221a8522a9bSTim J. Robbins 222a8522a9bSTim J. Robbins /* Grow the positions array to at least the specified size. */ 223a8522a9bSTim J. Robbins if (n > npos) { 224f457179aSTim J. Robbins oldnpos = npos; 225a8522a9bSTim J. Robbins if (npos == 0) 226a8522a9bSTim J. Robbins npos = n; 227a8522a9bSTim J. Robbins while (n > npos) 228a8522a9bSTim J. Robbins npos *= 2; 229a8522a9bSTim J. Robbins if ((positions = realloc(positions, npos)) == NULL) 230a8522a9bSTim J. Robbins err(1, "realloc"); 231f457179aSTim J. Robbins memset((char *)positions + oldnpos, 0, npos - oldnpos); 232a8522a9bSTim J. Robbins } 233a8522a9bSTim J. Robbins } 234a8522a9bSTim J. Robbins 23541b662c5SEd Schouten static int 236f0b4606fSTim J. Robbins b_cut(FILE *fp, const char *fname __unused) 237364d0a91STim J. Robbins { 238364d0a91STim J. Robbins int ch, col; 239364d0a91STim J. Robbins char *pos; 240364d0a91STim J. Robbins 241364d0a91STim J. Robbins ch = 0; 242364d0a91STim J. Robbins for (;;) { 243364d0a91STim J. Robbins pos = positions + 1; 244364d0a91STim J. Robbins for (col = maxval; col; --col) { 245364d0a91STim J. Robbins if ((ch = getc(fp)) == EOF) 246364d0a91STim J. Robbins return (0); 247364d0a91STim J. Robbins if (ch == '\n') 248364d0a91STim J. Robbins break; 249364d0a91STim J. Robbins if (*pos++) 250364d0a91STim J. Robbins (void)putchar(ch); 251364d0a91STim J. Robbins } 252364d0a91STim J. Robbins if (ch != '\n') { 253364d0a91STim J. Robbins if (autostop) 254364d0a91STim J. Robbins while ((ch = getc(fp)) != EOF && ch != '\n') 255364d0a91STim J. Robbins (void)putchar(ch); 256364d0a91STim J. Robbins else 257364d0a91STim J. Robbins while ((ch = getc(fp)) != EOF && ch != '\n'); 258364d0a91STim J. Robbins } 259364d0a91STim J. Robbins (void)putchar('\n'); 260364d0a91STim J. Robbins } 261364d0a91STim J. Robbins return (0); 262364d0a91STim J. Robbins } 263364d0a91STim J. Robbins 264393cf508STim J. Robbins /* 265393cf508STim J. Robbins * Cut based on byte positions, taking care not to split multibyte characters. 266393cf508STim J. Robbins * Although this function also handles the case where -n is not specified, 267364d0a91STim J. Robbins * b_cut() ought to be much faster. 268393cf508STim J. Robbins */ 26941b662c5SEd Schouten static int 270f4ac32deSDavid Malone b_n_cut(FILE *fp, const char *fname) 271393cf508STim J. Robbins { 272393cf508STim J. Robbins size_t col, i, lbuflen; 273393cf508STim J. Robbins char *lbuf; 274393cf508STim J. Robbins int canwrite, clen, warned; 275d900c384STim J. Robbins mbstate_t mbs; 276393cf508STim J. Robbins 277d900c384STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 278393cf508STim J. Robbins warned = 0; 279393cf508STim J. Robbins while ((lbuf = fgetln(fp, &lbuflen)) != NULL) { 280393cf508STim J. Robbins for (col = 0; lbuflen > 0; col += clen) { 281d900c384STim J. Robbins if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) { 282393cf508STim J. Robbins if (!warned) { 283393cf508STim J. Robbins warn("%s", fname); 284393cf508STim J. Robbins warned = 1; 285393cf508STim J. Robbins } 286d900c384STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 287393cf508STim J. Robbins clen = 1; 288393cf508STim J. Robbins } 289393cf508STim J. Robbins if (clen == 0 || *lbuf == '\n') 290393cf508STim J. Robbins break; 291393cf508STim J. Robbins if (col < maxval && !positions[1 + col]) { 292393cf508STim J. Robbins /* 293393cf508STim J. Robbins * Print the character if (1) after an initial 294393cf508STim J. Robbins * segment of un-selected bytes, the rest of 295393cf508STim J. Robbins * it is selected, and (2) the last byte is 296393cf508STim J. Robbins * selected. 297393cf508STim J. Robbins */ 298393cf508STim J. Robbins i = col; 299393cf508STim J. Robbins while (i < col + clen && i < maxval && 300393cf508STim J. Robbins !positions[1 + i]) 301393cf508STim J. Robbins i++; 302393cf508STim J. Robbins canwrite = i < col + clen; 303393cf508STim J. Robbins for (; i < col + clen && i < maxval; i++) 304393cf508STim J. Robbins canwrite &= positions[1 + i]; 305393cf508STim J. Robbins if (canwrite) 306393cf508STim J. Robbins fwrite(lbuf, 1, clen, stdout); 307393cf508STim J. Robbins } else { 308393cf508STim J. Robbins /* 309393cf508STim J. Robbins * Print the character if all of it has 310393cf508STim J. Robbins * been selected. 311393cf508STim J. Robbins */ 312393cf508STim J. Robbins canwrite = 1; 313393cf508STim J. Robbins for (i = col; i < col + clen; i++) 314393cf508STim J. Robbins if ((i >= maxval && !autostop) || 315393cf508STim J. Robbins (i < maxval && !positions[1 + i])) { 316393cf508STim J. Robbins canwrite = 0; 317393cf508STim J. Robbins break; 318393cf508STim J. Robbins } 319393cf508STim J. Robbins if (canwrite) 320393cf508STim J. Robbins fwrite(lbuf, 1, clen, stdout); 321393cf508STim J. Robbins } 322393cf508STim J. Robbins lbuf += clen; 323393cf508STim J. Robbins lbuflen -= clen; 324393cf508STim J. Robbins } 325393cf508STim J. Robbins if (lbuflen > 0) 326393cf508STim J. Robbins putchar('\n'); 327393cf508STim J. Robbins } 328364d0a91STim J. Robbins return (warned); 329393cf508STim J. Robbins } 330393cf508STim J. Robbins 33141b662c5SEd Schouten static int 332364d0a91STim J. Robbins c_cut(FILE *fp, const char *fname) 3339b50d902SRodney W. Grimes { 334364d0a91STim J. Robbins wint_t ch; 335364d0a91STim J. Robbins int col; 3362c39ae65SEivind Eklund char *pos; 3379b50d902SRodney W. Grimes 3382c39ae65SEivind Eklund ch = 0; 3399b50d902SRodney W. Grimes for (;;) { 3409b50d902SRodney W. Grimes pos = positions + 1; 3419b50d902SRodney W. Grimes for (col = maxval; col; --col) { 342364d0a91STim J. Robbins if ((ch = getwc(fp)) == WEOF) 343364d0a91STim J. Robbins goto out; 3449b50d902SRodney W. Grimes if (ch == '\n') 3459b50d902SRodney W. Grimes break; 3469b50d902SRodney W. Grimes if (*pos++) 347364d0a91STim J. Robbins (void)putwchar(ch); 3489b50d902SRodney W. Grimes } 3492c39ae65SEivind Eklund if (ch != '\n') { 3509b50d902SRodney W. Grimes if (autostop) 351364d0a91STim J. Robbins while ((ch = getwc(fp)) != WEOF && ch != '\n') 352364d0a91STim J. Robbins (void)putwchar(ch); 3539b50d902SRodney W. Grimes else 354364d0a91STim J. Robbins while ((ch = getwc(fp)) != WEOF && ch != '\n'); 3552c39ae65SEivind Eklund } 356364d0a91STim J. Robbins (void)putwchar('\n'); 3579b50d902SRodney W. Grimes } 358364d0a91STim J. Robbins out: 359364d0a91STim J. Robbins if (ferror(fp)) { 360364d0a91STim J. Robbins warn("%s", fname); 361364d0a91STim J. Robbins return (1); 362364d0a91STim J. Robbins } 363364d0a91STim J. Robbins return (0); 3649b50d902SRodney W. Grimes } 3659b50d902SRodney W. Grimes 36641b662c5SEd Schouten static int 367bd9dd0c6SAndrew Turner is_delim(wchar_t ch) 36818ba28c8SEitan Adler { 36918ba28c8SEitan Adler if (wflag) { 37018ba28c8SEitan Adler if (ch == ' ' || ch == '\t') 37118ba28c8SEitan Adler return 1; 37218ba28c8SEitan Adler } else { 37318ba28c8SEitan Adler if (ch == dchar) 37418ba28c8SEitan Adler return 1; 37518ba28c8SEitan Adler } 37618ba28c8SEitan Adler return 0; 37718ba28c8SEitan Adler } 37818ba28c8SEitan Adler 37918ba28c8SEitan Adler static int 380a5c4bafcSTim J. Robbins f_cut(FILE *fp, const char *fname) 3819b50d902SRodney W. Grimes { 382a5c4bafcSTim J. Robbins wchar_t ch; 383a5c4bafcSTim J. Robbins int field, i, isdelim; 384a5c4bafcSTim J. Robbins char *pos, *p; 3859b50d902SRodney W. Grimes int output; 38693738f50STim J. Robbins char *lbuf, *mlbuf; 387a2222839STim J. Robbins size_t clen, lbuflen, reallen; 3889b50d902SRodney W. Grimes 38993738f50STim J. Robbins mlbuf = NULL; 39018ba28c8SEitan Adler while ((lbuf = fgetln(fp, &lbuflen)) != NULL) { 391a2222839STim J. Robbins reallen = lbuflen; 3921928e20eSDima Dorfman /* Assert EOL has a newline. */ 3931928e20eSDima Dorfman if (*(lbuf + lbuflen - 1) != '\n') { 3941928e20eSDima Dorfman /* Can't have > 1 line with no trailing newline. */ 3951928e20eSDima Dorfman mlbuf = malloc(lbuflen + 1); 3961928e20eSDima Dorfman if (mlbuf == NULL) 3971928e20eSDima Dorfman err(1, "malloc"); 3981928e20eSDima Dorfman memcpy(mlbuf, lbuf, lbuflen); 3991928e20eSDima Dorfman *(mlbuf + lbuflen) = '\n'; 4001928e20eSDima Dorfman lbuf = mlbuf; 401a2222839STim J. Robbins reallen++; 4021928e20eSDima Dorfman } 403eaf92380SAndrey A. Chernov output = 0; 404a5c4bafcSTim J. Robbins for (isdelim = 0, p = lbuf;; p += clen) { 405a2222839STim J. Robbins clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL); 406a5c4bafcSTim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2) { 407a5c4bafcSTim J. Robbins warnc(EILSEQ, "%s", fname); 408a5c4bafcSTim J. Robbins free(mlbuf); 409a5c4bafcSTim J. Robbins return (1); 410a5c4bafcSTim J. Robbins } 411a5c4bafcSTim J. Robbins if (clen == 0) 412a5c4bafcSTim J. Robbins clen = 1; 4139b50d902SRodney W. Grimes /* this should work if newline is delimiter */ 41418ba28c8SEitan Adler if (is_delim(ch)) 4159b50d902SRodney W. Grimes isdelim = 1; 4169b50d902SRodney W. Grimes if (ch == '\n') { 4179b50d902SRodney W. Grimes if (!isdelim && !sflag) 4181928e20eSDima Dorfman (void)fwrite(lbuf, lbuflen, 1, stdout); 4199b50d902SRodney W. Grimes break; 4209b50d902SRodney W. Grimes } 4219b50d902SRodney W. Grimes } 4229b50d902SRodney W. Grimes if (!isdelim) 4239b50d902SRodney W. Grimes continue; 4249b50d902SRodney W. Grimes 4259b50d902SRodney W. Grimes pos = positions + 1; 4269b50d902SRodney W. Grimes for (field = maxval, p = lbuf; field; --field, ++pos) { 427a5c4bafcSTim J. Robbins if (*pos && output++) 428a5c4bafcSTim J. Robbins for (i = 0; dcharmb[i] != '\0'; i++) 429a5c4bafcSTim J. Robbins putchar(dcharmb[i]); 430a5c4bafcSTim J. Robbins for (;;) { 431a2222839STim J. Robbins clen = mbrtowc(&ch, p, lbuf + reallen - p, 432a5c4bafcSTim J. Robbins NULL); 433a5c4bafcSTim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2) { 434a5c4bafcSTim J. Robbins warnc(EILSEQ, "%s", fname); 435a5c4bafcSTim J. Robbins free(mlbuf); 436a5c4bafcSTim J. Robbins return (1); 437a5c4bafcSTim J. Robbins } 438a5c4bafcSTim J. Robbins if (clen == 0) 439a5c4bafcSTim J. Robbins clen = 1; 440a5c4bafcSTim J. Robbins p += clen; 44118ba28c8SEitan Adler if (ch == '\n' || is_delim(ch)) { 44218ba28c8SEitan Adler /* compress whitespace */ 44318ba28c8SEitan Adler if (wflag && ch != '\n') 44418ba28c8SEitan Adler while (is_delim(*p)) 44518ba28c8SEitan Adler p++; 446a5c4bafcSTim J. Robbins break; 44718ba28c8SEitan Adler } 448a5c4bafcSTim J. Robbins if (*pos) 449a5c4bafcSTim J. Robbins for (i = 0; i < (int)clen; i++) 450a5c4bafcSTim J. Robbins putchar(p[i - clen]); 4512c39ae65SEivind Eklund } 4529b50d902SRodney W. Grimes if (ch == '\n') 4539b50d902SRodney W. Grimes break; 4549b50d902SRodney W. Grimes } 4552c39ae65SEivind Eklund if (ch != '\n') { 4569b50d902SRodney W. Grimes if (autostop) { 4579b50d902SRodney W. Grimes if (output) 458a5c4bafcSTim J. Robbins for (i = 0; dcharmb[i] != '\0'; i++) 459a5c4bafcSTim J. Robbins putchar(dcharmb[i]); 4609b50d902SRodney W. Grimes for (; (ch = *p) != '\n'; ++p) 4619b50d902SRodney W. Grimes (void)putchar(ch); 4629b50d902SRodney W. Grimes } else 4639b50d902SRodney W. Grimes for (; (ch = *p) != '\n'; ++p); 4642c39ae65SEivind Eklund } 4659b50d902SRodney W. Grimes (void)putchar('\n'); 4669b50d902SRodney W. Grimes } 4671928e20eSDima Dorfman free(mlbuf); 468364d0a91STim J. Robbins return (0); 4699b50d902SRodney W. Grimes } 4709b50d902SRodney W. Grimes 471812bff99SPhilippe Charnier static void 472f4ac32deSDavid Malone usage(void) 4739b50d902SRodney W. Grimes { 474d51c6625SEivind Eklund (void)fprintf(stderr, "%s\n%s\n%s\n", 475d51c6625SEivind Eklund "usage: cut -b list [-n] [file ...]", 476d51c6625SEivind Eklund " cut -c list [file ...]", 47718ba28c8SEitan Adler " cut -f list [-s] [-w | -d delim] [file ...]"); 4789b50d902SRodney W. Grimes exit(1); 4799b50d902SRodney W. Grimes } 480