ftp.nice.ch/pub/next/unix/network/www/gform.1.1.NIHS.bs.gnutar.gz#/gform-1.1/send_to_printer.c

This is send_to_printer.c in view mode; [Download] [Up]

#include <string.h>

/***
 * Send buf to printer 
 *
 *  queue - The intended print queue.  
 * buf - The data to print.
 *
 * returns 0 on success
 *	  -1 on failure.
 *
 ***/
 
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>

#include "config.h"

#ifndef PRINTCMD
#define PRINTCMD "/usr/bin/lpr"
#endif
#ifndef PRINTQ
#define PRINTQ	"-P"
#endif
int send_to_printer(queue,buf)
char *queue, *buf;
{
    int fd[2], pid;
    FILE *f1;
    char thequeue[256];

/* set child process STDIN to parent fd */
	
    if (pipe(fd) <0)
	return(-1);	
  
    if ((pid = fork()) < 0)
 	return(-1);	
    else if (pid >0) {    		/* parent */
	close(fd[0]);	 		/* close read end */
	write(fd[1], buf, strlen(buf));
        close(fd[1]);
	if (waitpid(pid, NULL, 0) <0)
		return(-1);	
	return(0);
    } else {		/* child */
	close(fd[1]);	
	if (fd[0] != STDIN_FILENO) {
	   if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) 
		return(-1);	
	   close(fd[0]);
	}	/* invoke printer which expects STDIN */

	if (queue!=NULL)
		sprintf(thequeue,"%s%s",PRINTQ,queue);
	else
		thequeue[0]=0;

        if (execl(PRINTCMD,PRINTCMD,thequeue, 0) <0) 
          	return(-1); 

	close(fd[0]);
	exit(0);
    }
    return(0);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.