1*22440Sdist /*
2*22440Sdist  * Copyright (c) 1983 Regents of the University of California.
3*22440Sdist  * All rights reserved.  The Berkeley software License Agreement
4*22440Sdist  * specifies the terms and conditions for redistribution.
5*22440Sdist  */
6*22440Sdist 
713955Ssam #ifndef lint
8*22440Sdist static char sccsid[] = "@(#)startdaemon.c	5.1 (Berkeley) 06/06/85";
9*22440Sdist #endif not lint
1013955Ssam 
1112114Sralph /*
1212114Sralph  * Tell the printer daemon that there are new files in the spool directory.
1312114Sralph  */
1412114Sralph 
1513443Sralph #include <stdio.h>
1613443Sralph #include <sys/types.h>
1713443Sralph #include <sys/socket.h>
1813443Sralph #include <sys/un.h>
1913443Sralph #include "lp.local.h"
2012114Sralph 
2113443Sralph startdaemon(printer)
2213443Sralph 	char *printer;
2312114Sralph {
2413443Sralph 	struct sockaddr_un sun;
2513443Sralph 	register int s, n;
2612114Sralph 	char buf[BUFSIZ];
2712114Sralph 
2813443Sralph 	s = socket(AF_UNIX, SOCK_STREAM, 0);
2913443Sralph 	if (s < 0) {
3013443Sralph 		perr("socket");
3112114Sralph 		return(0);
3212743Sralph 	}
3313443Sralph 	sun.sun_family = AF_UNIX;
3413443Sralph 	strcpy(sun.sun_path, SOCKETNAME);
3513443Sralph 	if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
3613443Sralph 		perr("connect");
3713443Sralph 		(void) close(s);
3813443Sralph 		return(0);
3913443Sralph 	}
4012114Sralph 	(void) sprintf(buf, "\1%s\n", printer);
4113443Sralph 	n = strlen(buf);
4213443Sralph 	if (write(s, buf, n) != n) {
4313443Sralph 		perr("write");
4413443Sralph 		(void) close(s);
4512114Sralph 		return(0);
4612114Sralph 	}
4713443Sralph 	if (read(s, buf, 1) == 1) {
4813443Sralph 		if (buf[0] == '\0') {		/* everything is OK */
4913443Sralph 			(void) close(s);
5013443Sralph 			return(1);
5113443Sralph 		}
5213443Sralph 		putchar(buf[0]);
5312114Sralph 	}
5413443Sralph 	while ((n = read(s, buf, sizeof(buf))) > 0)
5513443Sralph 		fwrite(buf, 1, n, stdout);
5613443Sralph 	(void) close(s);
5713443Sralph 	return(0);
5812114Sralph }
5912743Sralph 
6012743Sralph static
6113443Sralph perr(msg)
6213443Sralph 	char *msg;
6312743Sralph {
6413443Sralph 	extern char *name;
6512743Sralph 	extern int sys_nerr;
6612743Sralph 	extern char *sys_errlist[];
6713443Sralph 	extern int errno;
6812743Sralph 
6913443Sralph 	printf("%s: %s: ", name, msg);
7012743Sralph 	fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout);
7112743Sralph 	putchar('\n');
7212743Sralph }
73