[Fwd: Re: Stdprn in GCC]

Decibels decibelshelp@charter.net
Wed Oct 23 15:21:00 GMT 2002



John Carter wrote:

>On Tue, 22 Oct 2002, David wrote:
>
>Hint 1: Clip out a small piece of code you having a problem with and send 
>it and the error message you are having difficulty with to the group. 
>Makes it a lot easy to give a good answer.
>
>You could do things like...
>FILE * stdprn;
>main() {
> stdprn = fopen( "/dev/lp0", "w");
>
>And it will head for the line printeer port on your machine.
>
>If you want it spooled or sent to a remote printer you have to be more 
>sophisticated.
>
>You may have to chmod o+rwx /dev/lp0 for the above simplistic trick to 
>work.
>
>  
>

Hello, Thanks for the help.
The printer is local, works fine using CUPS. Parallel port printer on 
/dev/lp0 --> printer/0
Hope I have provide more information that is helpful. Really trying to 
learn.

Here is the code at bottom. Actually it is from Sams Teach Yourself C in 
21 Days.
print_it.c, page 26.

If I compile it as seen I get only one error: `stdprn' undeclared (first 
use in this function).
Note: Should I also have 'console line printer' enabled in the kernel, 
seems to
be only for kernel messages. Otherwise printer works fine.

Using your example above I was able to change the code and get it to 
compile fine.
But if I put below in the varible declaration section in main() AND in the
function varible declaration section it will compile,
_____________________________
    FILE * stdprn;
    stdprn = fopen( "/dev/lp0", "w");
_____________________________
but strace shows seg fault of:

open("/dev/lp0", O_WRONLY|O_CREAT|O_TRUNC, 0666) = -1 EBUSY (Device or 
resource busy)
--- SIGSEGV (Segmentation fault) ---
+++ killed by SIGSEGV +++

/dev/lp0 does point to my printer:
/dev/lp0 -> printers/0

If I do the same with main(), but in the function put
FILE * stdprn; in the varible declaration section and put
stdprn = fopen(....); in the function block I get a seg fault with
a different error. Both are closer than have gotten before.

open("/dev/lp0", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
open("print_it.c", O_RDONLY)            = 4
--- SIGSEGV (Segmentation fault) ---
+++ killed by SIGSEGV +++

trying the 'chmod o=rwx /dev/lp0 didn't seem to work for root
or user, just with the user I get the device busy error.

/* print_it.c from book as is */
#include <stdlib.h>
#include <stdio.h>

void do_heading(char *filename);

int line =0, page = 0;

int main( int argv, char *argc[] )
{
    char buffer[256];
    FILE *fp;

    if( argv <2 )
    {
        fprintf(stderr, "\nProper Usage is: ");
        fprintf(stderr, "\n\nprint_it filename.ext\n" );
        return(1);
    }
    if (( fp = fopen( argc[1], "r" )) == NULL )
    {
        fprintf( stderr, "Error opening file, %s!", argc[1]);
        return(1);
    }

    page = 0;
    line = 1;
    do_heading( argc[1] );
 
    while( fgets( buffer, 256, fp ) != NULL )
    {
        if( line % 55 == 0 )
            do_heading( argc[1] );
        fprintf( stdprn, "%4d:\t%s", line++, buffer );
    }
   
    fprintf( stdprn, "\f" );
    fclose(fp);
    return 0;
}

/* Start function */

void do_heading( char *filename )
{
    page++;
   
    if( page > 1)
        fprintf( stdprn, "\f");
        fprintf( stdprn, "Page: %d, %s\n\n", page, filename );
}


Thanks, Dave




More information about the Gcc-help mailing list