home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / Emulatory / AROS / c / type.c < prev    next >
Encoding:
C/C++ Source or Header  |  1978-03-06  |  5.1 KB  |  302 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: type.c,v 1.5 1996/09/17 16:43:01 digulla Exp $
  4.     $Log: type.c,v $
  5.     Revision 1.5  1996/09/17 16:43:01  digulla
  6.     Use general startup code
  7.  
  8.     Revision 1.4  1996/09/13 17:52:11  digulla
  9.     Use IPTR
  10.  
  11.     Revision 1.3  1996/08/13 15:34:04  digulla
  12.     #include <exec/execbase.h> was missing
  13.  
  14.     Revision 1.2  1996/08/01 17:40:46  digulla
  15.     Added standard header for all files
  16.  
  17.     Desc:
  18.     Lang:
  19. */
  20. #include <exec/memory.h>
  21. #include <exec/execbase.h>
  22. #include <clib/exec_protos.h>
  23. #include <dos/dos.h>
  24. #include <clib/dos_protos.h>
  25.  
  26. #define BUFSIZE 8192
  27.  
  28. struct file
  29. {
  30.     BPTR fd;
  31.     UBYTE *cur;
  32.     ULONG cnt;
  33.     LONG error;
  34.     UBYTE buf[BUFSIZE];
  35. };
  36.  
  37. const UBYTE hs[16]="0123456789abcdef";
  38.  
  39. #define putc(f,c) (*(f)->cur++=(c),--(f)->cnt?0:put(f))
  40. static int put(struct file *f)
  41. {
  42.     LONG size,subsize;
  43.     STRPTR buf;
  44.     size=f->cur-f->buf;
  45.     buf=f->buf;
  46.     while(size)
  47.     {
  48.     subsize=Write(f->fd,buf,size);
  49.     if(subsize<=0)
  50.     {
  51.         f->error=IoErr();
  52.         return 1;
  53.     }
  54.     buf+=subsize;
  55.     size-=subsize;
  56.     }
  57.     f->cur=f->buf;
  58.     f->cnt=BUFSIZE;
  59.     return 0;
  60. }
  61.  
  62. #define getc(f) ((f)->cnt--?*(f)->cur++:get(f))
  63. static int get(struct file *f)
  64. {
  65.     LONG size;
  66.     size=Read(f->fd,f->buf,BUFSIZE);
  67.     if(size<0)
  68.     f->error=IoErr();
  69.     if(size<=0)
  70.     return -1;
  71.     f->cnt=size-1;
  72.     f->cur=f->buf;
  73.     return *f->cur++;
  74. }
  75.  
  76. static void putlinequick(struct file *f, ULONG offset, UBYTE *buf)
  77. {
  78.     int c, i, k;
  79.     UBYTE *b, *o;
  80.     o=f->cur;
  81.     if(offset>=0x10000)
  82.     {
  83.     if(offset>=0x100000)
  84.     {
  85.         if(offset>=0x1000000)
  86.         {
  87.         if(offset>=0x10000000)
  88.         {
  89.             *o++=hs[(offset>>28)&0xf];
  90.             f->cnt--;
  91.         }
  92.         *o++=hs[(offset>>24)&0xf];
  93.         f->cnt--;
  94.         }
  95.         *o++=hs[(offset>>20)&0xf];
  96.         f->cnt--;
  97.     }
  98.     *o++=hs[(offset>>16)&0xf];
  99.     f->cnt--;
  100.     }
  101.     *o++=hs[(offset>>12)&0xf];
  102.     *o++=hs[(offset>>8)&0xf];
  103.     *o++=hs[(offset>>4)&0xf];
  104.     *o++=hs[offset&0xf];
  105.     *o++=':';
  106.     *o++=' ';
  107.     b=buf;
  108.     for(i=0;i<4;i++)
  109.     {
  110.     for(k=0;k<4;k++)
  111.     {
  112.         c=*b++;
  113.         *o++=hs[c>>4];
  114.         *o++=hs[c&0xf];
  115.     }
  116.     *o++=' ';
  117.     }
  118.     b=buf;
  119.     for(i=0;i<16;i++)
  120.     {
  121.     c=*b++;
  122.     *o++=(c&0x7f)>=0x20&&c!=0x7f?c:'.';
  123.     }
  124.     *o++='\n';
  125.     f->cur=o;
  126.     f->cnt-=59;
  127. }
  128.  
  129. static int putline(struct file *f, ULONG offset, UBYTE *buf, ULONG num)
  130. {
  131.     int c, i;
  132.     UBYTE *b;
  133.     if(offset>=0x10000)
  134.     {
  135.     if(offset>=0x10000000&&putc(f,hs[(offset>>28)&0xf]))
  136.         return 1;
  137.     if(offset>=0x1000000&&putc(f,hs[(offset>>24)&0xf]))
  138.         return 1;
  139.     if(offset>=0x100000&&putc(f,hs[(offset>>20)&0xf]))
  140.         return 1;
  141.     if(offset>=0x10000&&putc(f,hs[(offset>>16)&0xf]))
  142.         return 1;
  143.     }
  144.     if(putc(f,hs[(offset>>12)&0xf]))
  145.     return 1;
  146.     if(putc(f,hs[(offset>>8)&0xf]))
  147.     return 1;
  148.     if(putc(f,hs[(offset>>4)&0xf]))
  149.     return 1;
  150.     if(putc(f,hs[offset&0xf]))
  151.     return 1;
  152.     if(putc(f,':'))
  153.     return 1;
  154.     if(putc(f,' '))
  155.     return 1;
  156.     b=buf;
  157.     for(i=0;i<16;i++)
  158.     {
  159.     if(i<num)
  160.     {
  161.         c=*b++;
  162.         if(putc(f,hs[c>>4]))
  163.         return 1;
  164.         if(putc(f,hs[c&0xf]))
  165.         return 1;
  166.     }else
  167.     {
  168.         if(putc(f,' '))
  169.         return 1;
  170.         if(putc(f,' '))
  171.         return 1;
  172.     }
  173.     if((i&3)==3)
  174.         if(putc(f,' '))
  175.         return 1;
  176.     }
  177.     b=buf;
  178.     for(i=0;i<num;i++)
  179.     {
  180.     c=*b++;
  181.     if(putc(f,(c&0x7f)>=0x20&&c!=0x7f?c:'.'))
  182.         return 1;
  183.     }
  184.     if(putc(f,'\n'))
  185.     return 1;
  186.     return 0;
  187. }
  188.  
  189. void hexdumpfile(struct file *in, struct file *out)
  190. {
  191.     UBYTE buf[16];
  192.     UBYTE *b;
  193.     LONG offset=0, n, c, tty;
  194.     tty=IsInteractive(out->fd);
  195.     for(;;)
  196.     {
  197.     if(in->cnt>16)
  198.     {
  199.         b=in->cur;
  200.         n=16;
  201.         in->cur+=16;
  202.         in->cnt-=16;
  203.     }else
  204.     {
  205.         b=buf;
  206.         for(n=0;n<16;n++)
  207.         {
  208.         c=getc(in);
  209.         if(c<0)
  210.             break;
  211.         b[n]=c;
  212.         }
  213.     }
  214.     if(n==16)
  215.     {
  216.         if(out->cnt>=63)
  217.         putlinequick(out,offset,b);
  218.         else
  219.         if(putline(out,offset,b,n))
  220.             return;
  221.     }else
  222.     {
  223.         if(n)
  224.         putline(out,offset,b,n);
  225.         if(out->cur!=out->buf)
  226.         put(out);
  227.         return;
  228.     }
  229.     if(tty)
  230.         if(put(out))
  231.         return;
  232.     offset+=n;
  233.     }
  234. }
  235.  
  236. LONG dumpfile(struct file *in, struct file *out)
  237. {
  238.     LONG c;
  239.     if(1/*IsInteractive(out->fd)*/)
  240.     for(;;)
  241.     {
  242.         c=getc(in);
  243.         if(c<0)
  244.         return 0;
  245.         if(putc(out,c)||(c=='\n'&&put(out)))
  246.         return 1;
  247.     }
  248. }
  249.  
  250. int main (int argc, char ** argv)
  251. {
  252.     IPTR args[5]={ 0, 0, 0, 0, 0 };
  253.     struct RDArgs *rda;
  254.     struct file *in, *out;
  255.     STRPTR *names;
  256.  
  257.     rda=ReadArgs("FROM/A/M,TO/K,OPT/K,HEX/S,NUMBER/S",args,NULL);
  258.     if(rda==NULL)
  259.     {
  260.     PrintFault(IoErr(),"Type");
  261.     return RETURN_FAIL;
  262.     }
  263.     names=(STRPTR *)args[0];
  264.  
  265.     in =AllocMem(sizeof(struct file),MEMF_ANY);
  266.     out=AllocMem(sizeof(struct file),MEMF_ANY);
  267.  
  268.     if(in!=NULL&&out!=NULL)
  269.     {
  270.     out->cur=out->buf;
  271.     out->cnt=BUFSIZE;
  272.     out->fd=Output();
  273.     while(*names!=NULL)
  274.     {
  275.         in->fd=Open(*names,MODE_OLDFILE);
  276.         if(in->fd)
  277.         {
  278.         in->cnt=0;
  279.         if(args[3])
  280.             hexdumpfile(in,out);
  281.         else
  282.             dumpfile(in,out);
  283.         Close(in->fd);
  284.         }else
  285.         {
  286.         PrintFault(IoErr(),"Type");
  287.         break;
  288.         }
  289.         names++;
  290.     }
  291.     }else
  292.     PrintFault(ERROR_NO_FREE_STORE,"Type");
  293.  
  294.     if(in!=NULL)
  295.     FreeMem(in,sizeof(struct file));
  296.     if(out!=NULL)
  297.     FreeMem(out,sizeof(struct file));
  298.     if(rda!=NULL)
  299.     FreeArgs(rda);
  300.     return 0;
  301. }
  302.