summaryrefslogtreecommitdiffstats
path: root/text-utils/display.c
diff options
context:
space:
mode:
authorSami Kerola2011-02-12 21:55:53 +0100
committerKarel Zak2011-02-21 14:38:23 +0100
commit85bf44b714ab184907eb448eba389218956d6a51 (patch)
treebe829d3b7f32bcc4583712b923d59998e87da8f8 /text-utils/display.c
parentipcs: really show all resources when -a and -i are combined (diff)
downloadkernel-qcow2-util-linux-85bf44b714ab184907eb448eba389218956d6a51.tar.gz
kernel-qcow2-util-linux-85bf44b714ab184907eb448eba389218956d6a51.tar.xz
kernel-qcow2-util-linux-85bf44b714ab184907eb448eba389218956d6a51.zip
hexdump: new usage(), xalloc and err.h stuff
New usage help screen and print version switch. Also fixes to exit codes, util linux xmalloc replaced emalloc and every error print is using libc error function. [kzak@redhat.com: - minor changes in formatting and coding style] Signed-off-by: Sami Kerola <kerolasa@iki.fi> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'text-utils/display.c')
-rw-r--r--text-utils/display.c44
1 files changed, 11 insertions, 33 deletions
diff --git a/text-utils/display.c b/text-utils/display.c
index 7a35e46fb..01805a20f 100644
--- a/text-utils/display.c
+++ b/text-utils/display.c
@@ -40,6 +40,7 @@
#include <stdlib.h>
#include <string.h>
#include "hexdump.h"
+#include "xalloc.h"
static void doskip(const char *, int);
static u_char *get(void);
@@ -227,13 +228,13 @@ get(void)
{
static int ateof = 1;
static u_char *curp, *savp;
- int n;
+ ssize_t n;
int need, nread;
u_char *tmpp;
if (!curp) {
- curp = emalloc(blocksize);
- savp = emalloc(blocksize);
+ curp = xmalloc(blocksize);
+ savp = xmalloc(blocksize);
} else {
tmpp = curp;
curp = savp;
@@ -264,8 +265,7 @@ get(void)
length == -1 ? need : MIN(length, need), stdin);
if (!n) {
if (ferror(stdin))
- (void)fprintf(stderr, "hexdump: %s: %s\n",
- _argv[-1], strerror(errno));
+ warn("%s", _argv[-1]);
ateof = 1;
continue;
}
@@ -303,9 +303,8 @@ int next(char **argv)
for (;;) {
if (*_argv) {
if (!(freopen(*_argv, "r", stdin))) {
- (void)fprintf(stderr, "hexdump: %s: %s\n",
- *_argv, strerror(errno));
- exitval = 1;
+ warn("%s", *_argv);
+ exitval = EXIT_FAILURE;
++_argv;
continue;
}
@@ -331,11 +330,8 @@ doskip(const char *fname, int statok)
struct stat sbuf;
if (statok) {
- if (fstat(fileno(stdin), &sbuf)) {
- (void)fprintf(stderr, "hexdump: %s: %s.\n",
- fname, strerror(errno));
- exit(1);
- }
+ if (fstat(fileno(stdin), &sbuf))
+ err(EXIT_FAILURE, "%s", fname);
if (S_ISREG(sbuf.st_mode) && skip > sbuf.st_size) {
/* If size valid and skip >= size */
skip -= sbuf.st_size;
@@ -344,26 +340,8 @@ doskip(const char *fname, int statok)
}
}
/* sbuf may be undefined here - do not test it */
- if (fseek(stdin, skip, SEEK_SET)) {
- (void)fprintf(stderr, "hexdump: %s: %s.\n",
- fname, strerror(errno));
- exit(1);
- }
+ if (fseek(stdin, skip, SEEK_SET))
+ err(EXIT_FAILURE, "%s", fname);
address += skip;
skip = 0;
}
-
-void *
-emalloc(int sz) {
- void *p;
-
- if (!(p = malloc((u_int)sz)))
- nomem();
- memset(p, 0, sz);
- return(p);
-}
-
-void nomem() {
- (void)fprintf(stderr, "hexdump: %s.\n", strerror(errno));
- exit(1);
-}