home *** CD-ROM | disk | FTP | other *** search
- Path: uunet!lll-winken!lll-tis!ames!necntc!ncoast!allbery
- From: bill@frito.UUCP
- Newsgroups: comp.sources.misc
- Subject: v03i043: hold -- yet another overwrite
- Message-ID: <8806060350.AA05696@ccicpg>
- Date: 4 Jun 88 23:54:48 GMT
- Sender: allbery@ncoast.UUCP
- Reply-To: bill@frito.UUCP
- Lines: 133
- Approved: allbery@ncoast.UUCP
-
- comp.sources.misc: Volume 3, Issue 43
- Submitted-By: "A. Nonymous" <bill@frito.UUCP>
- Archive-Name: hold
-
- Here's another version of hold.c, that lets you terminate a pipe with its
- input file, effectively putting the output back into the input file, as in
- tail SYSLOG | hold SYSLOG
- To build it, simply
- cc -O -o hold hold.c
- It's been compiled and run under V7, SYSV, XENIX and MS-DOS in its present
- form.
-
- Questions/bugs/flames to Bill Cox, (714)631-4452 (voice)
- or uunet!ccicpg!frito!bill
-
- Please let me know when you receive this, and if you have any comments or
- suggestions before posting it. THANKS for all the work that you do to
- make stuff like this available to people.
-
- thanks,
- Bill
-
- ========================== save this file as hold.c =========================
- /*
- * hold terminate a pipe, gather stdin into a temporary,
- * then rename the temporary to the argument's name.
- *
- * Example: ... | hold filename
- *
- */
-
- #ifdef MSDOS
- #include <io.h>
- #endif
-
- #include <stdio.h>
- #include <errno.h>
- FILE *fp = NULL;
- char fname[11] = "holdXXXXXX";
- int stdoutf;
-
- int main(argc, argv)
- int argc;
- char *argv[];
- {
- int c;
-
- stdoutf == 0;
- if (argc == 1)
- stdoutf == 1;
-
- if (argc > 2) {
- fprintf(stderr, "Usage: ... | hold filename\n");
- fprintf(stderr, " or ... | hold > filename\n");
- return(1);
- }
- if ((fp = fopen(mktemp(fname), "w")) == NULL)
- errclean(2, "open %s", fname, 0);
-
- while ((c = getchar()) != EOF)
- fputc(c, fp);
-
- if (ferror(stdin) || ferror(fp))
- errclean(3, "copy stdin to %s", 0, fname);
-
- if (fclose(fp) != 0)
- errclean(4, "close %s", fname, 0);
-
- if (stdoutf) {
- if (rename(fname, argv[1]) != 0)
- errclean(5, "rename %s to %s", fname, argv[1]);
- }
- else {
- if ((fp = fopen(fname, "r")) == NULL)
- errclean(6, "open %s", fname, 0);
-
- while ((c = getc(fp)) != EOF)
- fputc(c, stdout);
-
- if (ferror(stdout) || ferror(fp))
- errclean(7, "copy %s to stdout", 0, fname);
-
- if (fclose(fp) != 0)
- errclean(8, "close %s", fname, 0);
-
- if (unlink(fname) != 0)
- errclean(9, "remove %s", fname, 0);
- }
-
- return(0);
- }
-
-
- #ifndef MSDOS
- int rename(s1, s2) /* s2 = new name, s1 = existing name */
- char *s1, *s2;
- {
- /* assure that new name doesn't exist */
- if (unlink(s2) != 0 && errno != ENOENT) {
- errclean(10, "remove %s", s2, 0);
- return(1);
- }
- /* connect new name to existing file */
- if (link(s1, s2) != 0) {
- errclean(11, "link %s to %s", s1, s2);
- return(1);
- }
- /* remove old name for the file */
- if (unlink(s1) != 0) {
- errclean(12, "remove %s", s1, 0);
- return(1);
- }
- return(0);
- }
- #endif
-
- /*
- * errclean - output error message and exit to system
- */
- errclean(code, string, arg1, arg2)
- int code;
- char *string;
- char *arg1, *arg2;
- {
- char lstr[80];
-
- if (fp != NULL) {
- (void)unlink(fname);
- fp = NULL;
- }
- sprintf(lstr, "hold %2d: can't %s\n", code, string);
- fprintf(stderr, lstr, arg1, arg2);
- exit(code);
- }
-