home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / mtools_3.6.src.lzh / MTOOLS_3.6 / mmd.c < prev    next >
Text File  |  1997-11-12  |  3KB  |  178 lines

  1. /*
  2.  * mmd.c
  3.  * Makes an MSDOS directory
  4.  */
  5.  
  6.  
  7. #define LOWERCASE
  8.  
  9. #include "sysincludes.h"
  10. #include "msdos.h"
  11. #include "mtools.h"
  12. #include "vfat.h"
  13. #include "mainloop.h"
  14. #include "plain_io.h"
  15. #include "nameclash.h"
  16. #include "file.h"
  17. #include "fs.h"
  18.  
  19. /*
  20.  * Preserve the file modification times after the fclose()
  21.  */
  22.  
  23. typedef struct Arg_t {
  24.     char *target;
  25.     int nowarn;
  26.     int interactive;
  27.     int verbose;
  28.     int silent;
  29.     MainParam_t mp;
  30.  
  31.     Stream_t *SrcDir;
  32.     int entry;
  33.     ClashHandling_t ch;
  34.     Stream_t *targetDir;
  35. } Arg_t;
  36.  
  37.  
  38. /*
  39.  * Open the named file for read, create the cluster chain, return the
  40.  * directory structure or NULL on error.
  41.  */
  42. static struct directory *makeeit(char *dosname,
  43.                  char *longname,
  44.                  void *arg0,
  45.                  struct directory *dir)
  46. {
  47.     Stream_t *Target;
  48.     time_t now;
  49.     Arg_t *arg = (Arg_t *) arg0;
  50.     int fat;
  51.  
  52.     /* will it fit? At least one sector must be free */
  53.     if (! getfree(arg->targetDir)) {
  54.         fprintf(stderr,"Disk full\n");
  55.         return NULL;
  56.     }
  57.     
  58.     /* find out current time */
  59.     time(&now);
  60.     mk_entry(".", 0x10, 1, 0, now, dir);
  61.     Target = open_file(arg->targetDir, dir);    
  62.     if(!Target){
  63.         fprintf(stderr,"Could not open Target\n");
  64.         return NULL;
  65.     }
  66.  
  67.     dir_grow(Target, 0);
  68.     /* this allocates the first cluster for our directory */
  69.  
  70.     GET_DATA(arg->targetDir, 0, 0, 0, &fat);
  71.     mk_entry("..         ", 0x10, fat, 0, now, dir);
  72.     dir_write(Target, 1, dir);
  73.  
  74.     GET_DATA(Target, 0, 0, 0, &fat);
  75.     mk_entry(".          ", 0x10, fat, 0, now, dir);
  76.     dir_write(Target, 0, dir);
  77.  
  78.     mk_entry(dosname, 0x10, fat, 0, now, dir);
  79.     FREE(&Target);
  80.     return dir;
  81. }
  82.  
  83.  
  84. static void usage(void)
  85. {
  86.     fprintf(stderr,
  87.         "Mtools version %s, dated %s\n", mversion, mdate);
  88.     fprintf(stderr,
  89.         "Usage: %s [-itnmvV] file targetfile\n", progname);
  90.     fprintf(stderr,
  91.         "       %s [-itnmvV] file [files...] target_directory\n", 
  92.         progname);
  93.     exit(1);
  94. }
  95.  
  96. void mmd(int argc, char **argv, int type)
  97. {
  98.     int ret = 0;
  99.     Stream_t *Dir;
  100.     Arg_t arg;
  101.     int c;
  102.     char filename[VBUFSIZE];
  103.     int i;
  104.  
  105.     /* get command line options */
  106.  
  107.     init_clash_handling(& arg.ch);
  108.  
  109.     /* get command line options */
  110.     arg.nowarn = 0;
  111.     arg.interactive = 0;
  112.     arg.silent = 0;
  113.     while ((c = getopt(argc, argv, "XinvorsamORSAM")) != EOF) {
  114.         switch (c) {
  115.             case 'i':
  116.                 arg.interactive = 1;
  117.                 break;
  118.             case 'n':
  119.                 arg.nowarn = 1;
  120.                 break;
  121.             case 'v':
  122.                 arg.verbose = 1;
  123.                 break;
  124.             case 'X':
  125.                 arg.silent = 1;
  126.                 break;
  127.             case '?':
  128.                 usage();
  129.             default:
  130.                 if(handle_clash_options(&arg.ch, c))
  131.                     usage();
  132.                 break;
  133.         }
  134.     }
  135.  
  136.     if (argc - optind < 1)
  137.         usage();
  138.  
  139.     init_mp(&arg.mp);
  140.     arg.mp.arg = (void *) &arg;
  141.     arg.mp.openflags = O_RDWR;
  142.     ret = 0;
  143.  
  144.     for(i=optind; i < argc; i++){
  145.         if(got_signal)
  146.             break;
  147.         Dir = open_subdir(&arg.mp, argv[i], O_RDWR, 0, 0);
  148.         if(!Dir){
  149.             fprintf(stderr,"Could not open parent directory\n");
  150.             exit(1);
  151.         }
  152.         arg.targetDir = Dir;
  153.         get_name(argv[i], filename, arg.mp.mcwd);
  154.         if(!filename[0]){
  155.             if(!arg.silent)
  156.                 fprintf(stderr,"Empty filename\n");
  157.             ret |= MISSED_ONE;
  158.             FREE(&Dir);
  159.             continue;
  160.         }
  161.         if(mwrite_one(Dir, filename,0,
  162.                   makeeit, (void *)&arg, &arg.ch)<=0){
  163.             if(!arg.silent)
  164.                 fprintf(stderr,"Could not create %s\n", 
  165.                     argv[i]);
  166.             ret |= MISSED_ONE;
  167.         } else
  168.             ret |= GOT_ONE;
  169.         FREE(&Dir);
  170.     }
  171.  
  172.     if ((ret & GOT_ONE) && ( ret & MISSED_ONE))
  173.         exit(2);
  174.     if (ret & MISSED_ONE)
  175.         exit(1);
  176.     exit(0);
  177. }
  178.