home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / fileutil / stuff2.arj / RESPONSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  2.1 KB  |  120 lines

  1. /*
  2. Stuff 1.1
  3.  
  4. Handle a response file for the stuff program.
  5. Written by Andre van Dalen (uunet!mcvax!targon!andre).
  6. */
  7.  
  8. #include "stuff.h"
  9. #include <stdlib.h>
  10. #include <fcntl.h>
  11. #include <ctype.h>
  12. #ifndef UNBUF_IO
  13. #include <stdio.h>
  14. #endif
  15.  
  16. static char *myalloc(int);
  17. static void bad_file(char *);
  18. static void no_nesting(char *);
  19.  
  20. void do_response (filename)
  21. char *filename;
  22. {
  23.     static int response = 0;
  24.     char *argv[MAX_OPTS];
  25.     int argc = 0;
  26.     char *buf, *s, *e, *os;
  27.     int fd, len, i;
  28.  
  29.     if (response++)
  30.         no_nesting(filename);
  31.  
  32.     if ((fd = open(filename, O_RDONLY)) < 0)
  33.         bad_file(filename);
  34.  
  35.     /* Read file, assume that files are < 32 Kbytes */
  36.     
  37.     len = (int) lseek(fd, 0L, 2);
  38.     lseek(fd, 0L, 0);
  39.     buf = myalloc(len);
  40.     len = read(fd, buf, len);
  41.     close(fd);
  42.  
  43.     s = buf;
  44.     e = buf + len;
  45.  
  46.     /* Build an argv[] from this file */
  47.  
  48.     while (s < e)
  49.     {
  50.         if (!isspace(*s))
  51.         {
  52.             os = s;
  53.             while (s < e && !isspace(*s))
  54.             s++;
  55.         argv[argc] = myalloc(s - os + 1);
  56.         *s++ = '\0';
  57.         strcpy(argv[argc++], os);
  58.         }
  59.         while (s < e && isspace(*s))
  60.             s++;
  61.     }
  62.     
  63.     free(buf);
  64.  
  65.     /* Parse options */
  66.     
  67.     for (i=0 ; i < argc ; i++)
  68.         parseopt(argv, &i, argc);
  69.  
  70.     for (i=0 ; i < argc ; i++)
  71.         free(argv[i]);
  72.  
  73.     response = 0;
  74. }
  75.  
  76. static char *myalloc (len)
  77. int len;
  78. {
  79.     char *s;
  80.  
  81.     if ((s = malloc(len)) == NULL)
  82.     {
  83. #ifdef UNBUF_IO
  84.         errmsg ("stuff:  FATAL: Out of memory.\n");
  85. #else
  86.         fprintf (stderr, "stuff:  FATAL: Out of memory.\n");
  87. #endif
  88.         exit(1);
  89.     }
  90.     return s;
  91. }
  92.  
  93. static void no_nesting (filename)
  94. char *filename;
  95. {
  96. #ifdef UNBUF_IO
  97.     errmsg ("stuff:  FATAL: Nested response file ");
  98.     errmsg (filename);
  99.     errmsg (".\n");
  100. #else
  101.     fprintf (stderr, "stuff:  FATAL: Nested response file %s.\n", filename)
  102. ;
  103. #endif
  104.     exit(1);
  105. }
  106.  
  107. static void bad_file (filename)
  108. char *filename;
  109. {
  110. #ifdef UNBUF_IO
  111.     errmsg ("stuff:  FATAL: Cannot read response file ");
  112.     errmsg (filename);
  113.     errmsg (".\n");
  114. #else
  115.     fprintf (stderr, "stuff:  FATAL: Cannot read response file %s.\n", file
  116. name);
  117. #endif
  118.     exit(1);
  119. }
  120.