Fixed compatibility of output.
[AROS.git] / test / exec / dumpmem.c
blobab7e6119f804cb6b2893c78134db9c8c22d96fbb
1 /*
2 Copyright © 1995-2014 The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Memory Dump util functions.
6 Lang: english
7 */
9 #include <aros/debug.h>
10 #include <exec/memory.h>
11 #include <dos/dos.h>
12 #include <dos/exall.h>
13 #include <dos/datetime.h>
14 #include <proto/dos.h>
15 #include <proto/utility.h>
16 #include <utility/tagitem.h>
17 #include <utility/utility.h>
19 #include <proto/alib.h>
20 #include <proto/exec.h>
21 #include <proto/dos.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
28 int (*outfunc)(const char *, ...);
30 static int HexDump(const UBYTE *data, ULONG count)
32 ULONG t, end;
33 int i;
35 end = (count + 15) & -16;
37 for (t=0; t<end; t++)
39 if ( SetSignal(0L,0L) & SIGBREAKF_CTRL_C )
41 SetIoErr( ERROR_BREAK );
42 return RETURN_WARN;
45 if ((t&15) == 0)
46 outfunc("%p:", data + t);
48 if ((t&3) == 0)
49 outfunc(" ");
51 if (t < count)
52 outfunc("%02x", ((UBYTE *)data)[t]);
53 else
54 outfunc(" ");
56 if ((t&15) == 15)
58 outfunc(" ");
60 for (i=15; i>=0; i--)
62 UBYTE c = data[t-i];
64 if (isprint(c))
65 outfunc("%c", c);
66 else
67 outfunc(".");
69 outfunc("\n");
72 return RETURN_OK;
73 } /* HexDump */
75 static const char version[] __attribute__((used)) = "$VER: dumpmem 45.1 (10.4.2014)\n";
77 #define ARG_TEMPLATE "ADDRESS/A,SIZE/N/A,SERIAL/S,QUIET/S"
79 enum
81 ARG_ADDRESS,
82 ARG_SIZE,
83 ARG_SERIAL,
84 ARG_QUIET,
85 NOOFARGS
88 int main(int argc, char **argv)
90 IPTR args[NOOFARGS] =
92 0, // ARG_ADDRESS
93 0, // ARG_SIZE
94 FALSE, // ARG_SERIAL
95 FALSE // ARG_QUIET
98 struct RDArgs *rda;
99 IPTR start_address,dump_size = 0;
100 int PROGRAM_ERROR = RETURN_OK;
101 char *ERROR_TEXT = NULL;
102 char *HELPTXT;
104 HELPTXT =
105 "ADDRESS The start address to dump from (in hex)\n"
106 "SIZE The number of bytes to dump\n"
107 "SERIAL If specified, output will use serial debugging instead of stdout\n"
108 "QUIET Do not display warnings\n";
110 rda = ReadArgs(ARG_TEMPLATE, args, NULL);
111 if (NULL != rda)
113 if (args[ARG_ADDRESS]!=0)
116 if (args[ARG_SIZE]!=0)
118 BOOL serial_out = (BOOL)args[ARG_SERIAL];
119 BOOL quiet_out = (BOOL)args[ARG_QUIET];
120 ULONG *sizptr = (ULONG *)args[ARG_SIZE];
121 char * pEnd;
123 if ( sizptr != NULL ) dump_size = *sizptr;
124 else PROGRAM_ERROR = RETURN_FAIL;
126 start_address = strtoul((CONST_STRPTR) args[ARG_ADDRESS], &pEnd, 16 );
128 if ( dump_size <= 0)
130 ERROR_TEXT = "Size must be a positive value\n";
131 PROGRAM_ERROR = RETURN_FAIL;
134 /* a quick warning */
135 if ( ( !quiet_out ) && ( PROGRAM_ERROR != RETURN_FAIL ) )
137 TEXT szInput[256];
138 printf( "\n *Some memory areas should NOT be read\n use this tool carefully!\n would you like to proceed? ");
140 gets ( szInput );
142 printf( "\n" );
144 if ( ( strncmp( szInput, "n", 1) == 0 )
145 || ( strncmp( szInput, "N", 1) == 0 ) )
147 ERROR_TEXT = "User canceled...\n";
148 PROGRAM_ERROR = RETURN_FAIL;
152 outfunc = serial_out ? (APTR)kprintf : (APTR)printf;
154 if ( PROGRAM_ERROR != RETURN_FAIL )
156 outfunc("dumpmem - Memory Dump tool.\n"
157 "© Copyright the AROS Dev Team.\n"
158 "-----------------------------\n\n"
159 "Dumping From [%p] for %d bytes...\n\n",
160 (void *)start_address, dump_size);
162 PROGRAM_ERROR = HexDump((const UBYTE *)start_address, dump_size);
164 outfunc("\n\nDump Complete.\n");
167 else ERROR_TEXT = HELPTXT;
169 else ERROR_TEXT = HELPTXT;
170 FreeArgs(rda);
172 else ERROR_TEXT = HELPTXT;
174 if (ERROR_TEXT) puts( ERROR_TEXT );
175 return PROGRAM_ERROR;