1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)startdaemon.c	5.9 (Berkeley) 07/21/92";
10 #endif /* not lint */
11 
12 
13 #include <sys/param.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 
17 #include <dirent.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include "lp.h"
23 #include "pathnames.h"
24 
25 static void perr __P((char *));
26 
27 /*
28  * Tell the printer daemon that there are new files in the spool directory.
29  */
30 
31 int
32 startdaemon(printer)
33 	char *printer;
34 {
35 	struct sockaddr_un un;
36 	register int s, n;
37 	char buf[BUFSIZ];
38 
39 	s = socket(AF_UNIX, SOCK_STREAM, 0);
40 	if (s < 0) {
41 		perr("socket");
42 		return(0);
43 	}
44 	un.sun_family = AF_UNIX;
45 	strcpy(un.sun_path, _PATH_SOCKETNAME);
46 	if (connect(s, (struct sockaddr *)&un, strlen(un.sun_path) + 2) < 0) {
47 		perr("connect");
48 		(void) close(s);
49 		return(0);
50 	}
51 	(void) sprintf(buf, "\1%s\n", printer);
52 	n = strlen(buf);
53 	if (write(s, buf, n) != n) {
54 		perr("write");
55 		(void) close(s);
56 		return(0);
57 	}
58 	if (read(s, buf, 1) == 1) {
59 		if (buf[0] == '\0') {		/* everything is OK */
60 			(void) close(s);
61 			return(1);
62 		}
63 		putchar(buf[0]);
64 	}
65 	while ((n = read(s, buf, sizeof(buf))) > 0)
66 		fwrite(buf, 1, n, stdout);
67 	(void) close(s);
68 	return(0);
69 }
70 
71 static void
72 perr(msg)
73 	char *msg;
74 {
75 	extern char *name;
76 
77 	(void)printf("%s: %s: %s\n", name, msg, strerror(errno));
78 }
79