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