示例#1
0
文件: cut.c 项目: Abioy/FUZIX
void get_args(void)
{
    int i = 0;
    int arg_ptr = 0;
    int flag;

    num_args = 0;
    do {
	if (num_args == MAX_ARGS)
	    cuterror(MAX_ARGS_EXCEEDED_ERROR);
	if (!isdigit(line[i]) && line[i] != '-')
	    cuterror(SYNTAX_ERROR);

	args[arg_ptr] = 1;
	args[arg_ptr + 1] = BUFSIZ;
	flag = 1;

	while (line[i] != ',' && line[i] != 0) {
	    if (isdigit(line[i])) {
		args[arg_ptr] = 0;
		while (isdigit(line[i]))
		    args[arg_ptr] = 10 * args[arg_ptr] + line[i++] - '0';
		if (!args[arg_ptr])
		    cuterror(POSITION_ERROR);
		arg_ptr++;
	    }
	    if (line[i] == '-') {
		arg_ptr |= 1;
		i++;
		flag = 0;
	    }
	}
	if (flag && arg_ptr & 1)
	    args[arg_ptr] = args[arg_ptr - 1];
	if (args[num_args * 2] > args[num_args * 2 + 1])
	    cuterror(RANGE_ERROR);
	num_args++;
	arg_ptr = num_args * 2;
    }
    while (line[i++]);
}
示例#2
0
文件: cut.c 项目: Abioy/FUZIX
int main(int argc, char *argv[])
{
    int i = 1;
    int numberFilenames = 0;
    name = argv[0];

    if (argc == 1)
	cuterror(USAGE);

    while (i < argc) {
	if (argv[i][0] == '-') {
	    switch (argv[i++][1]) {
	    case 'd':
		if (mode == OPTIONC || mode == OPTIONB)
		    warn(DELIMITER_NOT_APPLICABLE, "d");
		delim = argv[i++][0];
		break;

	    case 'f':
		sprintf(line, "%s", argv[i++]);
		if (mode == OPTIONC || mode == OPTIONB)
		    warn(OVERRIDING_PREVIOUS_MODE, "f");
		mode = OPTIONF;
		break;

	    case 'b':
		sprintf(line, "%s", argv[i++]);
		if (mode == OPTIONF || mode == OPTIONC)
		    warn(OVERRIDING_PREVIOUS_MODE, "b");
		mode = OPTIONB;
		break;

	    case 'c':
		sprintf(line, "%s", argv[i++]);
		if (mode == OPTIONF || mode == OPTIONB)
		    warn(OVERRIDING_PREVIOUS_MODE, "c");
		mode = OPTIONC;
		break;

	    case 'i':
		flag_i = SET;
		break;

	    case 's':
		flag_s = SET;
		break;

	    case '\0':		/* - means: read from stdin */
		numberFilenames++;
		break;

	    case 'n':		/* needed for Posix, but no effect here */
		if (mode != OPTIONB)
		    warn(OPTION_NOT_APPLICABLE, "n");
		break;

	    default:
		warn(UNKNOWN_OPTION, &(argv[i - 1][1]));
	    }
	} else {
	    i++;
	    numberFilenames++;
	}
    }

    /* Here follow the checks, if the selected options are reasonable. */

    if (mode == OPTIONB)	/* since in Minix char == byte */
	mode = OPTIONC;

    /* Flag -s is only allowed with -f, otherwise warn and reset flag_s */
    if (flag_s == SET && (mode == OPTIONB || mode == OPTIONC)) {
	warn(OPTION_NOT_APPLICABLE, "s");
	flag_s = NOTSET;
    }

    /* Flag -i is only allowed with -f, otherwise warn and reset flag_i */
    if (flag_i == SET && mode == OPTIONF) {
	warn(OPTION_NOT_APPLICABLE, "s");
	flag_i = NOTSET;
    }

    get_args();

    if (numberFilenames != 0) {
	i = 1;
	while (i < argc) {
	    if (argv[i][0] == '-') {
		switch (argv[i][1]) {
		case 'f':
		case 'c':
		case 'b':
		case 'd':
		    i += 2;
		    break;

		case 'n':
		case 'i':
		case 's':
		    i++;
		    break;

		case '\0':
		    fd = stdin;
		    i++;
		    cut();
		    break;

		default:
		    i++;
		}
	    } else {
		if ((fd = fopen(argv[i++], "r")) == NULL) {
		    warn(FILE_NOT_READABLE, argv[i - 1]);
		} else {
		    cut();
		    fclose(fd);
		}
	    }
	}
    } else {
	fd = stdin;
	cut();
    }

    return (exit_status);
}
示例#3
0
文件: cut.c 项目: Abioy/FUZIX
void cut(void)
{
    int i, j, length, maxcol;
    char *columns[MAX_FIELD];

    while (fgets(line, BUFSIZ, fd)) {

	length = strlen(line) - 1;
	*(line + length) = 0;

	switch (mode) {
	case DUMP_STDIN:
	    printf("%s", line);
	    break;

	case OPTIONF:
	    maxcol = 0;
	    columns[maxcol++] = line;
	    for (i = 0; i < length; i++) {
		if (*(line + i) == delim) {
		    *(line + i) = 0;
		    if (maxcol == MAX_FIELD)
			cuterror(MAX_FIELDS_EXCEEDED_ERROR);
		    columns[maxcol] = line + i + 1;
		    while (*(line + i + 1) == delim && flag_i) {
			columns[maxcol]++;
			i++;
		    }
		    maxcol++;
		}
	    }
	    if (maxcol == 1) {
		if (flag_s != SET)
		    printf("%s", line);
	    } else {
		for (i = 0; i < num_args; i++) {
		    for (j = args[i * 2]; j <= args[i * 2 + 1]; j++)
			if (j <= maxcol) {
			    printf("%s", columns[j - 1]);
			    if (i != num_args - 1 || j != args[i * 2 + 1])
				putchar(delim);
			}
		}
	    }
	    break;

	case OPTIONC:
	    for (i = 0; i < num_args; i++) {
		for (j = args[i * 2]; j <= (args[i * 2 + 1] > length ? length :
					    args[i * 2 + 1]); j++)
		    putchar(*(line + j - 1));
	    }
	}

	if (maxcol == 1 && flag_s == SET) {
	    ;
	} else {
	    putchar('\n');
	}
    }
}
示例#4
0
void fcut_fields(FILE *fp, int PIDNO){
	int i, j, k,length, maxcol=0;
	char buff[256];
	char *columns[MAX_FIELD];
	char input[NBUFSIZ];
	int lenfln;
	char *tempfiles[MAX_ARGS];
	char *tempflno[MAX_ARGS];
	/*
	char *dir[]={"/tmp/", "./"};
	int dirno=1;
	*/

	char *fileparts[]={"t.",".",".tab"};
	char *md[] ={"w","a"};
	FILE *fw;
	int counter=0;
	int mode=0;
	char pid[6];
	int jj;
	int frstobs=0;
	int lastobs=0;
	int ln =0;
	sprintf(pid, "%d",PIDNO);

	/*
	printf("PIDNO= %d\n", PIDNO);
	printf("process ID= %s\n", pid);
	printf("num_args= %d\n", num_args);
	printf("fileparts[0]= %s\n", fileparts[0]);
	dirno=0;


	*/

	/* set up temp files*/
	for (k=0; k < num_args; k++) {
		tempflno[k]=malloc(4+1);

		if (tempflno[k] == NULL) {
			printf("Error: malloc failed in file no\n");
			exit(EXIT_FAILURE);
		}
		sprintf(tempflno[k],"%d", (k+1));
		/* lenfln =strlen(dir[dirno]) +2 + strlen(pid)+1 + strlen(tempflno[k])+4 + 1; */
		/* lenfln =strlen(dir[dirno]) +2 + strlen(PIDNOS)+1 + strlen(tempflno[k])+4 + 1;*/
		lenfln = strlen(PIDNOS)+ strlen(fileparts[1]) + strlen(tempflno[k])+ strlen(fileparts[2]) + 1;
		/* printf("filename length=%d\n",lenfln);*/
		tempfiles[k]= malloc(lenfln);

		if (tempfiles[k] == NULL) {
			printf("Error: malloc failed in file name\n");
			exit(EXIT_FAILURE);
		}
		/*

		strcpy(tempfiles[k], dir[dirno]);
		strcat(tempfiles[k], fileparts[0]);

		strcat(tempfiles[k], pid);



		*/
		strcat(tempfiles[k], PIDNOS);
		strcat(tempfiles[k], fileparts[1]);
		strcat(tempfiles[k], tempflno[k]);
		strcat(tempfiles[k], fileparts[2]);
		free(tempflno[k]);
	}
	jj=0;
	while (fgets(input, NBUFSIZ, fp)) {
		jj++;
		if (DEBUG){
			printf("\n\n++++++++++++++ %d-th iteration ++++++++++++++\n\n",jj);
		}
		if ( (num_sets) && (jj == 1) ) {
			frstobs=rsse[ln];
			lastobs=rsse[ln+1];
		}
		if (num_sets){
			printf("1st=%d\tlast=%d\n", frstobs, lastobs);
			if ( (jj < frstobs) || (jj >lastobs)) {
				continue;
			}
			if (jj == lastobs){

				printf("within:jj=%d\t1st=%d\tlast=%d\n", jj, frstobs, lastobs);
				if ( ((ln+2)/2) <= num_sets){
					ln=ln+2;
					frstobs=rsse[ln];
					lastobs=rsse[ln+1];
				}
			}
		}

		maxcol=0;
		length = strlen(input) - 1;
		*(input + length) = 0;
		columns[maxcol++] = input ;
		for (i = 0; i < length; i++) {
			if (*(input + i) == DELIM) {
				*(input+ i) = 0;
				if (maxcol == MAX_FIELD)
					cuterror(MAX_FIELDS_EXEEDED_ERROR);
				columns[maxcol] = input + i + 1;
				maxcol++;
			}
		}

		if (maxcol != 1) {
			if (counter > 0) {mode = 1;}
			for (i = 0; i < num_args; i++) {
				buff[0]='\0';
				/* printf("%d-th tempfile name=%s\n",i,tempfiles[i]);*/
				/* field set boundary */
				if ( (fw = fopen(tempfiles[i], md[mode])) == NULL){
					fprintf(stderr, "Error opening file.\n");
					exit(1);
				}
				for (j = args[i * 2]; j <= args[i * 2 + 1]; j++){
					if (j <= maxcol) {
						fprintf(fw,"%s", columns[j-1]);
						/* fprintf(fw, "%s", columns[j - 1]);*/
						if (i != num_args - 1 || j != args[i * 2 + 1]) {fputc(ODELIM, fw);}
					}
				}
					/* print newline character */

					fputc('\n', fw);
					fclose(fw);
				/* field set boundary */
			}
			counter++;

		} else if (OPTS==1) {
			if ( (fw = fopen("temp.1554.z.tab", "w")) == NULL){
				fprintf(stderr, "Error opening file.\n");
				exit(1);
			}
			fprintf(fw, "%s",input);
			fclose(fw);
		}
		columns[1]='\0';/* */
	}

	for (k=0;k<num_args;k++){
		free(tempfiles[k]);
	}
}
示例#5
0
void cut_fields(FILE *fd)
{
	int i, j, length, maxcol=0;
	char *columns[MAX_FIELD];
	char input[NBUFSIZ];
	char buff[256];
	int frstobs=0;
	int lastobs=0;
	int ln =0;
	int k,kk,jj;

	if(DEBUG){printf("/////////////// cut_fields ///////////////\n");}
	jj=0;
	while (fgets(input, NBUFSIZ, fd)) {
		jj++;
		if (DEBUG){
			printf("\n++++++++++++++ %d-th iteration ++++++++++++++\n",jj);
		}
	if ( (num_sets) && (jj == 1) ) {
		frstobs=rsse[ln];
		lastobs=rsse[ln+1];
	}
	if (num_sets){
		if (DEBUG){
			printf("1st=%d\tlast=%d\n", frstobs, lastobs);
		}
		if ( (jj < frstobs) || (jj >lastobs)) {
			continue;
		}
		if (jj == lastobs){
			if (DEBUG){
				printf("within:jj=%d\t1st=%d\tlast=%d\n", jj, frstobs, lastobs);
			}
			if ( ((ln+2)/2) <= num_sets){
				ln=ln+2;
				frstobs=rsse[ln];
				lastobs=rsse[ln+1];
			}
		}
	}

		k=0;
		kk=0;
		maxcol=0;
		length = strlen(input) - 1;
		/* length = a line of data without a new line character*/
		if (DEBUG){
			printf("length=%d\n",length);
		}
/*
		for(k=0;columns[k]!='\0';k++){
			printf("check k=%d\t",k);
			if (jj == 0){
				for (kk=0;columns[k][kk]!='\0';kk++){*/
			/*	printf("%d\n",strlen(columns[k]));
			}
			printf("- %d\n",kk);}
		}
		if(length >= MAX_FIELD){
			cuterror(MAX_FIELDS_EXEEDED_ERROR);
		}

*/

		*(input + length) = '\0';/*  input[length] = */
		columns[maxcol++] = input ;/*if maxcol=0, columns[0] = input then maxcol=1 */
		/*
			printf("maxcol=%d\n",maxcol);
			kk++;
			printf("kk=%d\n",kk);
		*/
/*
		if (DEBUG){
			printf("1st iteration\n");
			for(k=0;columns[k]!='\0';k++){
				printf("k=%d\t",k);
				for (kk=0;columns[k][kk]!='\0';kk++){
					printf("[%c]",columns[k][kk]);
				}
				printf("- %d\n",kk);
			}
		}
*/
		for (i = 0; i < length; i++) {
			if (*(input + i) == DELIM) {
				*(input+ i) = '\0';
				if (maxcol == MAX_FIELD){
					cuterror(MAX_FIELDS_EXEEDED_ERROR);
				}
				columns[maxcol] = input + i + 1;
				maxcol++;
			}
		}
		/* after the above block maxcol = no of vars in a line of data*/

/*
		if (DEBUG){
			printf("2nd iteration\n");
			printf("maxcol=%d\n\n",maxcol);
			for(k=0;columns[k]!='\0';k++){
				printf("k=%d\t",k);
				for (kk=0;columns[k][kk]!='\0';kk++){
					printf("[%c]",columns[k][kk]);
				}
				printf("- %d\n",kk);
			}
		}
*/
		if (maxcol != 1) {
		    for (i = 0; i < num_args; i++) {
				buff[0]='\0';

				for (j = args[i * 2]; j <= args[i * 2 + 1]; j++){
					if (j <= maxcol) {
						/* print a data point */
						printf("%s", columns[j - 1]);
						if (i != num_args - 1 || j != args[i * 2 + 1]){
							/* print a delimiter */
							putchar(ODELIM);
						}
					}
				}
			}
		} else if (OPTS==1) {
			printf("%s",input);
		}
		if (maxcol != 1){
			/* print a new line character */
			putchar('\n');
		}
/*
		if (DEBUG){
			printf("3rd iteration\n");
			for(k=0;columns[k]!='\0';k++){
				printf("k=%d\t%s\n", k, columns[k]);

					printf("k=%d\t",k);
					for (kk=0;columns[k][kk]!='\0';kk++){
						printf("[%c]",columns[k][kk]);
					}
					printf("- %d\n",kk);

			}
		}
*/
		columns[1]='\0';/* */
	}
}
示例#6
0
static void parse_list_g(char *lists){
	/*int EOL,BOL;*/
	char *ltok = NULL;
	char *ntok = NULL;
	char *junk;
	int s = 0, e = 0;
	int l;

	/* take apart the lists, one by one (they are seperated with commas */
	num_sets=0;
	#if DBG
		if(DEBUG){ printf("entering parse_list_g\n");}
	#endif

	while ((ltok = strsep(&lists, ",")) != NULL) {
		#if DBG
			if(DEBUG){printf("token split by comma(ltok)=%s\n", ltok);}
		#endif
		/* it's actually legal to pass an empty list */
		if (strlen(ltok) == 0){
			continue;
		}

		/* get the start pos */
		ntok = strsep(&ltok, "-");
		#if DBG
			if(DEBUG){printf("start_string(ntok)=%s\n", ntok);}
		#endif
		if (ntok == NULL) {
			#if DBG
				if(DEBUG){printf("\tstart_string(ntok) is NULL\n");}
   			#endif
			cuterror(SYNTAX_ERROR);
		} else {
			s = strtoul(ntok, &junk, 10);
			#if DBG
				if(DEBUG){printf("\tstart_string: after ulong =%d\n",s);}
			#endif
			if(e < 0){
				cuterror(SYNTAX_ERROR);
			} else if (*junk != '\0' ) {
				cuterror(SYNTAX_ERROR);
			}
		}

		/* get the end pos */
		ntok = strsep(&ltok, "-");
		#if DBG
			if(DEBUG){
				if (ntok != NULL){
					printf("\tend_string=%s\n", ntok);
				}
			}
		#endif
		if (ntok == NULL) {
			#if DBG
				if(DEBUG){printf("\tend_string is NULL: start_no = end_no\n");}
			#endif
			e = s;
		} else {
			e = strtoul(ntok, &junk, 10);
			#if DBG
				if (DEBUG){
					printf("\tend_string: after ulong=%d\n",e);
					printf("check the leftover string=%s\n", junk );
				}
			#endif
			if(e < 0){
				cuterror(SYNTAX_ERROR);
			} else if (*junk != '\0' ) {
				cuterror(SYNTAX_ERROR);
			}
		}
			#if DBG
				if (DEBUG){
					printf("entering the leftover processing\n");
				}
			#endif

		/* if there's something left to tokenize, the user past an invalid list */
		if (ltok){
			cuterror(SYNTAX_ERROR);
		}

		#if DBG
			if (DEBUG){
				printf("start_string=%d\tend_string=%d\n",s,e);
			}
		#endif

		/* add the new list */
		rsse[num_sets*2]=s;
		rsse[num_sets*2+1]=e;

		#if DBG
			if (DEBUG){
				printf("no of argument set(num_sets)=%d\n", num_sets);
				printf("contents of argment_set array\n");
				for (l=0;rsse[l] != 0 ;l++){
					printf("%d:%d\n",l, rsse[l]);
				}
			}
		#endif

		num_sets++;

		if ((num_sets*2+1)>= MAX_ARGS){
			cuterror(MAX_ARGS_EXEEDED_ERROR);
		}
	}

	/* make sure we got some cut positions out of all that */
	/* printf("entering last of parse_lists\n");*/
	if (num_sets <= 0){
		cuterror(SYNTAX_ERROR);
	} else {
		if(DEBUG){
			printf("no of case-subsetting num_sets=%d\n", num_sets);
			for (l=0;rsse[l] != 0 ;l++){
				printf("%d:%d\n",l, rsse[l]);
			}
			printf("\n");
		}
	}
}
示例#7
0
static void parse_lists(char *lists){
	int EOL,BOL;
	char *ltok = NULL;
	char *ntok = NULL;
	char *junk;
	int s = 0, e = 0,recid;
	int l;

	int fmt;
	/* take apart the lists, one by one (they are seperated with commas */
	num_args=0;

	#if DBG
		if(DEBUG){printf("entering parse_lists\n");}
	#endif
	while ((ltok = strsep(&lists, ",")) != NULL) {
		#if DBG
			if(DEBUG){printf("token split by comma(ltok)=%s\n", ltok);}
		#endif
		fmt =0;
		/* it's actually legal to pass an empty list */
		if (strlen(ltok) == 0){
			continue;
		}
		/* get the record pos */
		ntok = strsep(&ltok, ":");
		#if DBG
			if(DEBUG){printf("\trecord number(ntok)=%s\n", ntok);}
		#endif

		if (ltok) {
			recid=	atoi(ntok);

			#if DBG
				if(DEBUG){
					printf("\tcheck the remaining part\n");
					printf("\trecord number(ntok)=%s\n", ntok);
					printf("\trecord number after atoi=%d\n", recid);
				}
			#endif

			if ((recid>RECNUM) || (recid<1)){
				cuterror(SYNTAX_ERROR);
			}

			BOL = (recid-1)*(RECLEN+1);
			EOL = recid*(RECLEN+1)-1;

			#if DBG
				if(DEBUG){printf("BOL=%d\tEOL=%d\n",BOL,EOL);}
			#endif

		} else {
			recid=0;
			BOL = 0;
			EOL = INT_MAX;
			ltok=ntok;
		}
		/* get the start pos */
		ntok = strsep(&ltok, "-");
		if (ntok == NULL) {
			cuterror(SYNTAX_ERROR);
		} else if (strlen(ntok) == 0) {
			s = BOL;

			#if DBG
				if(DEBUG){
					printf("\tstart_string number(ntok) is empty\n");
					printf("\tstart_string number(s)=BOL(%d)\n",s);
				}
			#endif

		} else {
			s = strtoul(ntok, &junk, 10) + BOL;

			#if DBG
				if(DEBUG){
					printf("\tset start_string number(s)=%d\n",s);
				}
			#endif

			if(e < 0){
				cuterror(SYNTAX_ERROR);
			} else if (strlen(junk)) {

				if (*junk == '@'){
					junk++;
					if (*junk == 'c')  {
						fmt = -1;
					} else {
						/* */
						fmt = atoi(junk);
					}
					#if DBG
						if(DEBUG){
							printf("\thit the at mark\n");
							printf("\tset start_string number(s)=%d\n",s);
							printf("\tfrmt string=%s\n", junk);
							printf("\tformat code=%d\n", fmt);
						}
					#endif
				}
			} else if (*junk != '\0' ) {
				cuterror(SYNTAX_ERROR);
			}
		}

		/* get the end pos */
		ntok = strsep(&ltok, "-");

		if (ntok == NULL) {
			e = s;
			#if DBG
				if(DEBUG){
					printf("\tend string(e) is NULL\n");
					printf("\tset end string(e)=%d[start_string(s)]\n", e);
				}
			#endif

		} else if (strlen(ntok) == 0) {
			e = EOL;
			#if DBG
				if(DEBUG){
					printf("\tend string(e):length is 0\n");
					printf("\tset end string(e)=%d[EOL]\n", e);
				}
			#endif

		} else {
			e = strtoul(ntok, &junk, 10) + BOL;

			#if DBG
				if(DEBUG){
					printf("\tend_string after ul=%d\n",e);
					printf("\tcheck remaining string=%s\n",junk);
				}
			#endif

			if(e < 0){
				cuterror(SYNTAX_ERROR);
			} else if (e == 0) {
				/* if the user specified and end position of 0, that means "til the
				 * end of the line */
				e = EOL;

				#if DBG
					if(DEBUG){
						printf("\tend_string is 0\n");
						printf("\tset end_string(e)=%d[EOL]\n", e);
					}
				#endif

			} else if (strlen(junk)) {
				if (*junk == '@'){
					junk++;
					if (*junk == 'c')  {
						fmt = -1;
					} else {
						fmt = atoi(junk);
					}
					#if DBG
						if(DEBUG){
							printf("\thit the at mark\n");
							printf("\tset start_string number(s)=%d\n",s);
							printf("\tfrmt string=%s\n", junk);
							printf("\tformat code=%d\n", fmt);
						}
					#endif
				}

			} else if (*junk != '\0' ) {
				cuterror(SYNTAX_ERROR);
			}
		}

		/* if there's something left to tokenize, the user past an invalid list */
		if (ltok){
			cuterror(SYNTAX_ERROR);
		}
		/*printf("s=%d\te=%d\n",s,e);*/

		/* add the new list */
		args[num_args*2]=s;
		args[num_args*2+1]=e;

		argsn[num_args]=fmt;


		#if DBG
			if (DEBUG){
				printf("no of arguments set(num_args)=%d\n", num_args);
				printf("contents of args array\n");
				for (l=0;args[l] != 0 ;l++){
					printf("%d:%d\n",l, args[l]);
				}
				printf("\n");

				printf("contents of accompanying format set\n");
				for (l=0;l<=(num_args) ;l++){
					printf("%d:%d\n",l, argsn[l]);
				}
				printf("\n");
			}
		#endif
		num_args++;


		if ((num_args*2+1)>= MAX_ARGS){
			cuterror(MAX_ARGS_EXEEDED_ERROR);
		}
	}
		if(DEBUG){
			printf("no of column no pairs(num_args)=%d\n", num_args);
			printf("\ncolumn-subsetting array\n");
			for (l=0;args[l] != 0 ;l++){
				printf("%d:%d\n",l, args[l]);
			}
			printf("\ncontents of frmt array\n");
			for (l=0;l<=(num_args) ;l++){
				printf("%d:%d\n",l, argsn[l]);
			}
			printf("\n");
		}

	/* make sure we got some cut positions out of all that */
	/* printf("entering last of parse_lists\n");*/

	if (num_args == 0){
		cuterror(SYNTAX_ERROR);
	}
}