Open System Services Programmer's Guide

Example 52 Printing an OSS File With the lp Utility
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
FILE *fpin, *fpout;
#define MAXLINE 132
char lpbuf[MAXLINE];
char line[MAXLINE];
int main (int argc, char *argv[]) {
if(argc < 2) {
fprintf(stderr, "Usage: lpopen <pathname>\n");
exit(1);
}
/* Open the input file. */
if((fpin = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(1);
}
/* Create a string for popen() with the name of the file to be
printed in the title. */
sprintf(lpbuf, "lp -t %s", argv[1]);
/* Open the line printer program. */
if((fpout = popen(lpbuf, "w")) == NULL) {
fprintf(stderr, "popen error on %s\n", lpbuf);
exit(1);
}
/* Copy the contents of the file to the line printer filter. */
while(fgets(line, MAXLINE, fpin) != NULL) {
if(fputs(line, fpout) == EOF) {
fprintf(stderr, "fputs error to pipe\n");
exit(1);
}
}
/* Check for errors on input file processing. */
if(ferror(fpin)) {
fprintf(stderr, "fgets error\n");
exit(1);
}
/* Check for errors in execution of lp command. */
if(pclose(fpout) == -1) {
fprintf(stderr, "pclose error\n");
exit(1);
}
printf("File %s output to line printer\n", argv[1]);
exit(0);
}
Printing OSS Files With the Guardian API
You can send OSS files and output to the Guardian spooler or send them directly to a printer using
Guardian procedures.
Sending an OSS File to the Spooler
You can send OSS files and output to the Guardian print spooler and take advantage of the spooler
features. For example, your program can examine the queues of available printers and move a
job to a printer whose queue becomes unexpectedly shorter because of a job cancellation, or
away from a printer that is experiencing delays. You can also access spoolers that are on other
systems than the file you want to print.
Printer I/O 227