Пример #1
0
void editfile (list_ref list, char *filename) {
   char stdinline[1024];
   int stdincount = 0;
   for(;; ++stdincount) {
      printf ("%s: ", Exec_Name);
      char *linepos = fgets (stdinline, sizeof stdinline, stdin);
      if (linepos == NULL) break;
      if (want_echo) printf ("%s", stdinline);
      linepos = strchr (stdinline, '\n');
      if (linepos == NULL || stdinline[0] == '\0') {
         badline (stdincount, stdinline);
      }else {
         *linepos = '\0';
         switch (stdinline[0]) {
            case '$': setmove_list(list, MOVE_LAST, stdinline); break;
            case '*': print_all(list, stdinline); break;
            case '.': viewcurr_list(list, stdinline); break;
            case '0': setmove_list(list, MOVE_HEAD, stdinline); break;
            case '<': setmove_list(list, MOVE_PREV, stdinline); break;
            case '>': setmove_list(list, MOVE_NEXT, stdinline); break;
            case '@': debugdump_list (list, stdinline); break;
            case 'a': insert_line_after (list, stdinline+1); break;
            case 'd': delete_list(list, stdinline); break;
            case 'i': insert_line_before(list, stdinline+1); break;
            case 'r': insertfile(list, stdinline+1); break;
            case 'w': writefile (list, stdinline+1, filename); break;
            default : badline (stdincount, stdinline);
         }
      }
   }
   printf("%s\n", "^D");
}
Пример #2
0
Файл: moo.c Проект: 8l/FUZIX
int main(int argc, char *argv[])
{
	char moo[10];
	int nmoo;
	char line[256];
	int i, j;
	int nbull, ncow;
	union {
		time_t utime;
		int ubuf[2];
	} un;

	time(&un.utime);
	srand(un.ubuf[0] + un.ubuf[1]);
	if (argc < 2)
		nmoo = 4;
	else
		nmoo = atoi(argv[1]);
	if (nmoo < 1 || 10 < nmoo) {
		printf
		    ("Usage: moo [n]\n\twhere 1<=n<=10, n defaults to 4\n");
		exit(1);
	}
	for (;;) {
		printf("New game.\n");
		for (i = 0; i != nmoo; i++) {
		      Again:
			moo[i] = rand() / 100 % 10 + '0';
			for (j = 0; j != i; j++)
				if (moo[i] == moo[j])
					goto Again;
		}
		for (;;) {
			if (fgets(line, 255, stdin) == NULL)
				exit(0);
			if (badline(line, nmoo))
				printf(entdiff, nmoo);
			else {
				nbull = 0;
				ncow = 0;
				for (i = 0; i != nmoo; i++)
					for (j = 0; j != nmoo; j++)
						if (line[i] == moo[j])
							if (i == j)
								nbull++;
							else
								ncow++;
				if (nbull == nmoo) {
					printf("Right!\n");
					break;
				}
				printf("%d bull%c, %d cow%c\n",
				       nbull, nbull != 1 ? 's' : '\0',
				       ncow, ncow != 1 ? 's' : '\0');
			}
		}
	}
}
Пример #3
0
int
readLine(FILE *fp, char *buf, int maxSize)
{
    char *p;
    int n;

    while (1) {
        if (fgets(buf, maxSize, fp) == NULL)
            return 0;
        if (badline(buf))
            continue;
        else if ((n = handle_comment(buf)) > 0)
            return (n);
        break;
    }

    p = strchr(buf, '\n');
    *p = '\0';
    return (p - buf);
}
Пример #4
0
void editfile( struct options *options, list_ref list ){
   char stdinline[1024];
   char *operand = NULL;
   int stdincount = 0;
   int lineCount = 0;
   char *linepos = NULL;
   for(;;){
      if(!options->s_opt_silent)
		 printf("%s: ", progname); 
	  linepos = fgets( stdinline, sizeof stdinline, stdin );
      if( linepos == NULL ) break;
	  if(options->e_opt_echo && !options->s_opt_silent)
	     printf("%s",stdinline);
      linepos = strchr( stdinline, '\n' );
      if( linepos == NULL || stdinline[0] == '\0' ){
         badline( stdincount, stdinline );
      }else{
         *linepos = '\0';
		 operand=stdinline+1;
         switch( stdinline[0] ){
            case '$':
            	if(stdinline[1] != '\0') {
					badline(stdincount, stdinline);
					break;
				}
            	lastLine(list, options);
            	break;
            case '*':
            	if(stdinline[1] != '\0') {
					badline(stdincount, stdinline);
					break;
				}
            	printAll(list, options); 
            	break;
            case '.': 
            	if(stdinline[1] != '\0') {
					badline(stdincount, stdinline);
					break;
				}
            	printCurr(list, options);
            	break;
            case '0':
            	if(stdinline[1] != '\0') {
					badline(stdincount, stdinline);
					break;
				}
            	firstLine(list, options);
            	break;
            case '<': 
            	if(stdinline[1] != '\0') {
					badline(stdincount, stdinline);
					break;
				}
            	prevLine(list, options);
            	break;
            case '>': 
            	if(stdinline[1] != '\0') {
					badline(stdincount, stdinline);
					break;
				}
            	nextLine(list, options);
            	break;
            case '@': 
            	if(stdinline[1] != '\0') {
					badline(stdincount, stdinline);
					break;
				}
            	debugdump_list( list );
            	break;
            case 'a': 
            	insertAfter(list, operand, options);
            	break;
            case 'd': 
            	if(stdinline[1] != '\0') {
					badline(stdincount, stdinline);
					break;
				}
            	deleteLine(list, options);
            	break;
            case 'i': 
            	insertBefore(list, operand, options); 
            	break;
            case 'r':
				lineCount = readFile(list, operand, options); 
            	if(lineCount>=0 && !options->s_opt_silent)
					printf("%s: %d lines read.\n", progname, lineCount);
				break;
            case 'w': 
            	lineCount = writeFile(list, operand, options); 
            	if(lineCount>=0 && !options->s_opt_silent)
					printf("%s: %d lines written to %s\n", progname, lineCount, operand);
				break;
            default :
            	badline( stdincount, stdinline );
         };
      };
      ++stdincount;
   };
   if(!options->s_opt_silent)
	  printf("\n");
}
Пример #5
0
Suf_read_ASL(EdRead *R, int readall)
#endif
{
	int *d, isreal, i, k, n, nx, nx1;
	real *r, t;
	SufDesc *D;
	char *fmt;
	ASL *asl = R->asl;
	char sufname[128];

	if (xscanf(R, "%d %d %127s", &k, &n, sufname) != 3)
		badline(R);
	if (k < 0 || k > 7 || n <= 0)
		badline(R);
	isreal = k & ASL_Sufkind_real;
	k &= ASL_Sufkind_mask;
	nx = (&asl->i.n_var_)[k];
	if (k == 1)
		nx += n_lcon;
	if (n > nx)
		badline(R);
	if (readall & 1) {
 new_D:
		D = (SufDesc*)M1zapalloc(sizeof(SufDesc) + strlen(sufname) + 1);
		D->next = asl->i.suffixes[k];
		asl->i.suffixes[k] = D;
		asl->i.nsuff[k]++;
		asl->i.nsuffixes++;
		strcpy(D->sufname = (char*)(D+1), sufname);
		D->kind = k;
		if (isreal)
			D->kind |= ASL_Sufkind_real;
		}
	else for(D = asl->i.suffixes[k]; ; D = D->next) {
		if (!D) {
			if (readall)
				goto new_D;
 skip:
			/* Skip this suffix table */
			fmt = (char*)(isreal ? "%d %lf" : "%d %d");
			do if (xscanf(R,fmt,&k,&t) != 2)
					badline(R);
				while(--n);
			return;
			}
		if (k == (D->kind & ASL_Sufkind_mask)
		 && !strcmp(sufname,D->sufname))
			break;
		}
	if ((D->kind & ASL_Sufkind_outonly) == ASL_Sufkind_outonly)
		goto skip;
	nx1 = nx + D->nextra;
	if (D->kind & ASL_Sufkind_real) {
		D->u.i = 0;
		if (!(r = D->u.r))
			D->u.r = r = (real*)mem(nx1*sizeof(real));
		if (n < nx)
			memset(r,0,nx*sizeof(real));
		if (nx < nx1)
			memset(r+nx, 0, (nx1-nx)*sizeof(real));
		if (isreal)
			do  {
				if (xscanf(R,"%d %lf",&i,&t) != 2
				 || i < 0 || i >= nx)
					badline(R);
				r[i] = t;
				}
				while(--n);
		else
			do  {
				if (xscanf(R,"%d %d",&i,&k) != 2
				 || i < 0 || i >= nx)
					badline(R);
				r[i] = k;
				}
				while(--n);
		}
	else {
		D->u.r = 0;
		if (!(d = D->u.i))
			D->u.i = d = (int*)mem(nx1*sizeof(int));
		if (n < nx)
			memset(d,0,nx*sizeof(int));
		if (nx < nx1)
			memset(d+nx, 0, (nx1-nx)*sizeof(int));
		if (isreal)
			do {
				if (xscanf(R,"%d %lf",&i,&t) != 2
				 || i < 0 || i >= nx)
					badline(R);
				d[i] = (int)(t + 0.5);
				} while(--n);
		else
			do {
				if (xscanf(R,"%d %d",&i,&k) != 2
				 || i < 0 || i >= nx)
					badline(R);
				d[i] = k;
				}
				while(--n);
		}
	D->kind |= ASL_Sufkind_input;
	}
