popen.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Pipe to a Subprocess
  2. Copyright (C) 1991-2026 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <https://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. void
  17. write_data (FILE * stream)
  18. {
  19. int i;
  20. for (i = 0; i < 100; i++)
  21. fprintf (stream, "%d\n", i);
  22. if (ferror (stream))
  23. {
  24. fprintf (stderr, "Output to stream failed.\n");
  25. exit (EXIT_FAILURE);
  26. }
  27. }
  28. /*@group*/
  29. int
  30. main (void)
  31. {
  32. FILE *output;
  33. output = popen ("more", "w");
  34. if (!output)
  35. {
  36. fprintf (stderr,
  37. "incorrect parameters or too many files.\n");
  38. return EXIT_FAILURE;
  39. }
  40. write_data (output);
  41. if (pclose (output) != 0)
  42. {
  43. fprintf (stderr,
  44. "Could not run more or other error.\n");
  45. }
  46. return EXIT_SUCCESS;
  47. }
  48. /*@end group*/