Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209205
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: C/C++

2011-03-29 08:52:56

  1. /* upe(6.4):A Screen at a Time Printer: p
  2.  * created on Mar 29, 2011
  3. */

  4. #include "stdio.h"

  5. #define PAGESIZE 22
  6. char *progname; /* program name for error message */
/* p: print input in chunks (version 1) */
  1. main(int argc, char *argv[])
  2. {
  3.     int i;
  4.     FILE *fp, *efopen();

  5.     progname = argv[0];
  6.     if (argc == 1)
  7.      printf (stdin, PAGESIZE);
  8.     else
  9.      for (i = 1; i < argc; i++) {
  10.          fp = efopen(argv[i], "r");
  11.          print(fp, PAGESIZE);
  12.          fclose(fp);
  13.      }
  14.     return 0;
  15. }

  16. /* efopen: fopen file, die if can't */

  17. FILE *efopen(file, mode)
  18. {
  19.     FILE *fp, *fopen();
  20.     extern char *progname;

  21.     if ((fp = fopen(file, mode)) != NULL)
  22.      return fp;
  23.     fprintf(stderr, "%s: can't open file %s mode %s\n",
  24.         progname, file, mode);
  25.     return 1;
  26. }

  27. /* print: print fp in pagesize chunks */
  28. print(FILE *fp, int pagesize)
  29. {
  30.     static int lines = 0 ; /* number of lines so far */
  31.     char buf[BUFSIZ];

  32.     while (fgets(buf, sizeof buf, fp) != NULL)
  33.      if (++lines < pagesize)
  34.         fputs(buf, stdout);
  35.      else {
  36.          buf[strlen(buf)-1] = '\0';
  37.          fputs(buf, stdout);
  38.          fflush(stdout);
  39.          ttyin();
  40.          lines = 0;
  41.      }
  42. }

  43. /* ttyin: process response from /dev/tty (version 1) */
  44. ttyin()
  45. {
  46.     char buf[BUFSIZ];
  47.     FILE *efopen();
  48.     static FILE *tty = NULL;

  49.     if (tty == NULL)
  50.      tty = efopen("/dev/tty", "r");
  51.     if (fgets(buf, BUFSIZ, tty) == NULL || buf[0] == 'q')
  52.      exit(0);
  53.     else /* ordinary line */
  54.      return buf[0];
  55. }
阅读(448) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~