Пример #6
0
/*
 * Read mounttab line for line and return struct mtablist.
 */
int
read_mtab(void)
{
	struct mtablist **mtabpp, *mtabp;
	char *hostp, *dirp, *cp;
	char str[STRSIZ];
	char *timep, *endp;
	time_t actiontime;
	u_long ultmp;
	FILE *mtabfile;

	if ((mtabfile = fopen(PATH_MOUNTTAB, "r")) == NULL) {
		if (errno == ENOENT)
			return (0);
		else {
			syslog(LOG_ERR, "can't open %s", PATH_MOUNTTAB);
			return (0);
		}
	}
	actiontime = 0;
	mtabpp = &mtabhead;
	while (fgets(str, STRSIZ, mtabfile) != NULL) {
		cp = str;
		errno = 0;
		if (*cp == '#' || *cp == ' ' || *cp == '\n')
			continue;
		timep = strsep(&cp, " \t\n");
		if (timep == NULL || *timep == '\0') {
			badline("time", timep);
			continue;
		}
		hostp = strsep(&cp, " \t\n");
		if (hostp == NULL || *hostp == '\0') {
			badline("host", hostp);
			continue;
		}
		dirp = strsep(&cp, " \t\n");
		if (dirp == NULL || *dirp == '\0') {
			badline("dir", dirp);
			continue;
		}
		ultmp = strtoul(timep, &endp, 10);
		if (ultmp == ULONG_MAX || *endp != '\0') {
			badline("time", timep);
			continue;
		}
		actiontime = ultmp;
		if ((mtabp = malloc(sizeof (struct mtablist))) == NULL) {
			syslog(LOG_ERR, "malloc");
			fclose(mtabfile);
			return (0);
		}
		mtabp->mtab_time = actiontime;
		memmove(mtabp->mtab_host, hostp, MNTNAMLEN);
		mtabp->mtab_host[MNTNAMLEN - 1] = '\0';
		memmove(mtabp->mtab_dirp, dirp, MNTPATHLEN);
		mtabp->mtab_dirp[MNTPATHLEN - 1] = '\0';
		mtabp->mtab_next = (struct mtablist *)NULL;
		*mtabpp = mtabp;
		mtabpp = &mtabp->mtab_next;
	}
	fclose(mtabfile);
	return (1);
}
Пример #7
0
void editfile (list_ref list) {
    char stdinline[1024];
    int stdincount = 0;
    for(;; ++stdincount) {
        printf ("%s: ", Exec_Name);
        char *linepos = fgets (stdinline, sizeof stdinline, stdin);
        if(isspace(stdinline[0]) != 0) {
            continue;
        }
        if (linepos == NULL) break;
        if (want_echo) printf ("%s", stdinline);
        linepos = strchr (stdinline, '\n');
        if (linepos == NULL || stdinline[0] == '\0') {
            badline (stdincount, stdinline);
        } else {
            *linepos = '\0';
            switch (stdinline[0]) {
            case '$':
                setmove_list (list, MOVE_LAST);
                break;
            case '*':
                print_list(list);
                break;
            case '.':
                printf("%6d: %s\n",counter(list, curr),
                       viewcurr_list (list));
                break;
            case '0':
                setmove_list (list, MOVE_HEAD);
                break;
            case '<':
                setmove_list (list, MOVE_PREV);
                break;
            case '>':
                setmove_list (list, MOVE_NEXT);
                break;
            case '@':
                debugdump_list (list);
                break;
            case 'a':
                insertAfter(list, stdinline+1, curr);
                break;
            case 'd':
                delete_list (list);
                break;
            case 'i':
                insertBefore(list, stdinline+1);
                break;
            case 'r':
                putfilelist (list, stdinline+1, 0);
                break;
            case 'w':
                putfilelist (list, stdinline+1, 2);
                break;
            case '#':
                break;
            default :
                badline (stdincount, stdinline);
            }
        }
    }
    printf (" ^D\n");
}