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 / mlabel.c < prev    next >
Text File  |  1997-11-12  |  4KB  |  192 lines

  1. /*
  2.  * mlabel.c
  3.  * Make an MSDOS volume label
  4.  */
  5.  
  6. #include "sysincludes.h"
  7. #include "msdos.h"
  8. #include "mainloop.h"
  9. #include "vfat.h"
  10. #include "mtools.h"
  11. #include "nameclash.h"
  12.  
  13. char *label_name(char *filename, int verbose, 
  14.          int *mangled, char *ans)
  15. {
  16.     int len;
  17.     int i;
  18.     int have_lower, have_upper;
  19.  
  20.     strcpy(ans,"           ");
  21.     len = strlen(filename);
  22.     if(len > 11){
  23.         *mangled = 1;
  24.         len = 11;
  25.     } else
  26.         *mangled = 0;
  27.     strncpy(ans, filename, len);
  28.     have_lower = have_upper = 0;
  29.     for(i=0; i<11; i++){
  30.         if(islower(ans[i]))
  31.             have_lower = 1;
  32.         if(isupper(ans[i]))
  33.             have_upper = 1;
  34.         ans[i] = toupper(ans[i]);
  35.  
  36.         if(strchr("^+=/[]:,?*\\<>|\".", ans[i])){
  37.             *mangled = 1;
  38.             ans[i] = '~';
  39.         }
  40.     }
  41.     if (have_lower && have_upper)
  42.         *mangled = 1;
  43.     return ans;
  44. }
  45.  
  46. struct directory *labelit(char *dosname,
  47.               char *longname,
  48.               void *arg0,
  49.               struct directory *dir)
  50. {
  51.     time_t now;
  52.  
  53.     /* find out current time */
  54.     time(&now);
  55.     mk_entry(dosname, 0x8, 0, 0, now, dir);
  56.     return dir;
  57. }
  58.  
  59. static void usage(void)
  60. {
  61.     fprintf(stderr, "Mtools version %s, dated %s\n",
  62.         mversion, mdate);
  63.     fprintf(stderr, "Usage: %s [-vscV] drive:\n", progname);
  64.     exit(1);
  65. }
  66.  
  67.  
  68. void mlabel(int argc, char **argv, int type)
  69. {
  70.     int entry, verbose, clear, interactive, show, open_mode;
  71.     struct directory dir;
  72.     int result=0;
  73.     char longname[VBUFSIZE],filename[VBUFSIZE];
  74.     char shortname[13];
  75.     ClashHandling_t ch;
  76.     struct MainParam_t mp;
  77.     Stream_t *RootDir;
  78.     int c;
  79.     int mangled;
  80.     
  81.     init_clash_handling(&ch);
  82.     ch.name_converter = label_name;
  83.     ch.ignore_entry = -2;
  84.  
  85.     verbose = 0;
  86.     clear = 0;
  87.     show = 0;
  88.  
  89.     while ((c = getopt(argc, argv, "vcs")) != EOF) {
  90.         switch (c) {
  91.             case 'v':
  92.                 verbose = 1;
  93.                 break;
  94.             case 'c':
  95.                 clear = 1;
  96.                 break;
  97.             case 's':
  98.                 show = 1;
  99.                 break;
  100.             default:
  101.                 usage();
  102.             }
  103.     }
  104.  
  105.     if (argc - optind != 1 ||
  106.         !argv[optind][0] || argv[optind][1] != ':') 
  107.         usage();
  108.  
  109.     init_mp(&mp);
  110.     get_name(argv[optind], filename, mp.mcwd);
  111.     interactive = !filename[0] && !show && !clear;
  112.     if (filename[0] || clear || interactive)
  113.         open_mode = O_RDWR;
  114.     else
  115.         open_mode = O_RDONLY;
  116.  
  117.     RootDir = open_subdir(&mp, argv[optind], open_mode, 0, 0);
  118.     if(!RootDir && open_mode == O_RDWR && !clear && !filename[0] &&
  119.        ( errno == EACCES || errno == EPERM) ) {
  120.         show = 1;
  121.         interactive = 0;
  122.         RootDir = open_subdir(&mp, argv[optind], O_RDONLY, 0, 0);
  123.     }        
  124.     if(!RootDir) {
  125.         fprintf(stderr, "%s: Cannot initialize drive\n", argv[0]);
  126.         exit(1);
  127.     }
  128.  
  129.     entry = 0;
  130.     vfat_lookup(RootDir, &dir, &entry, 0, 0, ACCEPT_LABEL | MATCH_ANY,
  131.             NULL, shortname, longname);
  132.     
  133.     if(show || interactive){
  134.         if(entry == -1)
  135.             printf(" Volume has no label\n");
  136.         else if (*longname)
  137.             printf(" Volume label is %s (abbr=%s)\n",
  138.                    longname, shortname);
  139.         else
  140.             printf(" Volume label is %s\n", shortname);
  141.  
  142.     }
  143.  
  144.     /* ask for new label */
  145.     if(interactive){
  146.         fprintf(stderr,"Enter the new volume label : ");
  147.         fgets(filename, VBUFSIZE, stdin);
  148.         if(filename[0])
  149.             filename[strlen(filename)-1] = '\0';
  150.     }
  151.  
  152.     if(!show && entry != -1){
  153.         /* if we have a label, wipe it out before putting new one */
  154.         if(interactive && filename[0] == '\0')
  155.             if(ask_confirmation("Delete volume label (y/n): ",0,0)){
  156.                 FREE(&RootDir);
  157.                 exit(0);
  158.             }
  159.         dir.name[0] = DELMARK;
  160.         dir.attr = 0; /* for old mlabel */
  161.         dir_write(RootDir, entry-1, &dir);
  162.     }
  163.  
  164.     if (!show && filename[0] != '\0') {
  165.         ch.ignore_entry = 1;
  166.         result = mwrite_one(RootDir,filename,0,labelit,NULL,&ch) ? 
  167.           0 : 1;
  168.     }
  169.  
  170.     if(!show){
  171.         struct bootsector boot;
  172.         Stream_t *Fs = GetFs(RootDir);
  173.  
  174.         if(!filename[0])
  175.             strncpy(shortname, "NO NAME    ",11);
  176.         else
  177.             label_name(filename, verbose, &mangled, shortname);
  178.  
  179.         if(force_read(Fs,(char *)&boot,0,sizeof(boot)) == 
  180.            sizeof(boot) &&
  181.            boot.descr >= 0xf0 &&
  182.            boot.ext.old.dos4 == 0x29 &&
  183.            _WORD(boot.fatlen)){
  184.             strncpy(boot.ext.old.label, shortname, 11);
  185.             force_write(Fs, (char *)&boot, 0, sizeof(boot));
  186.         }
  187.     }
  188.  
  189.     FREE(&RootDir);
  190.     exit(result);
  191. }
  192.