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.6 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 /*
13  * Tell the printer daemon that there are new files in the spool directory.
14  */
15 
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <sys/un.h>
19 #include <stdio.h>
20 #include "lp.local.h"
21 #include "pathnames.h"
22 
23 startdaemon(printer)
24 	char *printer;
25 {
26 	struct sockaddr_un sun;
27 	register int s, n;
28 	char buf[BUFSIZ];
29 
30 	s = socket(AF_UNIX, SOCK_STREAM, 0);
31 	if (s < 0) {
32 		perr("socket");
33 		return(0);
34 	}
35 	sun.sun_family = AF_UNIX;
36 	strcpy(sun.sun_path, _PATH_SOCKETNAME);
37 	if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
38 		perr("connect");
39 		(void) close(s);
40 		return(0);
41 	}
42 	(void) sprintf(buf, "\1%s\n", printer);
43 	n = strlen(buf);
44 	if (write(s, buf, n) != n) {
45 		perr("write");
46 		(void) close(s);
47 		return(0);
48 	}
49 	if (read(s, buf, 1) == 1) {
50 		if (buf[0] == '\0') {		/* everything is OK */
51 			(void) close(s);
52 			return(1);
53 		}
54 		putchar(buf[0]);
55 	}
56 	while ((n = read(s, buf, sizeof(buf))) > 0)
57 		fwrite(buf, 1, n, stdout);
58 	(void) close(s);
59 	return(0);
60 }
61 
62 static
63 perr(msg)
64 	char *msg;
65 {
66 	extern int errno;
67 	extern char *name;
68 	char *strerror();
69 
70 	(void)printf("%s: %s: %s\n", name, msg, strerror(errno));
71 }
72