1*13955Ssam #ifndef lint
2*13955Ssam static char sccsid[] = "@(#)startdaemon.c	4.7 (Berkeley) 07/17/83";
3*13955Ssam #endif
4*13955Ssam 
512114Sralph /*
612114Sralph  * Tell the printer daemon that there are new files in the spool directory.
712114Sralph  */
812114Sralph 
913443Sralph #include <stdio.h>
1013443Sralph #include <sys/types.h>
1113443Sralph #include <sys/socket.h>
1213443Sralph #include <sys/un.h>
1313443Sralph #include "lp.local.h"
1412114Sralph 
1513443Sralph startdaemon(printer)
1613443Sralph 	char *printer;
1712114Sralph {
1813443Sralph 	struct sockaddr_un sun;
1913443Sralph 	register int s, n;
2012114Sralph 	char buf[BUFSIZ];
2112114Sralph 
2213443Sralph 	s = socket(AF_UNIX, SOCK_STREAM, 0);
2313443Sralph 	if (s < 0) {
2413443Sralph 		perr("socket");
2512114Sralph 		return(0);
2612743Sralph 	}
2713443Sralph 	sun.sun_family = AF_UNIX;
2813443Sralph 	strcpy(sun.sun_path, SOCKETNAME);
2913443Sralph 	if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
3013443Sralph 		perr("connect");
3113443Sralph 		(void) close(s);
3213443Sralph 		return(0);
3313443Sralph 	}
3412114Sralph 	(void) sprintf(buf, "\1%s\n", printer);
3513443Sralph 	n = strlen(buf);
3613443Sralph 	if (write(s, buf, n) != n) {
3713443Sralph 		perr("write");
3813443Sralph 		(void) close(s);
3912114Sralph 		return(0);
4012114Sralph 	}
4113443Sralph 	if (read(s, buf, 1) == 1) {
4213443Sralph 		if (buf[0] == '\0') {		/* everything is OK */
4313443Sralph 			(void) close(s);
4413443Sralph 			return(1);
4513443Sralph 		}
4613443Sralph 		putchar(buf[0]);
4712114Sralph 	}
4813443Sralph 	while ((n = read(s, buf, sizeof(buf))) > 0)
4913443Sralph 		fwrite(buf, 1, n, stdout);
5013443Sralph 	(void) close(s);
5113443Sralph 	return(0);
5212114Sralph }
5312743Sralph 
5412743Sralph static
5513443Sralph perr(msg)
5613443Sralph 	char *msg;
5712743Sralph {
5813443Sralph 	extern char *name;
5912743Sralph 	extern int sys_nerr;
6012743Sralph 	extern char *sys_errlist[];
6113443Sralph 	extern int errno;
6212743Sralph 
6313443Sralph 	printf("%s: %s: ", name, msg);
6412743Sralph 	fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout);
6512743Sralph 	putchar('\n');
6612743Sralph }
67