OSDN Git Service

(none)
[hos/hos-v4a.git] / aplfw / application / filecmd / filedump / filedump_main.c
1 /** 
2  *  Hyper Operating System  Application Framework
3  *
4  * @file  filedump_main.c
5  * @brief %jp{ファイルダンプコマンド}
6  *
7  * Copyright (C) 2006-2007 by Project HOS
8  * http://sourceforge.jp/projects/hos/
9  */
10
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include "hosaplfw.h"
15 #include "filedump.h"
16
17 #define FILEDUMP_BUFSIZE        4096
18
19
20 int FileDump_Main(int argc, char *argv[])
21 {
22         HANDLE                  hFile;
23         unsigned char   *pubBuf;
24         unsigned long   ulSize = 0x7fffffff;
25         unsigned long   ulPos  = 0;
26         int                             iReadSize;
27         int                             i;
28
29         /* 引数チェック */
30         if ( argc < 2 )
31         {
32                 StdIo_PrintFormat(
33                                 "<usage>\n"
34                                 " %s filename [size] [offest]\n\n",
35                                 argv[0]
36                         );
37                 return 1;
38         }
39
40         /* コマンドライン解析 */
41         if ( argc >= 3 )
42         {
43                 ulSize = (int)strtoul(argv[2], 0, 0);
44         }
45         if ( argc >= 4 )
46         {
47                 ulPos = (int)strtoul(argv[3], 0, 0);
48         }
49
50         
51         /* ファイルをを開く */  
52         if ( (hFile = File_Open(argv[1], FILE_OPEN_READ)) == HANDLE_NULL )
53         {
54                 return 1;
55         }
56         ulPos = (unsigned long)File_Seek(hFile, ulPos, FILE_SEEK_SET);
57                 
58         /* バッファ確保 */
59         pubBuf = (unsigned char *)Memory_Alloc(FILEDUMP_BUFSIZE);
60         if ( pubBuf == NULL )
61         {
62                 File_Close(hFile);
63                 return 1;
64         }
65         
66         
67         while ( ulSize > 0 )
68         {
69                 if ( ulSize > FILEDUMP_BUFSIZE )
70                 {
71                         iReadSize = FILEDUMP_BUFSIZE;
72                 }
73                 else
74                 {
75                         iReadSize = (int)ulSize;
76                 }
77                 
78                 iReadSize = File_Read(hFile, pubBuf, iReadSize);
79                 if ( iReadSize <= 0 )
80                 {
81                         break;
82                 }
83                 ulSize -= iReadSize;
84                 
85                 for ( i = 0; i < iReadSize; i++ )
86                 {
87                         if ( i % 16 == 0 )
88                         {
89                                 StdIo_PrintFormat("%08lx: ", ulPos);
90                         }
91                         StdIo_PrintFormat("%02x ",  pubBuf[i]);
92                         ulPos++;
93                         if ( i % 16 == 15 )
94                         {
95                                 StdIo_PrintFormat("\n");
96                         }
97                 }
98         }
99         StdIo_PrintFormat("\n");
100         
101         
102         /* メモリ開放 */
103         Memory_Free(pubBuf);
104
105         /* ファイルを閉じる */  
106         File_Close(hFile);
107
108         return 0;
109 }
110
111
112 /* end of file */