]> arthur.barton.de Git - netatalk.git/blobdiff - etc/psf/psf.c
Remove bdb env on exit
[netatalk.git] / etc / psf / psf.c
index 53d3a543124beec8b82ed60700def2faeafa3904..535daeead5fce3ee5991594138087449396b6ea6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: psf.c,v 1.4 2001-06-25 20:13:45 rufustfirefly Exp $
+ * $Id: psf.c,v 1.13 2009-10-16 01:50:50 didg Exp $
  *
  * Copyright (c) 1990,1995 Regents of The University of Michigan.
  * All Rights Reserved. See COPYRIGHT.
 #include <unistd.h>
 #endif /* HAVE_UNISTD_H */
 #include <sys/time.h>
+
+/* POSIX.1 sys/wait.h check */
 #include <sys/types.h>
+#ifdef HAVE_SYS_WAIT_H
 #include <sys/wait.h>
+#endif /* HAVE_SYS_WAIT_H */
+#ifndef WEXITSTATUS
+#define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
+#endif /* ! WEXITSTATUS */
+#ifndef WIFEXITED
+#define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
+#endif /* ! WIFEXITED */
+
 #include <sys/file.h>
-#include <sys/syslog.h>
+#include <syslog.h>
 #include <atalk/paths.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
 #include <signal.h>
+#include <errno.h>
 
 /* Forward Declarations */
 int pexecv(char *path, char *argv[]);
 int copyio();
 int textps();
 
-char           psapath[] = _PATH_PSA;
-char           *psaargv[] = { "psa", 0, 0, 0, 0 };
+static char            psapath[] = _PATH_PSA;
+static char            *psaargv[] = { "psa", NULL, NULL, NULL, NULL };
 
 /*
  * If we're not doing accounting, we just call pap as below.
@@ -53,23 +65,22 @@ char                *psaargv[] = { "psa", 0, 0, 0, 0 };
  * arg 4.  The second time, we call it with "-c" in arg 2, pagecount.ps
  * in arg 3, and 0 in arg 4.
  */
-char           pappath[] = _PATH_PAP;
-char           *papargv[] = { "pap", "-sstatus", 0, 0, 0, 0, 0, 0 };
+static char            pappath[] = _PATH_PAP;
+static char            *papargv[] = { "pap", "-sstatus", NULL, NULL, NULL, NULL, NULL, NULL };
 
-char           revpath[] = _PATH_PSORDER;
-char           *revargv[] = { "psorder", "-d", 0 };
+static char            revpath[] = _PATH_PSORDER;
+static char            *revargv[] = { "psorder", "-d", NULL };
 
-char           *filtargv[] = { 0, 0, 0 };
+static char            *filtargv[] = { NULL, NULL, NULL };
 
-char           inbuf[ 1024 * 8 ];
-int            inlen;
+static char            inbuf[ 1024 * 8 ];
+static int             inlen;
 
-FILE           *acctfile = NULL;
-int            literal;
-int            width = 80, length = 66, indent = 0;
-char           *prog, *name, *host;
+static int             literal;
+static int             width = 80, length = 66, indent = 0;
+static char            *prog, *name, *host;
 
