new copyright notice
[unix-history] / usr / src / usr.sbin / lpr / common_source / startdaemon.c
CommitLineData
d0aeaf5a
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
9d85c861
KB
3 * All rights reserved.
4 *
417f7a11 5 * %sccs.include.redist.c%
d0aeaf5a
DF
6 */
7
5f84f8f0 8#ifndef lint
417f7a11 9static char sccsid[] = "@(#)startdaemon.c 5.6 (Berkeley) %G%";
9d85c861 10#endif /* not lint */
5f84f8f0 11
c6e2cb21
RC
12/*
13 * Tell the printer daemon that there are new files in the spool directory.
14 */
15
8fed920b
RC
16#include <sys/types.h>
17#include <sys/socket.h>
18#include <sys/un.h>
7abf8d65 19#include <stdio.h>
8fed920b 20#include "lp.local.h"
7abf8d65 21#include "pathnames.h"
c6e2cb21 22
8fed920b
RC
23startdaemon(printer)
24 char *printer;
c6e2cb21 25{
8fed920b
RC
26 struct sockaddr_un sun;
27 register int s, n;
c6e2cb21
RC
28 char buf[BUFSIZ];
29
8fed920b
RC
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;
7abf8d65 36 strcpy(sun.sun_path, _PATH_SOCKETNAME);
8fed920b
RC
37 if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
38 perr("connect");
39 (void) close(s);
c6e2cb21 40 return(0);
f545660a 41 }
c6e2cb21 42 (void) sprintf(buf, "\1%s\n", printer);
8fed920b
RC
43 n = strlen(buf);
44 if (write(s, buf, n) != n) {
45 perr("write");
46 (void) close(s);
c6e2cb21
RC
47 return(0);
48 }
8fed920b
RC
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]);
c6e2cb21 55 }
8fed920b
RC
56 while ((n = read(s, buf, sizeof(buf))) > 0)
57 fwrite(buf, 1, n, stdout);
58 (void) close(s);
59 return(0);
c6e2cb21 60}
f545660a
RC
61
62static
8fed920b
RC
63perr(msg)
64 char *msg;
f545660a 65{
8fed920b 66 extern int errno;
7af1139d
KB
67 extern char *name;
68 char *strerror();
f545660a 69
7af1139d 70 (void)printf("%s: %s: %s\n", name, msg, strerror(errno));
f545660a 71}