home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / mtools-3.000 / mtools-3 / mtools-3.0 / mmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-08  |  3.4 KB  |  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 "streamcache.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.     StreamCache_t sc;
  29.  
  30.     Stream_t *SrcDir;
  31.     int entry;
  32.     ClashHandling_t ch;
  33.     Stream_t *targetDir;
  34.     Stream_t *targetFs;
  35.     char *progname;
  36. } Arg_t;
  37.  
  38.  
  39. /*
  40.  * Open the named file for read, create the cluster chain, return the
  41.  * directory structure or NULL on error.
  42.  */
  43. static struct directory *makeeit(char *dosname,
  44.                  char *longname,
  45.                  void *arg0,
  46.                  struct directory *dir)
  47. {
  48.     Stream_t *Target;
  49.     long now;
  50.     Arg_t *arg = (Arg_t *) arg0;
  51.     int fat;
  52.  
  53.     /* will it fit? At least one sector must be free */
  54.     if (! getfree(arg->targetFs)) {
  55.         fprintf(stderr,"Disk full\n");
  56.         return NULL;
  57.     }
  58.     
  59.     /* find out current time */
  60.     time(&now);
  61.     mk_entry(".", 0x10, 1, 0, now, dir);
  62.     Target = open_file(COPY(arg->targetFs), dir);
  63.     if(!Target){
  64.         FREE(&arg->targetFs);
  65.         fprintf(stderr,"Could not open Target\n");
  66.         return NULL;
  67.     }
  68.  
  69.     dir_grow(Target, arg->targetFs, 0);
  70.     /* this allocates the first cluster for our directory */
  71.  
  72.     GET_DATA(arg->targetDir, 0, 0, 0, &fat);
  73.     mk_entry("..         ", 0x10, fat, 0, now, dir);
  74.     dir_write(Target, 1, dir);
  75.  
  76.     GET_DATA(Target, 0, 0, 0, &fat);
  77.     mk_entry(".          ", 0x10, fat, 0, now, dir);
  78.     dir_write(Target, 0, dir);
  79.  
  80.     mk_entry(dosname, 0x10, fat, 0, now, dir);
  81.     FREE(&Target);
  82.     return dir;
  83. }
  84.  
  85.  
  86. void mmd(int argc, char **argv, int type)
  87. {
  88.     int ret = 0;
  89.     Stream_t *Dir, *Fs;
  90.     Arg_t arg;
  91.     int c, oops;
  92.     char filename[VBUFSIZE];
  93.     int i;
  94.  
  95.     arg.progname = argv[0];
  96.  
  97.     /* get command line options */
  98.  
  99.     init_clash_handling(& arg.ch);
  100.  
  101.     oops = 0;
  102.  
  103.     /* get command line options */
  104.     arg.nowarn = 0;
  105.     arg.interactive = 0;
  106.     while ((c = getopt(argc, argv, "invorsamORSAM")) != EOF) {
  107.         switch (c) {
  108.             case 'i':
  109.                 arg.interactive = 1;
  110.                 break;
  111.             case 'n':
  112.                 arg.nowarn = 1;
  113.                 break;
  114.             case 'v':
  115.                 arg.verbose = 1;
  116.                 break;
  117.             case '?':
  118.                 oops = 1;
  119.                 break;
  120.             default:
  121.                 if(handle_clash_options(&arg.ch, c))
  122.                     oops=1;
  123.                 break;
  124.         }
  125.     }
  126.  
  127.     if (oops || (argc - optind) < 1) {
  128.         fprintf(stderr,
  129.             "Mtools version %s, dated %s\n", mversion, mdate);
  130.         fprintf(stderr,
  131.             "Usage: %s [-itnmvV] file targetfile\n", argv[0]);
  132.         fprintf(stderr,
  133.             "       %s [-itnmvV] file [files...] target_directory\n", 
  134.             argv[0]);
  135.         cleanup_and_exit(1);
  136.     }
  137.  
  138.  
  139.     init_sc(&arg.sc);        
  140.     arg.sc.arg = (void *) &arg;
  141.     arg.sc.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.sc, argv[i], O_RDWR, &Fs);
  148.         if(!Dir){
  149.             fprintf(stderr,"Could not open parent directory\n");
  150.             cleanup_and_exit(1);
  151.         }
  152.         arg.targetDir = Dir;
  153.         arg.targetFs = Fs;
  154.         get_name(argv[i], filename, arg.sc.mcwd);
  155.         if(!filename[0]){
  156.             fprintf(stderr,"Empty filename\n");
  157.             ret |= MISSED_ONE;
  158.             FREE(&Dir);
  159.             continue;
  160.         }
  161.         if(mwrite_one(Dir, Fs, argv[0], filename,0,
  162.                   makeeit, (void *)&arg, &arg.ch)<=0){
  163.             fprintf(stderr,"Could not create %s\n", argv[i]);
  164.             ret |= MISSED_ONE;
  165.         } else
  166.             ret |= GOT_ONE;
  167.         FREE(&Dir);
  168.     }
  169.  
  170.     finish_sc(&arg.sc);
  171.  
  172.     if ((ret & GOT_ONE) && ( ret & MISSED_ONE))
  173.         cleanup_and_exit(2);
  174.     if (ret & MISSED_ONE)
  175.         cleanup_and_exit(1);
  176.     cleanup_and_exit(0);
  177. }
  178.