122440Sdist /*
222440Sdist  * Copyright (c) 1983 Regents of the University of California.
3*34203Sbostic  * All rights reserved.
4*34203Sbostic  *
5*34203Sbostic  * Redistribution and use in source and binary forms are permitted
6*34203Sbostic  * provided that this notice is preserved and that due credit is given
7*34203Sbostic  * to the University of California at Berkeley. The name of the University
8*34203Sbostic  * may not be used to endorse or promote products derived from this
9*34203Sbostic  * software without specific prior written permission. This software
10*34203Sbostic  * is provided ``as is'' without express or implied warranty.
1122440Sdist  */
1222440Sdist 
1313955Ssam #ifndef lint
14*34203Sbostic static char sccsid[] = "@(#)startdaemon.c	5.2 (Berkeley) 05/05/88";
15*34203Sbostic #endif /* not lint */
1613955Ssam 
1712114Sralph /*
1812114Sralph  * Tell the printer daemon that there are new files in the spool directory.
1912114Sralph  */
2012114Sralph 
2113443Sralph #include <stdio.h>
2213443Sralph #include <sys/types.h>
2313443Sralph #include <sys/socket.h>
2413443Sralph #include <sys/un.h>
2513443Sralph #include "lp.local.h"
2612114Sralph 
2713443Sralph startdaemon(printer)
2813443Sralph 	char *printer;
2912114Sralph {
3013443Sralph 	struct sockaddr_un sun;
3113443Sralph 	register int s, n;
3212114Sralph 	char buf[BUFSIZ];
3312114Sralph 
3413443Sralph 	s = socket(AF_UNIX, SOCK_STREAM, 0);
3513443Sralph 	if (s < 0) {
3613443Sralph 		perr("socket");
3712114Sralph 		return(0);
3812743Sralph 	}
3913443Sralph 	sun.sun_family = AF_UNIX;
4013443Sralph 	strcpy(sun.sun_path, SOCKETNAME);
4113443Sralph 	if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
4213443Sralph 		perr("connect");
4313443Sralph 		(void) close(s);
4413443Sralph 		return(0);
4513443Sralph 	}
4612114Sralph 	(void) sprintf(buf, "\1%s\n", printer);
4713443Sralph 	n = strlen(buf);
4813443Sralph 	if (write(s, buf, n) != n) {
4913443Sralph 		perr("write");
5013443Sralph 		(void) close(s);
5112114Sralph 		return(0);
5212114Sralph 	}
5313443Sralph 	if (read(s, buf, 1) == 1) {
5413443Sralph 		if (buf[0] == '\0') {		/* everything is OK */
5513443Sralph 			(void) close(s);
5613443Sralph 			return(1);
5713443Sralph 		}
5813443Sralph 		putchar(buf[0]);
5912114Sralph 	}
6013443Sralph 	while ((n = read(s, buf, sizeof(buf))) > 0)
6113443Sralph 		fwrite(buf, 1, n, stdout);
6213443Sralph 	(void) close(s);
6313443Sralph 	return(0);
6412114Sralph }
6512743Sralph 
6612743Sralph static
6713443Sralph perr(msg)
6813443Sralph 	char *msg;
6912743Sralph {
7013443Sralph 	extern char *name;
7112743Sralph 	extern int sys_nerr;
7212743Sralph 	extern char *sys_errlist[];
7313443Sralph 	extern int errno;
7412743Sralph 
7513443Sralph 	printf("%s: %s: ", name, msg);
7612743Sralph 	fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout);
7712743Sralph 	putchar('\n');
7812743Sralph }
79