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 / file_name.c < prev    next >
C/C++ Source or Header  |  1997-11-12  |  4KB  |  223 lines

  1. #include "sysincludes.h"
  2. #include "msdos.h"
  3. #include "mtools.h"
  4. #include "vfat.h"
  5. #include "codepage.h"
  6.  
  7. /* Write a DOS name + extension into a legal unix-style name.  */
  8. char *unix_normalize (char *ans, char *name, char *ext)
  9. {
  10.     char *a;
  11.     int j;
  12.     
  13.     for (a=ans,j=0; (j<8) && (name[j] > ' '); ++j,++a)
  14.         *a = name[j];
  15.     if(*ext > ' ') {
  16.         *a++ = '.';
  17.         for (j=0; j<3 && ext[j] > ' '; ++j,++a)
  18.             *a = ext[j];
  19.     }
  20.     *a++ = '\0';
  21.     return ans;
  22. }
  23.  
  24. typedef enum Case_l {
  25.     NONE,
  26.     UPPER,
  27.     LOWER 
  28. } Case_t;
  29.  
  30. static void TranslateToDos(char *s, char *t, int count,
  31.                char *end, Case_t *Case, int *mangled)
  32. {
  33.     *Case = NONE;
  34.     for( ;  *s && (s < end || !end); s++) {
  35.         if(!count) {
  36.             *mangled |= 3;
  37.             break;
  38.         }
  39.         /* skip spaces & dots */
  40.         if(*s == ' ' || *s == '.') {
  41.             *mangled |= 3;
  42.             continue;
  43.         }
  44.  
  45.         /* convert to dos */
  46.         if((*s) & 0x80) {
  47.             *mangled |= 1;
  48.             *t = to_dos(*s);
  49.         }
  50.  
  51.         if ((*s & 0x7f) < ' ' ) {
  52.             *mangled |= 3;
  53.             *t = '_';
  54.         } else if (islower(*s)) {
  55.             *t = toupper(*s);
  56.             if(*Case == UPPER && !mtools_no_vfat)
  57.                 *mangled |= 1;
  58.             else
  59.                 *Case = LOWER;
  60.         } else if (isupper(*s)) {
  61.             *t = *s;
  62.             if(*Case == LOWER && !mtools_no_vfat)
  63.                 *mangled |= 1;
  64.             else
  65.                 *Case = UPPER;
  66.         } else if((*s) & 0x80)
  67.             *t = mstoupper[(*t) & 0x7f];    /* upper case */
  68.         else
  69.             *t = *s;
  70.         count--;
  71.         t++;
  72.     }
  73. }
  74.  
  75. /* dos_name
  76.  *
  77.  * Convert a Unix-style filename to a legal MSDOS name and extension.
  78.  * Will truncate file and extension names, will substitute
  79.  * the character '~' for any illegal character(s) in the name.
  80.  */
  81. char *dos_name(char *name, int verbose, int *mangled, char *ans)
  82. {
  83.     char *s, *ext;
  84.     register int i;
  85.     Case_t BaseCase, ExtCase;
  86.  
  87.     *mangled = 0;
  88.  
  89.     /* skip drive letter */
  90.     if (name[0] && name[1] == ':')
  91.         name = &name[2];
  92.  
  93.     /* zap the leading path */
  94.     if ((s = strrchr(name, '/')))
  95.         name = s + 1;
  96.     if ((s = strrchr(name, '\\')))
  97.         name = s + 1;
  98.     
  99.     memset(ans, ' ', 11);
  100.     ans[11]='\0';
  101.  
  102.     /* skip leading dots and spaces */
  103.     i = strspn(name, ". ");
  104.     if(i) {
  105.         name += i;
  106.         *mangled = 3;
  107.     }
  108.         
  109.     ext = strrchr(name, '.');
  110.  
  111.     /* main name */
  112.     TranslateToDos(name, ans, 8, ext, &BaseCase, mangled);
  113.     if(ext)
  114.         TranslateToDos(ext+1, ans+8, 3, 0, &ExtCase,  mangled);
  115.  
  116.     if(*mangled & 2)
  117.         autorename_short(ans, 0);
  118.  
  119.     if(!*mangled) {
  120.         if(BaseCase == LOWER)
  121.             *mangled |= BASECASE;
  122.         if(ExtCase == LOWER)
  123.             *mangled |= EXTCASE;
  124.         if((BaseCase == LOWER || ExtCase == LOWER) &&
  125.            !mtools_no_vfat) {
  126.           *mangled |= 1;
  127.         }
  128.     }
  129.     return ans;
  130. }
  131.  
  132.  
  133. /*
  134.  * Get rid of spaces in an MSDOS 'raw' name (one that has come from the
  135.  * directory structure) so that it can be used for regular expression
  136.  * matching with a Unix filename.  Also used to 'unfix' a name that has
  137.  * been altered by dos_name().
  138.  */
  139.  
  140. char *unix_name(char *name, char *ext, char Case, char *ans)
  141. {
  142.     char *s, tname[9], text[4];
  143.     int i;
  144.  
  145.     strncpy(tname, (char *) name, 8);
  146.     tname[8] = '\0';
  147.     if ((s = strchr(tname, ' ')))
  148.         *s = '\0';
  149.  
  150.     if(!(Case & (BASECASE | EXTCASE)) && mtools_ignore_short_case)
  151.         Case |= BASECASE | EXTCASE;
  152.  
  153.     if(Case & BASECASE)
  154.         for(i=0;i<8 && tname[i];i++)
  155.             tname[i] = tolower(tname[i]);
  156.  
  157.     strncpy(text, (char *) ext, 3);
  158.     text[3] = '\0';
  159.     if ((s = strchr(text, ' ')))
  160.         *s = '\0';
  161.  
  162.     if(Case & EXTCASE)
  163.         for(i=0;i<3 && text[i];i++)
  164.             text[i] = tolower(text[i]);
  165.  
  166.     if (*text) {
  167.         strcpy(ans, tname);
  168.         strcat(ans, ".");
  169.         strcat(ans, text);
  170.     } else
  171.         strcpy(ans, tname);
  172.  
  173.     /* fix special characters (above 0x80) */
  174.     to_unix(ans,11);
  175.     return(ans);
  176. }
  177.  
  178.  
  179.  
  180. int unicode_read(struct unicode_char *in, char *out, int num)
  181. {
  182.     int j;
  183.  
  184.     for (j=0; j<num; ++j) {
  185.         if (in->uchar)
  186.             *out = '_';
  187.         else
  188.             *out = in->lchar;
  189.         ++out;
  190.         ++in;
  191.     }
  192.     return num;
  193. }
  194.  
  195. /* If null encountered, set *end to 0x40 and write nulls rest of way
  196.  * 950820: Win95 does not like this!  It complains about bad characters.
  197.  * So, instead: If null encountered, set *end to 0x40, write the null, and
  198.  * write 0xff the rest of the way (that is what Win95 seems to do; hopefully
  199.  * that will make it happy)
  200.  */
  201. /* Always return num */
  202. int unicode_write(char *in, struct unicode_char *out, int num, int *end_p)
  203. {
  204.     int j;
  205.  
  206.     for (j=0; j<num; ++j) {
  207.         out->uchar = '\0';    /* Hard coded to ASCII */
  208.         if (*end_p)
  209.             /* Fill with 0xff */
  210.             out->uchar = out->lchar = (char) 0xff;
  211.         else {
  212.             out->lchar = *in;
  213.             if (! *in) {
  214.                 *end_p = VSE_LAST;
  215.             }
  216.         }
  217.  
  218.         ++out;
  219.         ++in;
  220.     }
  221.     return num;
  222. }
  223.