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

  1. /*
  2. ** This program turns off an idle NeXT printer after some given time,
  3. ** written by Yunliang Yu <yu@math.duke.edu>, 11/5/94
  4. **
  5. ** Modified by Art Isbell (art@cubicsol.com) to take idleLimit as an optional
  6. ** argument with 180 seconds the default idleLimit if no argument is
  7. ** specified.  npoffd checks the printer idle time every checkPeriod (60 by
  8. ** default) seconds, so the actual idle limit can vary from idleLimit to
  9. ** idleLimit + checkPeriod seconds.  If this behavior isn't ideal, modify
  10. ** checkPeriod.
  11. **
  12. ** npoffd fails to turn off the printer if a print job is aborted before any
  13. ** printing has occurred because npoffd depends on the modification time of the
  14. ** printer device which isn't updated until a page is printed.  A fix for this
  15. ** situation is left as an exercise for the reader :-)
  16. **
  17. ** To build, as root:
  18. **
  19. ** cc -O2 -object -s -arch m68k npoffd.c -o /usr/local/etc/npoffd
  20. **
  21. ** To use, add the following to the /etc/rc.local file (assumes 180-second idle
  22. ** limit):
  23. **
  24. ** if [ -x /usr/local/etc/npoffd ]; then
  25. **     (sleep 5; /usr/local/etc/npoffd 180) &
  26. ** fi
  27. **
  28. ** Use at your own risk, if any.
  29. */
  30.  
  31. #import <libc.h>
  32. #import <time.h>
  33. #import <syslog.h>
  34. #import <sys/types.h>
  35. #import <sys/stat.h>
  36. #import <sys/file.h>
  37. #import <sys/ioctl.h>
  38. #import <mach/m68k/boolean.h>
  39. #import <dev/m68k/npio.h>
  40.  
  41. static const char *printerPort = "/dev/np0";
  42. static const char *syslogOpenMsg = "npoffd";
  43. static const char *syslogOffMsg = "printer off";
  44. static const char *syslogNoStatMsg = "can't stat %s, exit";
  45.  
  46. /* minimal idle limit before turning off */
  47. static const int idleLimitDefault = 180;
  48.  
  49. /* frequency the printer's idle time is checked */
  50. static const int checkPeriod = 60;
  51.  
  52. int main(int argc, char *argv[])
  53. {
  54.     char *chPtr;
  55.  
  56.     /* time printer port was last written.  Used to determine whether printing
  57.     ** has occurred within the past checkPeriod seconds. */
  58.     time_t previousPrinterPortModifyTime = 0;
  59.     int idleLimit;
  60.     struct stat statBuffer;
  61.  
  62.     /* Get argument, if any. */
  63.     if (argc != 2 || (idleLimit = (int)strtol(argv[1], &chPtr, 10)) <= 0)
  64.     {
  65.         idleLimit = idleLimitDefault;
  66.     }
  67.     /* Set up syslog. */
  68.     openlog(syslogOpenMsg, LOG_PID, LOG_DAEMON);
  69.     setlogmask(LOG_UPTO(LOG_ERR));
  70.  
  71.     /* Loop while printer port is accessible. */
  72.     while (stat(printerPort, &statBuffer) != -1)
  73.     {
  74.         /* last time printer port was written */
  75.         time_t printerPortModifyTime = statBuffer.st_mtime;
  76.         int fileDescriptor;
  77.  
  78.         /* If printing has occurred and printer has been idle for more than
  79.         ** idleLimit seconds, power off printer. */
  80.         if (printerPortModifyTime != previousPrinterPortModifyTime &&
  81.             time(0) - printerPortModifyTime > idleLimit &&
  82.             (fileDescriptor = open(printerPort, O_WRONLY | O_NDELAY, 0)) > 0)
  83.         {
  84.             /* structure for NPIOCPOP - printer op command */
  85.             struct npop printerOpStruct;
  86.  
  87.             printerOpStruct.np_op = NPSETPOWER;
  88.             printerOpStruct.np_power = 0;
  89.             ioctl(fileDescriptor, NPIOCPOP, &printerOpStruct);
  90.             close(fileDescriptor);
  91.             syslog(LOG_ERR, syslogOffMsg);
  92.             previousPrinterPortModifyTime = printerPortModifyTime;
  93.         }
  94.         sleep(checkPeriod);
  95.     }
  96.     syslog(LOG_ERR, syslogNoStatMsg, printerPort);
  97.     closelog();
  98.     exit(1);
  99. }
  100.