Home | History | Annotate | Line # | Download | only in ulptprint
      1 /*	$NetBSD: ulptprint.c,v 1.1 2009/12/15 16:01:50 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/types.h>
     29 #include <sys/dirent.h>
     30 #include <sys/mount.h>
     31 
     32 #include <rump/rump.h>
     33 #include <rump/rump_syscalls.h>
     34 
     35 #include <err.h>
     36 #include <errno.h>
     37 #include <fcntl.h>
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <string.h>
     41 #include <unistd.h>
     42 
     43 /*
     44  * Proof-of-concept program:
     45  *
     46  * Prints given (postscript) file by "catting" into the printer.
     47  */
     48 
     49 int
     50 main(int argc, char *argv[])
     51 {
     52 	char buf[8192];
     53 	ssize_t n;
     54 	int probeonly = 0;
     55 	int fd_src, fd_dst;
     56 
     57 	if (argc != 2)
     58 		errx(1, "need 2 args");
     59 
     60 	if (strcmp(argv[1], "probe") == 0)
     61 		probeonly = 1;
     62 
     63 	if (probeonly)
     64 		rump_boot_sethowto(RUMP_AB_VERBOSE);
     65 	rump_init();
     66 	if (probeonly)
     67 		exit(0);
     68 
     69 	fd_dst = rump_sys_open("/dev/ulpt0", O_RDWR);
     70 	if (fd_dst == -1)
     71 		err(1, "printer open");
     72 
     73 	fd_src = open(argv[1], O_RDONLY);
     74 	if (fd_src == -1)
     75 		err(1, "open source");
     76 
     77 	for (;;) {
     78 		n = read(fd_src, buf, sizeof(buf));
     79 		if (n == 0)
     80 			break;
     81 		if (n == -1)
     82 			err(1, "read");
     83 
     84 		if (rump_sys_write(fd_dst, buf, n) != n)
     85 			err(1, "write to printer");
     86 	}
     87 	rump_sys_close(fd_dst);
     88 
     89 	printf("done\n");
     90 }
     91