* aidepost.c: rewrote to unlink temp file before writing to it so that
authorArt Cancro <ajc@citadel.org>
Tue, 13 Apr 1999 02:15:29 +0000 (02:15 +0000)
committerArt Cancro <ajc@citadel.org>
Tue, 13 Apr 1999 02:15:29 +0000 (02:15 +0000)
          it will automatically go away if interrupted. Also ran indent -kr -i8

citadel/ChangeLog
citadel/aidepost.c

index 28153d1c0379218337be6208820de5e6d5ab15e1..94342c4bccb30dd243f4f2b0d28a6cdcd371368d 100644 (file)
@@ -1,3 +1,7 @@
+Mon Apr 12 22:13:26 EDT 1999 Art Cancro <ajc@uncnsrd.mt-kisco.ny.us>
+       * aidepost.c: rewrote to unlink temp file before writing to it so that
+         it will automatically go away if interrupted. Also ran indent -kr -i8
+
 1999-04-12 Nathan Bryant <bryant@cs.usm.maine.edu>
        * configure.in, Makefile.in: taught it how to generate OpenBSD shared
          libraries
index 5c4ecdeae437027369ec138514fc32a9da8ebf5a..c231f6ff09d176bc8c54ab07beaee92c02eddd6b 100644 (file)
@@ -1,6 +1,6 @@
 /* aidepost.c
  * This is just a little hack to copy standard input to a message in Aide>
- * v1.6
+ * v2.0
  * $Id$
  */
 
@@ -9,57 +9,85 @@
 #include <stdio.h>
 #include <time.h>
 #include <limits.h>
+#include <errno.h>
 #include "citadel.h"
 #include "config.h"
 
-void make_message(char *filename)
+void make_message(FILE *fp)
 {
-       FILE *fp;
        int a;
-       long bb,cc;
+       long bb, cc;
        time_t now;
        time(&now);
-       fp=fopen(filename,"wb"); if (fp==NULL) exit(22);
-       putc(255,fp);
-       putc(MES_NORMAL,fp);
-       putc(1,fp);
-       fprintf(fp,"Proom_aide"); putc(0,fp);
-       fprintf(fp,"T%ld",now); putc(0,fp);
-       fprintf(fp,"ACitadel"); putc(0,fp);
-       fprintf(fp,"OAide"); putc(0,fp);
-       fprintf(fp,"N%s",NODENAME); putc(0,fp);
-       putc('M',fp);
-       bb=ftell(fp);
-       while (a=getc(stdin), a>0) {
-               if (a!=8) putc(a,fp);
+       putc(255, fp);
+       putc(MES_NORMAL, fp);
+       putc(1, fp);
+       fprintf(fp, "Proom_aide");
+       putc(0, fp);
+       fprintf(fp, "T%ld", now);
+       putc(0, fp);
+       fprintf(fp, "ACitadel");
+       putc(0, fp);
+       fprintf(fp, "OAide");
+       putc(0, fp);
+       fprintf(fp, "N%s", NODENAME);
+       putc(0, fp);
+       putc('M', fp);
+       bb = ftell(fp);
+       while (a = getc(stdin), a > 0) {
+               if (a != 8)
+                       putc(a, fp);
                else {
-                       cc=ftell(fp);
-                       if (cc!=bb) fseek(fp,(-1L),1);
-                       }
+                       cc = ftell(fp);
+                       if (cc != bb)
+                               fseek(fp, (-1L), 1);
                }
-       putc(0,fp);
-       putc(0,fp);
-       putc(0,fp);
-       fclose(fp);
        }
+       putc(0, fp);
+       putc(0, fp);
+       putc(0, fp);
+}
 
 int main(int argc, char **argv)
 {
        char tempbase[32];
        char temptmp[64];
        char tempspool[64];
-       char movecmd[256];
-       
+       FILE *tempfp, *spoolfp;
+       int ch;
+
        get_config();
-       snprintf(tempbase, sizeof tempbase,"ap.%d", getpid());
-       snprintf(temptmp, sizeof temptmp,"/tmp/%s", tempbase);
-       snprintf(tempspool,sizeof tempspool,"./network/spoolin/%s", tempbase);
-       make_message(temptmp);
+       snprintf(tempbase, sizeof tempbase, "ap.%d", getpid());
+       snprintf(temptmp, sizeof temptmp, "/tmp/%s", tempbase);
+       snprintf(tempspool, sizeof tempspool, "./network/spoolin/%s", tempbase);
+
+       tempfp = fopen(temptmp, "wb+");
+       if (tempfp == NULL) {
+               perror("cannot open temp file");
+               exit(errno);
+       }
+       /* Unlink the temp file, so it automatically gets deleted by the OS if
+        * this program is interrupted or crashes.
+        */ unlink(temptmp);
 
-       snprintf(movecmd, sizeof movecmd, "/bin/mv %s %s", temptmp, tempspool);
-       system(movecmd);
+       /* Generate a message from stdin */
+       make_message(tempfp);
 
-       execlp("./netproc", "netproc", "-i", NULL);
-       fprintf(stderr,"aidepost: could not run netproc\n");
-       exit(1);
+       /* Copy it to a new temp file in the spool directory */
+       rewind(tempfp);
+
+       spoolfp = fopen(tempspool, "wb");
+       if (spoolfp == NULL) {
+               perror("cannot open spool file");
+               exit(errno);
        }
+       while (ch = getc(tempfp), (ch >= 0))
+               putc(ch, spoolfp);
+
+       fclose(tempfp);
+       fclose(spoolfp);
+
+       execlp("./netproc", "netproc", "-i", NULL);
+       perror("cannot run netproc");
+       exit(errno);
+}