This is the mail archive of the
cygwin-patches
mailing list for the Cygwin project.
Re: detect . in a/.//
On Sep 30 11:34, Christopher Faylor wrote:
> bool
> has_dot_last_component (const char *dir, bool test_dot_dot)
> {
> /* SUSv3: . and .. are not allowed as last components in various system
> calls. Don't test for backslash path separator since that's a Win32
> path following Win32 rules. */
> const char *last_comp = strrchr (dir, '\0');
>
> if (last_comp == dir)
> return false; /* Empty string. Probably shouldn't happen here? */
>
> /* Detect run of trailing slashes */
> while (last_comp > dir && *--last_comp == '/')
> continue;
>
> /* Detect just a run of slashes or a path that does not end with a slash. */
> if (*last_comp != '.')
> return false;
>
> /* We know we have a trailing dot here. Check that it really is a standalone "."
> path component by checking that it is at the beginning of the string or is
> preceded by a "/" */
> if (last_comp == dir || *--last_comp == '/')
> return true;
>
> /* If we're not checking for '..' we're done. Ditto if we're now pointing to
> a non-dot. */
> if (!test_dot_dot || *last_comp != '.')
> return false; /* either not testing for .. or this was not '..' */
>
> /* Repeat previous test for standalone or path component. */
> return last_comp == dir || last_comp[-1] == '/';
> }
Looks good to me. Easier to understand than the current code.
Corinna
--
Corinna Vinschen Please, send mails regarding Cygwin to
Cygwin Project Co-Leader cygwin AT cygwin DOT com
Red Hat