-struct papersize {
+static struct papersize {
     int                width;
     int                length;
    float       win;
@@ -79,13 +90,11 @@ struct papersize {
     { 80, 70, 8.27, 11.69 },                   /* A4 */
 };
 
-int main( ac, av ) 
-    int                ac;
-    char       **av;
+int main( int ac, char **av)
 {
     int                        c, rc, children = 0;
 #ifdef FUCKED
-    int                        psafileno, multiconn = 0, waitidle = 0, waitidle2 = 0;
+    int                        psafileno = 0, multiconn = 0, waitidle = 0, waitidle2 = 0;
 #endif /* FUCKED */
     int                        status;
     extern char                *optarg;
@@ -172,7 +181,7 @@ int main( ac, av )
 
 restart:
     if (( inlen = read( 0, inbuf, sizeof( inbuf ))) < 0 ) {
-       syslog( LOG_ERR, "read: %m" );
+       syslog( LOG_ERR, "read: %s", strerror(errno) );
        exit( 1 );
     }
     if ( inlen == 0 ) {        /* nothing to be done */
@@ -190,7 +199,7 @@ restart:
        psaargv[ 2 ] = name;
        psaargv[ 3 ] = host;
        if (( c = pexecv( psapath, psaargv )) < 0 ) {
-           syslog( LOG_ERR, "%s: %m", psapath );
+           syslog( LOG_ERR, "%s: %s", psapath, strerror(errno) );
            exit( 2 );
        }
        children++;
@@ -214,20 +223,20 @@ restart:
                    papargv[ 4 ] = "-E";
                    papargv[ 5 ] = _PATH_PAGECOUNT;
                    papargv[ 6 ] = "-";
-                   papargv[ 7 ] = 0;
+                   papargv[ 7 ] = NULL;
                } else if ( waitidle ) {
                    papargv[ 2 ] = "-w";
                    papargv[ 3 ] = "-c";
                    papargv[ 4 ] = "-E";
                    papargv[ 5 ] = _PATH_PAGECOUNT;
                    papargv[ 6 ] = "-";
-                   papargv[ 7 ] = 0;
+                   papargv[ 7 ] = NULL;
                } else {
                    papargv[ 2 ] = "-c";
                    papargv[ 3 ] = "-E";
                    papargv[ 4 ] = _PATH_PAGECOUNT;
                    papargv[ 5 ] = "-";
-                   papargv[ 6 ] = 0;
+                   papargv[ 6 ] = NULL;
                }
            } else {
                /*
@@ -237,17 +246,17 @@ restart:
                papargv[ 3 ] = _PATH_PAGECOUNT;
                papargv[ 4 ] = "-";
                papargv[ 5 ] = _PATH_PAGECOUNT;
-               papargv[ 6 ] = 0;
+               papargv[ 6 ] = NULL;
            }
 #endif /* FUCKED */
        } else {
            papargv[ 2 ] = "-c";
            papargv[ 3 ] = "-E";
-           papargv[ 4 ] = 0;
+           papargv[ 4 ] = NULL;
        }
 
        if (( c = pexecv( pappath, papargv )) < 0 ) {
-           syslog( LOG_ERR, "%s: %m", pappath );
+           syslog( LOG_ERR, "%s: %s", pappath, strerror(errno) );
            exit( 2 );
        }
        children++;
@@ -260,7 +269,7 @@ restart:
      */
     if ( strstr( prog, "rev" ) != NULL ) {
        if (( c = pexecv( revpath, revargv )) < 0 ) {
-           syslog( LOG_ERR, "%s: %m", revpath );
+           syslog( LOG_ERR, "%s: %s", revpath, strerror(errno) );
            exit( 2 );
        }
        syslog( LOG_INFO, "sending to rev[%d]", c );
@@ -274,7 +283,7 @@ restart:
     if ( *prog != 'i' && *prog != 'o' && *( prog + 1 ) == 'f' ) {
        filtargv[ 0 ] = filtargv[ 1 ] = prog;
        if (( c = pexecv( _PATH_PSFILTER, filtargv )) < 0 ) {
-           syslog( LOG_ERR, "%s: %m", _PATH_PSFILTER );
+           syslog( LOG_ERR, "%s: %s", _PATH_PSFILTER, strerror(errno) );
            exit( 2 );
        }
        syslog( LOG_INFO, "external filter[%d]", c );
@@ -301,18 +310,18 @@ restart:
        if ( waitidle2 ) {
            papargv[ 3 ] = "-W";
            papargv[ 4 ] = _PATH_PAGECOUNT;
-           papargv[ 5 ] = 0;
+           papargv[ 5 ] = NULL;
        } else if ( waitidle ) {
            papargv[ 3 ] = "-w";
            papargv[ 4 ] = _PATH_PAGECOUNT;
-           papargv[ 5 ] = 0;
+           papargv[ 5 ] = NULL;
        } else {
            papargv[ 3 ] = _PATH_PAGECOUNT;
-           papargv[ 4 ] = 0;
+           papargv[ 4 ] = NULL;
        }
 
        if (( c = pexecv( pappath, papargv )) < 0 ) {
-           syslog( LOG_ERR, "%s: %m", pappath );
+           syslog( LOG_ERR, "%s: %s", pappath, strerror(errno) );
            exit( 2 );
        }
        children++;
@@ -324,8 +333,8 @@ restart:
        close( 1 );
     }
     while ( children ) {
-       if (( c = wait3( &status, 0, 0 )) < 0 ) {
-           syslog( LOG_ERR, "wait3: %m" );
+       if (( c = wait3( &status, 0, NULL )) < 0 ) {
+           syslog( LOG_ERR, "wait3: %s", strerror(errno) );
            exit( 1 );
        }
        if ( WIFEXITED( status )) {
@@ -356,7 +365,7 @@ restart:
     exit( rc );
 }
 
-int copyio()
+int copyio(void)
 {
     /* implement the FSM needed to do the suspend. Note that
      * the last characters will be \031\001 so don't worry
@@ -384,7 +393,7 @@ notdone:
                    break;
                }
                if ( write( 1, "\031", 1 ) != 1 ) {
-                   syslog( LOG_ERR, "write: %m" );
+                   syslog( LOG_ERR, "write: %s", strerror(errno) );
                    return( 1 );
                }
                ctl = 0;
@@ -397,7 +406,7 @@ notdone:
        } else {
            if ( ctl == 1 ) {
                if ( write( 1, "\031", 1 ) != 1 ) {
-                   syslog( LOG_ERR, "write: %m" );
+                   syslog( LOG_ERR, "write: %s", strerror(errno) );
                    return( 1 );
                }
            }
@@ -412,7 +421,7 @@ notdone:
 
        inlen -= ctl;
        if (( inlen > 0 ) && ( write( 1, inbuf, inlen ) != inlen )) {
-           syslog( LOG_ERR, "write: %m" );
+           syslog( LOG_ERR, "write: %s", strerror(errno) );
            return( 1 );
        }
        if ( ctl == 2 ) {
@@ -433,13 +442,13 @@ notdone:
     }
 
     if ( inlen < 0 ) {
-       syslog( LOG_ERR, "read: %m" );
+       syslog( LOG_ERR, "read: %s", strerror(errno) );
        return( 1 );
     }
 
     if ( ctl == 1 ) {
        if ( write( 1, "\031", 1 ) != 1 ) {
-           syslog( LOG_ERR, "write: %m" );
+           syslog( LOG_ERR, "write: %s", strerror(errno) );
            return( 1 );
        }
     } else if ( ctl == 2 ) {
@@ -448,10 +457,10 @@ notdone:
     return( 0 );
 }
 
-char           *font = "Courier";
-int            point = 11;
+static char            *font = "Courier";
+static int             point = 11;
 
-char           pspro[] = "\
+static char            pspro[] = "\
 /GSV save def                                          % global VM\n\
 /SP {\n\
        /SV save def                                    % save vmstate\n\
@@ -470,10 +479,11 @@ char              pspro[] = "\
 /EP { SV restore showpage } bind def\n\
 %%EndProlog\n";
 
-int textps()
+int textps(void)
 {
     struct papersize   papersize;
-    int                        state = 0, line = 0, col = 0, npages = 0, rc, i;
+    int                        state = 0, line = 0, col = 0, npages = 0, rc;
+    unsigned int       i;
     char               *p, *end;
 
 #define elements(x)    (sizeof(x)/sizeof((x)[0]))
@@ -509,7 +519,7 @@ int textps()
                    /* output postscript prologue: */
                    if ( write( 1, pspro, sizeof( pspro ) - 1 ) !=
                            sizeof( pspro ) - 1 ) {
-                       syslog( LOG_ERR, "write prologue: %m" );
+                       syslog( LOG_ERR, "write prologue: %s", strerror(errno) );
                        return( 1 );
                    }
                    if ( name && host ) {
@@ -524,12 +534,24 @@ int textps()
                state |= ST_PAGE;
            }
            if ( state & ST_CONTROL && *p != '\001' ) {
+               /* It is a very bad thing to toss a job because it contains
+                * unprintable characters.  Instead, we will convert them to
+                * question marks.  This is adapted from a solution described
+                * by Werner Eugster <eugster@giub.unibe.ch> on his ApplePrint
+                * webpage (http://www.giub.unibe.ch/~eugster/appleprint.html).
+                *
+                * Note that this is rather ugly code.  The same change is
+                * applied identically at two different locations in this file.
+                * It would be better someday to combine the two.
+                */
                if ( !literal ) {
-                   fprintf( stderr, "unprintable character (0x%x)!\n",
-                           (unsigned char)031 );
-                   return( 2 );        /* Toss job */
+                       fprintf( stderr,
+                               "unprintable character (0x%x) converted to ?!\n",
+                               (unsigned char)*p );
+                       putchar( '?' ); /* Replace unprintable char with a question mark. */
+               } else {
+                       printf( "\\%o", (unsigned char)031 );
                }
-               printf( "\\%o", (unsigned char)031 );
                state &= ~ST_CONTROL;
                col++;
            }
@@ -619,11 +641,13 @@ int textps()
                }
                if ( !isascii( *p ) || !isprint( *p )) {
                    if ( !literal ) {
-                       fprintf( stderr, "unprintable character (0x%x)!\n",
-                               (unsigned char)*p );
-                       return( 2 );    /* Toss job */
+                        fprintf( stderr,
+                           "unprintable character (0x%x) converted to ?!\n",
+                           (unsigned char)*p );
+                       putchar( '?' ); /* Replace unprintable char with a question mark. */
+                   } else {
+                       printf( "\\%o", (unsigned char)*p );
                    }
-                   printf( "\\%o", (unsigned char)*p );
                } else {
                    putchar( *p );
                }
@@ -634,7 +658,7 @@ int textps()
        }
     } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
     if ( inlen < 0 ) {
-       syslog( LOG_ERR, "read: %m" );
+       syslog( LOG_ERR, "read: %s", strerror(errno) );
        return( 1 );
     }
     rc = 0;
@@ -664,8 +688,7 @@ out:
  * Manipulates file descriptors 0, 1, and 2, such that the new child
  * is reading from the parent's output.
  */
-int pexecv( path, argv )
-    char       *path, *argv[];
+int pexecv( char *path, char *argv[])
 {
     int                fd[ 2 ], c;