122440Sdist /*
222440Sdist  * Copyright (c) 1983 Regents of the University of California.
334203Sbostic  * All rights reserved.
434203Sbostic  *
542801Sbostic  * %sccs.include.redist.c%
622440Sdist  */
722440Sdist 
813955Ssam #ifndef lint
9*55470Sbostic static char sccsid[] = "@(#)startdaemon.c	5.9 (Berkeley) 07/21/92";
1034203Sbostic #endif /* not lint */
1113955Ssam 
1212114Sralph 
13*55470Sbostic #include <sys/param.h>
1413443Sralph #include <sys/socket.h>
1513443Sralph #include <sys/un.h>
16*55470Sbostic 
17*55470Sbostic #include <dirent.h>
18*55470Sbostic #include <errno.h>
1937968Sbostic #include <stdio.h>
20*55470Sbostic #include <unistd.h>
21*55470Sbostic #include <string.h>
22*55470Sbostic #include "lp.h"
2337968Sbostic #include "pathnames.h"
2412114Sralph 
25*55470Sbostic static void perr __P((char *));
26*55470Sbostic 
27*55470Sbostic /*
28*55470Sbostic  * Tell the printer daemon that there are new files in the spool directory.
29*55470Sbostic  */
30*55470Sbostic 
31*55470Sbostic int
3213443Sralph startdaemon(printer)
3313443Sralph 	char *printer;
3412114Sralph {
3550885Storek 	struct sockaddr_un un;
3613443Sralph 	register int s, n;
3712114Sralph 	char buf[BUFSIZ];
3812114Sralph 
3913443Sralph 	s = socket(AF_UNIX, SOCK_STREAM, 0);
4013443Sralph 	if (s < 0) {
4113443Sralph 		perr("socket");
4212114Sralph 		return(0);
4312743Sralph 	}
4450885Storek 	un.sun_family = AF_UNIX;
4550885Storek 	strcpy(un.sun_path, _PATH_SOCKETNAME);
4650885Storek 	if (connect(s, (struct sockaddr *)&un, strlen(un.sun_path) + 2) < 0) {
4713443Sralph 		perr("connect");
4813443Sralph 		(void) close(s);
4913443Sralph 		return(0);
5013443Sralph 	}
5112114Sralph 	(void) sprintf(buf, "\1%s\n", printer);
5213443Sralph 	n = strlen(buf);
5313443Sralph 	if (write(s, buf, n) != n) {
5413443Sralph 		perr("write");
5513443Sralph 		(void) close(s);
5612114Sralph 		return(0);
5712114Sralph 	}
5813443Sralph 	if (read(s, buf, 1) == 1) {
5913443Sralph 		if (buf[0] == '\0') {		/* everything is OK */
6013443Sralph 			(void) close(s);
6113443Sralph 			return(1);
6213443Sralph 		}
6313443Sralph 		putchar(buf[0]);
6412114Sralph 	}
6513443Sralph 	while ((n = read(s, buf, sizeof(buf))) > 0)
6613443Sralph 		fwrite(buf, 1, n, stdout);
6713443Sralph 	(void) close(s);
6813443Sralph 	return(0);
6912114Sralph }
7012743Sralph 
7146910Sbostic static void
7213443Sralph perr(msg)
7313443Sralph 	char *msg;
7412743Sralph {
7513443Sralph 	extern char *name;
7612743Sralph 
7742424Sbostic 	(void)printf("%s: %s: %s\n", name, msg, strerror(errno));
7812743Sralph }
79