home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1580 / anon-reply.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  1KB  |  46 lines

  1. /* anon-reply.c - put replies to be sent in ACS spool directory */
  2. #include <stdio.h>
  3. #include <sys/file.h>
  4. #include <errno.h>
  5. main(argc,argv)
  6. int argc;
  7. char *argv[];
  8. {
  9.   char buf[512];
  10.   char fname[40];
  11.   char pbuf[6];
  12.   int mypid, fd, rdlen;
  13.   if ( argc != 2 ) {
  14.     exit(1);
  15.   }
  16.   /* run as root */
  17.   if (setuid(0) != 0 ) {
  18.     perror("couldn\'t setuid root: ");
  19.     exit(1);
  20.   }
  21.   /* build the filename */
  22.   strcpy(fname,"/usr/personals/spool/REP");
  23.   sprintf(pbuf,"%05d",getpid());
  24.   strcat(fname,pbuf);
  25.   while (access(fname,F_OK) == 0) {
  26.     strcat(fname,".");
  27.     if (strlen(fname) == 39 ) break;
  28.   }
  29.   if ((fd = open(fname,O_WRONLY|O_CREAT,0600)) == -1) {
  30.     perror("can\'t open REP tmp file: ");
  31.     exit(1);
  32.   }
  33.   /* write the alias in as the first line in the file */
  34.   write(fd,argv[1],strlen(argv[1]));
  35.   write(fd,"\012",1);
  36.   /* read the message on stdin and write it to fd */
  37.   while ( rdlen = read(0,buf,512) ) {
  38.     if (write(fd,buf,rdlen) != rdlen) {
  39.       perror("write to REP tmp file failed: ");
  40.       exit(1);
  41.     }
  42.   }
  43.   close(fd);
  44.   exit(0);
  45. }
  46.