home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume03 / hold < prev    next >
Encoding:
Text File  |  1991-08-27  |  3.3 KB  |  146 lines

  1. Path: uunet!lll-winken!lll-tis!ames!necntc!ncoast!allbery
  2. From: bill@frito.UUCP
  3. Newsgroups: comp.sources.misc
  4. Subject: v03i043: hold -- yet another overwrite
  5. Message-ID: <8806060350.AA05696@ccicpg>
  6. Date: 4 Jun 88 23:54:48 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: bill@frito.UUCP
  9. Lines: 133
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. comp.sources.misc: Volume 3, Issue 43
  13. Submitted-By: "A. Nonymous" <bill@frito.UUCP>
  14. Archive-Name: hold
  15.  
  16. Here's another version of hold.c, that lets you terminate a pipe with its
  17. input file, effectively putting the output back into the input file, as in
  18.     tail SYSLOG | hold SYSLOG
  19. To build it, simply
  20.     cc -O -o hold hold.c
  21. It's been compiled and run under V7, SYSV, XENIX and MS-DOS in its present 
  22. form.
  23.  
  24. Questions/bugs/flames to Bill Cox, (714)631-4452 (voice)
  25.             or uunet!ccicpg!frito!bill
  26.  
  27. Please let me know when you receive this, and if you have any comments or
  28. suggestions before posting it.  THANKS for all the work that you do to
  29. make stuff like this available to people.
  30.  
  31. thanks,
  32. Bill
  33.  
  34. ========================== save this file as hold.c =========================
  35. /*
  36.  * hold  terminate a pipe, gather stdin into a temporary,
  37.  *     then rename the temporary to the argument's name.
  38.  *
  39.  * Example: ... | hold filename
  40.  *
  41.  */
  42.  
  43. #ifdef MSDOS
  44. #include <io.h>
  45. #endif
  46.  
  47. #include <stdio.h>
  48. #include <errno.h>
  49. FILE *fp = NULL;
  50. char fname[11] = "holdXXXXXX";
  51. int stdoutf;
  52.  
  53. int main(argc, argv)
  54.     int argc;
  55.     char *argv[];
  56. {
  57.     int c;
  58.  
  59.     stdoutf == 0;
  60.     if (argc == 1)
  61.         stdoutf == 1;
  62.         
  63.     if (argc > 2) {
  64.     fprintf(stderr, "Usage: ... | hold filename\n");
  65.     fprintf(stderr, " or    ... | hold > filename\n");
  66.     return(1);
  67.     }
  68.     if ((fp = fopen(mktemp(fname), "w")) == NULL) 
  69.     errclean(2, "open %s", fname, 0);
  70.     
  71.     while ((c = getchar()) != EOF)
  72.     fputc(c, fp);
  73.  
  74.     if (ferror(stdin) || ferror(fp))
  75.          errclean(3, "copy stdin to %s", 0, fname);
  76.         
  77.     if (fclose(fp) != 0) 
  78.     errclean(4, "close %s", fname, 0);
  79.  
  80.     if (stdoutf) {
  81.         if (rename(fname, argv[1]) != 0)    
  82.             errclean(5, "rename %s to %s", fname, argv[1]);
  83.     }
  84.     else {
  85.     if ((fp = fopen(fname, "r")) == NULL) 
  86.         errclean(6, "open %s", fname, 0);
  87.     
  88.     while ((c = getc(fp)) != EOF)
  89.         fputc(c, stdout);
  90.  
  91.     if (ferror(stdout) || ferror(fp))
  92.             errclean(7, "copy %s to stdout", 0, fname);
  93.         
  94.     if (fclose(fp) != 0) 
  95.         errclean(8, "close %s", fname, 0);
  96.  
  97.     if (unlink(fname) != 0)
  98.         errclean(9, "remove %s", fname, 0);
  99.     }
  100.  
  101.     return(0);
  102. }
  103.  
  104.  
  105. #ifndef MSDOS
  106. int rename(s1, s2)        /* s2 = new name, s1 = existing name */
  107.     char *s1, *s2;
  108. {
  109.     /* assure that new name doesn't exist */
  110.     if (unlink(s2) != 0 && errno != ENOENT) {
  111.     errclean(10, "remove %s", s2, 0);
  112.         return(1);   
  113.         }
  114.     /* connect new name to existing file */
  115.     if (link(s1, s2) != 0) {
  116.     errclean(11, "link %s to %s", s1, s2);
  117.         return(1);   
  118.         }
  119.     /* remove old name for the file */
  120.     if (unlink(s1) != 0) {
  121.     errclean(12, "remove %s", s1, 0);
  122.         return(1);   
  123.         }
  124.     return(0);
  125. }
  126. #endif
  127.  
  128. /*
  129.  * errclean - output error message and exit to system
  130.  */
  131. errclean(code, string, arg1, arg2)
  132.     int code;
  133.     char *string;
  134.     char *arg1, *arg2;
  135. {
  136.     char lstr[80];
  137.  
  138.     if (fp != NULL) {
  139.     (void)unlink(fname);
  140.         fp = NULL;
  141.     }
  142.     sprintf(lstr, "hold %2d: can't %s\n", code, string);
  143.     fprintf(stderr, lstr, arg1, arg2);
  144.     exit(code);
  145. }
  146.