/* Make a DR-Library  (c) Jens Mller 1994 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

#ifdef __TURBOC__
#include <ext.h>
#else
#include <fcntl.h>
#endif

#ifndef PATH_MAX
#define PATH_MAX   128
#endif


char		object_path[PATH_MAX], *library, *list_file;
struct
{
   char   a_fname[14];
   long   a_modti;
   char   a_userid;
   char   a_gid;
   int      a_fimode;
   long   a_fsize;
   int      reserved;
} obj;


#ifdef __TURBOC__
int cancel(void)
#else
cancel()
#endif
{
   if(kbhit() && getch() == 3)
      return(-1);
   return(0);
}


#ifdef __TURBOC__
int main(int argc, char **argv)
#else
int main(argc, argv)
   int      argc;
   char   **argv;
#endif
{
   FILE           *h_list;
   int            h_lib, o_handle, found, n = 0;
   char           obj_module[PATH_MAX], obj_path[PATH_MAX];
   char           *obj_ptr, *buf;
   long           help, rv;
   struct ffblk   f_info;
   unsigned int   beg_lib = 0xFF65;
   unsigned int   end_lib = 0;

   if(argc < 3 || argc > 4)
   {
      printf("\rusage: makelib library list_file [module_path]\n");
      return(-3);
   }

   obj_path[0] = 0;
   if(argc == 4)
      strcpy(obj_path, argv[3]);
   help = strlen(obj_path);
   if(help)
   {
      obj_ptr = obj_path + help;
      if(*(obj_ptr - 1) != '\\')
         *obj_ptr++ = '\\';
   } else
      obj_ptr = obj_path;

   h_list = fopen(argv[2], "r");
   if(h_list == NULL)
   {
      printf("\r%s not found\n", argv[2]);
      return(-3);
   }

   h_lib = creat(argv[1]);
   if(h_lib == -1)
   {
      printf("\rerror creating library %s\n", library);
      fclose(h_list);
      return(-3);
   }

   write(h_lib, &beg_lib, (size_t)2);
   while(fscanf(h_list, "%s", obj_module) == 1 && !cancel())
   {
      /* Test auf Kommentar */
      if (strchr( "#;*", obj_module[ 0 ] ) != NULL)
      {
         /* Zeile bis Schlu bergehen */
         while (fgetc (h_list) != '\n')
             ;

      } else {

         strcpy(obj_ptr, obj_module);

         found = findfirst(obj_path, &f_info, 0);
         if(found)
            printf("\rno file: %s\033K\n", obj_path);

         while(!found)
         {
            strcpy(obj_ptr, f_info.ff_name);
                printf ("\rincluding '%s'\033K", obj_path);

            o_handle = open(obj_path, O_RDONLY);
            if(o_handle == -1)
            {
               printf("\rerror opening module '%s'\033K\n",
                  obj_path);
            } else {
               buf = (char *)malloc((size_t) f_info.ff_fsize);
               if(!buf)
               {
                  printf("\rnot enough memory for %s\033K\n",
                     obj_module);
               } else {
                  rv = read(o_handle, buf, (size_t)f_info.ff_fsize);
                  if (rv != (long)f_info.ff_fsize)
                  {
                     printf("\rerror reading %s\033K\n",
                        f_info.ff_name);
                  } else {
                     strcpy(obj.a_fname, f_info.ff_name);
                     obj.a_modti  = ((long)f_info.ff_ftime << 16) | f_info.ff_fdate;
                     obj.a_fimode = f_info.ff_attrib;
                     obj.a_fsize  = f_info.ff_fsize;
                     obj.reserved = 0;
                     write(h_lib, &obj, (size_t)sizeof(obj));

                     rv = write(h_lib, buf, (size_t)obj.a_fsize);
                     if (rv != (long)obj.a_fsize)
                     {
                        printf("\rerror writing library\033K\n");
                        fseek(h_list, 0L, SEEK_END);
                     } else
                        n++;
                  }
                  free(buf);
               }
               close(o_handle);
            }
            found = findnext(&f_info);
         }
      }
   }
   write(h_lib, &end_lib, (size_t)2);
   close(h_lib);
   fclose(h_list);
   printf ("\rlibrary consists of %d object modules\033K\n", n);
   return(0);
}

