home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / stdio / test-popen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-09  |  1.1 KB  |  67 lines

  1. #include <ansidecl.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void
  6. DEFUN(write_data, (stream), FILE *stream)
  7. {
  8.   int i;
  9.   for (i=0; i<100; i++)
  10.     fprintf (stream, "%d\n", i);
  11.   if (ferror (stream))  {
  12.     fprintf (stderr, "Output to stream failed.\n");
  13.     exit (1);
  14.     }
  15. }
  16.  
  17. void
  18. DEFUN(read_data, (stream), FILE *stream)
  19. {
  20.   int i, j;
  21.  
  22.   for (i=0; i<100; i++)
  23.     {
  24.       if (fscanf (stream, "%d\n", &j) != 1 || j != i)
  25.     {
  26.       if (ferror (stream))
  27.         perror ("fscanf");
  28.       puts ("Test FAILED!");
  29.       exit (1);
  30.     }
  31.     }
  32. }
  33.  
  34. int
  35. DEFUN_VOID(main)
  36. {
  37.   FILE *output, *input;
  38.   int status;
  39.  
  40.   output = popen ("/bin/cat >tstpopen.tmp", "w");
  41.   if (output == NULL)
  42.     {
  43.       perror ("popen");
  44.       puts ("Test FAILED!");
  45.       exit (1);
  46.     }
  47.   write_data (output);
  48.   status = pclose (output);
  49.   fprintf (stderr, "pclose returned %d\n", status);
  50.   input = fopen ("tstpopen.tmp", "r");
  51.   if (input == NULL)
  52.     {
  53.       perror ("tstpopen.tmp");
  54.       puts ("Test FAILED!");
  55.       exit (1);
  56.     }
  57.   read_data (input);
  58.   (void) fclose (input);
  59.  
  60.   puts (status ? "Test FAILED!" : "Test succeeded.");
  61.   exit (status);
  62. }
  63.  
  64.   
  65.  
  66.   
  67.