2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
9 /******************************************************************************
13 Type {<file | pattern>} [TO <name>] [OPT H | N] [HEX | NUMBER]
17 FROM/A/M,TO/K,OPT/K,HEX/S,NUMBER/S
25 Displays content of a file
29 FROM -- one or more files to display
30 TO -- print output to file
31 OPT -- H or N (see HEX or NUMBER)
32 HEX -- displays output in hexadecimal format
33 NUMBER -- the lines are numbered
34 HEX and NUMBER are mutually exclusive
53 ******************************************************************************/
55 #include <exec/memory.h>
56 #include <exec/execbase.h>
57 #include <proto/exec.h>
58 #include <proto/dos.h>
63 static const char version
[] = "$VER: Type 42.0 (20.10.2005)\n";
66 #define MAX_PATH_LEN 512
87 const UBYTE hs
[16]="0123456789abcdef";
89 #define putc(f,c) (*(f)->cur++=(c),--(f)->cnt?0:put(f))
90 static int put(struct file
*f
)
98 subsize
=Write(f
->fd
,buf
,size
);
112 #define getc(f) ((f)->cnt--?*(f)->cur++:get(f))
113 static int get(struct file
*f
)
116 size
=Read(f
->fd
,f
->buf
,BUFSIZE
);
126 static void putlinequick(struct file
*f
, ULONG offset
, UBYTE
*buf
)
135 if(offset
>=0x1000000)
137 if(offset
>=0x10000000)
139 *o
++=hs
[(offset
>>28)&0xf];
142 *o
++=hs
[(offset
>>24)&0xf];
145 *o
++=hs
[(offset
>>20)&0xf];
148 *o
++=hs
[(offset
>>16)&0xf];
151 *o
++=hs
[(offset
>>12)&0xf];
152 *o
++=hs
[(offset
>>8)&0xf];
153 *o
++=hs
[(offset
>>4)&0xf];
172 *o
++=(c
&0x7f)>=0x20&&c
!=0x7f?c
:'.';
179 static int putline(struct file
*f
, ULONG offset
, UBYTE
*buf
, ULONG num
)
185 if(offset
>=0x10000000&&putc(f
,hs
[(offset
>>28)&0xf]))
187 if(offset
>=0x1000000&&putc(f
,hs
[(offset
>>24)&0xf]))
189 if(offset
>=0x100000&&putc(f
,hs
[(offset
>>20)&0xf]))
191 if(offset
>=0x10000&&putc(f
,hs
[(offset
>>16)&0xf]))
194 if(putc(f
,hs
[(offset
>>12)&0xf]))
196 if(putc(f
,hs
[(offset
>>8)&0xf]))
198 if(putc(f
,hs
[(offset
>>4)&0xf]))
200 if(putc(f
,hs
[offset
&0xf]))
214 if(putc(f
,hs
[c
&0xf]))
231 if(putc(f
,(c
&0x7f)>=0x20&&c
!=0x7f?c
:'.'))
239 LONG
hexdumpfile(struct file
*in
, struct file
*out
)
243 LONG offset
=0, n
, c
, tty
;
244 LONG retval
= RETURN_OK
;
246 tty
=IsInteractive(out
->fd
);
271 putlinequick(out
,offset
,b
);
273 if(putline(out
,offset
,b
,n
))
275 retval
= RETURN_ERROR
;
281 putline(out
,offset
,b
,n
);
282 if(out
->cur
!=out
->buf
)
289 retval
= RETURN_ERROR
;
294 if (CheckSignal(SIGBREAKF_CTRL_C
))
296 retval
= RETURN_WARN
;
305 void putlinenumber(struct file
* out
, unsigned short line
)
314 putc(out
, line
/x
+'0');
327 LONG
dumpfile(struct file
*in
, struct file
*out
, BOOL showline
)
330 unsigned short line
= 0;
331 LONG retval
= RETURN_OK
;
334 putlinenumber(out
, ++line
);
336 if(1/*IsInteractive(out->fd)*/)
350 if (lastc
==0x0a && showline
)
351 putlinenumber(out
, ++line
);
353 if(putc(out
,c
)||(c
=='\n' && put(out
)))
359 retval
= RETURN_ERROR
;
363 if ((c
== '\n') && CheckSignal(SIGBREAKF_CTRL_C
))
365 retval
= RETURN_WARN
;
374 static LONG
processfile(CONST_STRPTR name
, struct file
*in
, struct file
*out
, IPTR
*args
, LONG
*numfiles
)
378 in
->fd
= Open(name
, MODE_OLDFILE
);
385 error
= hexdumpfile(in
, out
);
387 error
= dumpfile(in
, out
, args
[3]);
401 struct AnchorPath apath
;
402 UBYTE buf
[MAX_PATH_LEN
- 1];
407 IPTR args
[5]={ 0, 0, 0, 0, 0 };
409 struct file
*in
, *out
;
411 int retval
= RETURN_OK
;
412 struct MyAnchorPath apath
;
414 rda
=ReadArgs("FROM/A/M,TO/K,OPT/K,HEX/S,NUMBER/S",args
,NULL
);
417 PrintFault(IoErr(),"Type");
420 names
=(STRPTR
*)args
[0];
422 in
=AllocMem(sizeof(struct file
),MEMF_ANY
);
423 out
=AllocMem(sizeof(struct file
),MEMF_ANY
);
425 if(in
!=NULL
&&out
!=NULL
)
429 apath
.apath
.ap_BreakBits
= SIGBREAKF_CTRL_C
;
430 apath
.apath
.ap_FoundBreak
= 0;
431 apath
.apath
.ap_Flags
= 0;
432 apath
.apath
.ap_Strlen
= MAX_PATH_LEN
;
434 out
->fd
= Open((STRPTR
) args
[1], MODE_NEWFILE
);
444 for (error
= MatchFirst(*names
, &apath
.apath
);
446 error
= MatchNext(&apath
.apath
))
448 error
= processfile(apath
.apath
.ap_Buf
, in
, out
, args
, &numfiles
);
452 MatchEnd(&apath
.apath
);
454 if (numfiles
== 0 && error
== ERROR_NO_MORE_ENTRIES
)
459 if (error
&& error
!= ERROR_NO_MORE_ENTRIES
)
461 if (*names
&& error
!= ERROR_BREAK
)
463 Printf("TYPE: can't open %s\n", (LONG
) *names
);
468 PrintFault(error
, NULL
);
480 /* If all files got dumped, return ok, else error.
482 retval
= *names
? RETURN_ERROR
: RETURN_OK
;
485 PrintFault(IoErr(), NULL
);
489 PrintFault(ERROR_NO_FREE_STORE
,"Type");
490 retval
= RETURN_ERROR
;
494 FreeMem(in
,sizeof(struct file
));
496 FreeMem(out
,sizeof(struct file
));