122440Sdist /*
222440Sdist  * Copyright (c) 1983 Regents of the University of California.
334203Sbostic  * All rights reserved.
434203Sbostic  *
5*42801Sbostic  * %sccs.include.redist.c%
622440Sdist  */
722440Sdist 
813955Ssam #ifndef lint
9*42801Sbostic static char sccsid[] = "@(#)startdaemon.c	5.6 (Berkeley) 06/01/90";
1034203Sbostic #endif /* not lint */
1113955Ssam 
1212114Sralph /*
1312114Sralph  * Tell the printer daemon that there are new files in the spool directory.
1412114Sralph  */
1512114Sralph 
1613443Sralph #include <sys/types.h>
1713443Sralph #include <sys/socket.h>
1813443Sralph #include <sys/un.h>
1937968Sbostic #include <stdio.h>
2013443Sralph #include "lp.local.h"
2137968Sbostic #include "pathnames.h"
2212114Sralph 
2313443Sralph startdaemon(printer)
2413443Sralph 	char *printer;
2512114Sralph {
2613443Sralph 	struct sockaddr_un sun;
2713443Sralph 	register int s, n;
2812114Sralph 	char buf[BUFSIZ];
2912114Sralph 
3013443Sralph 	s = socket(AF_UNIX, SOCK_STREAM, 0);
3113443Sralph 	if (s < 0) {
3213443Sralph 		perr("socket");
3312114Sralph 		return(0);
3412743Sralph 	}
3513443Sralph 	sun.sun_family = AF_UNIX;
3637968Sbostic 	strcpy(sun.sun_path, _PATH_SOCKETNAME);
3713443Sralph 	if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
3813443Sralph 		perr("connect");
3913443Sralph 		(void) close(s);
4013443Sralph 		return(0);
4113443Sralph 	}
4212114Sralph 	(void) sprintf(buf, "\1%s\n", printer);
4313443Sralph 	n = strlen(buf);
4413443Sralph 	if (write(s, buf, n) != n) {
4513443Sralph 		perr("write");
4613443Sralph 		(void) close(s);
4712114Sralph 		return(0);
4812114Sralph 	}
4913443Sralph 	if (read(s, buf, 1) == 1) {
5013443Sralph 		if (buf[0] == '\0') {		/* everything is OK */
5113443Sralph 			(void) close(s);
5213443Sralph 			return(1);
5313443Sralph 		}
5413443Sralph 		putchar(buf[0]);
5512114Sralph 	}
5613443Sralph 	while ((n = read(s, buf, sizeof(buf))) > 0)
5713443Sralph 		fwrite(buf, 1, n, stdout);
5813443Sralph 	(void) close(s);
5913443Sralph 	return(0);
6012114Sralph }
6112743Sralph 
6212743Sralph static
6313443Sralph perr(msg)
6413443Sralph 	char *msg;
6512743Sralph {
6642424Sbostic 	extern int errno;
6713443Sralph 	extern char *name;
6842424Sbostic 	char *strerror();
6912743Sralph 
7042424Sbostic 	(void)printf("%s: %s: %s\n", name, msg, strerror(errno));
7112743Sralph }
72