ulptprint.c revision 1.1
11.1Spooka/*	$NetBSD: ulptprint.c,v 1.1 2009/12/15 16:01:50 pooka Exp $	*/
21.1Spooka
31.1Spooka/*
41.1Spooka * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
51.1Spooka *
61.1Spooka * Redistribution and use in source and binary forms, with or without
71.1Spooka * modification, are permitted provided that the following conditions
81.1Spooka * are met:
91.1Spooka * 1. Redistributions of source code must retain the above copyright
101.1Spooka *    notice, this list of conditions and the following disclaimer.
111.1Spooka * 2. Redistributions in binary form must reproduce the above copyright
121.1Spooka *    notice, this list of conditions and the following disclaimer in the
131.1Spooka *    documentation and/or other materials provided with the distribution.
141.1Spooka *
151.1Spooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
161.1Spooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
171.1Spooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
181.1Spooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191.1Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201.1Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
211.1Spooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221.1Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231.1Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241.1Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251.1Spooka * SUCH DAMAGE.
261.1Spooka */
271.1Spooka
281.1Spooka#include <sys/types.h>
291.1Spooka#include <sys/dirent.h>
301.1Spooka#include <sys/mount.h>
311.1Spooka
321.1Spooka#include <rump/rump.h>
331.1Spooka#include <rump/rump_syscalls.h>
341.1Spooka
351.1Spooka#include <err.h>
361.1Spooka#include <errno.h>
371.1Spooka#include <fcntl.h>
381.1Spooka#include <stdio.h>
391.1Spooka#include <stdlib.h>
401.1Spooka#include <string.h>
411.1Spooka#include <unistd.h>
421.1Spooka
431.1Spooka/*
441.1Spooka * Proof-of-concept program:
451.1Spooka *
461.1Spooka * Prints given (postscript) file by "catting" into the printer.
471.1Spooka */
481.1Spooka
491.1Spookaint
501.1Spookamain(int argc, char *argv[])
511.1Spooka{
521.1Spooka	char buf[8192];
531.1Spooka	ssize_t n;
541.1Spooka	int probeonly = 0;
551.1Spooka	int fd_src, fd_dst;
561.1Spooka
571.1Spooka	if (argc != 2)
581.1Spooka		errx(1, "need 2 args");
591.1Spooka
601.1Spooka	if (strcmp(argv[1], "probe") == 0)
611.1Spooka		probeonly = 1;
621.1Spooka
631.1Spooka	if (probeonly)
641.1Spooka		rump_boot_sethowto(RUMP_AB_VERBOSE);
651.1Spooka	rump_init();
661.1Spooka	if (probeonly)
671.1Spooka		exit(0);
681.1Spooka
691.1Spooka	fd_dst = rump_sys_open("/dev/ulpt0", O_RDWR);
701.1Spooka	if (fd_dst == -1)
711.1Spooka		err(1, "printer open");
721.1Spooka
731.1Spooka	fd_src = open(argv[1], O_RDONLY);
741.1Spooka	if (fd_src == -1)
751.1Spooka		err(1, "open source");
761.1Spooka
771.1Spooka	for (;;) {
781.1Spooka		n = read(fd_src, buf, sizeof(buf));
791.1Spooka		if (n == 0)
801.1Spooka			break;
811.1Spooka		if (n == -1)
821.1Spooka			err(1, "read");
831.1Spooka
841.1Spooka		if (rump_sys_write(fd_dst, buf, n) != n)
851.1Spooka			err(1, "write to printer");
861.1Spooka	}
871.1Spooka	rump_sys_close(fd_dst);
881.1Spooka
891.1Spooka	printf("done\n");
901.1Spooka}
91