Пример #1
0
/*
 * Get the absolute name of the given relative directory.
 *
 * parameter directory: Directory name, relative to current directory.
 * return FAIL for failure, OK for success
 */
int mch_full_dir_name(char *directory, char *buffer, int len)
{
  int retval = OK;

  if(STRLEN(directory) == 0) {
    return mch_dirname((char_u *) buffer, len);
  }

  char old_dir[MAXPATHL];

  /* Get current directory name. */
  if (mch_dirname((char_u *) old_dir, MAXPATHL) == FAIL) {
    return FAIL;
  }

  /* We have to get back to the current dir at the end, check if that works. */
  if (mch_chdir(old_dir) != 0) {
    return FAIL;
  }

  if (mch_chdir(directory) != 0) {
    /* Do not return immediatly since we may be in the wrong directory. */
    retval = FAIL;
  }

  if (retval == FAIL || mch_dirname((char_u *) buffer, len) == FAIL) {
    /* Do not return immediatly since we are in the wrong directory. */
    retval = FAIL;
  }
   
  if (mch_chdir(old_dir) != 0) {
    /* That shouldn't happen, since we've tested if it works. */
    retval = FAIL;
    EMSG(_(e_prev_dir));
  }

  return retval;
}
Пример #2
0
/*
 * Get absolute file name into "buf[len]".
 *
 * return FAIL for failure, OK for success
 */
int mch_FullName(
        char_u      *fname,
        char_u      *buf,
        int len,
        int force                       /* also expand when already absolute path */
        )
{
  int l;
  char_u olddir[MAXPATHL];
  char_u      *p;
  int retval = OK;



  /* expand it if forced or not an absolute path */
  if (force || !mch_isFullName(fname)) {
    /*
     * If the file name has a path, change to that directory for a moment,
     * and then do the getwd() (and get back to where we were).
     * This will get the correct path name with "../" things.
     */
    if ((p = vim_strrchr(fname, '/')) != NULL) {

      /* Only change directory when we are sure we can return to where
       * we are now.  After doing "su" chdir(".") might not work. */
      if ((mch_dirname(olddir, MAXPATHL) == FAIL
         || mch_chdir((char *)olddir) != 0)) {
        p = NULL;               /* can't get current dir: don't chdir */
        retval = FAIL;
      } else   {
        /* The directory is copied into buf[], to be able to remove
         * the file name without changing it (could be a string in
         * read-only memory) */
        if (p - fname >= len)
          retval = FAIL;
        else {
          vim_strncpy(buf, fname, p - fname);
          if (mch_chdir((char *)buf))
            retval = FAIL;
          else
            fname = p + 1;
          *buf = NUL;
        }
      }
    }
    if (mch_dirname(buf, len) == FAIL) {
      retval = FAIL;
      *buf = NUL;
    }
    if (p != NULL) {
      l = mch_chdir((char *)olddir);
      if (l != 0)
        EMSG(_(e_prev_dir));
    }

    l = STRLEN(buf);
    if (l >= len - 1)
      retval = FAIL;       /* no space for trailing "/" */
    else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
             && STRCMP(fname, ".") != 0)
      STRCAT(buf, "/");
  }

  /* Catch file names which are too long. */
  if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
    return FAIL;

  /* Do not append ".", "/dir/." is equal to "/dir". */
  if (STRCMP(fname, ".") != 0)
    STRCAT(buf, fname);

  return OK;
}