common way to print text to a printer from inside a C-program

common way to print text to a printer from inside a C-program

Post by Brar Pienin » Wed, 07 Mar 2001 21:35:01



Hi,

making my first steps in programing linux in c (after reading some books and
howto's) i wonder which would be the common (best) way to print text to a
printer.

I would prefer to print to the printer in the same way I print to stdout
(using printf or puts), but I don't want to use the '/dev/lp'-file because
of the fact that a non-root-user could not access it and I dont want to
completly redirect stdout as the program prints to both the display and the
printer.
I would be happy if i could pass the text to the lpd and let it handle the
rest, but generating a file on the disk and printing it via the lpr-command
seems unprofessional to me.

It would be great if someone could give me some helping words or even a
short code-example.

Brar

 
 
 

common way to print text to a printer from inside a C-program

Post by Lew Pitch » Wed, 07 Mar 2001 22:07:43



>Hi,

>making my first steps in programing linux in c (after reading some books and
>howto's) i wonder which would be the common (best) way to print text to a
>printer.

>I would prefer to print to the printer in the same way I print to stdout
>(using printf or puts), but I don't want to use the '/dev/lp'-file because
>of the fact that a non-root-user could not access it and I dont want to
>completly redirect stdout as the program prints to both the display and the
>printer.
>I would be happy if i could pass the text to the lpd and let it handle the
>rest, but generating a file on the disk and printing it via the lpr-command
>seems unprofessional to me.

>It would be great if someone could give me some helping words or even a
>short code-example.

  #include <stdio.h>

  FILE *printer;

  if ((printer = popen("/usr/bin/lpr","w")) == NULL)
  {
        int count;

        fprintf(printer,"This is a print test.\n");
        for (count = 2; count < 102; ++count)
             fprintf(printer,"This is line %d\n",count);
        fprintf(printer,"This ends the print test\n");
        pclose(printer);
  }
  else fprintf(stderr,"Gak!! cant popen(\"/usr/bin/lpr\")\n");

Lew Pitcher
Information Technology Consultant
Toronto Dominion Bank Financial Group


(Opinions expressed are my own, not my employer's.)

 
 
 

common way to print text to a printer from inside a C-program

Post by Lew Pitch » Wed, 07 Mar 2001 22:42:50


Correction......




>>Hi,

>>making my first steps in programing linux in c (after reading some books and
>>howto's) i wonder which would be the common (best) way to print text to a
>>printer.

>>I would prefer to print to the printer in the same way I print to stdout
>>(using printf or puts), but I don't want to use the '/dev/lp'-file because
>>of the fact that a non-root-user could not access it and I dont want to
>>completly redirect stdout as the program prints to both the display and the
>>printer.
>>I would be happy if i could pass the text to the lpd and let it handle the
>>rest, but generating a file on the disk and printing it via the lpr-command
>>seems unprofessional to me.

>>It would be great if someone could give me some helping words or even a
>>short code-example.

>  #include <stdio.h>

>  FILE *printer;

   /* *** CORRECTION ***
   ** if ((printer = popen("/usr/bin/lpr","w")) == NULL)
   */

   if ((printer = popen("/usr/bin/lpr","w")) != NULL)  

- Show quoted text -

>  {
>        int count;

>    fprintf(printer,"This is a print test.\n");
>        for (count = 2; count < 102; ++count)
>             fprintf(printer,"This is line %d\n",count);
>        fprintf(printer,"This ends the print test\n");
>        pclose(printer);
>  }
>  else fprintf(stderr,"Gak!! cant popen(\"/usr/bin/lpr\")\n");

>Lew Pitcher
>Information Technology Consultant
>Toronto Dominion Bank Financial Group


>(Opinions expressed are my own, not my employer's.)

Lew Pitcher
Information Technology Consultant
Toronto Dominion Bank Financial Group


(Opinions expressed are my own, not my employer's.)

 
 
 

common way to print text to a printer from inside a C-program

Post by Brar Pienin » Thu, 08 Mar 2001 08:45:36


Hi Lew,

thank you very much for your code sample, it was exactly what i was lloking
for - short, easy to understand and fits perfect to my problem!

Brar

 
 
 

common way to print text to a printer from inside a C-program

Post by Danang Suharn » Sat, 10 Mar 2001 09:29:10


Hi guys...

I want to create a simple TCP server which an ability to detect that client
is accidentally disconnected. I've tried using functions "sigaction",
"signal", "fcntl", "ioctl", etc including their constantas like :

fcntl(fd, F_SETFL, O_NONBLOCK | O_ASYNC);
fcntl(fd, 10 /* F_SETSIG */ );
fcntl(fd, F_SETOWN, (long) getpid());
ioctl(fd, 0x5452 /* FIOASYNC */);

BUT.................

Still cannot successfully detect remote disconnection......

I really get impressed by the power of Telnet client in RedHat 6.1. This
small program always detect when remote server was down or something like
that.  I dig the it's source code and put some fprintf() in function
"peerdied" but when the message "Connection closed by foreign host" appears,
my text has never been displayed.......How can that be???

Regards,
cinoban

 
 
 

common way to print text to a printer from inside a C-program

Post by Eric P. McC » Sat, 10 Mar 2001 01:01:31



> I want to create a simple TCP server which an ability to detect that client
> is accidentally disconnected.

When read() returns zero, the connection was closed on the other end.

This behavior is compatible with select(): it will just mark the
appropriate fd as available for reading, and you'll get the above
behavior when you try to read().

Quote:> I've tried using functions "sigaction", "signal", "fcntl", "ioctl",
> etc including their constantas like :

You only get SIGPIPE when you try to _write_ to a disconnected
socket.

--

  "Knowing that a lot of people across the world with Geocities sites
absolutely despise me is about the only thing that can add a positive
spin to this situation."  - Something Awful, 1/11/2001

 
 
 

1. Printing to the printer from a C-Program

I need to write a c-program to print to the "default" printer which is
usually (and the Printing-HOWO confirms this) denoted as the device "lp".
But /proc/lp contains no device lp.  The printer is installed and working.
I can print from a file with the "lp  <filename>" command and if I
explicitly code the device name "/dev/lp0" into the program it will also
print correctly.

The main question is how can I tell if any printer is installed from a
c-program and what to call it when I open it for writing?

        Thanks

                Norm

2. Writing a Linux Network Driver

3. remote printer won't print non plain text print jobs.

4. security holes

5. Printing to two printers from one queue (Printers print identically not load balancing)

6. PPP-2.2 acting in very strange manner. HELP!

7. printing text on postscript printer

8. ftpd question

9. Ways to convert prn,ps to plain text format

10. How to print text to a ps printer?

11. running programs inside programs

12. Print to linux printer from Win95, got text, but no graphics

13. Printing text to a PCL printer