1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)startdaemon.c	8.1 (Berkeley) 06/06/93";
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 #ifndef SUN_LEN
47 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
48 #endif
49 	if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
50 		perr("connect");
51 		(void) close(s);
52 		return(0);
53 	}
54 	(void) sprintf(buf, "\1%s\n", printer);
55 	n = strlen(buf);
56 	if (write(s, buf, n) != n) {
57 		perr("write");
58 		(void) close(s);
59 		return(0);
60 	}
61 	if (read(s, buf, 1) == 1) {
62 		if (buf[0] == '\0') {		/* everything is OK */
63 			(void) close(s);
64 			return(1);
65 		}
66 		putchar(buf[0]);
67 	}
68 	while ((n = read(s, buf, sizeof(buf))) > 0)
69 		fwrite(buf, 1, n, stdout);
70 	(void) close(s);
71 	return(0);
72 }
73 
74 static void
75 perr(msg)
76 	char *msg;
77 {
78 	extern char *name;
79 
80 	(void)printf("%s: %s: %s\n", name, msg, strerror(errno));
81 }
82