]> arthur.barton.de Git - netatalk.git/blobdiff - bin/psorder/psorder.c
Remove bdb env on exit
[netatalk.git] / bin / psorder / psorder.c
index 994dfa2552b0f656856f0905d6b442ba950bfc4b..4a37bab579fc59dfd8124ecfda4b6a33e0027c78 100644 (file)
@@ -1,4 +1,6 @@
 /*
+ * $Id: psorder.c,v 1.10 2010-04-12 14:28:47 franklahm Exp $
+ *
  * Copyright (c) 1990,1991 Regents of The University of Michigan.
  * All Rights Reserved.
  *
@@ -23,7 +25,7 @@
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
+#endif /* HAVE_CONFIG_H */
 
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/uio.h>
 #include <sys/file.h>
 #include <ctype.h>
+#include <limits.h>
+#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
+#endif /* HAVE_FCNTL_H */
 #include <stdio.h>
 #include <string.h>
 #include <dirent.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif /* HAVE_UNISTD_H */
 #include "pa.h"
 #include "psorder.h"
 
 #include <atalk/paths.h>
 
-#define        DEBUG           0
-
 /*
  *                     Global Variables
  */
 
-u_char                 psbuf[ 8192 ];
-struct psinfo_st       psinfo;
-int                    orderflag, forceflag;
+static u_char                  psbuf[ 8192 ];
+static struct psinfo_st                psinfo;
+static int                     orderflag, forceflag;
 
-main( argc, argv )
-    int                argc;
-    char       **argv;
+static void
+filecleanup( int errorcode, int tfd, char *tfile)
 {
-    extern int optind;
-    char       *progname;
-    int                errflag = 0;
-    int                c;
 
-    while (( c = getopt( argc, argv, OPTSTR )) != -1 ) {
-       switch ( c ) {
-       case REVCHAR:
-           if ( orderflag ) errflag++;
-           else orderflag = REVERSE;
-           break;
-       case FORWCHAR:
-           if ( orderflag ) errflag++;
-           else orderflag = FORWARD;
-           break;
-       case FORCECHAR:
-           if ( forceflag ) errflag++;
-           else forceflag++;
-           break;
-       }
-    }
-    if ( errflag ) {
-       if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
-           progname = argv[ 0 ];
-       } else progname++;
-       fprintf( stderr, "usage: %s [-duf] [sourcefile]\n", progname );
-       return( -1 );
-    } else if ( !orderflag ) orderflag = FORWARD;
+/*
+       Close and unlink the temporary file.
+ */
 
-    if ( optind >= argc ) {
-       return( psorder( STDIN ));
+    if ( tfd != 0 ) {
+       if ( close( tfd ) != 0 ) {
+           perror( tfile );
+           exit( errorcode );
+       }
+       if ( unlink( tfile ) != 0 ) {
+           perror( tfile );
+           exit( errorcode );
+       }
     }
-    return( psorder( argv[ optind ] ));
-}
-
-int
-psorder( path )
-    char       *path;
-{
-    int                        tempfd;
-    int                        inputfd;
-    char               tempfile[MAXNAMLEN];
 
-    filesetup( path, &inputfd, tempfile, &tempfd );
-    readps( inputfd, tempfd, tempfile );
-    if ( lseek( tempfd, REWIND, SEEK_SET ) < 0 ) {
-       perror( tempfile );
-       filecleanup( -1, tempfd, tempfile );
-    }
-    writeps( tempfd, tempfile );
-    filecleanup( 0, tempfd, tempfile );
-    return( 0 );
+    exit( errorcode );
 }
 
