1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/************************************************************************/
/* Utility functions for printing. */
/************************************************************************/
# include "config.h"
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "utilPrinter.h"
# include <debugon.h>
/************************************************************************/
/* */
/* Find out what printers are available on the system. */
/* */
/* 1) BSD/Linux style. */
/* 2) SysV style. */
/* */
/************************************************************************/
static int utilPrinterGetLprPrinters( const char * command,
char * scratch,
int * pPrinterCount,
PrintDestination ** pDestinations )
{
int l;
FILE * f;
int found= 0;
PrintDestination * pd= *pDestinations;
int count= *pPrinterCount;
f= popen( command, "r" );
if ( ! f )
{ return 0; }
while( fgets( scratch, 250, f ) )
{
l= strlen( scratch );
if ( l > 0 && scratch[l-1] == '\n' )
{ scratch[l-1]= '\0'; }
if ( scratch[0] == '\t' || scratch[l-2] != ':' )
{ continue; }
scratch[l-2]= '\0';
pd= (PrintDestination *)realloc( pd, (count+ 1)*sizeof( PrintDestination ) );
if ( ! pd )
{ XDEB(pd); return -1; }
*pDestinations= pd;
pd[count].pdCommand= (char *)malloc( l+ 23 );
if ( ! pd[count].pdCommand )
{ LXDEB(l,pd[count].pdCommand); return -1; }
# if 0
Has the advantage that there is no maximum on the filesize.
Has the disadvantage that it does not work on SuSE Linux 6.0
sprintf( pd[count].pdCommand, "lpr -r -s -P '%s' '%s'",
scratch, "%s" );
# else
sprintf( pd[count].pdCommand, "lpr -r -P '%s' '%s'",
scratch, "%s" );
# endif
pd[count].pdPrintKind= APPprinterTMPFILE;
pd[count].pdPrinterName= (char *)malloc( l+ 1 );
if ( ! pd[count].pdPrinterName )
{ LXDEB(l,pd[count].pdPrinterName); return -1; }
strcpy( pd[count].pdPrinterName, scratch );
found++; count++;
}
pclose( f );
*pPrinterCount= count;
return found;
}
int utilPrinterGetPrinters( int * pPrinterCount,
int * pDefaultPrinter,
PrintDestination ** pDestinations )
{
PrintDestination * pd= (PrintDestination *)0;
int count= 0;
int defaultPrinter= -1;
char * defaultPrinterName= (char *)0;
FILE * f;
char scratch[250+1];
int l;
char * s;
/* 1 */
if ( utilPrinterGetLprPrinters( "( lpc status ) 2>/dev/null",
scratch, &count, &pd ) < 1 &&
utilPrinterGetLprPrinters( "( /usr/sbin/lpc status ) 2>/dev/null",
scratch, &count, &pd ) < 1 )
{ /* nothing.. sys5? */; }
/* 2 */
f= popen( "( lpstat -a ) 2>/dev/null", "r" );
if ( f )
{
char * firstLpPrinter= (char *)0;
while( fgets( scratch, 250, f ) )
{
s= strchr( scratch, ' ' );
if ( ! s )
{ continue; }
*s= '\0'; l= s- scratch;
pd= (PrintDestination *)realloc( pd, (count+ 1)*sizeof( PrintDestination ) );
if ( ! pd )
{ XDEB(pd); return -1; }
pd[count].pdCommand= (char *)malloc( l+ 12 );
if ( ! pd[count].pdCommand )
{ LXDEB(l,pd[count].pdCommand); return -1; }
sprintf( pd[count].pdCommand, "lp -s -d '%s'", scratch );
pd[count].pdPrintKind= APPprinterPIPE;
pd[count].pdPrinterName= (char *)malloc( l+ 1 );
if ( ! pd[count].pdPrinterName )
{ LXDEB(l,pd[count].pdPrinterName); return -1; }
strcpy( pd[count].pdPrinterName, scratch );
if ( ! firstLpPrinter )
{ firstLpPrinter= pd[count].pdPrinterName; }
count++;
}
pclose( f );
defaultPrinterName= getenv( "LPDEST" );
if ( ! defaultPrinterName )
{ defaultPrinterName= firstLpPrinter; }
}
if ( defaultPrinterName )
{
for ( l= 0; l < count; l++ )
{
if ( ! strcmp( defaultPrinterName, pd[l].pdPrinterName ) )
{ defaultPrinter= l; break; }
}
}
*pPrinterCount= count;
*pDefaultPrinter= defaultPrinter;
*pDestinations= pd;
return 0;
}
|