Loading ...
Sorry, an error occurred while loading the content.

suspicious code in expanding file name

Expand Messages
  • Bram Moolenaar
    I was looking throught the various implementations of mch_FullName(), to find differences between systems. I noticed some very strange code in the Mac version
    Message 1 of 7 , Jan 23, 2001
    • 0 Attachment
      I was looking throught the various implementations of mch_FullName(), to find
      differences between systems. I noticed some very strange code in the Mac
      version that:
      - overwrites the "fname" argument.
      - copies the file name back and forth.
      - appends to buf[], which has not been initialized.
      I wonder how this ever could have worked. Please check if this patch is OK.

      The only change that I intended to make was not to add a ":" when "fname" is
      empty. It should return the name of the current directory then, without a ":"
      appended.

      *** os_mac.c~ Tue Jan 9 16:40:54 2001
      --- os_mac.c Tue Jan 23 11:20:50 2001
      ***************
      *** 692,710 ****
      char_u c;
      int retval = OK;

      ! if (force || !mch_isFullName(fname)) /* if forced or not an absolute path */
      {
      /*
      * 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.
      */
      -
      - /* STRCAT(buf, fname);
      - slash_n_colon_adjust (buf);
      - fname[0] = 0;
      - STRCAT(fname, buf);
      - */
      if ((p = vim_strrchr(fname, ':')) != NULL)
      {
      p++;
      --- 692,705 ----
      char_u c;
      int retval = OK;

      ! if (force || !mch_isFullName(fname))
      {
      /*
      + * Forced or not an absolute path.
      * 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)
      {
      p++;
      ***************
      *** 730,749 ****
      *newdir = NUL;
      }
      l = STRLEN(buf);
      ! if (l && buf[l - 1] != ':') /*MAC*/
      STRCAT(buf, ":");
      if (p != NULL)
      mch_chdir((char *)olddir);
      }
      else
      {
      ! STRCAT(buf, fname);
      ! slash_n_colon_adjust (buf);
      ! fname[0] = 0;
      ! STRCAT(fname, buf);
      ! buf[0] = 0;
      }
      ! STRCAT(buf, fname);
      return retval;
      }

      --- 725,743 ----
      *newdir = NUL;
      }
      l = STRLEN(buf);
      ! if (l > 0 && buf[l - 1] != ':' && *fname != NUL)
      STRCAT(buf, ":");
      if (p != NULL)
      mch_chdir((char *)olddir);
      + STRCAT(buf, fname);
      }
      else
      {
      ! STRNCPY(buf, fname, len);
      ! buf[len - 1] = NUL;
      ! slash_n_colon_adjust(buf);
      }
      !
      return retval;
      }


      --
      For society, it's probably a good thing that engineers value function over
      appearance. For example, you wouldn't want engineers to build nuclear power
      plants that only _look_ like they would keep all the radiation inside.
      (Scott Adams - The Dilbert principle)

      /// Bram Moolenaar -- Bram@... -- http://www.moolenaar.net \\\
      ((( Creator of Vim - http://www.vim.org -- ftp://ftp.vim.org/pub/vim )))
      \\\ Help me helping AIDS orphans in Uganda - http://iccf-holland.org ///
    • H. Eckert
      ... Keep in mind though the semantics of pathnames under MacOS. : alone is similar to the . in Unix. The filename :foo.txt is the same as ./foo.txt .
      Message 2 of 7 , Jan 23, 2001
      • 0 Attachment
        Quoting Bram Moolenaar (Bram@...):
        > The only change that I intended to make was not to add a ":" when
        > "fname" is empty. It should return the name of the current directory
        > then, without a ":" appended.

        Keep in mind though the semantics of pathnames under MacOS.
        ":" alone is similar to the "." in Unix. The filename ":foo.txt"
        is the same as "./foo.txt".


        Greetings,
        Ripley
        --
        H. Eckert, 12051 Berlin
        ISO 8859-1: Ä=Ae, Ö=Oe, Ü=Ue, ä=ae, ö=oe, ü=ue, ß=sz.
      • Dany St-Amant
        ... This code have been a big kludge which need to be cleaned out. Unfortunately my paying work gave me little time to work on Vim [I barely had time to use
        Message 3 of 7 , Jan 23, 2001
        • 0 Attachment
          on 23/01/01 6:54, Bram Moolenaar at Bram@... wrote:

          >
          > I was looking throught the various implementations of mch_FullName(), to find
          > differences between systems. I noticed some very strange code in the Mac
          > version that:
          > - overwrites the "fname" argument.
          > - copies the file name back and forth.
          > - appends to buf[], which has not been initialized.
          > I wonder how this ever could have worked. Please check if this patch is OK.
          >
          > The only change that I intended to make was not to add a ":" when "fname" is
          > empty. It should return the name of the current directory then, without a ":"
          > appended.
          >

          This code have been a big kludge which need to be cleaned out. Unfortunately
          my "paying" work gave me little time to work on Vim [I barely had time to
          use my Mac :( ].

          I had big trouble to make Vim understand both Unix and Mac path name.
          Especially for the special character (i.e: Vim think that "\ " is a space,
          but the Mac think it a slash and a space [that's why Vim on Mac have big
          problem with folder or volume name containig space.])

          I'll check your difff against 6.0r.

          Dany,
        • Dany St-Amant
          ... The function used inside mch_dirname is said to add a ending colon. The code adding the colon inside mch_FullName is either a copy of the os_win??.c or
          Message 4 of 7 , Jan 23, 2001
          • 0 Attachment
            on 23/01/01 6:54, Bram Moolenaar at Bram@... wrote:

            >
            > I was looking throught the various implementations of mch_FullName(), to find
            > differences between systems. I noticed some very strange code in the Mac
            > version that:
            > - overwrites the "fname" argument.
            > - copies the file name back and forth.
            > - appends to buf[], which has not been initialized.
            > I wonder how this ever could have worked. Please check if this patch is OK.
            >
            > The only change that I intended to make was not to add a ":" when "fname" is
            > empty. It should return the name of the current directory then, without a ":"
            > appended.
            >

            The function used inside mch_dirname is said to add a ending colon. The code
            adding the colon inside "mch_FullName" is either a copy of the os_win??.c or
            the safety feature. As the code is right now if fname = "", buf will end
            with a colon.

            Tested with
            :new
            :echo expand("%:p:h")

            The output doesn't show the ending colon but traceing to the code prooves
            that the colon is still at the end. I'll try tomorrow to force the removal
            of the ending colon, when fname = "". Do you have any :echo command which
            could show me the wrong and right behaviour/

            It's a tad strange that the above
            :echo expand("%:p:h")
            returns something while
            :echo expand("%:p")
            returns nothing.


            Snippet of the code for reference:

            if (mch_dirname(buf, len) == FAIL)
            {
            retval = FAIL;
            *newdir = NUL;
            }
            l = STRLEN(buf);
            if (l && buf[l - 1] != ':' && *fname != NUL)
            STRCAT(buf, ":");
            if (p != NULL)
            mch_chdir((char *)olddir);
            STRCAT(buf, fname);


            Dany
          • Bram Moolenaar
            ... The idea is that when the file name is (nothing) the name of the directory is returned. Previously a path separator was added, now it isn t: get
            Message 5 of 7 , Jan 24, 2001
            • 0 Attachment
              Ripley wrote:

              > Quoting Bram Moolenaar (Bram@...):
              > > The only change that I intended to make was not to add a ":" when
              > > "fname" is empty. It should return the name of the current directory
              > > then, without a ":" appended.
              >
              > Keep in mind though the semantics of pathnames under MacOS.
              > ":" alone is similar to the "." in Unix. The filename ":foo.txt"
              > is the same as "./foo.txt".

              The idea is that when the file name is "" (nothing) the name of the directory
              is returned. Previously a path separator was added, now it isn't:

              get current directory
              append ":" (not anymore when fname is empty)
              append fname

              I suppose on the Mac these two are considered equal:

              ...:src:proto:
              ...:src:proto

              --
              A)bort, R)etry, P)lease don't bother me again

              /// Bram Moolenaar -- Bram@... -- http://www.moolenaar.net \\\
              ((( Creator of Vim - http://www.vim.org -- ftp://ftp.vim.org/pub/vim )))
              \\\ Help me helping AIDS orphans in Uganda - http://iccf-holland.org ///
            • Bram Moolenaar
              ... I suspect the mch_dirname() function adds a colon to directory names then. Try this patch: ... *************** ... STRCPY(buf, strerror(errno)); return
              Message 6 of 7 , Jan 24, 2001
              • 0 Attachment
                Dany St-Amant wrote:

                > The function used inside mch_dirname is said to add a ending colon. The code
                > adding the colon inside "mch_FullName" is either a copy of the os_win??.c or
                > the safety feature. As the code is right now if fname = "", buf will end
                > with a colon.
                >
                > Tested with
                > :new
                > :echo expand("%:p:h")

                I suspect the mch_dirname() function adds a colon to directory names then.
                Try this patch:

                *** os_mac.c~ Tue Jan 23 11:25:06 2001
                --- os_mac.c Wed Jan 24 11:03:35 2001
                ***************
                *** 556,561 ****
                --- 556,563 ----
                STRCPY(buf, strerror(errno));
                return FAIL;
                }
                + else if (*buf != NUL && buf[STRLEN(buf) - 1] == ':')
                + buf[STRLEN(buf) - 1] = NUL; /* remove trailing ':' */
                return OK;
                #endif
                }

                > The output doesn't show the ending colon but traceing to the code prooves
                > that the colon is still at the end. I'll try tomorrow to force the removal
                > of the ending colon, when fname = "". Do you have any :echo command which
                > could show me the wrong and right behaviour/

                slash_n_colon_adjust() may get in the way, I don't know.

                :echo expand("%:p:h") should be a good test: if the buffer has no name it
                would remove the last part of the path. Thus when the current directory is
                the Vim "src" directory, you would get a result with "src" removed.

                > It's a tad strange that the above
                > :echo expand("%:p:h")
                > returns something while
                > :echo expand("%:p")
                > returns nothing.

                The idea is that when a buffer has no name, then the full path of it still is
                no name. But "%:p:h" gets the directory the buffer is in, which is the
                current directory.

                --
                hundred-and-one symptoms of being an internet addict:
                2. You kiss your girlfriend's home page.

                /// Bram Moolenaar -- Bram@... -- http://www.moolenaar.net \\\
                ((( Creator of Vim - http://www.vim.org -- ftp://ftp.vim.org/pub/vim )))
                \\\ Help me helping AIDS orphans in Uganda - http://iccf-holland.org ///
              • Dany St-Amant
                ... The new patch seems to behave as you wish. When running from the src directory, :new, :echo expand( %:p:h ), returns Huge-2:vim:vim60r before it was
                Message 7 of 7 , Jan 25, 2001
                • 0 Attachment
                  on 24/01/01 5:53, Bram Moolenaar at Bram@... wrote:
                  > Dany St-Amant wrote:
                  >
                  >> The function used inside mch_dirname is said to add a ending colon. The code
                  >> adding the colon inside "mch_FullName" is either a copy of the os_win??.c or
                  >> the safety feature. As the code is right now if fname = "", buf will end
                  >> with a colon.
                  >
                  > I suspect the mch_dirname() function adds a colon to directory names then.
                  > Try this patch:
                  >
                  > *** os_mac.c~ Tue Jan 23 11:25:06 2001
                  > --- os_mac.c Wed Jan 24 11:03:35 2001
                  > ***************
                  > *** 556,561 ****
                  > --- 556,563 ----
                  > STRCPY(buf, strerror(errno));
                  > return FAIL;
                  > }
                  > + else if (*buf != NUL && buf[STRLEN(buf) - 1] == ':')
                  > + buf[STRLEN(buf) - 1] = NUL; /* remove trailing ':' */
                  > return OK;
                  > #endif
                  > }
                  >
                  >> The output doesn't show the ending colon but traceing to the code prooves
                  >> that the colon is still at the end. I'll try tomorrow to force the removal
                  >> of the ending colon, when fname = "". Do you have any :echo command which
                  >> could show me the wrong and right behaviour/
                  >
                  > slash_n_colon_adjust() may get in the way, I don't know.
                  >
                  > :echo expand("%:p:h") should be a good test: if the buffer has no name it
                  > would remove the last part of the path. Thus when the current directory is
                  > the Vim "src" directory, you would get a result with "src" removed.
                  >

                  The new patch seems to behave as you wish. When running from the src
                  directory, :new, :echo expand("%:p:h"), returns

                  Huge-2:vim:vim60r

                  before it was returning

                  Huge-2:vim:vim60r:src

                  The ending colon was remove by the :h

                  >> It's a tad strange that the above
                  >> :echo expand("%:p:h")
                  >> returns something while
                  >> :echo expand("%:p")
                  >> returns nothing.
                  >
                  > The idea is that when a buffer has no name, then the full path of it still is
                  > no name. But "%:p:h" gets the directory the buffer is in, which is the
                  > current directory.

                  Huh, I think you are saying the reverse of what you said two sentences
                  ago...

                  Dany
                Your message has been successfully submitted and would be delivered to recipients shortly.