docs/cut

diff code/cut.c__4.3bsd-uwisc.1986-11-07 @ 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__4.3bsd-uwisc.1986-11-07	Tue May 12 06:46:59 2015 +0200
     1.3 @@ -0,0 +1,125 @@
     1.4 +static char sccsid[] = "@(#)cut.c	1.3";
     1.5 +#
     1.6 +/* cut : cut and paste columns of a table (projection of a relation) */
     1.7 +/* Release 1.5; handles single backspaces as produced by nroff    */
     1.8 +# include <stdio.h>	/* make: cc cut.c */
     1.9 +# define NFIELDS 512	/* max no of fields or resulting line length */
    1.10 +# define BACKSPACE 8
    1.11 +main(argc, argv)
    1.12 +int argc; char **argv;
    1.13 +{
    1.14 +	int del = '\t';
    1.15 +	int i, j, count, poscnt, r, s, t;
    1.16 +	int endflag, supflag, cflag, fflag, backflag, filenr;
    1.17 +	int sel[NFIELDS];
    1.18 +	register int c;
    1.19 +	register char *p1;
    1.20 +	char *p2, outbuf[NFIELDS];
    1.21 +	FILE *inptr;
    1.22 +	endflag = supflag = cflag = fflag = 0;
    1.23 + 
    1.24 + 
    1.25 +while (argc > 1 && argv[1][0] == '-'){
    1.26 +	for (i = 1; (c = argv[1][i]) != '\0'; i++) {
    1.27 +		switch(c) {
    1.28 +			case 'd' : del = argv[1][++i];
    1.29 +				if (del == '\0') diag("no delimiter\n");
    1.30 +				break;
    1.31 +			case 's': supflag++ ;
    1.32 +				break;
    1.33 +			case 'c': cflag++ ;
    1.34 +				break;
    1.35 +			case 'f': fflag++ ;
    1.36 +				break;
    1.37 +			default : diag("Usage: cut [-s] [-d<char>] {-c<list> | -f<list>} file ...\n");
    1.38 +				break;
    1.39 +		}
    1.40 +		if (!endflag && (cflag || fflag)) {
    1.41 +			endflag = 1;
    1.42 +			r = s = t = 0;
    1.43 +			do {	c = argv[1][++i];
    1.44 +				switch(c) {
    1.45 +					case '-' : if (r) diagl();
    1.46 +						r = 1;
    1.47 +						if (t == 0)  s = 1;
    1.48 +						else {s = t; t = 0;}
    1.49 +						continue;
    1.50 +					case '\0' :
    1.51 +					case ','  : if (t >= NFIELDS) diagl();
    1.52 +						if (r) { if (t == 0) t = NFIELDS - 1;
    1.53 +							if (t<s) diagl();
    1.54 +							for(j = s; j <= t; j++) sel[j] = 1;
    1.55 +							}
    1.56 +						else sel[t] = (t > 0 ? 1 : 0);
    1.57 +						r = s = t = 0;
    1.58 +						if (c == '\0') {i--; break;}
    1.59 +						continue;
    1.60 +					default :
    1.61 +						if (c< '0' || c> '9') diagl();
    1.62 +						t = 10*t + c - '0';
    1.63 +						continue;
    1.64 +				}
    1.65 +				for (j = t = 0; j < NFIELDS; j++) t += sel[j];
    1.66 +				if (t == 0) diag("no fields\n");
    1.67 +			} while (c != '\0');
    1.68 +		}
    1.69 +	}
    1.70 +	--argc;
    1.71 +	++argv;
    1.72 +} /* end options */
    1.73 +if (!(cflag || fflag)) diagl();
    1.74 +
    1.75 +--argc;
    1.76 +filenr = 1;
    1.77 +do {	/* for all input files */
    1.78 +	if (argc > 0) inptr = fopen(argv[filenr], "r");
    1.79 +	else inptr = stdin;
    1.80 +  
    1.81 +	if (inptr == NULL) {
    1.82 +		write(2,"Cannot open :",14);
    1.83 +		diag(argv[filenr]);
    1.84 +	}
    1.85 +	endflag = 0;
    1.86 +	do {	/* for all lines of a file */
    1.87 +		count = poscnt = backflag = 0;
    1.88 +		p1 = &outbuf[0] - 1 ;
    1.89 +		p2 = p1;
    1.90 +		do { 	/* for all char of the line */
    1.91 +			c = fgetc(inptr);
    1.92 +			if (c == EOF) {
    1.93 +				endflag = 1;
    1.94 +				break;
    1.95 +				}
    1.96 +			if (count == NFIELDS - 1) diag("line too long\n");
    1.97 +			if (c != '\n') *++p1 = c;
    1.98 +			if (cflag && (c == BACKSPACE)) backflag++ ; else 
    1.99 +				{ if ( !backflag ) poscnt += 1 ; else backflag-- ;}
   1.100 +			if ( backflag > 1 ) diag("cannot handle multiple adjacent backspaces\n");
   1.101 +			if ( ((c == '\n') && count > 0)  || c == del || cflag) {
   1.102 +				count += 1;
   1.103 +				if (fflag) poscnt = count  ;
   1.104 +				if (sel[poscnt]) p2 = p1; else p1 = p2;
   1.105 +			}
   1.106 +		}while (c != '\n');
   1.107 +		if ( !endflag && (count > 0 || !supflag)) {
   1.108 +			if (*p1 == del) *p1 = '\0';
   1.109 +				else *++p1 = '\0'; /*suppress trailing delimiter*/
   1.110 +			puts(outbuf);
   1.111 +			}
   1.112 +	} while (!endflag) ;
   1.113 +fclose(inptr);
   1.114 +} while(++filenr <= argc);
   1.115 +}
   1.116 +
   1.117 +diag(s)
   1.118 +char *s;
   1.119 +{
   1.120 +	write(2, "cut : ", 6);
   1.121 +	while(*s)
   1.122 +		write(2,s++,1);
   1.123 +	exit(2);
   1.124 +}
   1.125 +diagl()
   1.126 +{
   1.127 +diag("bad list for c/f option\n");
   1.128 +}