Пример #1
0
Файл: print.c Проект: y-c/APUE
int
main(int argc, char *argv[])
{
	int				fd, sockfd, err, text, c;
	struct stat		sbuf;
	char			*host;
	struct addrinfo	*ailist, *aip;

	err = 0;
	text = 0;
	while ((c = getopt(argc, argv, "t")) != -1) {
		switch (c) {
		case 't':
			text = 1;
			break;

		case '?':
			err = 1;
			break;
		}
	}
	if (err || (optind != argc - 1))
		err_quit("usage: print [-t] filename");
	if ((fd = open(argv[optind], O_RDONLY)) < 0)
		err_sys("print: can't open %s", argv[1]);
	if (fstat(fd, &sbuf) < 0)
		err_sys("print: can't stat %s", argv[1]);
	if (!S_ISREG(sbuf.st_mode))
		err_quit("print: %s must be a regular file\n", argv[1]);

	/*
	 * Get the hostname of the host acting as the print server.
	 */
	if ((host = get_printserver()) == NULL)
		err_quit("print: no print server defined");
	if ((err = getaddrlist(host, "print", &ailist)) != 0)
		err_quit("print: getaddrinfo error: %s", gai_strerror(err));

	for (aip = ailist; aip != NULL; aip = aip->ai_next) {
		if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
			err = errno;
		} else if (connect_retry(sockfd, aip->ai_addr,
		  aip->ai_addrlen) < 0) {
			err = errno;
		} else {
			submit_file(fd, sockfd, argv[1], sbuf.st_size, text);
			exit(0);
		}
	}
	errno = err;
	err_ret("print: can't contact %s", host);
	exit(1);
}
Пример #2
0
int main(int argc, char *argv[])
{
	int fd, sfd, err, c;
	struct stat sbuf;
	char *host, *file;
	struct addrinfo	*ailist, *aip;
	char *orient;
	long jobid;
	int text;
	int sides;		/* 0=default 1=one-sided */
				/* 2=two-sided-long-edge  3=two-sided-short-edge */
	
	sides = 0;
	text = 0;
	err = 0;
	jobid = -1;
	while ((c = getopt(argc, argv, "is:to:c:h")) != -1) {
		switch (c) {
		case 'h':
			usage();
			exit(0);
		case 'c':
			jobid = atol(optarg);
			break;
		case 'i':
			job_status();
			exit(0);
		case 's':
			sides = atol(optarg);
			if (sides < 1 || sides > 3)
				err_sys("print: invalid sides option");
			break;
		case 'o':
			orient = optarg;
			break;
		case 't':
			text = 1;
			break;

		case '?':
			err = 1;
			break;
		}
	}
	if (err || (optind != argc - 1))
		err_quit("usage: print [-t] filename");
	file = argv[optind];
	if ((fd = open(file, O_RDONLY)) < 0)
		err_sys("print: can't open %s", file);
	if (fstat(fd, &sbuf) < 0)
		err_sys("print: can't stat %s", file);
	if (!S_ISREG(sbuf.st_mode))
		err_quit("print: %s must be a regular file", file);
	/*
	 * Get the hostname of the host acting as the print server.
	 */
	if ((host = get_printserver()) == NULL)
		err_quit("print: no print server defined");
	if ((err = getaddrlist(host, "print", &ailist)) != 0)
		err_quit("print: getaddrinfo error: %s", gai_strerror(err));

	for (aip = ailist; aip != NULL; aip = aip->ai_next) {
		if ((sfd = connect_retry(AF_INET, SOCK_STREAM, 0,
		  aip->ai_addr, aip->ai_addrlen)) < 0) {
			err = errno;
		} else {
			if (jobid != -1) /* cancel job */
				cancel_job(sfd, jobid);
			else 
				submit_file(fd, sfd, file, sbuf.st_size,
					    text, orient, sides);
			exit(0);
		}
	}
	err_exit(err, "print: can't contact %s", host);
}