home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Utilities / nppowerd-1.0-M / nppowerd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-20  |  2.6 KB  |  105 lines

  1. /*
  2.  *      nppowerd.c                               rjesse 19 oct 92
  3.  *
  4.  *      This daemon turns off a NeXT 400 dpi printer (/dev/np0)
  5.  *      that has been idle for more than "interval" minutes.
  6.  *
  7.  *      Usage:  nppowerd [interval minutes]
  8.  */
  9.  
  10. #define INTERVAL (60*60)        /* 60 minute default */
  11.  
  12. #include <stdio.h>
  13. #include <bsd/sys/types.h>
  14. #include <mach/boolean.h>
  15.  
  16. #include <bsd/sys/ioctl.h>
  17. #include <bsd/sys/file.h>
  18. #include <bsd/sys/stat.h>
  19.  
  20. #include <bsd/dev/m68k/npio.h>
  21.  
  22. char printer[] = "/dev/np0";
  23.  
  24. time_t
  25. pmtime()  {
  26.         struct stat pstat;
  27.  
  28.         if (stat(printer, &pstat) < 0)  {
  29.                 fputs("nppowerd: ", stderr);
  30.                 perror(printer);
  31.                 exit(1);
  32.         }
  33.         return pstat.st_mtime;
  34. }
  35.  
  36. turnpoff()  {
  37.         struct npop op;
  38.         register pfd;
  39.         extern errno;
  40.  
  41.         errno = 0;
  42.         if ((pfd=open(printer, O_WRONLY, 0)) < 0)
  43.                 return errno;
  44.         op.np_op = NPSETPOWER;
  45.         op.np_power = 0;
  46.         ioctl(pfd, NPIOCPOP, &op);
  47.         close(pfd);
  48.         return errno;
  49. }
  50.  
  51. main(ac, av)
  52. int ac;
  53. char **av;
  54. {
  55.         time_t offtime, mtime, idle;
  56.         register interval = INTERVAL;
  57.  
  58.         if (ac > 2) {
  59.                 fputs("Usage:  nppowerd [minutes]\n", stderr);
  60.                 return 1;
  61.         }
  62.  
  63.         switch (fork())  {
  64.         default:
  65.                 return 0;
  66.         case -1:
  67.                 return 1;
  68.         case 0:
  69.                 ; /* child */
  70.         }
  71.  
  72.         if (ac > 1)
  73.                 interval = atoi(av[1]) * 60;
  74.  
  75.         if (interval<60 || interval>(24*60*60) )
  76.                 interval = INTERVAL;
  77.  
  78.         /*
  79.          * it seems (see NP(4)) to be impossible to test whether the
  80.          * printer is on or off without first turning it on - opening
  81.          * the device, even without O_WRONLY, turns it on.  so we assume
  82.          * that upon nppowerd invocation (probably from /etc/rc) that
  83.          * the printer is on (it gets turned on during the boot sequence).
  84.          */
  85.  
  86.         offtime = 0;
  87.         mtime = pmtime();
  88.  
  89.         while (1)  {
  90.                 if (mtime != offtime)  {
  91.                         /* there has been some printer activity */
  92.                         while ((idle = time() - mtime) < interval)  {
  93.                                 sleep(interval-idle+1);
  94.                                 mtime = pmtime();
  95.                         }
  96.                         if (turnpoff() == 0)
  97.                                 offtime = mtime = pmtime();
  98.                 }
  99.                 sleep(interval);
  100.                 mtime = pmtime();
  101.         }
  102.         /*NOTREACHED*/
  103. }
  104.  
  105.