home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libstdio / stdiock.c < prev   
C/C++ Source or Header  |  1992-11-07  |  3KB  |  144 lines

  1. /*
  2.  * If this compiles and runs successfully, your stdio is very probably
  3.  * compatible with stdio.fast.
  4.  * Buffers by default; -u stops buffering.
  5.  * Writes on stdout by default; -i reads from stdin.
  6.  */
  7. #include <stdio.h>
  8.  
  9. FILE *fp = stdout;
  10. int status = 0;
  11.  
  12. main(argc, argv)
  13. char **argv;
  14. {
  15.     int wantbuf = 1, usestdin = 0, nosetbuf = 0;
  16.     char line[1024];
  17.     static char buf[BUFSIZ];
  18.     static char buggered[] =
  19. "Your stdio appears to be buggered: fwrite of 4 bytes doesn't yield 4.\n";
  20.  
  21.     for (; argc > 1; argv++, argc--) {
  22.         if (strcmp(argv[1], "-u") == 0)
  23.             wantbuf = 0;
  24.         if (strcmp(argv[1], "-i") == 0) {
  25.             usestdin = 1;
  26.             fp = stdin;
  27.         }
  28.         if (strcmp(argv[1], "-f") == 0)
  29.             fp = fopen("/tmp/stdiock", "w");
  30.         if (strcmp(argv[1], "-n") == 0)
  31.             nosetbuf = 1;
  32.     }
  33.  
  34.     evalfile("start");
  35.  
  36.     if (nosetbuf)
  37.         ;
  38.     else if (wantbuf) {
  39.         setbuf(fp, buf);
  40.         evalfile("setbuf(buf)");
  41.     } else {
  42.         setbuf(fp, (char *)NULL);
  43.         evalfile("setbuf(0)");
  44.     }
  45.  
  46.     if (usestdin) {
  47.         (void) fgets(line, sizeof line, fp);
  48.         evalfile("fgets");
  49.     } else {
  50.         if (fwrite("your", 1, 4, fp) != 4) {
  51.             status = 1;
  52.             (void) write(1, buggered, strlen(buggered));
  53.         }
  54.         evalfile("fwrite");
  55.  
  56.         (void) fputs(" stdio seems to be ", stdout);
  57.         evalfile("fputs");
  58.     }
  59.  
  60.     if (wantbuf && fp->_ptr == NULL) {
  61.         status = 1;
  62.         (void) fputs("incompatible (_ptr) ", stdout);
  63.     } else
  64.         (void) fputs("compatible (_ptr) ", stdout);
  65.     if (fp->_cnt >= 0 && (nosetbuf || fp->_cnt <= BUFSIZ))
  66.         (void) fputs("compatible (_cnt)", stdout);
  67.     else {
  68.         status = 1;
  69.         (void) fputs("incompatible (_cnt)", stdout);
  70.     }
  71.     evalfile("test");
  72.  
  73.     (void) fputs(" with stdio.fast\n", stdout);
  74.     evalfile("fputs");
  75.  
  76.     (void) fflush(stdout);
  77.     (void) putc('\n', stdout);
  78.     evalfile("putc 1");
  79.     (void) putc(' ', stdout);
  80.     evalfile("putc 2");
  81.     (void) putc('\n', stdout);
  82.     evalfile("putc 3");
  83.  
  84.     (void) fflush(fp);
  85.     (void) fflush(stdout);
  86.     evalfile("fflush");
  87.  
  88.     exit(status);
  89.  
  90.     /* next line is not executed, but drags in the appropriate fread */
  91.     (void) fread(buf, 1, sizeof buf, stdin);
  92. }
  93.  
  94. /* write on stdout without using stdio. ugh */
  95. evalfile(s)
  96. char *s;
  97. {
  98.     static char ptr[] = "ptr ";
  99.     static char cnt[] = " cnt ";
  100.     static char *bufend = NULL;
  101.     static char buggered[] =
  102. "Your stdio appears to be buggered: _ptr+_cnt does not yield a constant.\n";
  103.     static char negcnt[] = "_cnt can go negative.\n";
  104.  
  105.     if (fp->_ptr != NULL && fp->_cnt != 0) {
  106.         if (fp->_cnt < 0) {
  107.             status = 1;
  108.             (void) write(1, negcnt, strlen(negcnt));
  109.         }
  110.         if (bufend == NULL)
  111.             bufend = (char *)fp->_ptr + fp->_cnt;
  112.         if (bufend != (char *)fp->_ptr + fp->_cnt) {
  113.             status = 1;
  114.             (void) write(1, buggered, strlen(buggered));
  115.         }
  116.     }
  117.  
  118.     (void) write(1, s, strlen(s));
  119.     (void) write(1, "\t", 1);
  120.     (void) write(1, ptr, strlen(ptr));
  121.     prn((int)fp->_ptr, 10);
  122.     (void) write(1, cnt, strlen(cnt));
  123.     prn((int)fp->_cnt, 10);
  124.     (void) write(1, "\n", 1);
  125. }
  126.  
  127. prn(i, rad)
  128. int i, rad;
  129. {
  130.     int quot, rem;
  131.     char c;
  132.  
  133.     if (i < 0) {
  134.         (void) write(1, "-", 1);
  135.         i = -i;
  136.     }
  137.     quot = i / rad;
  138.     rem = i % rad;
  139.     if (quot > 0)
  140.         prn(quot, rad);
  141.     c = rem + '0';
  142.     (void) write(1, &c, 1);
  143. }
  144.