386BSD 0.1 development
[unix-history] / usr / src / usr.bin / hexdump / display.c
index 1fdceb8..01175c9 100644 (file)
@@ -2,31 +2,51 @@
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)display.c  5.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)display.c  5.11 (Berkeley) 3/9/91";
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <errno.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <stdio.h>
-#include <strings.h>
+#include <stdlib.h>
+#include <string.h>
 #include "hexdump.h"
 
 #include "hexdump.h"
 
+enum _vflag vflag = FIRST;
+
 static off_t address;                  /* address/offset in stream */
 static off_t eaddress;                 /* end address */
 static off_t savaddress;               /* saved address/offset in stream */
 static off_t address;                  /* address/offset in stream */
 static off_t eaddress;                 /* end address */
 static off_t savaddress;               /* saved address/offset in stream */
@@ -145,11 +165,14 @@ display()
                    }
        if (endfu) {
                /*
                    }
        if (endfu) {
                /*
-                * if eaddress not set, file size was multiple of blocksize,
-                * and no partial block ever found.
+                * if eaddress not set, error or file size was multiple of
+                * blocksize, and no partial block ever found.
                 */
                 */
-               if (!eaddress)
-                       eaddress = address + blocksize; 
+               if (!eaddress) {
+                       if (!address)
+                               return;
+                       eaddress = address;
+               }
                for (pr = endfu->nextpr; pr; pr = pr->nextpr)
                        switch(pr->flags) {
                        case F_ADDRESS:
                for (pr = endfu->nextpr; pr; pr = pr->nextpr)
                        switch(pr->flags) {
                        case F_ADDRESS:
@@ -179,6 +202,8 @@ bpad(pr)
        while (*p2++ = *p1++);
 }
 
        while (*p2++ = *p1++);
 }
 
+static char **_argv;
+
 u_char *
 get()
 {
 u_char *
 get()
 {
@@ -220,6 +245,9 @@ get()
                n = fread((char *)curp + nread, sizeof(u_char),
                    length == -1 ? need : MIN(length, need), stdin);
                if (!n) {
                n = fread((char *)curp + nread, sizeof(u_char),
                    length == -1 ? need : MIN(length, need), stdin);
                if (!n) {
+                       if (ferror(stdin))
+                               (void)fprintf(stderr, "hexdump: %s: %s\n",
+                                   _argv[-1], strerror(errno));
                        ateof = 1;
                        continue;
                }
                        ateof = 1;
                        continue;
                }
@@ -227,8 +255,9 @@ get()
                if (length != -1)
                        length -= n;
                if (!(need -= n)) {
                if (length != -1)
                        length -= n;
                if (!(need -= n)) {
-                       if (vflag == ALL || bcmp(curp, savp, blocksize)) {
-                               if (vflag == DUP)
+                       if (vflag == ALL || vflag == FIRST ||
+                           bcmp(curp, savp, blocksize)) {
+                               if (vflag == DUP || vflag == FIRST)
                                        vflag = WAIT;
                                return(curp);
                        }
                                        vflag = WAIT;
                                return(curp);
                        }
@@ -251,7 +280,6 @@ next(argv)
 {
        extern int errno, exitval;
        static int done;
 {
        extern int errno, exitval;
        static int done;
-       static char **_argv;
        int statok;
 
        if (argv) {
        int statok;
 
        if (argv) {
@@ -315,7 +343,7 @@ char *
 emalloc(size)
        int size;
 {
 emalloc(size)
        int size;
 {
-       char *p, *malloc();
+       char *p;
 
        if (!(p = malloc((u_int)size)))
                nomem();
 
        if (!(p = malloc((u_int)size)))
                nomem();
@@ -325,6 +353,8 @@ emalloc(size)
 
 nomem()
 {
 
 nomem()
 {
-       (void)fprintf(stderr, "hexdump: out of memory.\n");
+       extern int errno;
+
+       (void)fprintf(stderr, "hexdump: %s.\n", strerror(errno));
        exit(1);
 }
        exit(1);
 }