summaryrefslogtreecommitdiff
path: root/minix/commands/lp/lp.c
blob: 8ea373aab01b8e6faf2aeddcab41b32e5fc161cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*	lp 1.4 - Send file to the lineprinter		Author: Kees J. Bot
 *								3 Dec 1989
 */
#define nil 0
#include <sys/types.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>

char LPD1[] = "/usr/sbin/lpd";		/* Proper place of lpd */
char LPD2[] = "/usr/bin/lpd";		/* Minix has no sbin directories. */

void report(char *mess)
{
	fprintf(stderr, "lp: %s: %s\n", mess, strerror(errno));
}

void fatal(char *mess)
{
	report(mess);
	exit(1);
}

void lp(char *file)
/* Start the lpd daemon giving it the file to spool and print. */
{
	int pid, status;

	if (file[0] != '/' || (pid= fork()) == 0) {
		execl(LPD1, LPD1, file, (char *) nil);
		if (errno != ENOENT) fatal(LPD1);
		execl(LPD2, LPD2, file, (char *) nil);
		fatal(LPD2);
	}

	if (pid < 0) fatal("can't fork");

	if (waitpid(pid, &status, 0) < 0) fatal("wait");

	if (status != 0) exit(1);
}

char path[PATH_MAX+1];
int cwdsize;

int main(int argc, char **argp)
{
	int e=0;
	char *file;

	if (argc <= 1) lp("stdin");

	/* Lpd requires full path names, so find out where we are. */
	if (getcwd(path, sizeof(path)) == nil)
		fatal("Can't determine current directory");

	cwdsize= strlen(path);

	/* Hand each file to lpd. */
	while ((file= *++argp) != nil) {

		close(0);

		if (open(file, O_RDONLY) != 0) {
			report(file);
			e=1;
			continue;
		}
		if (file[0] == '/') {
			lp(file);
			continue;
		}
		if (cwdsize + 1 + strlen(file) + 1 > sizeof(path)) {
			fprintf(stderr,
				"lp: full pathname of %s is too long\n",
				file);
			e=1;
			continue;
		}
		path[cwdsize] = '/';
		strcpy(path + cwdsize + 1, file);

		lp(path);
	}
	exit(e);
}