about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2007-12-27 22:31:01 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2007-12-27 22:31:01 +0000
commitdcde1624d79c372394e65c99771fe64a28a82ee5 (patch)
tree6ef16add2693ae8d966c79bbdc570fc2a3c0acdb /lib
parentb9310d8e36427001a9e67fafa2462616d1f6bfff (diff)
downloadnetpbm-mirror-dcde1624d79c372394e65c99771fe64a28a82ee5.tar.gz
netpbm-mirror-dcde1624d79c372394e65c99771fe64a28a82ee5.tar.xz
netpbm-mirror-dcde1624d79c372394e65c99771fe64a28a82ee5.zip
Release 10.41.00
git-svn-id: http://svn.code.sf.net/p/netpbm/code/advanced@499 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile10
-rw-r--r--lib/bitio.h4
-rw-r--r--lib/colorname.h2
-rw-r--r--lib/fileio.c12
-rw-r--r--lib/libpam.h2
-rw-r--r--lib/libpbm1.c28
-rw-r--r--lib/libpbm2.c25
-rw-r--r--lib/libpbm3.c1
-rw-r--r--lib/libpgm1.c5
-rw-r--r--lib/libpgm2.c1
-rw-r--r--lib/libpm.c972
-rw-r--r--lib/libpnm2.c3
-rw-r--r--lib/libpnm3.c3
-rw-r--r--lib/libppm2.c1
-rw-r--r--lib/libppmcmap.c1
-rw-r--r--lib/pam.h4
-rw-r--r--lib/pammap.h4
-rw-r--r--lib/pbm.h11
-rw-r--r--lib/pbmfont.h2
-rw-r--r--lib/pgm.h5
-rw-r--r--lib/pm.h19
-rw-r--r--lib/pm_gamma.h2
-rw-r--r--lib/pmfileio.c948
-rw-r--r--lib/pnm.h9
-rw-r--r--lib/ppm.h7
-rw-r--r--lib/ppmcmap.h40
-rw-r--r--lib/util/Makefile2
-rw-r--r--lib/util/mallocvar.h2
-rw-r--r--lib/util/nstring.c40
-rw-r--r--lib/util/nstring.h5
30 files changed, 1190 insertions, 980 deletions
diff --git a/lib/Makefile b/lib/Makefile
index be960250..34547e7a 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -25,7 +25,7 @@ else
   LIBSYSTEM = libsystem.o
 endif
 
-LIBOBJECTS = libpm.o fileio.o bitio.o colorname.o \
+LIBOBJECTS = libpm.o pmfileio.o fileio.o bitio.o colorname.o \
 	libpbm1.o libpbm2.o libpbm3.o libpbmfont.o \
 	libpgm1.o libpgm2.o \
 	libppm1.o libppm2.o libppmcmap.o libppmcolor.o libppmfuzzy.o \
@@ -48,7 +48,7 @@ MANUALS5 = pbm pgm ppm pnm pam
 INTERFACE_HEADERS =  pm.h pbm.h bitio.h pbmfont.h \
 	pgm.h ppm.h ppmcmap.h ppmfloyd.h colorname.h \
 	pnm.h pam.h pammap.h util/shhopt.h util/nstring.h util/mallocvar.h \
-	pm_system.h pm_gamma.h
+	pm_system.h pm_gamma.h ppmdraw.h ppmdfont.h \
 
 DATAFILES = rgb.txt
 
@@ -240,9 +240,11 @@ install.hdr: $(INTERFACE_HEADERS:%=%_installhdr)
 # prefer not to "install" them, but just to access the Netpbm source
 # directory when you compile your programs.
 
-%_installhdr: $(PKGDIR)/include
+.PHONY: $(INTERFACE_HEADERS:%=%_installhdr)
+
+$(INTERFACE_HEADERS:%=%_installhdr): $(PKGDIR)/include/netpbm
 	$(INSTALL) -c -m $(INSTALL_PERM_HDR) \
-	  $(SRCDIR)/lib/$(@:%_installhdr=%) $(PKGDIR)/include/;
+	  $(SRCDIR)/lib/$(@:%_installhdr=%) $(PKGDIR)/include/netpbm/
 
 .PHONY: install.staticlib
 install.staticlib: $(PKGDIR)/link
diff --git a/lib/bitio.h b/lib/bitio.h
index 15fe0e8a..dfc5a153 100644
--- a/lib/bitio.h
+++ b/lib/bitio.h
@@ -32,7 +32,7 @@
 #ifndef _BITIO_H_
 #define _BITIO_H_
 
-#include "pm.h"
+#include <netpbm/pm.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -86,4 +86,4 @@ pm_bitwrite(BITSTREAM     b,
 #ifdef __cplusplus
 }
 #endif
-#endif /* _BITIO_H_ */
+#endif
diff --git a/lib/colorname.h b/lib/colorname.h
index d33980e4..74583144 100644
--- a/lib/colorname.h
+++ b/lib/colorname.h
@@ -3,7 +3,7 @@
 
 #include <string.h>
 #include <stdio.h>
