Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <dir.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. static char *
  7. max_ptr(char *p1, char *p2)
  8. {
  9.   if (p1 > p2)
  10.     return p1;
  11.   else
  12.     return p2;
  13. }
  14.  
  15. int
  16. fnsplit (const char *path, char *drive, char *dir,
  17.          char *name, char *ext)
  18. {
  19.   int flags = 0, len;
  20.   const char *pp, *pe;
  21.  
  22.   if (drive)
  23.     *drive = '\0';
  24.   if (dir)
  25.     *dir = '\0';
  26.   if (name)
  27.     *name = '\0';
  28.   if (ext)
  29.     *ext = '\0';
  30.  
  31.   pp = path;
  32.  
  33.   if ((isalpha(*pp) || strchr("[\\]^_`", *pp)) && (pp[1] == ':'))
  34.   {
  35.     flags |= DRIVE;
  36.     if (drive)
  37.     {
  38.       strncpy(drive, pp, 2);
  39.       drive[2] = '\0';
  40.     }
  41.     pp += 2;
  42.   }
  43.  
  44.   pe = max_ptr(strrchr(pp, '\\'), strrchr(pp, '/'));
  45.   if (pe)
  46.   {
  47.     flags |= DIRECTORY;
  48.     pe++;
  49.     len = pe - pp;
  50.     if (dir)
  51.     {
  52.       strncpy(dir, pp, len);
  53.       dir[len] = '\0';
  54.     }
  55.     pp = pe;
  56.   }
  57.   else
  58.     pe = pp;
  59.  
  60.   /* Special case: "c:/path/." or "c:/path/.."
  61.      These mean FILENAME, not EXTENSION.  */
  62.   while (*pp == '.')
  63.     ++pp;
  64.   if (pp > pe)
  65.   {
  66.     flags |= FILENAME;
  67.     if (name)
  68.     {
  69.       len = pp - pe;
  70.       strncpy(name, pe, len);
  71.       name[len] = '\0';
  72.     }
  73.   }
  74.  
  75.   pe = strrchr(pp, '.');
  76.   if (pe)
  77.   {
  78.     flags |= EXTENSION;
  79.     if (ext)
  80.       strcpy(ext, pe);
  81.   }
  82.   else
  83.     pe = strchr( pp, '\0');
  84.  
  85.   if (pp != pe)
  86.   {
  87.     flags |= FILENAME;
  88.     len = pe - pp;
  89.     if (name)
  90.     {
  91.       strncpy(name, pp, len);
  92.       name[len] = '\0';
  93.     }
  94.   }
  95.  
  96.   if (strcspn(path, "*?[") < strlen(path))
  97.     flags |= WILDCARDS;
  98.  
  99.   return flags;
  100. }
  101.  
  102. #ifdef TEST
  103.  
  104. #include <stdio.h>
  105.  
  106. int
  107. main(void)
  108. {
  109.   char arg[81], drive[81], dir[81], fname[81], ext[81];
  110.  
  111.   fputs("> ", stdout); fflush(stdout);
  112.   gets(arg);
  113.  
  114.   printf("`%s' (%x): `%s' `%s' `%s' `%s'\n", arg,
  115.          fnsplit(arg, drive, dir, fname, ext), drive, dir, fname, ext);
  116.  
  117.   return 0;
  118. }
  119.  
  120. #endif
  121.