1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)startdaemon.c	5.2 (Berkeley) 05/05/88";
15 #endif /* not lint */
16 
17 /*
18  * Tell the printer daemon that there are new files in the spool directory.
19  */
20 
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include "lp.local.h"
26 
27 startdaemon(printer)
28 	char *printer;
29 {
30 	struct sockaddr_un sun;
31 	register int s, n;
32 	char buf[BUFSIZ];
33 
34 	s = socket(AF_UNIX, SOCK_STREAM, 0);
35 	if (s < 0) {
36 		perr("socket");
37 		return(0);
38 	}
39 	sun.sun_family = AF_UNIX;
40 	strcpy(sun.sun_path, SOCKETNAME);
41 	if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
42 		perr("connect");
43 		(void) close(s);
44 		return(0);
45 	}
46 	(void) sprintf(buf, "\1%s\n", printer);
47 	n = strlen(buf);
48 	if (write(s, buf, n) != n) {
49 		perr("write");
50 		(void) close(s);
51 		return(0);
52 	}
53 	if (read(s, buf, 1) == 1) {
54 		if (buf[0] == '\0') {		/* everything is OK */
55 			(void) close(s);
56 			return(1);
57 		}
58 		putchar(buf[0]);
59 	}
60 	while ((n = read(s, buf, sizeof(buf))) > 0)
61 		fwrite(buf, 1, n, stdout);
62 	(void) close(s);
63 	return(0);
64 }
65 
66 static
67 perr(msg)
68 	char *msg;
69 {
70 	extern char *name;
71 	extern int sys_nerr;
72 	extern char *sys_errlist[];
73 	extern int errno;
74 
75 	printf("%s: %s: ", name, msg);
76 	fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout);
77 	putchar('\n');
78 }
79