home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / SRC / msdos_diskaccess.lzh / MS_DISK_ACCESS / fixname.c < prev    next >
Text File  |  1990-05-10  |  2KB  |  80 lines

  1. /*
  2.  * Convert a Unix filename to a legal MSDOS name.  Returns a pointer to
  3.  * the 'fixed' name.  Will truncate file and extension names, will
  4.  * substitute the letter 'X' for any illegal character in the name.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "msdos.h"
  10.  
  11. char *
  12. fixname(filename, verbose)
  13. char *filename;
  14. int verbose;
  15. {
  16.     static char *dev[8] = {"CON", "AUX", "COM1", "LPT1", "PRN", "LPT2",
  17.     "LPT3", "NUL"};
  18.     char *s, *ans, *name, *ext, *strcpy(), *strpbrk(), *strrchr();
  19.     char buf[MAX_PATH], *malloc();
  20.     int dot, modified;
  21.     register int i;
  22.  
  23.     strcpy(buf, filename);
  24.     name = buf;
  25.                     /* zap the leading path */
  26.     if (s = strrchr(name, '/'))
  27.         name = s+1;
  28.     if (s = strrchr(name, '\\'))
  29.         name = s+1;
  30.  
  31.     ext = "";
  32.     dot = 0;
  33.     for (i=strlen(buf)-1; i>=0; i--) {
  34.         if (buf[i] == '.' && !dot) {
  35.             dot = 1;
  36.             buf[i] = '\0';
  37.             ext = &buf[i+1];
  38.         }
  39.         if (islower(buf[i]))
  40.             buf[i] = toupper(buf[i]);
  41.     }
  42.     if (*name == '\0') {
  43.         name = "X";
  44.         if (verbose)
  45.             printf("\"%s\" Null name component, using \"%s.%s\"\n", filename, name, ext);
  46.     }
  47.     for (i=0; i<8; i++) {
  48.         if (!strcmp(name, dev[i])) {
  49.             *name = 'X';
  50.             if (verbose)
  51.                 printf("\"%s\" Is a device name, using \"%s.%s\"\n", filename, name, ext);
  52.         }
  53.     }
  54.     if (strlen(name) > 8) {
  55.         *(name+8) = '\0';
  56.         if (verbose)
  57.             printf("\"%s\" Name too long, using, \"%s.%s\"\n", filename, name, ext);
  58.     }
  59.     if (strlen(ext) > 3) {
  60.         *(ext+3) = '\0';
  61.         if (verbose)
  62.             printf("\"%s\" Extension too long, using \"%s.%s\"\n", filename, name, ext);
  63.     }
  64.     modified = 0;
  65.     while (s = strpbrk(name, "^+=/[]:',?*\\<>|\". ")) {
  66.         modified++;
  67.         *s = 'X';
  68.     }
  69.     while (s = strpbrk(ext, "^+=/[]:',?*\\<>|\". ")) {
  70.         modified++;
  71.         *s = 'X';
  72.     }
  73.     if (modified && verbose)
  74.         printf("\"%s\" Contains illegal character(s), using \"%s.%s\"\n", filename, name, ext);
  75.  
  76.     ans = malloc(12);
  77.     sprintf(ans, "%-8.8s%-3.3s", name, ext);
  78.     return(ans);
  79. }
  80.