1*13443Sralph /*	startdaemon.c	4.6	83/06/29	*/
212114Sralph /*
312114Sralph  * Tell the printer daemon that there are new files in the spool directory.
412114Sralph  */
512114Sralph 
6*13443Sralph #include <stdio.h>
7*13443Sralph #include <sys/types.h>
8*13443Sralph #include <sys/socket.h>
9*13443Sralph #include <sys/un.h>
10*13443Sralph #include "lp.local.h"
1112114Sralph 
12*13443Sralph startdaemon(printer)
13*13443Sralph 	char *printer;
1412114Sralph {
15*13443Sralph 	struct sockaddr_un sun;
16*13443Sralph 	register int s, n;
1712114Sralph 	char buf[BUFSIZ];
1812114Sralph 
19*13443Sralph 	s = socket(AF_UNIX, SOCK_STREAM, 0);
20*13443Sralph 	if (s < 0) {
21*13443Sralph 		perr("socket");
2212114Sralph 		return(0);
2312743Sralph 	}
24*13443Sralph 	sun.sun_family = AF_UNIX;
25*13443Sralph 	strcpy(sun.sun_path, SOCKETNAME);
26*13443Sralph 	if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
27*13443Sralph 		perr("connect");
28*13443Sralph 		(void) close(s);
29*13443Sralph 		return(0);
30*13443Sralph 	}
3112114Sralph 	(void) sprintf(buf, "\1%s\n", printer);
32*13443Sralph 	n = strlen(buf);
33*13443Sralph 	if (write(s, buf, n) != n) {
34*13443Sralph 		perr("write");
35*13443Sralph 		(void) close(s);
3612114Sralph 		return(0);
3712114Sralph 	}
38*13443Sralph 	if (read(s, buf, 1) == 1) {
39*13443Sralph 		if (buf[0] == '\0') {		/* everything is OK */
40*13443Sralph 			(void) close(s);
41*13443Sralph 			return(1);
42*13443Sralph 		}
43*13443Sralph 		putchar(buf[0]);
4412114Sralph 	}
45*13443Sralph 	while ((n = read(s, buf, sizeof(buf))) > 0)
46*13443Sralph 		fwrite(buf, 1, n, stdout);
47*13443Sralph 	(void) close(s);
48*13443Sralph 	return(0);
4912114Sralph }
5012743Sralph 
5112743Sralph static
52*13443Sralph perr(msg)
53*13443Sralph 	char *msg;
5412743Sralph {
55*13443Sralph 	extern char *name;
5612743Sralph 	extern int sys_nerr;
5712743Sralph 	extern char *sys_errlist[];
58*13443Sralph 	extern int errno;
5912743Sralph 
60*13443Sralph 	printf("%s: %s: ", name, msg);
6112743Sralph 	fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout);
6212743Sralph 	putchar('\n');
6312743Sralph }
64