home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / MN325SRC.ZIP / makenl-3.2.5 / src / osdosful.c < prev    next >
C/C++ Source or Header  |  2005-02-06  |  4KB  |  111 lines

  1. /* $Id: osdosful.c,v 1.4 2004/09/05 10:43:57 mbroek Exp $ */
  2.  
  3. #define HAVE_OS_FULLPATH
  4.  
  5. #include <ctype.h>
  6.  
  7. #define FUNCNAME "[dos] os_fullpath"
  8. /* make an absolute path from given relative path */
  9. int os_fullpath(char *dst, const char *src, size_t bufsiz)
  10. {
  11.     int rc = -1;
  12.     char tmp[MYMAXDIR];
  13.     char curdir[MYMAXDIR];
  14.     int curdrnum;
  15.     int reqdrnum;
  16.  
  17.     curdrnum = getdisk();
  18.     Debug1("Old drive number: %d\n", curdrnum);
  19.     mklog(4, "Old drive number: %d", curdrnum)
  20.     if (!src || !*src)
  21.         src = ".";
  22.  
  23.     if (src[1] == ':')          /* drive letter specified */
  24.     {
  25.         Debug("You specified a drive letter\n");
  26.     mklog(4, "You specified a drive letter");
  27.         reqdrnum = toupper(src[0]) - 'A';
  28.         if (reqdrnum != curdrnum) /* requested drive is not current drive */
  29.         {
  30.             Debug1("Switching to drive %d\n", reqdrnum);
  31.         mklog(4, "Switching to drive %d", reqdrnum);
  32.             setdisk(reqdrnum);  /* set current disk */
  33.             if (getdisk() != reqdrnum) /* Specified drive does not exist */
  34.             {
  35.                 Debug("This drive does not exist\n");
  36.         mklog(4, "This drive does not exist");
  37.                 return -1;
  38.             }
  39.         }
  40.         src += 2;               /* Skip drive letter */
  41.     }
  42.  
  43.     Debug1("Searching for '%s' on current drive\n", src);
  44.     mklog(4, "Searching for '%s' on current drive", src);
  45.     if (getcwd(curdir, bufsiz) != NULL)
  46.     {
  47.         char *fname;
  48.         char *dir = tmp;
  49.  
  50.         /* requested drive is now current drive, curdrnum is the original
  51.            drive reqdrnum is the requested drive curdir is the original
  52.            directory on the current drive src is the requested relative
  53.            path without drive */
  54.         strcpy(dir, src);
  55.         fname = strrchr(dir, '\\');
  56.         if (!fname)
  57.             fname = strrchr(dir, '/');
  58.         if (!fname)             /* no backslash */
  59.         {
  60.             Debug("You don't have any backslash in the file name.\n");
  61.         mklog(4, "You don't have any backslash in the file name.");
  62.             if (!(dir[0] == '.' && (dir[1] == '.' || dir[1] == '\0')))
  63.             {
  64.                 Debug("I assume this file is relative to cwd.\n");
  65.         mklog(4, "I assume this file is relative to cwd.");
  66.                 fname = dir;
  67.                 dir = ".";
  68.             }
  69.             else
  70.             {
  71.                 Debug("Looks like relative directory only");
  72.         mklog(4, "Looks like relative directory only");
  73.                 fname = "";
  74.             }
  75.         }
  76.         else
  77.             *fname++ = '\0';
  78.  
  79.         Debug1("Directory is now %s\n", dir);
  80.         Debug1("File name is now %s\n", fname);
  81.     mklog(4, "Directory is now %s, File name is now %s", dir, fname);
  82.         /* fname = pure file name */
  83.         /* dir = relative path, only directory */
  84.  
  85.         /* If dir is empty it means root directory */
  86.         if (chdir((*dir) ? dir : "\\") == 0)
  87.         {
  88.             Debug("chdir() suceeded. The directory exists.\n");
  89.         mklog(4, "chdir() suceeded. The directory exists.");
  90.             if (getcwd(dst, bufsiz) != NULL)
  91.             {
  92.                 rc = 0;
  93.                 if (fname && *fname)
  94.                 {
  95.                     if (strlen(dst) != 3) /* Does not look like "C:\" */
  96.                         strcat(dst, "\\");
  97.                     strcat(dst, fname);
  98.                 }
  99.                 Debug1("final full name is %s\n", fname);
  100.         mklog(4, "final full name is %s", fname);
  101.             }
  102.         }
  103.         chdir(curdir);
  104.     }
  105.     setdisk(curdrnum);
  106.     os_filecanonify(dst);
  107.     return rc;
  108. }
  109.  
  110. #undef FUNCNAME
  111.