-void
-filesetup( inputfile, infd, tfile, tfd )
-    char       *inputfile;
-    int                *infd;
-    char       *tfile;
-    int                *tfd;
+static void
+filesetup( char *inputfile, int *infd, char *tfile, int *tfd)
 {
     struct stat                st;
     char               *template = _PATH_TMPPAGEORDER;
@@ -141,13 +106,17 @@ filesetup( inputfile, infd, tfile, tfd )
 #if DEBUG
     fprintf( stderr, "Input file or stdin and stdout opened.\n" );
     fprintf( stderr, "Input file descriptor is %d .\n", *infd );
-#endif
+#endif /* DEBUG */
 
 /*
        make temporary file
  */
 
+#if defined(NAME_MAX)
+    (void *)strncpy( tfile, template, NAME_MAX );
+#else
     (void *)strncpy( tfile, template, MAXNAMLEN );
+#endif
     if (( *tfd = mkstemp( tfile )) == -1 ) {
        fprintf( stderr, "can't create temporary file %s\n", tfile );
        filecleanup( -1, -1, "" );
@@ -156,7 +125,7 @@ filesetup( inputfile, infd, tfile, tfd )
 #if DEBUG
     fprintf( stderr, "Temporary file %s created and opened.\n", tfile );
     fprintf( stderr, "Temporary file descriptor is %d .\n", *tfd );
-#endif
+#endif /* DEBUG */
 
     psinfo.firstpage = NULL;
     psinfo.lastpage = NULL;
@@ -169,15 +138,115 @@ filesetup( inputfile, infd, tfile, tfd )
     return;
 }
 
-void
-readps( inputfd, tempfd, tempfile )
-    int                        inputfd;
-    int                        tempfd;
-    char               *tempfile;
+static struct pspage_st
+*getpspage(off_t off)
+{
+    struct pspage_st   *newpspage;
+
+    newpspage = (struct pspage_st *)malloc( sizeof( struct pspage_st )); 
+    if ( newpspage != NULL ) {
+       newpspage->offset = off;
+       newpspage->nextpage = NULL;
+       *newpspage->lable = '\0';
+       *newpspage->ord = '\0';
+    }
+    return( newpspage );
+}
+
+static int
+handletok(off_t count, char *token)
+{
+    int                        incdoc = 0;
+    struct pspage_st   *newpage;
+    char               *tmp;
+
+    if (( strncmp( PENDDOC, token, strlen( PENDDOC )) == 0 ) && incdoc ) {
+       incdoc--;
+#if DEBUG
+       fprintf( stderr, "found an EndDoc\n" );
+#endif /* DEBUG */
+
+    } else if ( strncmp( PBEGINDOC, token, strlen( PBEGINDOC )) == 0 ) {
+       incdoc++;
+#if DEBUG
+       fprintf( stderr, "found a BeginDoc\n" );
+#endif /* DEBUG */
+
+    } else if ( !incdoc && 
+           ( strncmp( PPAGE, token, strlen( PPAGE )) == 0 )) {
+#if DEBUG
+       fprintf( stderr, "found a Page\n" );
+#endif /* DEBUG */
+       if (( newpage = getpspage( count )) == NULL ) {
+           return( -1 );
+       }
+       if ( psinfo.firstpage == NULL ) {
+           newpage->prevpage = NULL;
+           psinfo.firstpage = newpage;
+       } else {
+           newpage->prevpage = psinfo.lastpage;
+           psinfo.lastpage->nextpage = newpage;
+       }
+       psinfo.lastpage = newpage;
+       while ( *token++ != ':' );
+       if (( tmp = strtok( token, WHITESPACE )) != NULL ) {
+           (void)strncpy( newpage->lable, tmp, NUMLEN );
+           if (( tmp = strtok( NULL, WHITESPACE )) != NULL ) {
+               (void)strncpy( newpage->ord, tmp, ORDLEN );
+           }
+       }
+#if DEBUG
+       fprintf( stderr, "page lable %s, page ord %s\n", newpage->lable,
+               newpage->ord );
+#endif /* DEBUG */
+
+    } else if ( !incdoc && 
+           ( strncmp( PPAGES, token, strlen( PPAGES )) == 0 )) {
+#if DEBUG
+       fprintf( stderr, "found a Pages\n" );
+#endif /* DEBUG */
+       psinfo.pages.offset = count;
+       psinfo.pages.end = strlen( token ) + count;
+       while ( *token++ != ':' );
+       while ( isspace( *token )) token++;
+       if ( strncmp( ATEND, token, strlen( ATEND )) == 0 ) {
+#if DEBUG
+           fprintf( stderr, "it is a Pages: (atend)\n" );
+#endif /* DEBUG */
+           psinfo.pages.offset = 0;
+           psinfo.pages.end = 0;
+       } else {
+           if (( tmp = strtok( token, WHITESPACE )) != NULL ) {
+               (void)strncpy( psinfo.pages.num, tmp, NUMLEN );
+               if (( tmp = strtok( NULL, WHITESPACE )) != NULL ) {
+                   (void)strncpy( psinfo.pages.order, tmp, ORDERLEN );
+               }
+           }
+#if DEBUG
+           fprintf( stderr, "number of pages %s\n", psinfo.pages.num );
+           fprintf( stderr, "order control number %s\n", psinfo.pages.order );
+#endif /* DEBUG */
+       }
+
+    } else if ( !incdoc && 
+           ( strncmp( PTRAILER, token, strlen( PTRAILER )) == 0 )) {
+#if DEBUG
+       fprintf( stderr, "found the Trailer\n" );
+#endif /* DEBUG */
+       if  ( psinfo.trailer == 0 ) {
+           psinfo.trailer = count;
+       }
+    }
+
+    return( 0 );
+}
+
+static void
+readps(int inputfd, int tempfd, char *tempfile)
 {
     off_t              ccread = 0;
     off_t              ccmatch;
-    char               *curtok = 0;
+    char               *curtok = NULL;
     FILE               *tempstream;
     pa_buf_t           *pb;
     int                        n;
@@ -207,7 +276,7 @@ readps( inputfd, tempfd, tempfile )
     }
 #if DEBUG
     fprintf( stderr, "%s\n", curtok );
-#endif
+#endif /* DEBUG */
 
 /*
  * not postscript
@@ -215,7 +284,7 @@ readps( inputfd, tempfd, tempfile )
     if ( strcmp( curtok, PPSADOBE ) != 0 ) {
 #if DEBUG
     fprintf( stderr, "in the not postscript section of readps\n" );
-#endif
+#endif /* DEBUG */
        while (( c = pa_getchar( pb )) != 0 ) {
            ccread++;
            (void)putc( c, tempstream );
@@ -232,7 +301,7 @@ readps( inputfd, tempfd, tempfile )
  */
 #if DEBUG
     fprintf( stderr, "in the postscript section of readps\n" );
-#endif
+#endif /* DEBUG */
     while (( c = pa_getchar( pb )) != 0 ) {
        ccread++;
        (void)putc( c, tempstream );
@@ -241,10 +310,10 @@ readps( inputfd, tempfd, tempfile )
        if ((( pc == '\r' ) || ( pc == '\n' )) && ( cc == '%' )) {
 #if DEBUG
            fprintf( stderr, "supposed start of match, cc = %c\n", cc );
-#endif
+#endif /* DEBUG */
            pa_match( pb );
            ccmatch = ccread - 1;
-           while ( c = pa_getchar( pb )) {
+           while ( ( c = pa_getchar( pb ) ) ) {
                if ( c != 0 ) {
                    ccread++;
                    (void)putc( c, tempstream );
@@ -255,7 +324,7 @@ readps( inputfd, tempfd, tempfile )
                    curtok = pa_gettok( pb );
 #if DEBUG
                    fprintf( stderr, "%s\n", curtok );
-#endif
+#endif /* DEBUG */
                    if ( handletok( ccmatch, curtok ) < 0 ) {
                        perror( "malloc died" );
                        filecleanup( -1, tempfd, tempfile );
@@ -272,100 +341,66 @@ readps( inputfd, tempfd, tempfile )
     return;
 }
 
-int
-handletok( count, token )
-    off_t              count;
-    char               *token;
+static void
+temp2out(int tempfd, char *tempfile, off_t length)
 {
-    int                        incdoc = 0;
-    struct pspage_st   *newpage;
-    char               *tmp;
-
-    if (( strncmp( PENDDOC, token, strlen( PENDDOC )) == 0 ) && incdoc ) {
-       incdoc--;
-#if DEBUG
-       fprintf( stderr, "found an EndDoc\n" );
-#endif
-
-    } else if ( strncmp( PBEGINDOC, token, strlen( PBEGINDOC )) == 0 ) {
-       incdoc++;
-#if DEBUG
-       fprintf( stderr, "found a BeginDoc\n" );
-#endif
-
-    } else if ( !incdoc && 
-           ( strncmp( PPAGE, token, strlen( PPAGE )) == 0 )) {
-#if DEBUG
-       fprintf( stderr, "found a Page\n" );
-#endif
-       if (( newpage = getpspage( count )) == NULL ) {
-           return( -1 );
-       }
-       if ( psinfo.firstpage == NULL ) {
-           newpage->prevpage = NULL;
-           psinfo.firstpage = newpage;
-       } else {
-           newpage->prevpage = psinfo.lastpage;
-           psinfo.lastpage->nextpage = newpage;
-       }
-       psinfo.lastpage = newpage;
-       while ( *token++ != ':' );
-       if (( tmp = strtok( token, WHITESPACE )) != NULL ) {
-           (void)strncpy( newpage->lable, tmp, NUMLEN );
-           if (( tmp = strtok( NULL, WHITESPACE )) != NULL ) {
-               (void)strncpy( newpage->ord, tmp, ORDLEN );
-           }
-       }
-#if DEBUG
-       fprintf( stderr, "page lable %s, page ord %s\n", newpage->lable,
-               newpage->ord );
-#endif
+    int                        ccread;
+    int                        ccwrite;
+    int                        size;
 
-    } else if ( !incdoc && 
-           ( strncmp( PPAGES, token, strlen( PPAGES )) == 0 )) {
-#if DEBUG
-       fprintf( stderr, "found a Pages\n" );
-#endif
-       psinfo.pages.offset = count;
-       psinfo.pages.end = strlen( token ) + count;
-       while ( *token++ != ':' );
-       while ( isspace( *token )) token++;
-       if ( strncmp( ATEND, token, strlen( ATEND )) == 0 ) {
-#if DEBUG
-           fprintf( stderr, "it is a Pages: (atend)\n" );
-#endif
-           psinfo.pages.offset = 0;
-           psinfo.pages.end = 0;
-       } else {
-           if (( tmp = strtok( token, WHITESPACE )) != NULL ) {
-               (void)strncpy( psinfo.pages.num, tmp, NUMLEN );
-               if (( tmp = strtok( NULL, WHITESPACE )) != NULL ) {
-                   (void)strncpy( psinfo.pages.order, tmp, ORDERLEN );
+    while ( length > 0 ) {
+       if ( length > sizeof( psbuf )) {
+           size = sizeof( psbuf );
+       } else size = length;
+       if (( ccread = read( tempfd, psbuf, size )) > 0 ) {
+           size = ccread;
+           while ( ccread > 0 ) {
+               ccwrite = write( 1, psbuf, ccread );
+               if ( ccwrite < 0 ) {
+                   perror( "stdout" );
+                   filecleanup( ccwrite, tempfd, tempfile );
+               } else {
+                   ccread -= ccwrite;
                }
            }
-#if DEBUG
-           fprintf( stderr, "number of pages %s\n", psinfo.pages.num );
-           fprintf( stderr, "order control number %s\n", psinfo.pages.order );
-#endif
        }
-
-    } else if ( !incdoc && 
-           ( strncmp( PTRAILER, token, strlen( PTRAILER )) == 0 )) {
-#if DEBUG
-       fprintf( stderr, "found the Trailer\n" );
-#endif
-       if  ( psinfo.trailer == 0 ) {
-           psinfo.trailer = count;
+       if ( ccread < 0 ) {
+           perror( "temporary file" );
+           filecleanup( ccread, tempfd, tempfile );
        }
+       length -= size;
     }
+}
 
-    return( 0 );
+static void
+writelable(int tempfd, char *tempfile, char *lable)
+{
+    char               line[256];
+    int                        ccwrite;
+    int                        linelen;
+    char               *argone;
+    char               *argtwo;
+
+    if ( strcmp( lable, PPAGES ) == 0 ) {
+       argone = psinfo.pages.num;
+       argtwo = psinfo.pages.order;
+    } else {
+       argone = argtwo = NULL;
+    }
+    (void)sprintf( line, "%s %s %s", lable, argone, argtwo );
+    linelen = strlen( line );
+
+    ccwrite = write( 1, line, linelen );
+    if ( ccwrite < 0 ) {
+       perror( "stdout" );
+       filecleanup( ccwrite, tempfd, tempfile );
+    } else {
+       linelen -= ccwrite;
+    }
 }
 
-void
-writeps( tempfd, tempfile )
-    int                        tempfd;
-    char               *tempfile;
+static void
+writeps(int tempfd, char *tempfile)
 {
     struct stat                st;
     off_t              endofpage;
@@ -459,107 +494,62 @@ writeps( tempfd, tempfile )
     return;
 }
 
-void
-writelable( tempfd, tempfile, lable )
-    int                        tempfd;
-    char               *tempfile;
-    char               *lable;
+static int
+psorder(char *path)
 {
-    char               line[256];
-    int                        ccwrite;
-    int                        linelen;
-    char               *argone;
-    char               *argtwo;
-
-    if ( strcmp( lable, PPAGES ) == 0 ) {
-       argone = psinfo.pages.num;
-       argtwo = psinfo.pages.order;
-    } else {
-       argone = argtwo = NULL;
-    }
-    (void)sprintf( line, "%s %s %s", lable, argone, argtwo );
-    linelen = strlen( line );
+    int                        tempfd;
+    int                        inputfd;
+#if defined(NAME_MAX)
+    char               tempfile[NAME_MAX];
+#else
+    char               tempfile[MAXNAMLEN];
+#endif
 
-    ccwrite = write( 1, line, linelen );
-    if ( ccwrite < 0 ) {
-       perror( "stdout" );
-       filecleanup( ccwrite, tempfd, tempfile );
-    } else {
-       linelen -= ccwrite;
+    filesetup( path, &inputfd, tempfile, &tempfd );
+    readps( inputfd, tempfd, tempfile );
+    if ( lseek( tempfd, REWIND, SEEK_SET ) < 0 ) {
+       perror( tempfile );
+       filecleanup( -1, tempfd, tempfile );
     }
+    writeps( tempfd, tempfile );
+    filecleanup( 0, tempfd, tempfile );
+    return( 0 );
 }
 
-void
-temp2out( tempfd, tempfile, length )
-    int                        tempfd;
-    char               *tempfile;
-    off_t              length;
+int main(int argc, char **argv)
 {
-    int                        ccread;
-    int                        ccwrite;
-    int                        size;
+    extern int optind;
+    char       *progname;
+    int                errflag = 0;
+    int                c;
 
-    while ( length > 0 ) {
-       if ( length > sizeof( psbuf )) {
-           size = sizeof( psbuf );
-       } else size = length;
-       if (( ccread = read( tempfd, psbuf, size )) > 0 ) {
-           size = ccread;
-           while ( ccread > 0 ) {
-               ccwrite = write( 1, psbuf, ccread );
-               if ( ccwrite < 0 ) {
-                   perror( "stdout" );
-                   filecleanup( ccwrite, tempfd, tempfile );
-               } else {
-                   ccread -= ccwrite;
-               }
-           }
-       }
-       if ( ccread < 0 ) {
-           perror( "temporary file" );
-           filecleanup( ccread, tempfd, tempfile );
+    while (( c = getopt( argc, argv, OPTSTR )) != -1 ) {
+       switch ( c ) {
+       case REVCHAR:
+           if ( orderflag ) errflag++;
+           else orderflag = REVERSE;
+           break;
+       case FORWCHAR:
+           if ( orderflag ) errflag++;
+           else orderflag = FORWARD;
+           break;
+       case FORCECHAR:
+           if ( forceflag ) errflag++;
+           else forceflag++;
+           break;
        }
-       length -= size;
     }
-}
-
-struct pspage_st
-*getpspage( off )
-    off_t              off;
-{
-    struct pspage_st   *newpspage;
+    if ( errflag ) {
+       if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
+           progname = argv[ 0 ];
+       } else progname++;
+       fprintf( stderr, "usage: %s [-duf] [sourcefile]\n", progname );
+       return( -1 );
+    } else if ( !orderflag ) orderflag = FORWARD;
 
-    newpspage = (struct pspage_st *)malloc( sizeof( struct pspage_st )); 
-    if ( newpspage != NULL ) {
-       newpspage->offset = off;
-       newpspage->nextpage = NULL;
-       *newpspage->lable = '\0';
-       *newpspage->ord = '\0';
+    if ( optind >= argc ) {
+       return( psorder( STDIN ));
     }
-    return( newpspage );
+    return( psorder( argv[ optind ] ));
 }
 
-void
-filecleanup( errorcode, tfd, tfile )
-    int                        errorcode;
-    int                        tfd;
-    char               *tfile;
-{
-
-/*
-       Close and unlink the temporary file.
- */
-
-    if ( tfd != 0 ) {
-       if ( close( tfd ) != 0 ) {
-           perror( tfile );
-           exit( errorcode );
-       }
-       if ( unlink( tfile ) != 0 ) {
-           perror( tfile );
-           exit( errorcode );
-       }
-    }
-
-    exit( errorcode );
-}