commit 8fa085f1d375dade1e0423b00d22549dc7604af5
parent 8484ac15905bfff3892d9a7dcb3475ba018bb8e4
Author: sin <sin@2f30.org>
Date: Mon, 4 Mar 2013 13:14:12 +0000
memzap: Use getopt()
Diffstat:
M | memzap.c | | | 53 | +++++++++++++++++++++++++++++++++++++++++++---------- |
M | proto.h | | | 1 | + |
2 files changed, 44 insertions(+), 10 deletions(-)
diff --git a/memzap.c b/memzap.c
@@ -4,6 +4,17 @@
static int verbose = 0;
+static void
+usage(const char *prog)
+{
+ fprintf(stderr, "usage: %s [OPTIONS] prog\n", prog);
+ fprintf(stderr, " -s\tSpecify symbol to trace\n");
+ fprintf(stderr, " -r\tSpecify region to trace\n");
+ fprintf(stderr, " -l\tLength of traced region\n");
+ fprintf(stderr, " -v\tEnable verbose output\n");
+ fprintf(stderr, " -h\tThis help screen\n");
+}
+
int
main(int argc, char *argv[])
{
@@ -11,22 +22,44 @@ main(int argc, char *argv[])
struct mem_tree *mt_old;
struct mem_region_diff *rdiff;
struct mdiff_hdr hdr;
+ int c;
int stat;
pid_t pid;
unsigned char *buf, *buf_new;
- char *addr;
- size_t len;
+ char *addr = NULL;
+ size_t len = 0;
int fd;
+ char *prog = *argv;
+
+ while ((c = getopt(argc, argv, "hs:l:r:v")) != -1) {
+ switch (c) {
+ case 'h':
+ usage(prog);
+ return 0;
+ case 'v':
+ verbose = 1;
+ break;
+ case 'r':
+ addr = (char *)strtoul(optarg, NULL, 16);
+ break;
+ case 'l':
+ len = strtoul(optarg, NULL, 10);
+ break;
+ case '?':
+ default:
+ return 1;
+ }
+ }
+ argc -= optind;
+ argv += optind;
- if (argc != 4) {
- fprintf(stderr, "usage: %s <program> <address> <len>\n", *argv);
+ if (argc != 1) {
+ usage(prog);
return 1;
}
- setbuf(stdout, NULL);
-
- addr = (char *)strtoul(argv[2], NULL, 16);
- len = strtoul(argv[3], NULL, 10);
+ if (!addr || !len)
+ errx(1, "no memory region/len specified");
if (verbose > 0)
printf("Base address: %p, length: %zu\n",
@@ -39,7 +72,7 @@ main(int argc, char *argv[])
switch (pid) {
case 0:
traceme();
- execl(argv[1], argv[1], (char *)NULL);
+ execl(*argv, *argv, (char *)NULL);
_Exit(1);
default:
break;
@@ -65,7 +98,7 @@ main(int argc, char *argv[])
fd = create_mdiff("test.mdiff");
memset(&hdr, 0, sizeof(hdr));
- strncpy(hdr.magic, "MDIFF", 5);
+ strncpy((char *)hdr.magic, "MDIFF", 5);
hdr.blksize = BLKSIZE;
mdiff_start_diff(fd);
do {
diff --git a/proto.h b/proto.h
@@ -19,6 +19,7 @@
#include <limits.h>
#include <signal.h>
#include <err.h>
+#include <getopt.h>
#include "queue.h"
#include "tree.h"