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 the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)startdaemon.c	5.3 (Berkeley) 06/30/88";
20 #endif /* not lint */
21 
22 /*
23  * Tell the printer daemon that there are new files in the spool directory.
24  */
25 
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
30 #include "lp.local.h"
31 
32 startdaemon(printer)
33 	char *printer;
34 {
35 	struct sockaddr_un sun;
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 	sun.sun_family = AF_UNIX;
45 	strcpy(sun.sun_path, SOCKETNAME);
46 	if (connect(s, &sun, strlen(sun.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
72 perr(msg)
73 	char *msg;
74 {
75 	extern char *name;
76 	extern int sys_nerr;
77 	extern char *sys_errlist[];
78 	extern int errno;
79 
80 	printf("%s: %s: ", name, msg);
81 	fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout);
82 	putchar('\n');
83 }
84