home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1875 < prev    next >
Internet Message Format  |  1990-12-28  |  3KB

  1. From: rsalz@bbn.com (Rich Salz)
  2. Newsgroups: comp.unix.programmer,alt.sources
  3. Subject: Re: how to put a program into a .plan file
  4. Message-ID: <2867@litchi.bbn.com>
  5. Date: 27 Sep 90 19:13:51 GMT
  6.  
  7. >>% /etc/mknod ~/.plan p (Assuming that any existing .plan is not there)
  8.  
  9. In <978@bbt.UUCP> rgs@bbt.UUCP (steinbeiser) writes:
  10. >Ok, how do you check the status of the file to see if it has
  11. >been open by someone else?
  12.  
  13. /*  $Revision$
  14. **
  15. **  A process to create dynamic .plan files.  Creates a fifo, waits for
  16. **  someone to connect to it.  Optional first argument is the directory
  17. **  to chdir(2) to, as in "plan ~rsalz &"; default is value of $HOME.
  18. **
  19. **  Right now, this just keeps a count and runs fortune.  A neat hack
  20. **  would be to replace /usr/etc/in.fingerd with code that does a
  21. **  getpeername()/gethostbyaddr(), and goes back to the requesting
  22. **  host to do a finger there.
  23. */
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <errno.h>
  28.  
  29. #if    !defined(FD_SET)
  30.     /* Some systems have it typedef'd wrong, so use #define. */
  31. #define fd_set        int
  32. #define    FD_SET(n, p)    (*(p) |= (1 << (n)))
  33. #define    FD_CLR(n, p)    (*(p) &= ~(1 << (n)))
  34. #define    FD_ISSET(n, p)    (*(p) & (1 << (n)))
  35. #define    FD_ZERO(p)    (*(p) = 0)
  36. #endif    /* !defined(FD_SET) */
  37.  
  38. extern int    errno;
  39. extern char    *getenv();
  40.  
  41. static char    PLAN[] = ".plan";
  42. static char    FORTUNE[] = "/usr/emacs/dist/etc/yow";
  43. /*static char    FORTUNE[] = "/usr/games/fortune";*/
  44.  
  45.  
  46. main(ac, av)
  47.     int        ac;
  48.     char    *av[];
  49. {
  50.     char    *p;
  51.     int        Count;
  52.     int        fd;
  53.     FILE    *F;
  54.     FILE    *In;
  55.     fd_set    writers;
  56.     char    buff[256];
  57.  
  58.     /* Go to the right directory. */
  59.     if (ac == 2)
  60.     p = av[1];
  61.     else if ((p = getenv("HOME")) == NULL) {
  62.     fprintf(stderr, "No $HOME.\n");
  63.     exit(1);
  64.     }
  65.     if (chdir(p) < 0) {
  66.     perror("Can't cd to $HOME");
  67.     exit(1);
  68.     }
  69.  
  70.     /* Remove any old one, create a new one. */
  71.     if (unlink(PLAN) < 0 && errno != ENOENT) {
  72.     perror("Can't unlink");
  73.     exit(1);
  74.     }
  75.     (void)umask(0);
  76.     if (mknod(PLAN, S_IFIFO | 0644, 0) < 0) {
  77.     perror("Can't mknod");
  78.     exit(1);
  79.     }
  80.  
  81.     /* Enter the server loop. */
  82.     for (Count = 0; ; ) {
  83.  
  84.     /* Open the fifo for writing. */
  85.     if ((F = fopen(PLAN, "w")) == NULL) {
  86.         perror("Can't fopen");
  87.         (void)unlink(PLAN);
  88.         exit(1);
  89.     }
  90.     fd = fileno(F);
  91.  
  92.     /* Wait until someone else opens it for reading, so that we can
  93.      * write on it. */
  94.     FD_ZERO(&writers);
  95.     FD_SET(fd, &writers);
  96.     if (select(fd + 1, (fd_set *)NULL, &writers, (fd_set *)NULL,
  97.         (struct timeval *)NULL) < 0) {
  98.         if (errno != EINTR)
  99.         perror("Can't select");
  100.         continue;
  101.     }
  102.     if (!FD_ISSET(fd, &writers))
  103.         /* "Can't happen" */
  104.         continue;
  105.  
  106.     /* Say hello to the nice stranger. */
  107.     fprintf(F, "Well, hello there -- this is fortune #%d:\n", ++Count);
  108.     if ((In = popen(FORTUNE, "r")) == NULL)
  109.         fprintf(F, "\tSorry, cookie jar is empty.\n");
  110.     else {
  111.         while (fgets(buff, sizeof buff, In))
  112.         fputs(buff, F);
  113.         (void)pclose(In);
  114.     }
  115.  
  116.     /* Close it so they stop reading, and pause to make sure of it. */
  117.     (void)fclose(F);
  118.     (void)sleep(1);
  119.     }
  120.  
  121.     /* NOTREACHED */
  122. }
  123. -- 
  124. Please send comp.sources.unix-related mail to rsalz@uunet.uu.net.
  125. Use a domain-based address or give alternate paths, or you may lose out.
  126.