home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / assembler / tasm / c / CKSUM next >
Encoding:
Text File  |  1992-07-30  |  6.4 KB  |  285 lines

  1. /*
  2.         Evaluate Check-sum for Prom FILE
  3.         R.J.P.   18/7/92
  4. */
  5. #include        <stdio.h>
  6. #include        <stdlib.h>
  7. #ifdef          _ARC_
  8. #include        "kernel.h"
  9. #else
  10. #include        <dos.h>
  11. #include        <dir.h>
  12. #include        <mem.h>
  13. #endif
  14.  
  15. #include        <string.h>
  16. #include        <ctype.h>
  17.  
  18. #undef          BOOL
  19. #undef          FALSE
  20. #undef          TRUE
  21.  
  22. typedef         unsigned int  Uint ;
  23. typedef         unsigned char Uchar ;
  24. #define         BOOL    Uint
  25. #define         FALSE   0
  26. #define         TRUE    1
  27.  
  28. #ifdef  _ARC_
  29.  
  30. char *dirscan (const char *f)
  31. {
  32.         _kernel_swi_regs regs;
  33.         _kernel_oserror *err;
  34.  
  35.         static int ptr;
  36.         static int len;
  37.         static int last_notwild;
  38.         static char *file;
  39.         static char *dir;
  40.         static char *name;
  41.         static char path[255];
  42.         static char res[255];
  43.  
  44.         if (f && *f)
  45.         {
  46.                 strcpy (path, f);
  47.  
  48.                 /* Try for a directory prefix */
  49.                 file = strrchr (path, '.');
  50.  
  51.                 /* If not, try for a filing system prefix */
  52.                 if (file == NULL)
  53.                 {
  54.                         if (*path == '-')
  55.                                 file = strchr (path + 1, '-');
  56.                         else
  57.                                 file = strchr (path, ':');
  58.                 }
  59.  
  60.                 if (file)
  61.                 {
  62.                         len = file - path + 1;
  63.                         strncpy (res, path, len);
  64.  
  65.                         name = &res[len];
  66.                         len = 255 - len;
  67.  
  68.                         dir = path;
  69.                         *file++ = '\0';
  70.                 }
  71.                 else
  72.                 {
  73.                         name = res;
  74.                         len = 255;
  75.  
  76.                         file = path;
  77.                         dir = "";
  78.                 }
  79.  
  80.                 /* Is the file name wildcarded? */
  81.                 if (strchr (file, '*') || strchr (file, '#'))
  82.                         last_notwild = 0;
  83.                 else
  84.                 {
  85.                         strcpy (name, file);
  86.                         last_notwild = 1;
  87.                         return res;
  88.                 }
  89.  
  90.                 ptr = 0;
  91.         }
  92.  
  93.         /* If we're looking for more from a non-wild name, no luck */
  94.         if (last_notwild)
  95.                 return 0;
  96.  
  97.         regs.r[0] = 9;
  98.         regs.r[1] = (int) dir;
  99.         regs.r[2] = (int) name;
  100.         regs.r[4] = ptr;
  101.         regs.r[5] = len;
  102.         regs.r[6] = (int) file;
  103.  
  104.         do
  105.         {
  106.                 regs.r[3] = 1;
  107.                 err = _kernel_swi (0x0C, ®s, ®s);
  108.  
  109.                 if (err)
  110.                 {
  111.                         fprintf (stderr, "*** Error (%d) - %s\n",
  112.                                  err->errnum, err->errmess);
  113.                         exit (1);
  114.                 }
  115.  
  116.         } while (regs.r[3] == 0 && regs.r[4] >= 0);
  117.  
  118.         ptr = regs.r[4];
  119.  
  120.         if (ptr < 0)
  121.                 return 0;
  122.  
  123.         return res;
  124. }
  125. #else
  126.  
  127. #define  _ATTR_    (FA_DIREC | FA_RDONLY | FA_HIDDEN)
  128. Uint   DirectorySize( const char  *wild ,int fattr,BOOL B_Pack )
  129. {
  130.         int  N     =       0   ;
  131.         struct   ffblk  LFN ;
  132.         struct   ffblk   *l     =   &LFN ;
  133.  
  134.  
  135.         if( findfirst(wild , l , fattr  )  )
  136.                     return 0  ;
  137.  
  138.         do {
  139.         if (B_Pack)
  140.            N += strlen( l->ff_name ) + 1 ;
  141.         else
  142.            N += sizeof( l->ff_name ) ;
  143.         }
  144.         while( !findnext( l ) ) ;
  145.         return N ;
  146. }
  147.  
  148.  
  149. Uint      DirectoryList(
  150.                 Uint      *N,      /* Number of files Found */
  151.                 char      *fn_vec,   /* Names in vector */
  152.                 const char *wild,
  153.                 int       fattr,
  154.                 BOOL      B_Pack
  155.                 )
  156. {
  157.         struct  ffblk    LFN ;
  158.         struct  ffblk   *l = &LFN ;
  159.  
  160.         *N    =    0 ;
  161.         if( findfirst(wild,l,fattr) )
  162.               goto   LB_EXIT   ;
  163.  
  164.         do
  165.         {
  166.         strcpy( fn_vec , l->ff_name ) ;
  167.         if (B_Pack)
  168.                fn_vec += strlen( l->ff_name ) + 1;
  169.         else
  170.                fn_vec += sizeof(l->ff_name) ;
  171.  
  172.         *N   +=   1   ;
  173.         }
  174.          while( !findnext( l ) ) ;
  175. LB_EXIT:
  176.         return   *N  ;
  177. }
  178. #endif
  179.  
  180.  
  181. int  main(int ac,char **av)
  182. {
  183. #ifndef         _ARC_
  184.        Uint     nf ;
  185.        char     Path[133] ;
  186.        char     w[133] ;
  187.        char    *FP,*cp ;
  188.        struct  ffblk  *tf ;
  189. #endif
  190.        char    *FL ;
  191.        Uint    nc ;
  192. #ifdef   _ARC_
  193.        unsigned   short  cks = 0 ;
  194. #else
  195.        Uint    cks = 0 ;
  196. #endif
  197.        Uchar   nxb ;
  198.        
  199.        FILE    *IB ;
  200.        BOOL    B_lf ;
  201.  
  202.     while( --ac )
  203.     {
  204.     printf("File List of %s\n" , *++av ) ;
  205.  
  206. #ifdef   _ARC_
  207. #else   
  208.     strcpy(Path,*av) ;
  209.  
  210.     if( (cp = strrchr(Path,'\\')),cp != 0 )
  211.                         *(cp+1) = 0 ;
  212.     else if( (cp = strrchr(Path,':')),cp != 0 )
  213.                         *(cp+1) = 0 ;
  214.     else
  215.                         *Path = 0 ;
  216.  
  217.     if( ( nf = DirectorySize( *av ,_ATTR_,FALSE ) ), nf == 0 )
  218.     {
  219.        fprintf(stderr,"*Need file parameter\n") ;
  220.        exit(3) ;
  221.     }
  222.     FP = FL = (char *)malloc( nf * sizeof(char)) ;
  223.     nf  = DirectoryList( &nf , FL , *av , _ATTR_ ,FALSE) ;
  224. #endif
  225.  
  226. #ifdef  _ARC_    
  227.     for ( FL = dirscan( *av) ; FL ; FL = dirscan(0) )
  228.     {
  229.         if((IB = fopen(FL,"rb")),IB == 0 )
  230.         {
  231.            fprintf(stderr,"*Can't read [%s]\n",FL) ;
  232.            exit(2) ;
  233.         }
  234.         printf("%s\n",FL) ;
  235. #else    
  236.     while ( nf-- )
  237.     {
  238.         sprintf(w,"%s%s",Path,FL) ;
  239.         if((IB = fopen(w,"rb")),IB == 0 )
  240.         {
  241.            fprintf(stderr,"*Can't read [%s]\n",w) ;
  242.            free(FP) ;
  243.            exit(2) ;
  244.         }
  245.         printf("%s\n",w) ;
  246. #endif
  247.  
  248.         for(nc=0;;nc++)
  249.         {
  250.            nxb = fgetc(IB) ;
  251.            if(feof(IB)) break ;
  252.            cks += nxb ;
  253.         }
  254.         printf("Check-Sum = %04x  (%d Bytes)\n",cks,nc) ;
  255.         fseek(IB,0x40L,SEEK_SET) ;
  256.         printf("Prom Details\n") ;
  257.         
  258.         for(B_lf=TRUE , nxb=0;nxb!=0xff;)
  259.         {
  260.            nxb =  (Uchar)fgetc(IB) ;
  261.            if(feof(IB)) break ;
  262.            if ( nxb == '\n' )
  263.            {  B_lf = TRUE ; putchar( '\n' ) ; }
  264.            else
  265.            if ( B_lf && isspace(nxb) )  ;
  266.            else
  267.            { putchar(nxb) ; B_lf = FALSE ; }
  268.         }
  269.         fclose(IB) ;
  270.         printf("\n\n") ;
  271. #ifdef  _ARC_
  272. #else
  273.         FP += sizeof( tf->ff_name)  ;
  274. #endif
  275.       }
  276.      printf("\n\n" ) ;
  277.      
  278. #ifdef _ARC_
  279. #else
  280.      free( FL ) ;
  281. #endif
  282.  }
  283. return 0 ;
  284. }
  285.