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