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 / unixname.c < prev   
Text File  |  1991-08-03  |  2KB  |  120 lines

  1. /*
  2.  * unixname(), getname(), getpath()
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include "msdos.h"
  8.  
  9. /*
  10.  * Get rid of spaces in a MSDOS 'raw' name (one that has come from the
  11.  * directory structure) so that it can be used for regular expression
  12.  * matching with a unix file name.  Also used to 'unfix' a name that has
  13.  * been altered by fixname().  Returns a pointer to the unix style name.
  14.  */
  15.  
  16. char *
  17. unixname(name, ext)
  18. char *name, *ext;
  19. {
  20.     char *s, tname[9], text[4], *strcpy(), *strcat(), *strchr();
  21.     char *ans, *malloc();
  22.  
  23.     strcpy(tname, name);
  24.     if (s = strchr(tname, ' '))
  25.         *s = '\0';
  26.  
  27.     strcpy(text, ext);
  28.     if (s = strchr(text, ' '))
  29.         *s = '\0';
  30.  
  31.     ans = malloc(13);
  32.  
  33.     if (*text) {
  34.         strcpy(ans, tname);
  35.         strcat(ans, ".");
  36.         strcat(ans, text);
  37.     }
  38.     else
  39.         strcpy(ans, tname);
  40.     s = ans;
  41.     while (*s) {
  42.         if (!isalnum(*s) && *s != '$' && *s != '_' && *s != '.')
  43.             *s = 'x';
  44.         s++;
  45.     } 
  46.     return(ans);
  47. }
  48.  
  49. /*
  50.  * Get name component of filename.  Translates name to upper case.  Returns
  51.  * pointer to new name.
  52.  */
  53.  
  54. char *
  55. getname(filename)
  56. char *filename;
  57. {
  58.     char *s, *ans, *malloc(), *temp, *strcpy(), *strrchr();
  59.     char buf[MAX_PATH];
  60.  
  61.     strcpy(buf, filename);
  62.     temp = buf;
  63.                     /* find the last separator */
  64.     if (s = strrchr(temp, '/'))
  65.         temp = s+1;
  66.     if (s = strrchr(temp, '\\'))
  67.         temp = s+1;
  68.                     /* xlate to upper case */
  69.     for (s = temp; *s; ++s) {
  70.         if (islower(*s))
  71.             *s = toupper(*s);
  72.     }
  73.  
  74.     ans = malloc((unsigned int) strlen(temp)+1);
  75.     strcpy(ans, temp);
  76.     return(ans);
  77. }
  78.  
  79. /*
  80.  * Get the path component of the filename.  Translates to upper case.
  81.  * Returns pointer to path.  Doesn't alter leading separator, always
  82.  * strips trailing separator (unless it is the path itself).
  83.  */
  84.  
  85. char *
  86. getpath(filename)
  87. char *filename;
  88. {
  89.     char *s, *temp, *ans, *malloc(), *strcpy(), *strrchr();
  90.     char buf[MAX_PATH];
  91.     int has_sep;
  92.  
  93.     strcpy(buf, filename);
  94.     temp = buf;
  95.                     /* find last separator */
  96.     has_sep = 0;
  97.     if (s = strrchr(temp, '/')) {
  98.         has_sep++;
  99.         temp = s;
  100.     }
  101.     if (s = strrchr(temp, '\\')) {
  102.         has_sep++;
  103.         temp = s;
  104.     }
  105.  
  106.     *temp = '\0';
  107.                     /* translate to upper case */
  108.     for (s = buf; *s; ++s) {
  109.         if (islower(*s))
  110.             *s = toupper(*s);
  111.     }
  112.                     /* if separator alone, put it back */
  113.     if (!strlen(buf) && has_sep)
  114.         strcpy(buf, "/");
  115.  
  116.     ans = malloc((unsigned int) strlen(buf)+1);
  117.     strcpy(ans, buf);
  118.     return(ans);
  119. }
  120.