ftp.nice.ch/pub/next/unix/printing/nppowerd.1.0.N.bs.tar.gz#/nppowerd.1.0.N.bs/nppowerd.c

This is nppowerd.c in view mode; [Download] [Up]

/*
 *      nppowerd.c                               rjesse 19 oct 92
 *
 *      This daemon turns off a NeXT 400 dpi printer (/dev/np0)
 *      that has been idle for more than "interval" minutes.
 *
 *      Usage:  nppowerd [interval minutes]
 */

#define INTERVAL (60*60)        /* 60 minute default */

#include <stdio.h>
#include <bsd/sys/types.h>
#include <mach/boolean.h>

#include <bsd/sys/ioctl.h>
#include <bsd/sys/file.h>
#include <bsd/sys/stat.h>

#include <bsd/dev/m68k/npio.h>

char printer[] = "/dev/np0";

time_t
pmtime()  {
        struct stat pstat;

        if (stat(printer, &pstat) < 0)  {
                fputs("nppowerd: ", stderr);
                perror(printer);
                exit(1);
        }
        return pstat.st_mtime;
}

turnpoff()  {
        struct npop op;
        register pfd;
        extern errno;

        errno = 0;
        if ((pfd=open(printer, O_WRONLY, 0)) < 0)
                return errno;
        op.np_op = NPSETPOWER;
        op.np_power = 0;
        ioctl(pfd, NPIOCPOP, &op);
        close(pfd);
        return errno;
}

main(ac, av)
int ac;
char **av;
{
        time_t offtime, mtime, idle;
        register interval = INTERVAL;

        if (ac > 2) {
                fputs("Usage:  nppowerd [minutes]\n", stderr);
                return 1;
        }

        switch (fork())  {
        default:
                return 0;
        case -1:
                return 1;
        case 0:
                ; /* child */
        }

        if (ac > 1)
                interval = atoi(av[1]) * 60;

        if (interval<60 || interval>(24*60*60) )
                interval = INTERVAL;

        /*
         * it seems (see NP(4)) to be impossible to test whether the
         * printer is on or off without first turning it on - opening
         * the device, even without O_WRONLY, turns it on.  so we assume
         * that upon nppowerd invocation (probably from /etc/rc) that
         * the printer is on (it gets turned on during the boot sequence).
         */

        offtime = 0;
        mtime = pmtime();

        while (1)  {
                if (mtime != offtime)  {
                        /* there has been some printer activity */
                        while ((idle = time() - mtime) < interval)  {
                                sleep(interval-idle+1);
                                mtime = pmtime();
                        }
                        if (turnpoff() == 0)
                                offtime = mtime = pmtime();
                }
                sleep(interval);
                mtime = pmtime();
        }
        /*NOTREACHED*/
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.