⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postreverse.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
    if ( temp_file != NULL )	unlink(temp_file);}   /* End of done *//*****************************************************************************/char *copystdin(){    int		fd_out;			/* for the temporary file */    int		fd_in;			/* for stdin */    int		count;			/* number of bytes put in buf[] *//* * * Copies stdin to a temporary file and returns the pathname of that file to the * caller. It's an expensive way of doing things, because it means we end up * reading the input file three times - rather than just twice. Could probably be * fixed by creating the temporary file on the fly as we read the file the first * time. * */    if ( (temp_file = tempnam(temp_dir, "post")) == NULL )	error(FATAL, "can't generate temp file name");    if ( (fd_out = creat(temp_file, 0660)) == -1 )	error(FATAL, "can't open %s", temp_file);    fd_in = fileno(stdin);    while ( (count = read(fd_in, buf, sizeof(buf))) > 0 )	if ( write(fd_out, buf, count) != count )	    error(FATAL, "error writing to %s", temp_file);    close(fd_out);    return(temp_file);}   /* End of copystdin *//*****************************************************************************/reverse(){/* * * Begins by looking for the ENDPROLOG comment in the input file. Everything up to * that comment is copied to the output file. If the comment isn't found the entire * input file is copied and moreprolog() returns FALSE. Otherwise readpages() reads * the rest of the input file and remembers (in pages[]) where each page starts and * ends. In addition everything bracketed by %%BeginGlobal and %%EndGlobal comments * is immediately added to the new prologue (or setup section) and ends up being * removed from the individual pages. When readpages() finds the TRAILER comment * or gets to the end of the input file we go back to the pages[] array and use * the saved offsets to write the pages out in reverse order. Finally everything * from the TRAILER comment to the end of the input file is copied to the output * file. * */    if ( moreprolog(ENDPROLOG) == TRUE )  {	readpages();	writepages();	trailer();    }	/* End if */}   /* End of reverse *//*****************************************************************************/moreprolog(str)    char	*str;			/* copy everything up to this string */{    int		len;			/* length of FORMSPERPAGE string */    int		vlen;			/* length of VERSION string *//* * * Looks for string *str at the start of a line and copies everything up to that * string to the output file. If *str isn't found the entire input file will end * up being copied to the output file and FALSE will be returned to the caller. * The first call (made from reverse()) looks for ENDPROLOG. Any other call comes * from readpages() and will be looking for the ENDSETUP comment. *  */    len = strlen(FORMSPERPAGE);    vlen = strlen(VERSION);    while ( fgets(buf, sizeof(buf), fp_in) != NULL )  {	if ( strcmp(buf, str) == 0 )	    return(TRUE);	else if ( strncmp(buf, FORMSPERPAGE, len) == 0 )	    forms = atoi(&buf[len+1]);	else if ( strncmp(buf, VERSION, vlen) == 0 )	    version = atof(&buf[vlen+1]);	fprintf(fp_out, "%s", buf);    }	/* End while */    return(FALSE);}   /* End of moreprolog *//*****************************************************************************/readpages(){    int		endpagelen;		/* length of ENDPAGE */    int		pagelen;		/* and PAGE strings */    int		sawendpage = TRUE;	/* ENDPAGE equivalent marked last page */    int		gotpage = FALSE;	/* TRUE disables BEGINSETUP stuff *//* * * Records starting and ending positions of the requested pages (usually all of * them), puts global definitions in the prologue, and remembers where the TRAILER * was found. * * Page boundaries are marked by the strings PAGE, ENDPAGE, or perhaps both. * Application programs will normally find one or the other more convenient, so * in most cases only one kind of page delimiter will be found in a particular * document. * */    pages[0].start = ftell(fp_in);	/* first page starts after ENDPROLOG */    endprolog = ENDPROLOG;    endpagelen = strlen(ENDPAGE);    pagelen = strlen(PAGE);    while ( fgets(buf, sizeof(buf), fp_in) != NULL )	if ( buf[0] != '%' )	    continue;	else if ( strncmp(buf, ENDPAGE, endpagelen) == 0 )  {	    if ( in_olist(page++) == ON )  {		pages[next_page].empty = FALSE;		pages[next_page++].stop = ftell(fp_in);	    }	/* End if */	    pages[next_page].start = ftell(fp_in);	    sawendpage = TRUE;	    gotpage = TRUE;	} else if ( strncmp(buf, PAGE, pagelen) == 0 )  {	    if ( sawendpage == FALSE && in_olist(page++) == ON )  {		pages[next_page].empty = FALSE;		pages[next_page++].stop = ftell(fp_in) - strlen(buf);	    }	/* End if */	    pages[next_page].start = ftell(fp_in) - strlen(buf);	    sawendpage = FALSE;	    gotpage = TRUE;	} else if ( gotpage == FALSE && strcmp(buf, BEGINSETUP) == 0 )  {	    fprintf(fp_out, "%s", endprolog);	    fprintf(fp_out, "%s", BEGINSETUP);	    moreprolog(ENDSETUP);	    endprolog = ENDSETUP;	} else if ( strcmp(buf, BEGINGLOBAL) == 0 )  {	    moreprolog(ENDGLOBAL);	} else if ( strcmp(buf, TRAILER) == 0 )  {	    if ( sawendpage == FALSE )		pages[next_page++].stop = ftell(fp_in) - strlen(buf);	    endoff = ftell(fp_in);	    break;	}   /* End if */}   /* End of readpages *//*****************************************************************************/writepages(){    int		i, j, k;		/* loop indices *//* * * Goes through the pages[] array, usually from the bottom up, and writes out all * the pages. Documents that print more than one form per page cause things to get * a little more complicated. Each physical page has to have its subpages printed * in the correct order, and we have to build a few dummy subpages for the last * (and now first) sheet of paper, otherwise things will only occasionally work. * */    fprintf(fp_out, "%s", endprolog);    if ( noreverse == FALSE )		/* fill out the first page */	for ( i = (forms - next_page % forms) % forms; i > 0; i--, next_page++ )	    pages[next_page].empty = TRUE;    else forms = next_page;		/* turns reversal off in next loop */    for ( i = next_page - forms; i >= 0; i -= forms )	for ( j = i, k = 0; k < forms; j++, k++ )	    if ( pages[j].empty == TRUE ) {		if ( ignoreversion == TRUE || version > 3.1 ) {		    fprintf(fp_out, "%s 0 0\n", PAGE);		    fprintf(fp_out, "/saveobj save def\n");		    fprintf(fp_out, "showpage\n");		    fprintf(fp_out, "saveobj restore\n");		    fprintf(fp_out, "%s 0 0\n", ENDPAGE);		} else {		    fprintf(fp_out, "%s 0 0\n", PAGE);		    fprintf(fp_out, "save showpage restore\n");		    fprintf(fp_out, "%s 0 0\n", ENDPAGE);		}   /* End else */	    } else copypage(pages[j].start, pages[j].stop);}   /* End of writepages *//*****************************************************************************/copypage(start, stop)    long	start;			/* starting from this offset */    long	stop;			/* and ending here */{/* * * Copies the page beginning at offset start and ending at stop to the output * file. Global definitions are skipped since they've already been added to the * prologue. * */    fseek(fp_in, start, 0);    while ( ftell(fp_in) < stop && fgets(buf, sizeof(buf), fp_in) != NULL )	if ( buf[0] == '%' && strcmp(buf, BEGINGLOBAL) == 0 )	    while ( fgets(buf, sizeof(buf), fp_in) != NULL && strcmp(buf, ENDGLOBAL) != 0 ) ;	else fprintf(fp_out, "%s", buf);}   /* End of copypage *//*****************************************************************************/trailer(){/* * * Makes sure everything from the TRAILER string to EOF is copied to the output * file. * */    if ( endoff > 0 )  {	fprintf(fp_out, "%s", TRAILER);	fseek(fp_in, endoff, 0);	while ( fgets(buf, sizeof(buf), fp_in) != NULL )	    fprintf(fp_out, "%s", buf);    }	/* End if */}   /* End of trailer *//*****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -