Last active
May 14, 2023 18:57
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 1987 (c) Stephen R. Bourne (from the Unix system V Environment). | |
* I have been true to the style, except for adding typedeclarations in order | |
* for it to compile without warnings on gcc (-std90?) without warnings. | |
* McUsr 14.05.2023 | |
*/ | |
#include <stdio.h> | |
#define MAXF 256 | |
#define MAXL 4096 | |
#define IFS '\t' | |
#define OFS '\t' | |
int fv[MAXF] ; /* numerical equivalent of arguments. */ | |
int nf ; /* number of columns to print */ | |
int mf; /* number of fields in current record */ | |
char *fp[MAXF]; /* pointers into 'L' at field boundaries */ | |
char L[MAXL]; /* current line buffer */ | |
void putf( int n); | |
int main(int argc, char **argv ) | |
{ | |
register char *cp; | |
register char **ap; | |
register int c; | |
int f; | |
/* read arguments into fv[...] */ | |
while (argc>1) { | |
if(sscanf(argv[1],"%d", &fv[nf++]) != 1 ){ | |
printf("usage: field [ n ] ...\n") ; | |
return(2); | |
} | |
argc--; argv++ ; | |
} | |
/* read and copy input */ | |
nf--; | |
cp = L; | |
ap = fp; | |
*ap++ = cp ; | |
while(1) { | |
c = getc(stdin) ; | |
if(c=='\n' || c==EOF ) { | |
int fc; | |
if(cp==L && c==EOF ) break; | |
*cp++ = 0; | |
mf = ap-fp; | |
/* print this line */ | |
for(fc = 0; fc <= nf; fc++) { | |
putf(fv[fc]-1); | |
if(fc != nf) putchar(OFS) ; | |
} | |
if(c == EOF) break ; | |
putchar('\n'); | |
cp = L ; | |
ap = fp ; | |
*ap++ = cp ; | |
} | |
else if(c == IFS) { | |
*cp++ = 0 ; | |
*ap++ = cp; | |
} | |
else *cp++ = c; | |
} | |
return (0); | |
} /* main */ | |
/* output field n from the current line */ | |
void putf( int n) | |
{ | |
register char *cp = fp[n] ; | |
register char c; | |
if(n<0 || n >=mf) return ; | |
while(c = *cp++) putchar(c) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment