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-ping.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  880b  |  38 lines

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