/* MEMTOROM © LEGER Frederic 1996*/

#include <stdio.h>
#include <stat.h>
#include <string.h>

main(int argc,char *argv[])
{
        struct stat sttamp;

        FILE *infile;
        FILE *outfile;
        int i,k,l,c;
        char s[]=".BIN";
        printf("MEMTOROM © LEGER Frédéric 1996\n");
        if (argc == 3){
                infile = fopen(argv[1], "r");

                if ((infile == NULL) || (stat(argv[1], &sttamp) == -1)){
                        printf("Impossible d'ouvrir le fichier %s en lecture !!!\n",argv[1]);
                        }
                else  {
                                outfile = fopen(strcat(argv[2],s),"wb");
                                if (outfile == NULL){
                                        printf("Impossible d'ouvrir le fichier %s.BIN en écriture !!!\n",argv[2]);
                                }
                                else {
                                        i=0;
                                        while ( i != sttamp.st_size){
                                                 k=getc(infile);
                                                 l=getc(infile);
                                                 if (k<58){k=k-48;}
                                                 else {k=k-55;}
                                                 if (l<58){l=(l-48)*16;}
                                                 else {l=(l-55)*16;}
                                                 c=k+l;
                                                 fputc(c,outfile);
                                                 i=i+2;
                                        }
                                        fclose(infile);
                                        fclose(outfile);
                                }
                        }
        }
        else{
        printf("MEMTOROM : Usage MEMTOROM infile outfile\n");
        }
}