home *** CD-ROM | disk | FTP | other *** search
/ norge.freeshell.org (192.94.73.8) / 192.94.73.8.tar / 192.94.73.8 / pub / computers / pcjr / arc / ZIPUP1_1.LZH / ZIPUP.C next >
Text File  |  1989-10-16  |  11KB  |  310 lines

  1. /* ZIPUP 1.1  --- the working program    (c) 1989 Bob Jaques */
  2. /* 10/16/89   1515                                Jim Barth  */
  3.  
  4. /* ******************************************************************** */
  5. /* Please note that this program has a routine written by Mike Smedley  */
  6. /* in his excellent Version 5.0 of CXL, which is downloadable from most */
  7. /* good bulletin boards.  ZIPUP was compiled in Turbo C Version 2.0     */
  8. /* using the following command line:  tcc zipup.c cxltcs.lib            */
  9. /* ******************************************************************** */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <bios.h>
  14. #include <conio.h>
  15. #include <dos.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <io.h>
  19. #include <dir.h>
  20. #include "cxlstr.h"
  21.  
  22. #define DEFAULT "ZIPUP.TXT"
  23. void clrscr(void);
  24. int print_usage(void);
  25. int fcloseall(void);
  26. int ch,output,rem,quot,wanted[4];
  27. long int offset,find,count,i,length,c,endbyte,firstbyte;
  28. FILE *fp,*comm_file;
  29. char *itoa(int value1,char *str,int radix);
  30. char path1,path2,pathname[120],string[80];
  31. char line[10] = ".ZIP";
  32. char *strupr(char *s),*p;
  33. char *strcpy (char *dest,const char *src);
  34. char *strchr(const char *line,int NUMBER_LEFT);
  35. char *strcat (char *dest,const char *src);
  36. char dots[60] = ".................................................";
  37. char zipfile[100],tempname[100],zipcomment[100],t1[100];
  38. char *strncpy(char *destination, const char *source,size_t num_bytes);
  39. int file_exists(char *filename,int amode);
  40. int fseek(FILE *fp,long offset,int origin);
  41. int fgetc(FILE *fp);
  42. int fputc(int character,FILE *fp);
  43. long ftell(FILE *fp);
  44. int byte1 = 0,byte2 = 0,done = 0,finished = 0,character;
  45.  
  46.  
  47. /*
  48.    ============================
  49.    = T H E  M A I N  L O O P  =
  50.    ============================
  51. */
  52.  
  53. main(argc, argv)        /* As far as aruguments go, I am only looking for */
  54. int argc;               /* a maximum of two command-line arguments.       */
  55. char *argv[];           /* ZIPUP expects to see:          */
  56.                         /*    ZIPUP ZIPFILE[.ZIP] COMMENT.TXT             */
  57.                         /* The program will use a default of ZIPUP.TXT    */
  58.                         /* in the currect directory if no comment file is */
  59.                         /* specified.                                     */
  60.  
  61.  
  62. {
  63. strcat (zipfile,argv[1]);
  64. strcat (zipcomment,argv[2]);
  65. printf ("\n\n\nZIPUP Version 1.1 \t\t\t   (c) 1989 Bob Jaques & Jim Barth\n\n");
  66.   if (strlen(zipfile) >= 1)
  67.     {
  68.       if ((strlen(zipfile) > 3) &&  (strstr(strupr(zipfile),"*.ZIP") != NULL))
  69.         {
  70.           p = strleft(zipfile,strlen(zipfile) - 5); /* This little bit of */
  71.           find_if_comment(zipcomment);              /* code is to handle  */
  72.           strcpy (t1,zipfile);                      /* the wildcard of    */
  73.           printf("\n");                             /* "ZIPUP *.ZIP"      */
  74.           process_wildcards (zipcomment, t1,p);
  75.           exit (0);
  76.         }
  77.   if (strchr (zipfile,'.') == NULL)
  78.     {                                   /*  This is to handle the    */
  79.        strcpy (t1,"");                  /*  case where the user just */
  80.        strcpy (t1,zipfile);             /*  entered ZIPUP ZIPFILE    */
  81.        strcat (t1,".ZIP");              /*  without a. ZIP extention.*/
  82.        strcpy (pathname,"");
  83.        strcat (pathname,t1);
  84.        if (!file_exists (pathname,0))
  85.         {
  86.           printf("%s is not in this directory!",t1);
  87.           print_usage();
  88.           exit (1);
  89.         }
  90.    }
  91.    else {
  92.      strcat (t1,zipfile);
  93.      strcat (pathname,t1);
  94.        if (!file_exists (t1,0))
  95.          {
  96.             printf("Unable to open %s.\n",t1);
  97.             print_usage();
  98.             exit(1);
  99.          }
  100.     }
  101.   }
  102.   else
  103.   if (strlen(zipfile) < 1)
  104.     {
  105.       printf("You must supply a \"ZIP\" file(s) to process!");
  106.       print_usage();
  107.       exit(1);
  108.     }
  109.  
  110.      find_if_comment(zipcomment);     /* Make sure the specified comment */
  111.                                       /* file does indeed exist.         */
  112.      add_the_comments(zipcomment,t1); /* Now that the comment files DOES */
  113.                                       /* exist, we can add the comments  */
  114.                                       /* the .ZIP file(s).               */
  115.  
  116.  
  117.      exit (0);                        /* Set the ERRORLEVEL to "0" since */
  118.                                       /* we got this far with no errors. */
  119. }      /* =====================
  120.           =END OF MAIN ROUTINE=
  121.           =====================*/
  122.  
  123.  
  124.  
  125.  
  126.  
  127. /*
  128.    ========================================================================
  129.    = This is the function that will search for the place to insert the    =
  130.    = comment file into the ".ZIP" file.  It also computes the length of   =
  131.    = of the desired comment file to be added.                             =
  132.    = Some of the coding may be unorthodox, but, hey, it works!            =
  133.    ========================================================================
  134. */
  135.  
  136. add_the_comments()
  137. {
  138.    wanted[0] = 80;    /*  These are the values that are looked for as a */
  139.    wanted[1] = 75;    /*  "key".  If you add 16 to the memory address   */
  140.    wanted[2] = 5;     /*  wanted[3], this will make the beginning of    */
  141.    wanted[3] = 6;     /*  the place in the "ZIP" file that marks the    */
  142.                       /*  length of the comment file.                   */
  143.  
  144.  
  145.    comm_file = fopen(zipcomment,"rb");  /* Open the comment file for */
  146.    count = 0;                           /* our reading only          */
  147.    while ((ch = fgetc (comm_file)) !=EOF)
  148.         {                   /* Very simply, this is counting the length */
  149.          count++;           /* of the comment file.  Count = length     */
  150.         }
  151.  
  152.  {
  153.  div_t n;                    /* Divides the length of the comment file */
  154.  n = div (count,256);        /* into two bytes for placement in the    */
  155.  byte1 = n.rem;              /* structure of the ".ZIP" file.          */
  156.  byte2 = n.quot;             /*                                        */
  157.  }
  158.  
  159.  
  160.  fp = fopen(t1, "rb+");
  161.  fseek(fp,0,SEEK_END);
  162.  count = 0;
  163.  firstbyte = 1;
  164.  endbyte = ftell(fp);
  165.  strcpy (string,"");
  166.  strcpy (string,strupr(t1));
  167.  strcat (string,strleft(dots,50 - strlen (t1)));
  168.  strcat (string, " ");
  169.  gotoxy (4,wherey());
  170.  printf("%s",string);
  171.  count = endbyte - 4;
  172.  find = 0;
  173.  LOOP1:
  174.    for (i = 0; i < 3;i++)
  175.     {
  176.      if (count == firstbyte)
  177.        {
  178.            printf ("Corrupted ZIP file"); /* Well, it there must be     */
  179.            fclose(fp);                    /* SOMETHING wrong if we got  */
  180.            fclose(comm_file);             /* to the beginning of the    */
  181.                                           /* file without finding what  */
  182.                                           /* we were looking for.       */
  183.  
  184.            exit(1);                       /* Something went wrong so    */
  185.                                           /* exit with ERRORLEVEL @ 1   */
  186.        }
  187.     fseek (fp,count + i,SEEK_SET);
  188.     find = fgetc(fp);
  189.       if (find != wanted[i])
  190.         {
  191.            --count;
  192.            goto LOOP1;
  193.         }
  194.      }
  195.  
  196.            gotoxy (53,wherey());
  197.            printf("Stripping old comment");
  198.            count = count + 20;
  199.            fseek(fp,count,SEEK_SET);
  200.            character = byte1;
  201.            fputc(character,fp);
  202.            character = byte2;
  203.            fputc(character,fp);
  204.  
  205.            gotoxy (53,wherey());
  206.            printf("Adding new comment   ");
  207.            rewind(comm_file);
  208.            while (( ch = fgetc (comm_file)) != EOF)
  209.            fputc (ch,fp);
  210.            fclose(comm_file);
  211.  
  212.            fseek(fp,0,SEEK_END);
  213.            fclose(fp);
  214.            gotoxy (73,wherey());
  215.            printf("Done!\n");
  216.            return(0);
  217.  
  218.  
  219.  
  220. /* This simple function simply checks for the presence of a file that */
  221. /* is stored in the variable PATHNAME.                                */
  222. }
  223. file_exists()
  224. {
  225. return (access(pathname, 0) == 0);
  226. }
  227.  
  228.  
  229. /* This little function prints the expected usage of ZIPUP.  This     */
  230. /* function is referred to in the case of all errors and unrecognized */
  231. /* inputs from the command line.                                      */
  232. print_usage()
  233. {
  234.    printf("\n\n");
  235.    printf("USAGE: ZIPUP File Comment\n");
  236.    printf("where      File = Path and Filename of file(s) to process.\n");
  237.    printf("        Comment = Path and Filename of comment file to add.\n\n");
  238.    printf("If no comment file given, default is ZIPUP.TXT\n");
  239. }
  240.  
  241.  
  242. /* The following routine, PROCESS_WILDCARDS, was the easiest way I found */
  243. /* to handle the case where the user entered "ZIPUP *.ZIP" on the        */
  244. /* command line.  Some of it probably doesn't seem logical, but, again,  */
  245. /* it works!                                                             */
  246. process_wildcards()
  247. {
  248.    struct ffblk ffblk;
  249.    int done,number = 1;
  250.    done = findfirst(t1,&ffblk,0);
  251.    while (!done)
  252.    {
  253.      strcpy (t1,p);
  254.      strcat (t1,ffblk.ff_name);
  255.      /*printf("In process_wildcards: t1 = %s\n",t1); */
  256.      printf("%d.  ",number);
  257.      number++;
  258.      add_the_comments(t1,zipcomment);
  259.      strcpy (t1,"");
  260.      done = findnext(&ffblk);
  261.    }
  262. }
  263.  
  264. /*  This last function, find_if_comment, simply looks for the requested    */
  265. /*  comment file from the command line.  If one was not specified, ZIPUP   */
  266. /*  will default to ZIPUP.TXT, which is expected to be in the same         */
  267. /*  directory that ZIPUP is run from.  This function also checks for the   */
  268. /*  presence of ZIPUP.TXT if no other comment file was requested to be     */
  269. /*  processed.                                                             */
  270. find_if_comment()
  271. {
  272.   if (*zipcomment != NULL)
  273.    {
  274.       strcpy (pathname,"");
  275.       strcpy (pathname, zipcomment);
  276.       if (!file_exists (pathname,0))
  277.         {
  278.           printf("Unable to open comment file %s.\n",strupr(zipcomment));
  279.           print_usage ();
  280.           exit (1);
  281.         }
  282.   }
  283.   else  {
  284.     printf("No comment file specified -- using default of ZIPUP.TXT.\n");
  285.     strcpy (zipcomment,"");
  286.     strcat (zipcomment,DEFAULT);
  287.     strcpy (pathname,zipcomment);
  288.     if (!file_exists (pathname,0))
  289.       {
  290.        printf("\nThere is no default comment file in this directory.\n");
  291.        print_usage ();
  292.        exit (1);
  293.       }
  294.   }
  295.  return(pathname);
  296. }
  297.  
  298. /* ************************************************************************ */
  299. /* Well, that's all there is to it.  If anyone knows of any "better" way to */
  300. /* to do what some of this code does, please share it with us.  We are      */
  301. /* eager to learn a different approach.                                     */
  302. /*                                                                          */
  303. /* The authors can be reached at:                                           */
  304. /*    Bob Jaques                           Jim Barth                        */
  305. /*    6814 Helena DR NE                    11544 Kevin Lane NE              */
  306. /*    Bremerton, WA  98310                 Poulsbo, WA  98370               */
  307. /*    FIDO (1:350/20 12-14.4K HST)         FIDO (1:350/21 12-2400 MNP)      */
  308. /* ************************************************************************ */
  309. 
  310.