-#include "ppm.h"
+#include <netpbm/ppm.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/lib/fileio.c b/lib/fileio.c
index d891b05a..300ae303 100644
--- a/lib/fileio.c
+++ b/lib/fileio.c
@@ -85,10 +85,18 @@ pm_getuint(FILE * const ifP) {
     do {
         unsigned int const digitVal = ch - '0';
 
-        if (i > INT_MAX/10 - digitVal)
+        if (i > INT_MAX/10)
             pm_error("ASCII decimal integer in file is "
                      "too large to be processed.  ");
-        i = i * 10 + digitVal;
+        
+        i *= 10;
+
+        if (i > INT_MAX - digitVal)
+            pm_error("ASCII decimal integer in file is "
+                     "too large to be processed.  ");
+
+        i += digitVal;
+
         ch = pm_getc(ifP);
     } while (ch >= '0' && ch <= '9');
 
diff --git a/lib/libpam.h b/lib/libpam.h
index 4b8e6693..9f8a34d0 100644
--- a/lib/libpam.h
+++ b/lib/libpam.h
@@ -4,7 +4,7 @@
 #ifndef LIBPAM_H_INCLUDED
 #define LIBPAM_H_INCLUDED
 
-#include "pam.h"
+#include "pgm.h"
 
 void
 pnm_readpaminitrestaspnm(FILE * const fileP, 
diff --git a/lib/libpbm1.c b/lib/libpbm1.c
index 8dd491a7..c559c373 100644
--- a/lib/libpbm1.c
+++ b/lib/libpbm1.c
@@ -17,13 +17,33 @@
 #define _LARGE_FILES  
 
 #include <stdio.h>
-#include "pbm.h"
-#include "libpbm.h"
+
+#include "mallocvar.h"
 #include "shhopt.h"
+#include "pbm.h"
+
+
+
+bit *
+pbm_allocrow(unsigned int const cols) {
+
+    bit * bitrow;
+
+    MALLOCARRAY(bitrow, cols);
+
+    if (bitrow == NULL)
+        pm_error("Unable to allocate space for a %u-column bit row", cols);
+
+    return bitrow;
+}
+
+
 
 void
-pbm_init(int *argcP, char *argv[]) {
-    pm_proginit(argcP, argv);
+pbm_init(int *   const argcP,
+         char ** const argv) {
+
+    pm_proginit(argcP, (const char **)argv);
 }
 
 
diff --git a/lib/libpbm2.c b/lib/libpbm2.c
index 19ca93b3..df1443a3 100644
--- a/lib/libpbm2.c
+++ b/lib/libpbm2.c
@@ -10,6 +10,8 @@
 ** implied warranty.
 */
 
+#include <limits.h>
+
 #include "pbm.h"
 #include "libpbm.h"
 #include "fileio.h"
@@ -57,6 +59,28 @@ pbm_readpbminitrest( file, colsP, rowsP )
 
 
 
+static void
+validateComputableSize(unsigned int const cols,
+                       unsigned int const rows) {
+/*----------------------------------------------------------------------------
+   Validate that the dimensions of the image are such that it can be
+   processed in typical ways on this machine without worrying about
+   overflows.  Note that in C, arithmetic is always modulus
+   arithmetic, so if your values are too big, the result is not what
+   you expect.  That failed expectation can be disastrous if you use
+   it to allocate memory.
+
+   A common operation is adding 1 or 2 to the highest row or
+   column number in the image, so we make sure that's possible.
+-----------------------------------------------------------------------------*/
+    if (cols > INT_MAX - 2)
+        pm_error("image width (%u) too large to be processed", cols);
+    if (rows > INT_MAX - 2)
+        pm_error("image height (%u) too large to be processed", rows);
+}
+
+
+
 void
 pbm_readpbminit(FILE * const ifP,
                 int *  const colsP,
@@ -88,6 +112,7 @@ pbm_readpbminit(FILE * const ifP,
     default:
         pm_error("bad magic number - not a Netpbm file");
     }
+    validateComputableSize(*colsP, *rowsP);
 }
 
 
diff --git a/lib/libpbm3.c b/lib/libpbm3.c
index 2a811748..5759e65d 100644
--- a/lib/libpbm3.c
+++ b/lib/libpbm3.c
@@ -11,7 +11,6 @@
 */
 
 #include "pbm.h"
-#include "libpbm.h"
 #include "bitreverse.h"
 
 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 301) && defined (__SSE__)
diff --git a/lib/libpgm1.c b/lib/libpgm1.c
index 34cc6392..ff5b711b 100644
--- a/lib/libpgm1.c
+++ b/lib/libpgm1.c
@@ -50,13 +50,14 @@ void
 pgm_init(int *   const argcP,
          char ** const argv) {
 
-    pbm_init( argcP, argv );
+    pbm_init(argcP, argv);
 }
 
 
 
 void
-pgm_nextimage(FILE * const file, int * const eofP) {
+pgm_nextimage(FILE * const file,
+              int *  const eofP) {
     pm_nextimage(file, eofP);
 }
 
diff --git a/lib/libpgm2.c b/lib/libpgm2.c
index 9f607d73..650d2cb5 100644
--- a/lib/libpgm2.c
+++ b/lib/libpgm2.c
@@ -16,7 +16,6 @@
 #include "pm_c_util.h"
 #include "mallocvar.h"
 #include "pgm.h"
-#include "libpgm.h"
 
 
 
diff --git a/lib/libpm.c b/lib/libpm.c
index 9b1ff679..1adad8b6 100644
--- a/lib/libpm.c
+++ b/lib/libpm.c
@@ -1,72 +1,45 @@
 /**************************************************************************
                                   libpm.c
 ***************************************************************************
-  This is the most fundamental Netpbm library.  It contains routines
-  not specific to any particular Netpbm format.
+  This file contains fundamental libnetpbm services.
 
   Some of the subroutines in this library are intended and documented
   for use by Netpbm users, but most of them are just used by other
   Netpbm library subroutines.
-
-  Before May 2001, this function was served by the libpbm library
-  (in addition to being the library for handling the PBM format).
-
 **************************************************************************/
-#define _SVID_SOURCE
-    /* Make sure P_tmpdir is defined in GNU libc 2.0.7 (_XOPEN_SOURCE 500
-       does it in other libc's).  pm_config.h defines TMPDIR as P_tmpdir
-       in some environments.
-    */
-#define _XOPEN_SOURCE 500    /* Make sure ftello, fseeko are defined */
-#define _LARGEFILE_SOURCE 1  /* Make sure ftello, fseeko are defined */
-#define _LARGEFILE64_SOURCE 1 
-#define _FILE_OFFSET_BITS 64
-    /* This means ftello() is really ftello64() and returns a 64 bit file
-       position.  Unless the C library doesn't have ftello64(), in which 
-       case ftello() is still just ftello().
-
-       Likewise for all the other C library file functions.
 
-       And off_t and fpos_t are 64 bit types instead of 32.  Consequently,
-       pm_filepos_t might be 64 bits instead of 32.
-    */
-#define _LARGE_FILES  
-    /* This does for AIX what _FILE_OFFSET_BITS=64 does for GNU */
-#define _LARGE_FILE_API
-    /* This makes the the x64() functions available on AIX */
+#define _XOPEN_SOURCE 500    /* Make sure ftello, fseeko are defined */
 
 #include <unistd.h>
 #include <stdio.h>
-#include <fcntl.h>
 #include <stdarg.h>
 #include <string.h>
 #include <errno.h>
 #include <setjmp.h>
-#ifdef __DJGPP__
-  #include <io.h>
-#endif
 #include <time.h>
+#include <limits.h>
 
 #include "pm_c_util.h"
+#include "mallocvar.h"
 #include "version.h"
 #include "compile.h"
 #include "nstring.h"
 #include "shhopt.h"
-#include "mallocvar.h"
+
 #include "pm.h"
 
 /* The following are set by pm_init(), then used by subsequent calls to other
    pm_xxx() functions.
    */
-static const char * pm_progname;
-static bool pm_showmessages;  
-    /* Programs should display informational messages (because the user didn't
-       specify the --quiet option).
-    */
+const char * pm_progname;
 
 int pm_plain_output;
     /* Boolean: programs should produce output in plain format */
 
+static bool pm_showmessages;  
+    /* Programs should display informational messages (because the user didn't
+       specify the --quiet option).
+    */
 static jmp_buf * pm_jmpbufP = NULL;
     /* A description of the point to which the program should hyperjump
        if a libnetpbm function encounters an error (libnetpbm functions
@@ -216,13 +189,12 @@ pm_error(const char format[], ...) {
 }
 
 
-/* Variable-sized arrays. */
 
-char *
+void *
 pm_allocrow(unsigned int const cols,
             unsigned int const size) {
 
-    char * itrow;
+    unsigned char * itrow;
 
     if (UINT_MAX / cols < size)
         pm_error("Arithmetic overflow multiplying %u by %u to get the "
@@ -238,7 +210,7 @@ pm_allocrow(unsigned int const cols,
 
 
 void
-pm_freerow(char * const itrow) {
+pm_freerow(void * const itrow) {
     free(itrow);
 }
 
@@ -385,12 +357,12 @@ pm_freearray(char ** const rowIndex,
 /* Case-insensitive keyword matcher. */
 
 int
-pm_keymatch(char *       const strarg, 
+pm_keymatch(const char *       const strarg, 
             const char * const keywordarg, 
             int          const minchars) {
     int len;
-    const char *keyword;
-    char *str;
+    const char * keyword;
+    const char * str;
 
     str = strarg;
     keyword = keywordarg;
@@ -660,7 +632,7 @@ showNetpbmHelp(const char progname[]) {
 
 
 void
-pm_proginit(int * const argcP, char * argv[]) {
+pm_proginit(int * const argcP, const char * argv[]) {
 /*----------------------------------------------------------------------------
    Do various initialization things that all programs in the Netpbm package,
    and programs that emulate such programs, should do.
@@ -747,12 +719,13 @@ pm_setMessage(int const newState, int * const oldStateP) {
 }
 
 
+
 char *
 pm_arg0toprogname(const char arg0[]) {
 /*----------------------------------------------------------------------------
    Given a value for argv[0] (a command name or file name passed to a 
    program in the standard C calling sequence), return the name of the
-   Netpbm program to which is refers.
+   Netpbm program to which it refers.
 
    In the most ordinary case, this is simply the argument itself.
 
@@ -760,7 +733,7 @@ pm_arg0toprogname(const char arg0[]) {
    after the last slash, and if there is a .exe on it (as there is for
    DJGPP), that is removed.
 
-   The return value is in static storage within.  It is null-terminated,
+   The return value is in static storage within.  It is NUL-terminated,
    but truncated at 64 characters.
 -----------------------------------------------------------------------------*/
     static char retval[64+1];
@@ -795,902 +768,55 @@ pm_randseed(void) {
 
 
 
-/* File open/close that handles "-" as stdin/stdout and checks errors. */
-
-FILE*
-pm_openr(const char * const name) {
-    FILE* f;
-
-    if (strcmp(name, "-") == 0)
-        f = stdin;
-    else {
-#ifndef VMS
-        f = fopen(name, "rb");
-#else
-        f = fopen(name, "r", "ctx=stm");
-#endif
-        if (f == NULL) 
-            pm_error("Unable to open file '%s' for reading.  "
-                     "fopen() returns errno %d (%s)", 
-                     name, errno, strerror(errno));
-    }
-    return f;
-}
-
-
-
-FILE*
-pm_openw(const char * const name) {
-    FILE* f;
-
-    if (strcmp(name, "-") == 0)
-        f = stdout;
-    else {
-#ifndef VMS
-        f = fopen(name, "wb");
-#else
-        f = fopen(name, "w", "mbc=32", "mbf=2");  /* set buffer factors */
-#endif
-        if (f == NULL) 
-            pm_error("Unable to open file '%s' for writing.  "
-                     "fopen() returns errno %d (%s)", 
-                     name, errno, strerror(errno));
-    }
-    return f;
-}
-
-
-
-static const char *
-tmpDir(void) {
-/*----------------------------------------------------------------------------
-   Return the name of the directory in which we should create temporary
-   files.
-
-   The name is a constant in static storage.
------------------------------------------------------------------------------*/
-    const char * tmpdir;
-        /* running approximation of the result */
-
-    tmpdir = getenv("TMPDIR");   /* Unix convention */
-
-    if (!tmpdir || strlen(tmpdir) == 0)
-        tmpdir = getenv("TMP");  /* Windows convention */
-
-    if (!tmpdir || strlen(tmpdir) == 0)
-        tmpdir = getenv("TEMP"); /* Windows convention */
-
-    if (!tmpdir || strlen(tmpdir) == 0)
-        tmpdir = TMPDIR;
-
-    return tmpdir;
-}
-
-
-
-static int
-mkstempx(char * const filenameBuffer) {
+unsigned int
+pm_parse_width(const char * const arg) {
 /*----------------------------------------------------------------------------
-  This is meant to be equivalent to POSIX mkstemp().
-
-  On some old systems, mktemp() is a security hazard that allows a hacker
-  to read or write our temporary file or cause us to read or write some
-  unintended file.  On other systems, mkstemp() does not exist.
-
-  A Windows/mingw environment is one which doesn't have mkstemp()
-  (2006.06.15).
-
-  We assume that if a system doesn't have mkstemp() that its mktemp()
-  is safe, or that the total situation is such that the problems of
-  mktemp() are not a problem for the user.
+   Return the image width represented by the decimal ASCIIZ string
+   'arg'.  Fail if it doesn't validly represent a width or represents
+   a width that can't be conveniently used in computation.
 -----------------------------------------------------------------------------*/
-    int retval;
-    int fd;
-    unsigned int attempts;
-    bool gotFile;
-    bool error;
-
-    for (attempts = 0, gotFile = FALSE, error = FALSE;
-         !gotFile && !error && attempts < 100;
-         ++attempts) {
-
-        char * rc;
-        rc = mktemp(filenameBuffer);
-
-        if (rc == NULL)
-            error = TRUE;
-        else {
-            int rc;
-
-            rc = open(filenameBuffer, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
-
-            if (rc == 0) {
-                fd = rc;
-                gotFile = TRUE;
-            } else {
-                if (errno == EEXIST) {
-                    /* We'll just have to keep trying */
-                } else 
-                    error = TRUE;
-            }
-        }
-    }    
-    if (gotFile)
-        retval = fd;
-    else
-        retval = -1;
-
-    return retval;
-}
-
-
-
-static int
-mkstemp2(char * const filenameBuffer) {
-
-#if HAVE_MKSTEMP
-    if (0)
-        mkstempx(NULL);  /* defeat compiler unused function warning */
-    return mkstemp(filenameBuffer);
-#else
-    return mkstempx(filenameBuffer);
-#endif
-}
-
-
-
-static void
-makeTmpfileWithTemplate(const char *  const filenameTemplate,
-                        FILE **       const filePP,
-                        const char ** const filenameP,
-                        const char ** const errorP) {
-    
-    char * filenameBuffer;  /* malloc'ed */
-
-    filenameBuffer = strdup(filenameTemplate);
-
-    if (filenameBuffer == NULL)
-        asprintfN(errorP, "Unable to allocate storage for temporary "
-                  "file name");
-    else {
-        int rc;
-        
-        rc = mkstemp2(filenameBuffer);
-        
-        if (rc < 0)
-            asprintfN(errorP,
-                      "Unable to create temporary file according to name "
-                      "pattern '%s'.  mkstemp() failed with errno %d (%s)",
-                      filenameTemplate, errno, strerror(errno));
-        else {
-            int const fd = rc;
-            
-            FILE * fileP;
-            fileP = fdopen(fd, "w+b");
-            
-            if (fileP == NULL)
-                asprintfN(errorP, "Unable to create temporary file.  "
-                          "fdopen() failed with errno %d (%s)",
-                          errno, strerror(errno));
-            else {
-                *errorP = NULL;
-                *filePP = fileP;
-                *filenameP = filenameBuffer;
-            }
-            if (*errorP) {
-                unlink(filenameBuffer);
-                close(fd);
-            }
-        }
-        if (*errorP)
-            strfree(filenameBuffer);
-    }
-}
-
-
-
-void
-pm_make_tmpfile(FILE **       const filePP,
-                const char ** const filenameP) {
-
-    const char * filenameTemplate;
-    unsigned int fnamelen;
-    const char * tmpdir;
-    const char * dirseparator;
+    unsigned int width;
     const char * error;
 
-    fnamelen = strlen(pm_progname) + 10; /* "/" + "_XXXXXX\0" */
-
-    tmpdir = tmpDir();
+    interpret_uint(arg, &width, &error);
 
-    if (tmpdir[strlen(tmpdir) - 1] == '/')
-        dirseparator = "";
-    else
-        dirseparator = "/";
-    
-    asprintfN(&filenameTemplate, "%s%s%s%s", 
-              tmpdir, dirseparator, pm_progname, "_XXXXXX");
-
-    if (filenameTemplate == NULL)
-        asprintfN(&error,
-                  "Unable to allocate storage for temporary file name");
-    else {
-        makeTmpfileWithTemplate(filenameTemplate, filePP, filenameP, &error);
-
-        strfree(filenameTemplate);
-    }
     if (error) {
-        pm_errormsg("%s", error);
+        pm_error("'%s' is invalid as an image width.  %s", arg, error);
         strfree(error);
-        pm_longjmp();
-    }
-}
-
-
-
-FILE * 
-pm_tmpfile(void) {
-
-    FILE * fileP;
-    const char * tmpfile;
-
-    pm_make_tmpfile(&fileP, &tmpfile);
-
-    unlink(tmpfile);
-
-    strfree(tmpfile);
-
-    return fileP;
-}
-
-
-
-FILE *
-pm_openr_seekable(const char name[]) {
-/*----------------------------------------------------------------------------
-  Open the file named by name[] such that it is seekable (i.e. it can be
-  rewound and read in multiple passes with fseek()).
-
-  If the file is actually seekable, this reduces to the same as
-  pm_openr().  If not, we copy the named file to a temporary file
-  and return that file's stream descriptor.
-
-  We use a file that the operating system recognizes as temporary, so
-  it picks the filename and deletes the file when Caller closes it.
------------------------------------------------------------------------------*/
-    int stat_rc;
-    int seekable;  /* logical: file is seekable */
-    struct stat statbuf;
-    FILE * original_file;
-    FILE * seekable_file;
-
-    original_file = pm_openr((char *) name);
-
-    /* I would use fseek() to determine if the file is seekable and 
-       be a little more general than checking the type of file, but I
-       don't have reliable information on how to do that.  I have seen
-       streams be partially seekable -- you can, for example seek to
-       0 if the file is positioned at 0 but you can't actually back up
-       to 0.  I have seen documentation that says the errno for an
-       unseekable stream is EBADF and in practice seen ESPIPE.
-
-       On the other hand, regular files are always seekable and even if
-       some other file is, it doesn't hurt much to assume it isn't.
-    */
-
-    stat_rc = fstat(fileno(original_file), &statbuf);
-    if (stat_rc == 0 && S_ISREG(statbuf.st_mode))
-        seekable = TRUE;
-    else 
-        seekable = FALSE;
-
-    if (seekable) {
-        seekable_file = original_file;
     } else {
-        seekable_file = pm_tmpfile();
-        
-        /* Copy the input into the temporary seekable file */
-        while (!feof(original_file) && !ferror(original_file) 
-               && !ferror(seekable_file)) {
-            char buffer[4096];
-            int bytes_read;
-            bytes_read = fread(buffer, 1, sizeof(buffer), original_file);
-            fwrite(buffer, 1, bytes_read, seekable_file);
-        }
-        if (ferror(original_file))
-            pm_error("Error reading input file into temporary file.  "
-                     "Errno = %s (%d)", strerror(errno), errno);
-        if (ferror(seekable_file))
-            pm_error("Error writing input into temporary file.  "
-                     "Errno = %s (%d)", strerror(errno), errno);
-        pm_close(original_file);
-        {
-            int seek_rc;
-            seek_rc = fseek(seekable_file, 0, SEEK_SET);
-            if (seek_rc != 0)
-                pm_error("fseek() failed to rewind temporary file.  "
-                         "Errno = %s (%d)", strerror(errno), errno);
-        }
+        if (width > INT_MAX-10)
+            pm_error("Width %u is too large for computations.", width);
+        if (width == 0)
+            pm_error("Width argument must be a positive number.  You "
+                     "specified 0.");
     }
-    return seekable_file;
-}
-
-
-
-void
-pm_close(FILE * const f) {
-    fflush(f);
-    if (ferror(f))
-        pm_message("A file read or write error occurred at some point");
-    if (f != stdin)
-        if (fclose(f) != 0)
-            pm_error("close of file failed with errno %d (%s)",
-                     errno, strerror(errno));
-}
-
-
-
-/* The pnmtopng package uses pm_closer() and pm_closew() instead of 
-   pm_close(), apparently because the 1999 Pbmplus package has them.
-   I don't know what the difference is supposed to be.
-*/
-
-void
-pm_closer(FILE * const f) {
-    pm_close(f);
-}
-
-
-
-void
-pm_closew(FILE * const f) {
-    pm_close(f);
-}
-
-
-
-/* Endian I/O.
-
-   Before Netpbm 10.27 (March 2005), these would return failure on EOF
-   or I/O failure.  For backward compatibility, they still have the return
-   code, but it is always zero and the routines abort the program in case
-   of EOF or I/O failure.  A program that wants to handle failure differently
-   must use lower level (C library) interfaces.  But that level of detail
-   is uncharacteristic of a Netpbm program; the ease of programming that
-   comes with not checking a return code is more Netpbm.
-
-   It is also for historical reasons that these return signed values,
-   when clearly unsigned would make more sense.
-*/
-
-
-
-static void
-abortWithReadError(FILE * const ifP) {
-
-    if (feof(ifP))
-        pm_error("Unexpected end of input file");
-    else
-        pm_error("Error (not EOF) reading file.");
-}
-
-
-
-void
-pm_readchar(FILE * const ifP,
-            char * const cP) {
-    
-    int c;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-
-    *cP = c;
-}
-
-
-
-void
-pm_writechar(FILE * const ofP,
-             char   const c) {
-
-    putc(c, ofP);
-}
-
-
-
-int
-pm_readbigshort(FILE *  const ifP, 
-                short * const sP) {
-    int c;
-
-    unsigned short s;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    s = (c & 0xff) << 8;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    s |= c & 0xff;
-
-    *sP = s;
-
-    return 0;
-}
-
-
-
-int
-pm_writebigshort(FILE * const ofP, 
-                 short  const s) {
-
-    putc((s >> 8) & 0xff, ofP);
-    putc(s & 0xff, ofP);
-
-    return 0;
-}
-
-
-
-int
-pm_readbiglong(FILE * const ifP, 
-               long * const lP) {
-
-    int c;
-    unsigned long l;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l = c << 24;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c << 16;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c << 8;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c;
-
-    *lP = l;
-
-    return 0;
-}
-
-
-
-int
-pm_writebiglong(FILE * const ofP, 
-                long   const l) {
-
-    putc((l >> 24) & 0xff, ofP);
-    putc((l >> 16) & 0xff, ofP);
-    putc((l >>  8) & 0xff, ofP);
-    putc((l >>  0) & 0xff, ofP);
-
-    return 0;
-}
-
-
-
-int
-pm_readlittleshort(FILE *  const ifP, 
-                   short * const sP) {
-    int c;
-    unsigned short s;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    s = c & 0xff;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    s |= (c & 0xff) << 8;
-
-    *sP = s;
-
-    return 0;
-}
-
-
-
-int
-pm_writelittleshort(FILE * const ofP, 
-                    short  const s) {
-
-    putc((s >> 0) & 0xff, ofP);
-    putc((s >> 8) & 0xff, ofP);
-
-    return 0;
-}
-
-
-
-int
-pm_readlittlelong(FILE * const ifP, 
-                  long * const lP) {
-    int c;
-    unsigned long l;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l = c;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c << 8;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c << 16;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c << 24;
-
-    *lP = l;
-
-    return 0;
-}
-
-
-
-int
-pm_writelittlelong(FILE * const ofP, 
-                   long   const l) {
-
-    putc((l >>  0) & 0xff, ofP);
-    putc((l >>  8) & 0xff, ofP);
-    putc((l >> 16) & 0xff, ofP);
-    putc((l >> 24) & 0xff, ofP);
-
-    return 0;
-}
-
-
-
-int 
-pm_readmagicnumber(FILE * const ifP) {
-
-    int ich1, ich2;
-
-    ich1 = getc(ifP);
-    ich2 = getc(ifP);
-    if (ich1 == EOF || ich2 == EOF)
-        pm_error( "Error reading magic number from Netpbm image stream.  "
-                  "Most often, this "
-                  "means your input file is empty." );
-
-    return ich1 * 256 + ich2;
-}
-
-
-
-/* Read a file of unknown size to a buffer. Return the number of bytes
-   read. Allocate more memory as we need it. The calling routine has
-   to free() the buffer.
-
-   Oliver Trepte, oliver@fysik4.kth.se, 930613 */
-
-#define PM_BUF_SIZE 16384      /* First try this size of the buffer, then
-                                  double this until we reach PM_MAX_BUF_INC */
-#define PM_MAX_BUF_INC 65536   /* Don't allocate more memory in larger blocks
-                                  than this. */
-
-char *
-pm_read_unknown_size(FILE * const file, 
-                     long * const nread) {
-    long nalloc;
-    char * buf;
-    bool eof;
-
-    *nread = 0;
-    nalloc = PM_BUF_SIZE;
-    MALLOCARRAY(buf, nalloc);
-
-    eof = FALSE;  /* initial value */
-
-    while(!eof) {
-        int val;
-
-        if (*nread >= nalloc) { /* We need a larger buffer */
-            if (nalloc > PM_MAX_BUF_INC)
-                nalloc += PM_MAX_BUF_INC;
-            else
-                nalloc += nalloc;
-            REALLOCARRAY_NOFAIL(buf, nalloc);
-        }
-
-        val = getc(file);
-        if (val == EOF)
-            eof = TRUE;
-        else 
-            buf[(*nread)++] = val;
-    }
-    return buf;
-}
-
-
-
-union cheat {
-    uint32n l;
-    short s;
-    unsigned char c[4];
-};
-
-
-
-short
-pm_bs_short(short const s) {
-    union cheat u;
-    unsigned char t;
-
-    u.s = s;
-    t = u.c[0];
-    u.c[0] = u.c[1];
-    u.c[1] = t;
-    return u.s;
-}
-
-
-
-long
-pm_bs_long(long const l) {
-    union cheat u;
-    unsigned char t;
-
-    u.l = l;
-    t = u.c[0];
-    u.c[0] = u.c[3];
-    u.c[3] = t;
-    t = u.c[1];
-    u.c[1] = u.c[2];
-    u.c[2] = t;
-    return u.l;
-}
-
-
-
-void
-pm_tell2(FILE *       const fileP, 
-         void *       const fileposP,
-         unsigned int const fileposSize) {
-/*----------------------------------------------------------------------------
-   Return the current file position as *filePosP, which is a buffer
-   'fileposSize' bytes long.  Abort the program if error, including if
-   *fileP isn't a file that has a position.
------------------------------------------------------------------------------*/
-    /* Note: FTELLO() is either ftello() or ftell(), depending on the
-       capabilities of the underlying C library.  It is defined in
-       pm_config.h.  ftello(), in turn, may be either ftell() or
-       ftello64(), as implemented by the C library.
-    */
-    pm_filepos const filepos = FTELLO(fileP);
-    if (filepos < 0)
-        pm_error("ftello() to get current file position failed.  "
-                 "Errno = %s (%d)\n", strerror(errno), errno);
-
-    if (fileposSize == sizeof(pm_filepos)) {
-        pm_filepos * const fileposP_filepos = fileposP;
-        *fileposP_filepos = filepos;
-    } else if (fileposSize == sizeof(long)) {
-        if (sizeof(pm_filepos) > sizeof(long) &&
-            filepos >= (pm_filepos) 1 << (sizeof(long)*8))
-            pm_error("File size is too large to represent in the %u bytes "
-                     "that were provided to pm_tell2()", fileposSize);
-        else {
-            long * const fileposP_long = fileposP;
-            *fileposP_long = (long)filepos;
-        }
-    } else
-        pm_error("File position size passed to pm_tell() is invalid: %u.  "
-                 "Valid sizes are %u and %u", 
-                 fileposSize, (unsigned int)sizeof(pm_filepos),
-                 (unsigned int) sizeof(long));
+    return width;
 }
 
 
 
 unsigned int
-pm_tell(FILE * const fileP) {
-    
-    long filepos;
-
-    pm_tell2(fileP, &filepos, sizeof(filepos));
-
-    return filepos;
-}
-
-
-
-void
-pm_seek2(FILE *             const fileP, 
-         const pm_filepos * const fileposP,
-         unsigned int       const fileposSize) {
+pm_parse_height(const char * const arg) {
 /*----------------------------------------------------------------------------
-   Position file *fileP to position *fileposP.  Abort if error, including
-   if *fileP isn't a seekable file.
+  Same as pm_parse_width(), but for height.
 -----------------------------------------------------------------------------*/
-    if (fileposSize == sizeof(pm_filepos)) 
-        /* Note: FSEEKO() is either fseeko() or fseek(), depending on the
-           capabilities of the underlying C library.  It is defined in
-           pm_config.h.  fseeko(), in turn, may be either fseek() or
-           fseeko64(), as implemented by the C library.
-        */
-        FSEEKO(fileP, *fileposP, SEEK_SET);
-    else if (fileposSize == sizeof(long)) {
-        long const fileposLong = *(long *)fileposP;
-        fseek(fileP, fileposLong, SEEK_SET);
-    } else
-        pm_error("File position size passed to pm_seek() is invalid: %u.  "
-                 "Valid sizes are %u and %u", 
-                 fileposSize, (unsigned int)sizeof(pm_filepos),
-                 (unsigned int) sizeof(long));
-}
-
-
-
-void
-pm_seek(FILE * const fileP, unsigned long filepos) {
-/*----------------------------------------------------------------------------
------------------------------------------------------------------------------*/
-
-    pm_filepos fileposBuff;
-
-    fileposBuff = filepos;
-
-    pm_seek2(fileP, &fileposBuff, sizeof(fileposBuff));
-}
-
+    unsigned int height;
+    const char * error;
 
+    interpret_uint(arg, &height, &error);
 
-void
-pm_nextimage(FILE * const file, int * const eofP) {
-/*----------------------------------------------------------------------------
-   Position the file 'file' to the next image in the stream, assuming it is
-   now positioned just after the current image.  I.e. read off any white
-   space at the end of the current image's raster.  Note that the raw formats
-   don't permit such white space, but this routine tolerates it anyway, 
-   because the plain formats do permit white space after the raster.
-
-   Iff there is no next image, return *eofP == TRUE.
-
-   Note that in practice, we will not normally see white space here in
-   a plain PPM or plain PGM stream because the routine to read a
-   sample from the image reads one character of white space after the
-   sample in order to know where the sample ends.  There is not
-   normally more than one character of white space (a newline) after
-   the last sample in the raster.  But plain PBM is another story.  No white
-   space is required between samples of a plain PBM image.  But the raster
-   normally ends with a newline nonetheless.  Since the sample reading code
-   will not have read that newline, it is there for us to read now.
------------------------------------------------------------------------------*/
-    bool eof;
-    bool nonWhitespaceFound;
-
-    eof = FALSE;
-    nonWhitespaceFound = FALSE;
-
-    while (!eof && !nonWhitespaceFound) {
-        int c;
-        c = getc(file);
-        if (c == EOF) {
-            if (feof(file)) 
-                eof = TRUE;
-            else
-                pm_error("File error on getc() to position to image");
-        } else {
-            if (!isspace(c)) {
-                int rc;
-
-                nonWhitespaceFound = TRUE;
-
-                /* Have to put the non-whitespace character back in
-                   the stream -- it's part of the next image.  
-                */
-                rc = ungetc(c, file);
-                if (rc == EOF) 
-                    pm_error("File error doing ungetc() "
-                             "to position to image.");
-            }
-        }
+    if (error) {
+        pm_error("'%s' is invalid as an image height.  %s", arg, error);
+        strfree(error);
+    } else {
+        if (height > INT_MAX-10)
+            pm_error("Height %u is too large for computations.", height);
+        if (height == 0)
+            pm_error("Height argument must be a positive number.  You "
+                     "specified 0.");
     }
-    *eofP = eof;
-}
-
-
-
-void
-pm_check(FILE *               const file, 
-         enum pm_check_type   const check_type, 
-         pm_filepos           const need_raster_size,
-         enum pm_check_code * const retval_p) {
-/*----------------------------------------------------------------------------
-   This is not defined for use outside of libnetpbm.
------------------------------------------------------------------------------*/
-    struct stat statbuf;
-    pm_filepos curpos;  /* Current position of file; -1 if none */
-    int rc;
-
-#ifdef LARGEFILEDEBUG
-    pm_message("pm_filepos received by pm_check() is %u bytes.",
-               sizeof(pm_filepos));
-#endif
-    /* Note: FTELLO() is either ftello() or ftell(), depending on the
-       capabilities of the underlying C library.  It is defined in
-       pm_config.h.  ftello(), in turn, may be either ftell() or
-       ftello64(), as implemented by the C library.
-    */
-    curpos = FTELLO(file);
-    if (curpos >= 0) {
-        /* This type of file has a current position */
-            
-        rc = fstat(fileno(file), &statbuf);
-        if (rc != 0) 
-            pm_error("fstat() failed to get size of file, though ftello() "
-                     "successfully identified\n"
-                     "the current position.  Errno=%s (%d)",
-                     strerror(errno), errno);
-        else if (!S_ISREG(statbuf.st_mode)) {
-            /* Not a regular file; we can't know its size */
-            if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE;
-        } else {
-            pm_filepos const have_raster_size = statbuf.st_size - curpos;
-            
-            if (have_raster_size < need_raster_size)
-                pm_error("File has invalid format.  The raster should "
-                         "contain %u bytes, but\n"
-                         "the file ends after only %u bytes.",
-                         (unsigned int) need_raster_size, 
-                         (unsigned int) have_raster_size);
-            else if (have_raster_size > need_raster_size) {
-                if (retval_p) *retval_p = PM_CHECK_TOO_LONG;
-            } else {
-                if (retval_p) *retval_p = PM_CHECK_OK;
-            }
-        }
-    } else
-        if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE;
+    return height;
 }
 
 
 
-void
-pm_drain(FILE *         const fileP,
-         unsigned int   const limit,
-         unsigned int * const bytesReadP) {
-/*----------------------------------------------------------------------------
-  Read bytes from *fileP until EOF and return as *bytesReadP how many there
-  were.
-
-  But don't read any more than 'limit'.
-
-  This is a good thing to call after reading an input file to be sure you
-  didn't leave some input behind, which could mean you didn't properly
-  interpret the file.
------------------------------------------------------------------------------*/
-    unsigned int bytesRead;
-    bool eof;
-
-    for (bytesRead = 0, eof = false; !eof && bytesRead < limit;) {
-
-        int rc;
-
-        rc = fgetc(fileP);
-
-        eof = (rc == EOF);
-        if (!eof)
-            ++bytesRead;
-    }
-    *bytesReadP = bytesRead;
-}
diff --git a/lib/libpnm2.c b/lib/libpnm2.c
index 7e4f7e2a..fdfe0dc0 100644
--- a/lib/libpnm2.c
+++ b/lib/libpnm2.c
@@ -15,13 +15,10 @@
 #include "pnm.h"
 
 #include "ppm.h"
-#include "libppm.h"
 
 #include "pgm.h"
-#include "libpgm.h"
 
 #include "pbm.h"
-#include "libpbm.h"
 
 void
 pnm_writepnminit(FILE * const fileP, 
diff --git a/lib/libpnm3.c b/lib/libpnm3.c
index 4df8041c..71df0cbf 100644
--- a/lib/libpnm3.c
+++ b/lib/libpnm3.c
@@ -13,13 +13,10 @@
 #include "pnm.h"
 
 #include "ppm.h"
-#include "libppm.h"
 
 #include "pgm.h"
-#include "libpgm.h"
 
 #include "pbm.h"
-#include "libpbm.h"
 
 xel
 pnm_backgroundxel( xel** xels, int cols, int rows, xelval maxval, int format )
diff --git a/lib/libppm2.c b/lib/libppm2.c
index 52c4ab16..78898910 100644
--- a/lib/libppm2.c
+++ b/lib/libppm2.c
@@ -17,7 +17,6 @@
 #include "pm_c_util.h"
 #include "mallocvar.h"
 #include "ppm.h"
-#include "libppm.h"
 
 void
 ppm_writeppminit(FILE*  const fileP, 
diff --git a/lib/libppmcmap.c b/lib/libppmcmap.c
index 954488d7..055bfc8b 100644
--- a/lib/libppmcmap.c
+++ b/lib/libppmcmap.c
@@ -15,7 +15,6 @@
 #include "pm_c_util.h"
 #include "nstring.h"
 #include "mallocvar.h"
-#include "libppm.h"
 #include "ppm.h"
 #include "ppmcmap.h"
 
diff --git a/lib/pam.h b/lib/pam.h
index 8f9f5d12..c28c5c2c 100644
--- a/lib/pam.h
+++ b/lib/pam.h
@@ -6,8 +6,8 @@
 #ifndef PAM_H
 #define PAM_H
 
-#include "pm.h"
-#include "pnm.h"
+#include <netpbm/pm.h>
+#include <netpbm/pnm.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/lib/pammap.h b/lib/pammap.h
index a53a9c23..c9c1deed 100644
--- a/lib/pammap.h
+++ b/lib/pammap.h
@@ -9,8 +9,8 @@
 
 #ifndef PAMMAP_H
 #define PAMMAP_H
-#include "colorname.h"
-#include "pam.h"
+#include <netpbm/colorname.h>
+#include <netpbm/pam.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/lib/pbm.h b/lib/pbm.h
index 1591c77f..1c3fedc5 100644
--- a/lib/pbm.h
+++ b/lib/pbm.h
@@ -1,7 +1,7 @@
 #ifndef PBM_H_INCLUDED
 #define PBM_H_INCLUDED
 
-#include "pm.h"
+#include <netpbm/pm.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -33,13 +33,18 @@ typedef unsigned char bit;
 
 /* Declarations of routines. */
 
-void pbm_init ARGS(( int* argcP, char* argv[] ));
+void
+pbm_init(int *   const argcP,
+         char ** const argv);
+
 void
 pbm_nextimage(FILE *file, int * const eofP);
 
+bit *
+pbm_allocrow(unsigned int const cols);
+
 #define pbm_allocarray(cols, rows) \
   ((bit**) pm_allocarray(cols, rows, sizeof(bit)))
-#define pbm_allocrow(cols) ((bit*) pm_allocrow(cols, sizeof(bit)))
 #define pbm_freearray(bits, rows) pm_freearray((char**) bits, rows)
 #define pbm_freerow(bitrow) pm_freerow((char*) bitrow)
 #define pbm_packed_bytes(cols) (((cols)+7)/8)
diff --git a/lib/pbmfont.h b/lib/pbmfont.h
index 8a81c37a..d287bc6c 100644
--- a/lib/pbmfont.h
+++ b/lib/pbmfont.h
@@ -68,7 +68,7 @@ pbm_dissectfont(const bit ** const font,
 struct font* pbm_loadfont(const char * const filename);
 struct font* pbm_loadpbmfont(const char * const filename);
 struct font* pbm_loadbdffont(const char * const filename);
-void pbm_dumpfont ARGS(( struct font* fn ));
+void pbm_dumpfont(struct font * const fnP);
 
 #ifdef __cplusplus
 }
diff --git a/lib/pgm.h b/lib/pgm.h
index 86935307..2de8d531 100644
--- a/lib/pgm.h
+++ b/lib/pgm.h
@@ -4,7 +4,8 @@
 #ifndef _PGM_H_
 #define _PGM_H_
 
-#include "pbm.h"
+#include <netpbm/pm.h>
+#include <netpbm/pbm.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -68,7 +69,7 @@ pgm_init(int *   const argcP,
 gray *
 pgm_allocrow(unsigned int const cols);
 
-#define pgm_freerow(grayrow) free(grayrow)
+#define pgm_freerow(grayrow) pm_freerow(grayrow)
 
 #define pgm_allocarray( cols, rows ) \
   ((gray**) pm_allocarray( cols, rows, sizeof(gray) ))
diff --git a/lib/pm.h b/lib/pm.h
index 232fc363..b38580ff 100644
--- a/lib/pm.h
+++ b/lib/pm.h
@@ -13,7 +13,7 @@
 #ifndef PM_H_INCLUDED
 #define PM_H_INCLUDED
 
-#include "pm_config.h"
+#include <netpbm/pm_config.h>
 
 #include <sys/types.h>
 #include <ctype.h>
@@ -91,12 +91,13 @@ extern int pm_plain_output;
     /* Output functions are to produce plain (as opposed to raw) format
        regardless of their 'plainformat' arguments.
     */
+extern const char * pm_progname;
 
 void 
 pm_init(const char * const progname, unsigned int const flags);
 
 void 
-pm_proginit(int* const argcP, char* argv[]);
+pm_proginit(int * const argcP, const char * argv[]);
 
 void
 pm_setMessage(int const newState, int * const oldStateP);
@@ -116,7 +117,7 @@ pm_nextimage(FILE * const file, int * const eofP);
 char** 
 pm_allocarray (int const cols, int const rows, int const size );
 
-char * 
+void * 
 pm_allocrow(unsigned int const cols,
             unsigned int const size);
 
@@ -124,12 +125,14 @@ void
 pm_freearray (char** const its, int const rows);
 
 void 
-pm_freerow(char* const itrow);
+pm_freerow(void * const row);
 
 
 /* Obsolete -- use shhopt instead */
 int 
-pm_keymatch (char* const str, const char* const keyword, int const minchars);
+pm_keymatch(const char * const str,
+            const char * const keyword,
+            int          const minchars);
 
 
 int PURE_FN_ATTR
@@ -356,6 +359,12 @@ pm_arg0toprogname(const char arg0[]);
 unsigned int
 pm_randseed(void);
 
+unsigned int
+pm_parse_width(const char * const arg);
+
+unsigned int
+pm_parse_height(const char * const arg);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/pm_gamma.h b/lib/pm_gamma.h
index 92b34145..6630e05c 100644
--- a/lib/pm_gamma.h
+++ b/lib/pm_gamma.h
@@ -1,7 +1,7 @@
 #ifndef _PM_GAMMA_H_
 #define _PM_GAMMA_H_
 
-#include "pm_config.h"
+#include <netpbm/pm_config.h>
 
 #include <math.h>
 
diff --git a/lib/pmfileio.c b/lib/pmfileio.c
new file mode 100644
index 00000000..e2cd3d06
--- /dev/null
+++ b/lib/pmfileio.c
@@ -0,0 +1,948 @@
+/**************************************************************************
+                                 pmfileio.c
+***************************************************************************
+  This file contains fundamental file I/O stuff for libnetpbm.
+  These are external functions, unlike 'fileio.c', but are not
+  particular to any Netpbm format.
+**************************************************************************/
+#define _SVID_SOURCE
+    /* Make sure P_tmpdir is defined in GNU libc 2.0.7 (_XOPEN_SOURCE 500
+       does it in other libc's).  pm_config.h defines TMPDIR as P_tmpdir
+       in some environments.
+    */
+#define _XOPEN_SOURCE 500    /* Make sure ftello, fseeko are defined */
+#define _LARGEFILE_SOURCE 1  /* Make sure ftello, fseeko are defined */
+#define _LARGEFILE64_SOURCE 1 
+#define _FILE_OFFSET_BITS 64
+    /* This means ftello() is really ftello64() and returns a 64 bit file
+       position.  Unless the C library doesn't have ftello64(), in which 
+       case ftello() is still just ftello().
+
+       Likewise for all the other C library file functions.
+
+       And off_t and fpos_t are 64 bit types instead of 32.  Consequently,
+       pm_filepos_t might be 64 bits instead of 32.
+    */
+#define _LARGE_FILES  
+    /* This does for AIX what _FILE_OFFSET_BITS=64 does for GNU */
+#define _LARGE_FILE_API
+    /* This makes the the x64() functions available on AIX */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+#ifdef __DJGPP__
+  #include <io.h>
+#endif
+
+#include "pm_c_util.h"
+#include "mallocvar.h"
+#include "nstring.h"
+
+#include "pm.h"
+
+
+
+/* File open/close that handles "-" as stdin/stdout and checks errors. */
+
+FILE*
+pm_openr(const char * const name) {
+    FILE* f;
+
+    if (strcmp(name, "-") == 0)
+        f = stdin;
+    else {
+#ifndef VMS
+        f = fopen(name, "rb");
+#else
+        f = fopen(name, "r", "ctx=stm");
+#endif
+        if (f == NULL) 
+            pm_error("Unable to open file '%s' for reading.  "
+                     "fopen() returns errno %d (%s)", 
+                     name, errno, strerror(errno));
+    }
+    return f;
+}
+
+
+
+FILE*
+pm_openw(const char * const name) {
+    FILE* f;
+
+    if (strcmp(name, "-") == 0)
+        f = stdout;
+    else {
+#ifndef VMS
+        f = fopen(name, "wb");
+#else
+        f = fopen(name, "w", "mbc=32", "mbf=2");  /* set buffer factors */
+#endif
+        if (f == NULL) 
+            pm_error("Unable to open file '%s' for writing.  "
+                     "fopen() returns errno %d (%s)", 
+                     name, errno, strerror(errno));
+    }
+    return f;
+}
+
+
+
+static const char *
+tmpDir(void) {
+/*----------------------------------------------------------------------------
+   Return the name of the directory in which we should create temporary
+   files.
+
+   The name is a constant in static storage.
+-----------------------------------------------------------------------------*/
+    const char * tmpdir;
+        /* running approximation of the result */
+
+    tmpdir = getenv("TMPDIR");   /* Unix convention */
+
+    if (!tmpdir || strlen(tmpdir) == 0)
+        tmpdir = getenv("TMP");  /* Windows convention */
+
+    if (!tmpdir || strlen(tmpdir) == 0)
+        tmpdir = getenv("TEMP"); /* Windows convention */
+
+    if (!tmpdir || strlen(tmpdir) == 0)
+        tmpdir = TMPDIR;
+
+    return tmpdir;
+}
+
+
+
+static int
+mkstempx(char * const filenameBuffer) {
+/*----------------------------------------------------------------------------
+  This is meant to be equivalent to POSIX mkstemp().
+
+  On some old systems, mktemp() is a security hazard that allows a hacker
+  to read or write our temporary file or cause us to read or write some
+  unintended file.  On other systems, mkstemp() does not exist.
+
+  A Windows/mingw environment is one which doesn't have mkstemp()
+  (2006.06.15).
+
+  We assume that if a system doesn't have mkstemp() that its mktemp()
+  is safe, or that the total situation is such that the problems of
+  mktemp() are not a problem for the user.
+-----------------------------------------------------------------------------*/
+    int retval;
+    int fd;
+    unsigned int attempts;
+    bool gotFile;
+    bool error;
+
+    for (attempts = 0, gotFile = FALSE, error = FALSE;
+         !gotFile && !error && attempts < 100;
+         ++attempts) {
+
+        char * rc;
+        rc = mktemp(filenameBuffer);
+
+        if (rc == NULL)
+            error = TRUE;
+        else {
+            int rc;
+
+            rc = open(filenameBuffer, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
+
+            if (rc == 0) {
+                fd = rc;
+                gotFile = TRUE;
+            } else {
+                if (errno == EEXIST) {
+                    /* We'll just have to keep trying */
+                } else 
+                    error = TRUE;
+            }
+        }
+    }    
+    if (gotFile)
+        retval = fd;
+    else
+        retval = -1;
+
+    return retval;
+}
+
+
+
+static int
+mkstemp2(char * const filenameBuffer) {
+
+#if HAVE_MKSTEMP
+    if (0)
+        mkstempx(NULL);  /* defeat compiler unused function warning */
+    return mkstemp(filenameBuffer);
+#else
+    return mkstempx(filenameBuffer);
+#endif
+}
+
+
+
+static void
+makeTmpfileWithTemplate(const char *  const filenameTemplate,
+                        FILE **       const filePP,
+                        const char ** const filenameP,
+                        const char ** const errorP) {
+    
+    char * filenameBuffer;  /* malloc'ed */
+
+    filenameBuffer = strdup(filenameTemplate);
+
+    if (filenameBuffer == NULL)
+        asprintfN(errorP, "Unable to allocate storage for temporary "
+                  "file name");
+    else {
+        int rc;
+        
+        rc = mkstemp2(filenameBuffer);
+        
+        if (rc < 0)
+            asprintfN(errorP,
+                      "Unable to create temporary file according to name "
+                      "pattern '%s'.  mkstemp() failed with errno %d (%s)",
+                      filenameTemplate, errno, strerror(errno));
+        else {
+            int const fd = rc;
+            
+            FILE * fileP;
+            fileP = fdopen(fd, "w+b");
+            
+            if (fileP == NULL)
+                asprintfN(errorP, "Unable to create temporary file.  "
+                          "fdopen() failed with errno %d (%s)",
+                          errno, strerror(errno));
+            else {
+                *errorP = NULL;
+                *filePP = fileP;
+                *filenameP = filenameBuffer;
+            }
+            if (*errorP) {
+                unlink(filenameBuffer);
+                close(fd);
+            }
+        }
+        if (*errorP)
+            strfree(filenameBuffer);
+    }
+}
+
+
+
+void
+pm_make_tmpfile(FILE **       const filePP,
+                const char ** const filenameP) {
+
+    const char * filenameTemplate;
+    unsigned int fnamelen;
+    const char * tmpdir;
+    const char * dirseparator;
+    const char * error;
+
+    fnamelen = strlen(pm_progname) + 10; /* "/" + "_XXXXXX\0" */
+
+    tmpdir = tmpDir();
+
+    if (tmpdir[strlen(tmpdir) - 1] == '/')
+        dirseparator = "";
+    else
+        dirseparator = "/";
+    
+    asprintfN(&filenameTemplate, "%s%s%s%s", 
+              tmpdir, dirseparator, pm_progname, "_XXXXXX");
+
+    if (filenameTemplate == NULL)
+        asprintfN(&error,
+                  "Unable to allocate storage for temporary file name");
+    else {
+        makeTmpfileWithTemplate(filenameTemplate, filePP, filenameP, &error);
+
+        strfree(filenameTemplate);
+    }
+    if (error) {
+        pm_errormsg("%s", error);
+        strfree(error);
+        pm_longjmp();
+    }
+}
+
+
+
+FILE * 
+pm_tmpfile(void) {
+
+    FILE * fileP;
+    const char * tmpfile;
+
+    pm_make_tmpfile(&fileP, &tmpfile);
+
+    unlink(tmpfile);
+
+    strfree(tmpfile);
+
+    return fileP;
+}
+
+
+
+FILE *
+pm_openr_seekable(const char name[]) {
+/*----------------------------------------------------------------------------
+  Open the file named by name[] such that it is seekable (i.e. it can be
+  rewound and read in multiple passes with fseek()).
+
+  If the file is actually seekable, this reduces to the same as
+  pm_openr().  If not, we copy the named file to a temporary file
+  and return that file's stream descriptor.
+
+  We use a file that the operating system recognizes as temporary, so
+  it picks the filename and deletes the file when Caller closes it.
+-----------------------------------------------------------------------------*/
+    int stat_rc;
+    int seekable;  /* logical: file is seekable */
+    struct stat statbuf;
+    FILE * original_file;
+    FILE * seekable_file;
+
+    original_file = pm_openr((char *) name);
+
+    /* I would use fseek() to determine if the file is seekable and 
+       be a little more general than checking the type of file, but I
+       don't have reliable information on how to do that.  I have seen
+       streams be partially seekable -- you can, for example seek to
+       0 if the file is positioned at 0 but you can't actually back up
+       to 0.  I have seen documentation that says the errno for an
+       unseekable stream is EBADF and in practice seen ESPIPE.
+
+       On the other hand, regular files are always seekable and even if
+       some other file is, it doesn't hurt much to assume it isn't.
+    */
+
+    stat_rc = fstat(fileno(original_file), &statbuf);
+    if (stat_rc == 0 && S_ISREG(statbuf.st_mode))
+        seekable = TRUE;
+    else 
+        seekable = FALSE;
+
+    if (seekable) {
+        seekable_file = original_file;
+    } else {
+        seekable_file = pm_tmpfile();
+        
+        /* Copy the input into the temporary seekable file */
+        while (!feof(original_file) && !ferror(original_file) 
+               && !ferror(seekable_file)) {
+            char buffer[4096];
+            int bytes_read;
+            bytes_read = fread(buffer, 1, sizeof(buffer), original_file);
+            fwrite(buffer, 1, bytes_read, seekable_file);
+        }
+        if (ferror(original_file))
+            pm_error("Error reading input file into temporary file.  "
+                     "Errno = %s (%d)", strerror(errno), errno);
+        if (ferror(seekable_file))
+            pm_error("Error writing input into temporary file.  "
+                     "Errno = %s (%d)", strerror(errno), errno);
+        pm_close(original_file);
+        {
+            int seek_rc;
+            seek_rc = fseek(seekable_file, 0, SEEK_SET);
+            if (seek_rc != 0)
+                pm_error("fseek() failed to rewind temporary file.  "
+                         "Errno = %s (%d)", strerror(errno), errno);
+        }
+    }
+    return seekable_file;
+}
+
+
+
+void
+pm_close(FILE * const f) {
+    fflush(f);
+    if (ferror(f))
+        pm_message("A file read or write error occurred at some point");
+    if (f != stdin)
+        if (fclose(f) != 0)
+            pm_error("close of file failed with errno %d (%s)",
+                     errno, strerror(errno));
+}
+
+
+
+/* The pnmtopng package uses pm_closer() and pm_closew() instead of 
+   pm_close(), apparently because the 1999 Pbmplus package has them.
+   I don't know what the difference is supposed to be.
+*/
+
+void
+pm_closer(FILE * const f) {
+    pm_close(f);
+}
+
+
+
+void
+pm_closew(FILE * const f) {
+    pm_close(f);
+}
+
+
+
+/* Endian I/O.
+
+   Before Netpbm 10.27 (March 2005), these would return failure on EOF
+   or I/O failure.  For backward compatibility, they still have the return
+   code, but it is always zero and the routines abort the program in case
+   of EOF or I/O failure.  A program that wants to handle failure differently
+   must use lower level (C library) interfaces.  But that level of detail
+   is uncharacteristic of a Netpbm program; the ease of programming that
+   comes with not checking a return code is more Netpbm.
+
+   It is also for historical reasons that these return signed values,
+   when clearly unsigned would make more sense.
+*/
+
+
+
+static void
+abortWithReadError(FILE * const ifP) {
+
+    if (feof(ifP))
+        pm_error("Unexpected end of input file");
+    else
+        pm_error("Error (not EOF) reading file.");
+}
+
+
+
+void
+pm_readchar(FILE * const ifP,
+            char * const cP) {
+    
+    int c;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+
+    *cP = c;
+}
+
+
+
+void
+pm_writechar(FILE * const ofP,
+             char   const c) {
+
+    putc(c, ofP);
+}
+
+
+
+int
+pm_readbigshort(FILE *  const ifP, 
+                short * const sP) {
+    int c;
+
+    unsigned short s;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    s = (c & 0xff) << 8;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    s |= c & 0xff;
+
+    *sP = s;
+
+    return 0;
+}
+
+
+
+int
+pm_writebigshort(FILE * const ofP, 
+                 short  const s) {
+
+    putc((s >> 8) & 0xff, ofP);
+    putc(s & 0xff, ofP);
+
+    return 0;
+}
+
+
+
+int
+pm_readbiglong(FILE * const ifP, 
+               long * const lP) {
+
+    int c;
+    unsigned long l;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l = c << 24;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c << 16;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c << 8;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c;
+
+    *lP = l;
+
+    return 0;
+}
+
+
+
+int
+pm_writebiglong(FILE * const ofP, 
+                long   const l) {
+
+    putc((l >> 24) & 0xff, ofP);
+    putc((l >> 16) & 0xff, ofP);
+    putc((l >>  8) & 0xff, ofP);
+    putc((l >>  0) & 0xff, ofP);
+
+    return 0;
+}
+
+
+
+int
+pm_readlittleshort(FILE *  const ifP, 
+                   short * const sP) {
+    int c;
+    unsigned short s;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    s = c & 0xff;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    s |= (c & 0xff) << 8;
+
+    *sP = s;
+
+    return 0;
+}
+
+
+
+int
+pm_writelittleshort(FILE * const ofP, 
+                    short  const s) {
+
+    putc((s >> 0) & 0xff, ofP);
+    putc((s >> 8) & 0xff, ofP);
+
+    return 0;
+}
+
+
+
+int
+pm_readlittlelong(FILE * const ifP, 
+                  long * const lP) {
+    int c;
+    unsigned long l;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l = c;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c << 8;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c << 16;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c << 24;
+
+    *lP = l;
+
+    return 0;
+}
+
+
+
+int
+pm_writelittlelong(FILE * const ofP, 
+                   long   const l) {
+
+    putc((l >>  0) & 0xff, ofP);
+    putc((l >>  8) & 0xff, ofP);
+    putc((l >> 16) & 0xff, ofP);
+    putc((l >> 24) & 0xff, ofP);
+
+    return 0;
+}
+
+
+
+int 
+pm_readmagicnumber(FILE * const ifP) {
+
+    int ich1, ich2;
+
+    ich1 = getc(ifP);
+    ich2 = getc(ifP);
+    if (ich1 == EOF || ich2 == EOF)
+        pm_error( "Error reading magic number from Netpbm image stream.  "
+                  "Most often, this "
+                  "means your input file is empty." );
+
+    return ich1 * 256 + ich2;
+}
+
+
+
+/* Read a file of unknown size to a buffer. Return the number of bytes
+   read. Allocate more memory as we need it. The calling routine has
+   to free() the buffer.
+
+   Oliver Trepte, oliver@fysik4.kth.se, 930613
+*/
+
+#define PM_BUF_SIZE 16384      /* First try this size of the buffer, then
+                                  double this until we reach PM_MAX_BUF_INC */
+#define PM_MAX_BUF_INC 65536   /* Don't allocate more memory in larger blocks
+                                  than this. */
+
+char *
+pm_read_unknown_size(FILE * const file, 
+                     long * const nread) {
+    long nalloc;
+    char * buf;
+    bool eof;
+
+    *nread = 0;
+    nalloc = PM_BUF_SIZE;
+    MALLOCARRAY(buf, nalloc);
+
+    eof = FALSE;  /* initial value */
+
+    while(!eof) {
+        int val;
+
+        if (*nread >= nalloc) { /* We need a larger buffer */
+            if (nalloc > PM_MAX_BUF_INC)
+                nalloc += PM_MAX_BUF_INC;
+            else
+                nalloc += nalloc;
+            REALLOCARRAY_NOFAIL(buf, nalloc);
+        }
+
+        val = getc(file);
+        if (val == EOF)
+            eof = TRUE;
+        else 
+            buf[(*nread)++] = val;
+    }
+    return buf;
+}
+
+
+
+union cheat {
+    uint32n l;
+    short s;
+    unsigned char c[4];
+};
+
+
+
+short
+pm_bs_short(short const s) {
+    union cheat u;
+    unsigned char t;
+
+    u.s = s;
+    t = u.c[0];
+    u.c[0] = u.c[1];
+    u.c[1] = t;
+    return u.s;
+}
+
+
+
+long
+pm_bs_long(long const l) {
+    union cheat u;
+    unsigned char t;
+
+    u.l = l;
+    t = u.c[0];
+    u.c[0] = u.c[3];
+    u.c[3] = t;
+    t = u.c[1];
+    u.c[1] = u.c[2];
+    u.c[2] = t;
+    return u.l;
+}
+
+
+
+void
+pm_tell2(FILE *       const fileP, 
+         void *       const fileposP,
+         unsigned int const fileposSize) {
+/*----------------------------------------------------------------------------
+   Return the current file position as *filePosP, which is a buffer
+   'fileposSize' bytes long.  Abort the program if error, including if
+   *fileP isn't a file that has a position.
+-----------------------------------------------------------------------------*/
+    /* Note: FTELLO() is either ftello() or ftell(), depending on the
+       capabilities of the underlying C library.  It is defined in
+       pm_config.h.  ftello(), in turn, may be either ftell() or
+       ftello64(), as implemented by the C library.
+    */
+    pm_filepos const filepos = FTELLO(fileP);
+    if (filepos < 0)
+        pm_error("ftello() to get current file position failed.  "
+                 "Errno = %s (%d)\n", strerror(errno), errno);
+
+    if (fileposSize == sizeof(pm_filepos)) {
+        pm_filepos * const fileposP_filepos = fileposP;
+        *fileposP_filepos = filepos;
+    } else if (fileposSize == sizeof(long)) {
+        if (sizeof(pm_filepos) > sizeof(long) &&
+            filepos >= (pm_filepos) 1 << (sizeof(long)*8))
+            pm_error("File size is too large to represent in the %u bytes "
+                     "that were provided to pm_tell2()", fileposSize);
+        else {
+            long * const fileposP_long = fileposP;
+            *fileposP_long = (long)filepos;
+        }
+    } else
+        pm_error("File position size passed to pm_tell() is invalid: %u.  "
+                 "Valid sizes are %u and %u", 
+                 fileposSize, (unsigned int)sizeof(pm_filepos),
+                 (unsigned int) sizeof(long));
+}
+
+
+
+unsigned int
+pm_tell(FILE * const fileP) {
+    
+    long filepos;
+
+    pm_tell2(fileP, &filepos, sizeof(filepos));
+
+    return filepos;
+}
+
+
+
+void
+pm_seek2(FILE *             const fileP, 
+         const pm_filepos * const fileposP,
+         unsigned int       const fileposSize) {
+/*----------------------------------------------------------------------------
+   Position file *fileP to position *fileposP.  Abort if error, including
+   if *fileP isn't a seekable file.
+-----------------------------------------------------------------------------*/
+    if (fileposSize == sizeof(pm_filepos)) 
+        /* Note: FSEEKO() is either fseeko() or fseek(), depending on the
+           capabilities of the underlying C library.  It is defined in
+           pm_config.h.  fseeko(), in turn, may be either fseek() or
+           fseeko64(), as implemented by the C library.
+        */
+        FSEEKO(fileP, *fileposP, SEEK_SET);
+    else if (fileposSize == sizeof(long)) {
+        long const fileposLong = *(long *)fileposP;
+        fseek(fileP, fileposLong, SEEK_SET);
+    } else
+        pm_error("File position size passed to pm_seek() is invalid: %u.  "
+                 "Valid sizes are %u and %u", 
+                 fileposSize, (unsigned int)sizeof(pm_filepos),
+                 (unsigned int) sizeof(long));
+}
+
+
+
+void
+pm_seek(FILE * const fileP, unsigned long filepos) {
+/*----------------------------------------------------------------------------
+-----------------------------------------------------------------------------*/
+
+    pm_filepos fileposBuff;
+
+    fileposBuff = filepos;
+
+    pm_seek2(fileP, &fileposBuff, sizeof(fileposBuff));
+}
+
+
+
+void
+pm_nextimage(FILE * const file, int * const eofP) {
+/*----------------------------------------------------------------------------
+   Position the file 'file' to the next image in the stream, assuming it is
+   now positioned just after the current image.  I.e. read off any white
+   space at the end of the current image's raster.  Note that the raw formats
+   don't permit such white space, but this routine tolerates it anyway, 
+   because the plain formats do permit white space after the raster.
+
+   Iff there is no next image, return *eofP == TRUE.
+
+   Note that in practice, we will not normally see white space here in
+   a plain PPM or plain PGM stream because the routine to read a
+   sample from the image reads one character of white space after the
+   sample in order to know where the sample ends.  There is not
+   normally more than one character of white space (a newline) after
+   the last sample in the raster.  But plain PBM is another story.  No white
+   space is required between samples of a plain PBM image.  But the raster
+   normally ends with a newline nonetheless.  Since the sample reading code
+   will not have read that newline, it is there for us to read now.
+-----------------------------------------------------------------------------*/
+    bool eof;
+    bool nonWhitespaceFound;
+
+    eof = FALSE;
+    nonWhitespaceFound = FALSE;
+
+    while (!eof && !nonWhitespaceFound) {
+        int c;
+        c = getc(file);
+        if (c == EOF) {
+            if (feof(file)) 
+                eof = TRUE;
+            else
+                pm_error("File error on getc() to position to image");
+        } else {
+            if (!isspace(c)) {
+                int rc;
+
+                nonWhitespaceFound = TRUE;
+
+                /* Have to put the non-whitespace character back in
+                   the stream -- it's part of the next image.  
+                */
+                rc = ungetc(c, file);
+                if (rc == EOF) 
+                    pm_error("File error doing ungetc() "
+                             "to position to image.");
+            }
+        }
+    }
+    *eofP = eof;
+}
+
+
+
+void
+pm_check(FILE *               const file, 
+         enum pm_check_type   const check_type, 
+         pm_filepos           const need_raster_size,
+         enum pm_check_code * const retval_p) {
+/*----------------------------------------------------------------------------
+   This is not defined for use outside of libnetpbm.
+-----------------------------------------------------------------------------*/
+    struct stat statbuf;
+    pm_filepos curpos;  /* Current position of file; -1 if none */
+    int rc;
+
+#ifdef LARGEFILEDEBUG
+    pm_message("pm_filepos received by pm_check() is %u bytes.",
+               sizeof(pm_filepos));
+#endif
+    /* Note: FTELLO() is either ftello() or ftell(), depending on the
+       capabilities of the underlying C library.  It is defined in
+       pm_config.h.  ftello(), in turn, may be either ftell() or
+       ftello64(), as implemented by the C library.
+    */
+    curpos = FTELLO(file);
+    if (curpos >= 0) {
+        /* This type of file has a current position */
+            
+        rc = fstat(fileno(file), &statbuf);
+        if (rc != 0) 
+            pm_error("fstat() failed to get size of file, though ftello() "
+                     "successfully identified\n"
+                     "the current position.  Errno=%s (%d)",
+                     strerror(errno), errno);
+        else if (!S_ISREG(statbuf.st_mode)) {
+            /* Not a regular file; we can't know its size */
+            if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE;
+        } else {
+            pm_filepos const have_raster_size = statbuf.st_size - curpos;
+            
+            if (have_raster_size < need_raster_size)
+                pm_error("File has invalid format.  The raster should "
+                         "contain %u bytes, but\n"
+                         "the file ends after only %u bytes.",
+                         (unsigned int) need_raster_size, 
+                         (unsigned int) have_raster_size);
+            else if (have_raster_size > need_raster_size) {
+                if (retval_p) *retval_p = PM_CHECK_TOO_LONG;
+            } else {
+                if (retval_p) *retval_p = PM_CHECK_OK;
+            }
+        }
+    } else
+        if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE;
+}
+
+
+
+void
+pm_drain(FILE *         const fileP,
+         unsigned int   const limit,
+         unsigned int * const bytesReadP) {
+/*----------------------------------------------------------------------------
+  Read bytes from *fileP until EOF and return as *bytesReadP how many there
+  were.
+
+  But don't read any more than 'limit'.
+
+  This is a good thing to call after reading an input file to be sure you
+  didn't leave some input behind, which could mean you didn't properly
+  interpret the file.
+-----------------------------------------------------------------------------*/
+    unsigned int bytesRead;
+    bool eof;
+
+    for (bytesRead = 0, eof = false; !eof && bytesRead < limit;) {
+
+        int rc;
+
+        rc = fgetc(fileP);
+
+        eof = (rc == EOF);
+        if (!eof)
+            ++bytesRead;
+    }
+    *bytesReadP = bytesRead;
+}
diff --git a/lib/pnm.h b/lib/pnm.h
index 94d4ff85..19727c4c 100644
--- a/lib/pnm.h
+++ b/lib/pnm.h
@@ -4,7 +4,8 @@
 #ifndef _PNM_H_
 #define _PNM_H_
 
-#include "ppm.h"
+#include <netpbm/pm.h>
+#include <netpbm/ppm.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -28,7 +29,9 @@ typedef pixval xelval;
 
 /* Declarations of routines. */
 
-void pnm_init ARGS(( int* argcP, char* argv[] ));
+void
+pnm_init(int *   const argcP,
+         char ** const argv);
 
 void
 pnm_nextimage(FILE *file, int * const eofP);
@@ -36,7 +39,7 @@ pnm_nextimage(FILE *file, int * const eofP);
 xel *
 pnm_allocrow(unsigned int const cols);
 
-#define pnm_freerow(xelrow) free(xelrow)
+#define pnm_freerow(xelrow) pm_freerow(xelrow)
 
 #define pnm_allocarray( cols, rows ) \
   ((xel**) pm_allocarray( cols, rows, sizeof(xel) ))
diff --git a/lib/ppm.h b/lib/ppm.h
index 7f7d6446..604b9cb1 100644
--- a/lib/ppm.h
+++ b/lib/ppm.h
@@ -3,7 +3,8 @@
 #ifndef _PPM_H_
 #define _PPM_H_
 
-#include "pgm.h"
+#include <netpbm/pm.h>
+#include <netpbm/pgm.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -50,7 +51,7 @@ typedef struct {
 #define PPM_TYPE PPM_FORMAT
 
 
-#include "ppmcmap.h"
+#include <netpbm/ppmcmap.h>
 
 /* Macro for turning a format number into a type number. */
 
@@ -85,7 +86,7 @@ ppm_allocrow(unsigned int const cols);
 
 #define ppm_freearray(pixels, rows) pm_freearray((char**) pixels, rows)
 
-#define ppm_freerow(pixelrow) free(pixelrow);
+#define ppm_freerow(pixelrow) pm_freerow(pixelrow);
 
 pixel**
 ppm_readppm(FILE *   const fileP, 
diff --git a/lib/ppmcmap.h b/lib/ppmcmap.h
index b44dcbea..a39d27c0 100644
--- a/lib/ppmcmap.h
+++ b/lib/ppmcmap.h
@@ -89,13 +89,39 @@ void
 ppm_freecolorhash(colorhash_table const cht);
 
 
-colorhash_table ppm_colorrowtocolorhash ARGS((pixel *colorrow, int ncolors));
-pixel * ppm_computecolorrow ARGS((pixel **pixels, int cols, int rows, int maxcolors, int *ncolorsP));
-pixel * ppm_mapfiletocolorrow ARGS((FILE *file, int maxcolors, int *ncolorsP, pixval *maxvalP));
-void    ppm_colorrowtomapfile ARGS((FILE *ofp, pixel *colormap, int ncolors, pixval maxval));
-void    ppm_sortcolorrow (pixel * const colorrow, const int ncolors, 
-                          int (*cmpfunc)(pixel *, pixel *) );
-int     ppm_addtocolorrow ARGS((pixel *colorrow, int *ncolorsP, int maxcolors, pixel *pixelP));
+colorhash_table
+ppm_colorrowtocolorhash(pixel * const colorrow,
+                        int     const ncolors);
+
+pixel *
+ppm_computecolorrow(pixel ** const pixels,
+                    int      const cols,
+                    int      const rows,
+                    int      const maxcolors,
+                    int *    const ncolorsP);
+
+pixel *
+ppm_mapfiletocolorrow(FILE *   const fileP,
+                      int      const maxcolors,
+                      int *    const ncolorsP,
+                      pixval * const maxvalP);
+
+void
+ppm_colorrowtomapfile(FILE *  const ofP,
+                      pixel * const colormap,
+                      int     const ncolors,
+                      pixval  const maxval);
+
+void
+ppm_sortcolorrow(pixel * const colorrow,
+                 int     const ncolors, 
+                 int (*cmpfunc)(pixel *, pixel *));
+
+int
+ppm_addtocolorrow(pixel * const colorrow,
+                  int *   const ncolorsP,
+                  int     const maxcolors,
+                  pixel * const pixelP);
 
 int
 ppm_findclosestcolor(const pixel * const colormap, 
diff --git a/lib/util/Makefile b/lib/util/Makefile
index d770555f..49396010 100644
--- a/lib/util/Makefile
+++ b/lib/util/Makefile
@@ -17,7 +17,7 @@ all: $(UTILOBJECTS)
 
 include $(SRCDIR)/Makefile.common
 
-INCLUDES = -I $(BUILDDIR) -I $(SRCDIR)/$(SUBDIR)/..
+INCLUDES = -I$(SRCDIR)/$(SUBDIR) -I. -Iimportinc
 
 $(UTILOBJECTS):%.o:%.c importinc
 	$(CC) -c $(INCLUDES) -DNDEBUG $(CPPFLAGS) $(CFLAGS) $(CFLAGS_SHLIB) \
diff --git a/lib/util/mallocvar.h b/lib/util/mallocvar.h
index e5b7b1ea..b750e177 100644
--- a/lib/util/mallocvar.h
+++ b/lib/util/mallocvar.h
@@ -83,7 +83,7 @@ reallocProduct(void **      const blockP,
     arrayName = array; \
 } while (0)
 
-#define REALLOCARRAY(arrayName, nElements) { \
+#define REALLOCARRAY(arrayName, nElements) do { \
     void * array; \
     array = arrayName; \
     reallocProduct(&array, nElements, sizeof(arrayName[0])); \
diff --git a/lib/util/nstring.c b/lib/util/nstring.c
index feb8215e..0fa78c7a 100644
--- a/lib/util/nstring.c
+++ b/lib/util/nstring.c
@@ -915,3 +915,43 @@ strishex(const char * const subject) {
 
     return retval;
 }
+
+
+
+void
+interpret_uint(const char *   const string,
+               unsigned int * const valueP,
+               const char **  const errorP) {
+
+    if (string[0] == '\0')
+        asprintfN(errorP, "Null string.");
+    else {
+        /* strtoul() does a bizarre thing where if the number is out
+           of range, it returns a clamped value but tells you about it
+           by setting errno = ERANGE.  If it is not out of range,
+           strtoul() leaves errno alone.
+        */
+        char * tail;
+        unsigned long ulongValue;
+        
+        errno = 0;  /* So we can tell if strtoul() overflowed */
+
+        ulongValue = strtoul(string, &tail, 10);
+
+        if (tail[0] != '\0')
+            asprintfN(errorP, "Non-digit stuff in string: %s", tail);
+        else if (errno == ERANGE)
+            asprintfN(errorP, "Number too large");
+        else if (ulongValue > UINT_MAX)
+            asprintfN(errorP, "Number too large");
+        else if (string[0] == '-')
+            asprintfN(errorP, "Negative number");
+            /* Sleazy code; string may have leading spaces. */
+        else {
+            *valueP = ulongValue;
+            *errorP = NULL;
+        }
+    }
+}
+
+
diff --git a/lib/util/nstring.h b/lib/util/nstring.h
index 9d61cfa5..1e5ca4c3 100644
--- a/lib/util/nstring.h
+++ b/lib/util/nstring.h
@@ -183,6 +183,11 @@ memmemN(const void * const haystackArg,
 bool
 strishex(const char * const subject);
 
+void
+interpret_uint(const char *   const string,
+               unsigned int * const valueP,
+               const char **  const errorP);
+
 #ifdef __cplusplus
 }
 #endif