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

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: LINUX

2011-03-28 09:58:21

  1. /* upe(6.2): Program argument: vis version 2
  2. upe(6.3): File access: vis version 3
  3.    created on Mar 28, 2011
  4.    */

  5. /* vis: make funny characters visible (version 3) */

  6. #include "stdio.h"
  7. #include "ctype.h"
  8. int strip = 0; /* 1 => discard special characters */

  9. int main(int argc, char *argv[])
  10. {
  11.     int i;
  12.     FILE *fp;

  13.     while(argc > 1 && argv[1][0] == '-') {
  14.         switch (argv[1][1]) {
  15.             case 's': /* -s: strip funny chars */
  16.                 strip = 1;
  17.                 break;
  18.             default:
  19.                 fprintf(stderr, "%s: unknown arg %s\n",
  20.                             argv[0], argv[1]);
  21.                 exit(1);
  22.         }
  23.         argc--;
  24.         argv++;
  25.     }
  26.     if (argc == 1)
  27.      vis(stdin);
  28.     else
  29.      for (i = 1; i < argc; i++)
  30.         if ((fp=fopen(argv[i], "r")) == NULL) {
  31.             fprintf(stderr, "%s: can't open %s\n",
  32.                         argv[0], argv[i]);
  33.             exit(1);
  34.         } else {
  35.             vis(fp);
  36.             fclose(fp);
  37.         }
  38.     return 0;
  39. }


  40. /* vis: make chars visible in FILE *fp */
  41. vis(FILE *fp)
  42. {
  43.     int c;

  44.     while ((c = getc(fp)) != EOF)
  45.      if (isascii(c) &&
  46.                  (isprint(c) || c=='\n' || c=='\t' || c==' '))
  47.         putchar(c); //put on terminal
  48.      else if (!strip)
  49.         printf("\\%03o", c);
  50. }
阅读(385) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~