home *** CD-ROM | disk | FTP | other *** search
/ Mega CD-ROM 1 / megacd_rom_1.zip / megacd_rom_1 / GNUISH / SWALIB0.ZIP / TESTSWAP.C < prev    next >
C/C++ Source or Header  |  1990-09-10  |  3KB  |  121 lines

  1. /* testswap.c - test swaplib.
  2.    Copyright (C) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  3.  
  4.    This file is part of SWAPLIB (the library), a library for efficient
  5.    execution of child processes under MS-DOS.
  6.  
  7.    The library is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 1, or (at your option)
  10.    any later version.
  11.  
  12.    The library is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with the library; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.    $Header: e:/gnu/swaplib/RCS/testswap.c'v 0.9 90/09/09 21:44:25 tho Stable $
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27. #include "swaplib.h"
  28.  
  29. #define VERBOSE_CMD(cmd)        \
  30.   printf ("Executing: %s...\n", #cmd);    \
  31.   if ((cmd) == -1)            \
  32.     {                    \
  33.       perror ("failed");        \
  34.       exit (2);                \
  35.     }
  36.  
  37. #define VERBOSE_PIPE(cmd, pipe)            \
  38.   printf ("Opening pipe: %s...\n", #cmd);    \
  39.   pipe = cmd;                    \
  40.   if (!pipe)                    \
  41.     {                        \
  42.       perror ("failed");            \
  43.       exit (2);                    \
  44.     }
  45.  
  46.  
  47. void *xmalloc (size_t size);
  48. void *xrealloc (void *ptr, size_t size);
  49. extern void main (int ac, char **av);
  50.  
  51. void *
  52. xmalloc (size_t size)
  53. {
  54.   void *result = malloc (size);
  55.   if (result == NULL)
  56.     {
  57.       fprintf (stderr, "memory exhausted");
  58.       abort ();
  59.     }
  60.   return result;
  61. }
  62.  
  63. void *
  64. xrealloc (void *ptr, size_t size)
  65. {
  66.   void *result = realloc (ptr, size);
  67.   if (result == NULL)
  68.     {
  69.       fprintf (stderr, "memory exhausted");
  70.       abort ();
  71.     }
  72.   return result;
  73. }
  74.  
  75. void
  76. main (int argc, char **argv)
  77. {
  78.   if (argc < 2)
  79.     fprintf (stderr, "usage: testswap cmd [args]\n");
  80.   else
  81.     {
  82.       FILE *dir;
  83.       FILE *sort;
  84.       char buf[256];
  85.  
  86.       char *envv[2];
  87.  
  88.       envv[0] = "This = a test!";
  89.       envv[1] = NULL;
  90.  
  91.       printf ("Exporting the environment \"%s\"\n", envv[0]);
  92.       VERBOSE_CMD (swap_spawnlpe ("command", "command", "/c",
  93.                   "set", NULL, envv));
  94.  
  95.       VERBOSE_CMD (swap_spawnvp (argv[1], argv + 1));
  96.  
  97.       VERBOSE_PIPE (swap_popen ("dir", "r"), dir);
  98.  
  99.       VERBOSE_PIPE (swap_popen ("sort", "w"), sort);
  100.  
  101.       while (fgets (buf, 255, dir))
  102.     {
  103.       printf ("dir:  %s", buf);
  104.       fprintf (sort, "sort: %s", buf);
  105.     }
  106.  
  107.       printf ("Closing the `dir' pipe...\n");
  108.       printf ("return code %d.\n", swap_pclose (dir));
  109.  
  110.       printf ("The `sort' pipe will be closed `atexit'...\n");
  111.     }
  112. }
  113.  
  114. /* 
  115.  * Local Variables:
  116.  * mode:C
  117.  * ChangeLog:ChangeLog
  118.  * compile-command:make
  119.  * End:
  120.  */
  121.