2 Copyright © 1995-2008, 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
51 ******************************************************************************/
53 #include <exec/memory.h>
54 #include <exec/execbase.h>
55 #include <proto/exec.h>
56 #include <proto/dos.h>
61 const TEXT version
[] = "$VER: Type 42.1 (20.10.2005)\n";
64 #define MAX_PATH_LEN 512
85 const UBYTE hs
[16]="0123456789abcdef";
87 #define putc(f,c) (*(f)->cur++=(c),--(f)->cnt?0:put(f))
88 static int put(struct file
*f
)
96 subsize
=Write(f
->fd
,buf
,size
);
110 #define getc(f) ((f)->cnt--?*(f)->cur++:get(f))
111 static int get(struct file
*f
)
114 size
=Read(f
->fd
,f
->buf
,BUFSIZE
);
124 static void putlinequick(struct file
*f
, ULONG offset
, UBYTE
*buf
)
133 if(offset
>=0x1000000)
135 if(offset
>=0x10000000)
137 *o
++=hs
[(offset
>>28)&0xf];
140 *o
++=hs
[(offset
>>24)&0xf];
143 *o
++=hs
[(offset
>>20)&0xf];
146 *o
++=hs
[(offset
>>16)&0xf];
149 *o
++=hs
[(offset
>>12)&0xf];
150 *o
++=hs
[(offset
>>8)&0xf];
151 *o
++=hs
[(offset
>>4)&0xf];
170 *o
++=(c
&0x7f)>=0x20&&c
!=0x7f?c
:'.';
177 static int putline(struct file
*f
, ULONG offset
, UBYTE
*buf
, ULONG num
)
183 if(offset
>=0x10000000&&putc(f
,hs
[(offset
>>28)&0xf]))
185 if(offset
>=0x1000000&&putc(f
,hs
[(offset
>>24)&0xf]))
187 if(offset
>=0x100000&&putc(f
,hs
[(offset
>>20)&0xf]))
189 if(offset
>=0x10000&&putc(f
,hs
[(offset
>>16)&0xf]))
192 if(putc(f
,hs
[(offset
>>12)&0xf]))
194 if(putc(f
,hs
[(offset
>>8)&0xf]))
196 if(putc(f
,hs
[(offset
>>4)&0xf]))
198 if(putc(f
,hs
[offset
&0xf]))
212 if(putc(f
,hs
[c
&0xf]))
229 if(putc(f
,(c
&0x7f)>=0x20&&c
!=0x7f?c
:'.'))
237 LONG
hexdumpfile(struct file
*in
, struct file
*out
)
241 LONG offset
=0, n
, c
, tty
;
242 LONG retval
= RETURN_OK
;
244 tty
=IsInteractive(out
->fd
);
269 putlinequick(out
,offset
,b
);
271 if(putline(out
,offset
,b
,n
))
273 retval
= RETURN_ERROR
;
279 putline(out
,offset
,b
,n
);
280 if(out
->cur
!=out
->buf
)
287 retval
= RETURN_ERROR
;
292 if (CheckSignal(SIGBREAKF_CTRL_C
))
294 retval
= RETURN_WARN
;
303 void putlinenumber(struct file
* out
, unsigned short line
)
312 putc(out
, line
/x
+'0');
325 LONG
dumpfile(struct file
*in
, struct file
*out
, BOOL showline
)
328 unsigned short line
= 0;
329 LONG retval
= RETURN_OK
;
332 putlinenumber(out
, ++line
);
334 if(1/*IsInteractive(out->fd)*/)
348 if (lastc
==0x0a && showline
)
349 putlinenumber(out
, ++line
);
351 if(putc(out
,c
)||(c
=='\n' && put(out
)))
357 retval
= RETURN_ERROR
;
361 if ((c
== '\n') && CheckSignal(SIGBREAKF_CTRL_C
))
363 retval
= RETURN_WARN
;
372 static LONG
processfile(CONST_STRPTR name
, struct file
*in
, struct file
*out
, IPTR
*args
, LONG
*numfiles
)
376 in
->fd
= Open(name
, MODE_OLDFILE
);
383 error
= hexdumpfile(in
, out
);
385 error
= dumpfile(in
, out
, args
[ARG_NUMBER
]);
399 struct AnchorPath apath
;
400 UBYTE buf
[MAX_PATH_LEN
- 1];
405 IPTR args
[5]={ 0, 0, 0, 0, 0 };
407 struct file
*in
, *out
;
409 int retval
= RETURN_OK
;
410 struct MyAnchorPath apath
;
412 rda
=ReadArgs("FROM/A/M,TO/K,OPT/K,HEX/S,NUMBER/S",args
,NULL
);
415 PrintFault(IoErr(),"Type");
418 names
=(STRPTR
*)args
[0];
420 in
=AllocMem(sizeof(struct file
),MEMF_ANY
);
421 out
=AllocMem(sizeof(struct file
),MEMF_ANY
);
423 if(in
!=NULL
&&out
!=NULL
)
427 apath
.apath
.ap_BreakBits
= SIGBREAKF_CTRL_C
;
428 apath
.apath
.ap_FoundBreak
= 0;
429 apath
.apath
.ap_Flags
= 0;
430 apath
.apath
.ap_Strlen
= MAX_PATH_LEN
;
432 out
->fd
= Open((STRPTR
) args
[ARG_TO
], MODE_NEWFILE
);
442 for (error
= MatchFirst(*names
, &apath
.apath
);
444 error
= MatchNext(&apath
.apath
))
446 error
= processfile(apath
.apath
.ap_Buf
, in
, out
, args
, &numfiles
);
450 MatchEnd(&apath
.apath
);
452 if (numfiles
== 0 && error
== ERROR_NO_MORE_ENTRIES
)
457 if (error
&& error
!= ERROR_NO_MORE_ENTRIES
)
459 if (*names
&& error
!= ERROR_BREAK
)
461 Printf("TYPE: can't open %s\n", (IPTR
) *names
);
466 PrintFault(error
, NULL
);
478 /* If all files got dumped, return ok, else error.
480 retval
= *names
? RETURN_ERROR
: RETURN_OK
;
483 PrintFault(IoErr(), NULL
);
487 PrintFault(ERROR_NO_FREE_STORE
,"Type");
488 retval
= RETURN_ERROR
;
492 FreeMem(in
,sizeof(struct file
));
494 FreeMem(out
,sizeof(struct file
));