122440Sdist /*
2*61840Sbostic  * Copyright (c) 1983, 1993
3*61840Sbostic  *	The Regents of the University of California.  All rights reserved.
434203Sbostic  *
542801Sbostic  * %sccs.include.redist.c%
622440Sdist  */
722440Sdist 
813955Ssam #ifndef lint
9*61840Sbostic static char sccsid[] = "@(#)startdaemon.c	8.1 (Berkeley) 06/06/93";
1034203Sbostic #endif /* not lint */
1113955Ssam 
1212114Sralph 
1355470Sbostic #include <sys/param.h>
1413443Sralph #include <sys/socket.h>
1513443Sralph #include <sys/un.h>
1655470Sbostic 
1755470Sbostic #include <dirent.h>
1855470Sbostic #include <errno.h>
1937968Sbostic #include <stdio.h>
2055470Sbostic #include <unistd.h>
2155470Sbostic #include <string.h>
2255470Sbostic #include "lp.h"
2337968Sbostic #include "pathnames.h"
2412114Sralph 
2555470Sbostic static void perr __P((char *));
2655470Sbostic 
2755470Sbostic /*
2855470Sbostic  * Tell the printer daemon that there are new files in the spool directory.
2955470Sbostic  */
3055470Sbostic 
3155470Sbostic 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);
4658239Storek #ifndef SUN_LEN
4758239Storek #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
4858239Storek #endif
4958239Storek 	if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
5013443Sralph 		perr("connect");
5113443Sralph 		(void) close(s);
5213443Sralph 		return(0);
5313443Sralph 	}
5412114Sralph 	(void) sprintf(buf, "\1%s\n", printer);
5513443Sralph 	n = strlen(buf);
5613443Sralph 	if (write(s, buf, n) != n) {
5713443Sralph 		perr("write");
5813443Sralph 		(void) close(s);
5912114Sralph 		return(0);
6012114Sralph 	}
6113443Sralph 	if (read(s, buf, 1) == 1) {
6213443Sralph 		if (buf[0] == '\0') {		/* everything is OK */
6313443Sralph 			(void) close(s);
6413443Sralph 			return(1);
6513443Sralph 		}
6613443Sralph 		putchar(buf[0]);
6712114Sralph 	}
6813443Sralph 	while ((n = read(s, buf, sizeof(buf))) > 0)
6913443Sralph 		fwrite(buf, 1, n, stdout);
7013443Sralph 	(void) close(s);
7113443Sralph 	return(0);
7212114Sralph }
7312743Sralph 
7446910Sbostic static void
7513443Sralph perr(msg)
7613443Sralph 	char *msg;
7712743Sralph {
7813443Sralph 	extern char *name;
7912743Sralph 
8042424Sbostic 	(void)printf("%s: %s: %s\n", name, msg, strerror(errno));
8112743Sralph }
82