2011-06-23 13:14:40

by yuyichao-mit

[permalink] [raw]
Subject: extra slash in current path

The problem is like this:

$ cd /
$ pwd
/

$ cd /
$ pwd
//

$ cd ///
$ pwd
/

Why can there be a extra slash (and exactly one) in current path? Is
this designed on purpose?
This is not really a big deal but I think it can confuse some program.

Thanks.

Yichao


2011-06-23 13:17:57

by Alessandro Suardi

[permalink] [raw]
Subject: Re: extra slash in current path

On Thu, Jun 23, 2011 at 3:14 PM, yuyichao-mit <[email protected]> wrote:
> The problem is like this:
>
> $ cd /
> $ pwd
> /
>
> $ cd /
> $ pwd
> //
>
> $ cd ///
> $ pwd
> /
>
> Why can there be a extra slash (and exactly one) in current path? Is
> this designed on purpose?
> This is not really a big deal but I think it can confuse some program.

Multiple consecutive slashes should not confuse any program.

http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_266

--alessandro

?"There's always a siren singing you to shipwreck"

?? (Radiohead, "There There")

2011-06-23 13:27:18

by yuyichao-mit

[permalink] [raw]
Subject: Re: extra slash in current path

Well, actually, I mean why is // so special if more than two slash at
the beginning of absolute path become only one slash?

Yichao Yu

On Thu, Jun 23, 2011 at 9:17 AM, Alessandro Suardi
<[email protected]> wrote:
> On Thu, Jun 23, 2011 at 3:14 PM, yuyichao-mit <[email protected]> wrote:
>> The problem is like this:
>>
>> $ cd /
>> $ pwd
>> /
>>
>> $ cd /
>> $ pwd
>> //
>>
>> $ cd ///
>> $ pwd
>> /
>>
>> Why can there be a extra slash (and exactly one) in current path? Is
>> this designed on purpose?
>> This is not really a big deal but I think it can confuse some program.
>
> Multiple consecutive slashes should not confuse any program.
>
> http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_266
>
> --alessandro
>
> ?"There's always a siren singing you to shipwreck"
>
> ?? (Radiohead, "There There")
>

2011-06-23 13:31:21

by Lars Täuber

[permalink] [raw]
Subject: Re: extra slash in current path

Hi,

Am Thu, 23 Jun 2011 09:14:18 -0400
yuyichao-mit <[email protected]> schrieb:
> The problem is like this:
>
> $ cd /
> $ pwd
> /
>
> $ cd /
> $ pwd
> //

didn't you mean?:
$ cd //
$ pwd
//


Regards
Lars

>
> $ cd ///
> $ pwd
> /
>
> Why can there be a extra slash (and exactly one) in current path? Is
> this designed on purpose?
> This is not really a big deal but I think it can confuse some program.
>
> Thanks.
>
> Yichao

2011-06-23 13:33:00

by yuyichao-mit

[permalink] [raw]
Subject: Re: extra slash in current path

On Thu, Jun 23, 2011 at 9:31 AM, Lars T?uber <[email protected]> wrote:
> Hi,
>
> Am Thu, 23 Jun 2011 09:14:18 -0400
> yuyichao-mit <[email protected]> schrieb:
>> The problem is like this:
>>
>> $ cd /
>> $ pwd
>> /
>>
>> $ cd /
>> $ pwd
>> //
>
> didn't you mean?:
> $ cd //
> $ pwd
> //

yes, exactly.

Sorry for the typo (copy-paste~~~)

>
>
> Regards
> Lars
>
>>
>> $ cd ///
>> $ pwd
>> /
>>
>> Why can there be a extra slash (and exactly one) in current path? Is
>> this designed on purpose?
>> This is not really a big deal but I think it can confuse some program.
>>
>> Thanks.
>>
>> Yichao
>

2011-06-23 13:54:54

by Richard Weinberger

[permalink] [raw]
Subject: Re: extra slash in current path

On Thu, Jun 23, 2011 at 3:32 PM, yuyichao-mit <[email protected]> wrote:
> On Thu, Jun 23, 2011 at 9:31 AM, Lars T?uber <[email protected]> wrote:
>> Hi,
>>
>> Am Thu, 23 Jun 2011 09:14:18 -0400
>> yuyichao-mit <[email protected]> schrieb:
>>> The problem is like this:
>>>
>>> $ cd /
>>> $ pwd
>>> /
>>>
>>> $ cd /
>>> $ pwd
>>> //
>>
>> didn't you mean?:
>> $ cd //
>> $ pwd
>> //
>
> yes, exactly.
>
> Sorry for the typo (copy-paste~~~)
>

Is this really a kernel issue?
rw@raccoon:~> cd //
rw@raccoon://> pwd
//
rw@raccoon://> ls -l /proc/self/cwd
lrwxrwxrwx 1 rw users 0 23. Jun 15:53 /proc/self/cwd -> /

--
Thanks,
//richard

2011-06-23 14:11:29

by yuyichao-mit

[permalink] [raw]
Subject: Re: extra slash in current path

On Thu, Jun 23, 2011 at 9:54 AM, richard -rw- weinberger
<[email protected]> wrote:
> On Thu, Jun 23, 2011 at 3:32 PM, yuyichao-mit <[email protected]> wrote:
>> On Thu, Jun 23, 2011 at 9:31 AM, Lars T?uber <[email protected]> wrote:
>>> Hi,
>>>
>>> Am Thu, 23 Jun 2011 09:14:18 -0400
>>> yuyichao-mit <[email protected]> schrieb:
>>>> The problem is like this:
>>>>
>>>> $ cd /
>>>> $ pwd
>>>> /
>>>>
>>>> $ cd /
>>>> $ pwd
>>>> //
>>>
>>> didn't you mean?:
>>> $ cd //
>>> $ pwd
>>> //
>>
>> yes, exactly.
>>
>> Sorry for the typo (copy-paste~~~)
>>
>
> Is this really a kernel issue?
> rw@raccoon:~> cd //
> rw@raccoon://> pwd
> //
> rw@raccoon://> ls -l /proc/self/cwd
> lrwxrwxrwx 1 rw users 0 23. Jun 15:53 /proc/self/cwd -> /

well, that's true, but this is indeed the retrun value of get_current_dir_name.

#include <unistd.h>
#include <stdio.h>

int main()
{
char *cwd = get_current_dir_name();
printf("%s\n", cwd);
return 0;
}

$ cd //
$ pwd
//
$ main
//

so is this a glibc stuff??
(that's y I also send to linux-c-programming.)

>
> --
> Thanks,
> //richard
>

2011-06-23 14:25:09

by Richard Weinberger

[permalink] [raw]
Subject: Re: extra slash in current path

On Thu, Jun 23, 2011 at 4:11 PM, yuyichao-mit <[email protected]> wrote:
> On Thu, Jun 23, 2011 at 9:54 AM, richard -rw- weinberger
> <[email protected]> wrote:
>> On Thu, Jun 23, 2011 at 3:32 PM, yuyichao-mit <[email protected]> wrote:
>>> On Thu, Jun 23, 2011 at 9:31 AM, Lars T?uber <[email protected]> wrote:
>>>> Hi,
>>>>
>>>> Am Thu, 23 Jun 2011 09:14:18 -0400
>>>> yuyichao-mit <[email protected]> schrieb:
>>>>> The problem is like this:
>>>>>
>>>>> $ cd /
>>>>> $ pwd
>>>>> /
>>>>>
>>>>> $ cd /
>>>>> $ pwd
>>>>> //
>>>>
>>>> didn't you mean?:
>>>> $ cd //
>>>> $ pwd
>>>> //
>>>
>>> yes, exactly.
>>>
>>> Sorry for the typo (copy-paste~~~)
>>>
>>
>> Is this really a kernel issue?
>> rw@raccoon:~> cd //
>> rw@raccoon://> pwd
>> //
>> rw@raccoon://> ls -l /proc/self/cwd
>> lrwxrwxrwx 1 rw users 0 23. Jun 15:53 /proc/self/cwd -> /
>
> well, that's true, but this is indeed the retrun value of get_current_dir_name.
>
> #include <unistd.h>
> #include <stdio.h>
>
> int main()
> {
> ? ? ? ?char *cwd = get_current_dir_name();
> ? ? ? ?printf("%s\n", cwd);
> ? ? ? ?return 0;
> }
>
> $ cd //
> $ pwd
> //
> $ main
> //
>
> so is this a glibc stuff??

I guess glibc does some caching.

> (that's y I also send to linux-c-programming.)

Ah, ok.

--
Thanks,
//richard

2011-06-23 14:27:15

by Nick Bowler

[permalink] [raw]
Subject: Re: extra slash in current path

On 2011-06-23 10:11 -0400, yuyichao-mit wrote:
> On Thu, Jun 23, 2011 at 9:54 AM, richard -rw- weinberger
> > Is this really a kernel issue?
> > rw@raccoon:~> cd //
> > rw@raccoon://> pwd
> > //
> > rw@raccoon://> ls -l /proc/self/cwd
> > lrwxrwxrwx 1 rw users 0 23. Jun 15:53 /proc/self/cwd -> /
>
> well, that's true, but this is indeed the retrun value of get_current_dir_name.

glibc's get_current_dir_name will honour the PWD environment variable in some
cases, which is where the // actually comes from (i.e., it does not come
from the kernel).

This funny behaviour of cd is actually specified by POSIX (man 1p cd):

8. The curpath value shall then be converted to canonical form
as follows, considering each component from beginning to
end, in sequence:
[...]
c. An implementation may further simplify curpath by
removing any trailing slash characters that are not also
leading slashes, replacing multiple non-leading
consecutive slashes with a single slash, and replacing
three or more leading slashes with a single slash. If,
as a result of this canonicalization, the curpath
variable is null, no further steps shall be taken.

9. [...] The PWD environment variable shall be set to curpath.

Cheers,
--
Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)

2011-06-23 14:31:10

by Michael Tokarev

[permalink] [raw]
Subject: Re: extra slash in current path

23.06.2011 18:11, yuyichao-mit wrote:

>>>> $ cd //
>>>> $ pwd
>>>> //

The current directory is cached in $PWD environment
variable by shell (bash at least). This variable is
checked in glibc (getcwd() et al) - if it is set and
stat($PWD) is the same as stat("."), that value is used.

It has nothing to do with kernel, probably the issue
is in bash which somehow allows double slash for root
dir (but not triple).

/mjt

2011-06-23 14:52:10

by Lars Täuber

[permalink] [raw]
Subject: Re: extra slash in current path

Am Thu, 23 Jun 2011 18:31:06 +0400
Michael Tokarev <[email protected]> schrieb:
> 23.06.2011 18:11, yuyichao-mit wrote:
>
> >>>> $ cd //
> >>>> $ pwd
> >>>> //
>
> The current directory is cached in $PWD environment
> variable by shell (bash at least). This variable is
> checked in glibc (getcwd() et al) - if it is set and
> stat($PWD) is the same as stat("."), that value is used.

It also is somehow cached inside bash/dash for it's built in commands.
$ cd //
$ export PWD=5
$ pwd
//
$ /bin/pwd
/


Regards
Lars

2011-06-23 14:53:40

by yuyichao-mit

[permalink] [raw]
Subject: Re: extra slash in current path

On Thu, Jun 23, 2011 at 10:31 AM, Michael Tokarev <[email protected]> wrote:
> 23.06.2011 18:11, yuyichao-mit wrote:
>
>>>>> $ cd //
>>>>> $ pwd
>>>>> //
>
> The current directory is cached in $PWD environment
> variable by shell (bash at least). ?This variable is
> checked in glibc (getcwd() et al) - if it is set and
> stat($PWD) is the same as stat("."), that value is used.
>
> It has nothing to do with kernel, probably the issue
> is in bash which somehow allows double slash for root
> dir (but not triple).

u r right. chdir in c and cd in csh doesn't have this problem.

it is bash stuff.

>
> /mjt
>

2011-06-23 15:02:49

by Yichao Yu

[permalink] [raw]
Subject: Re: extra slash in current path

On Thu, Jun 23, 2011 at 10:52 AM, Lars T?uber <[email protected]> wrote:
> Am Thu, 23 Jun 2011 18:31:06 +0400
> Michael Tokarev <[email protected]> schrieb:
>> 23.06.2011 18:11, yuyichao-mit wrote:
>>
>> >>>> $ cd //
>> >>>> $ pwd
>> >>>> //
>>
>> The current directory is cached in $PWD environment
>> variable by shell (bash at least). ?This variable is
>> checked in glibc (getcwd() et al) - if it is set and
>> stat($PWD) is the same as stat("."), that value is used.
>
> It also is somehow cached inside bash/dash for it's built in commands.
> $ cd //
> $ export PWD=5

this PWD=5 is not necessary.

> $ pwd
> //
> $ /bin/pwd
> /

$ export PWD=////
$ /bin/pwd
/
$ pwd
//
$ echo $PWD
////
$ a.out (simply output get_current_dir_name())
////
so getcwd indeed use $PWD (for symlink?) but pwd and /bin/pwd both
simplify the output although bash simplify it in a strange way~~~

~~interesting

>
>
> Regards
> Lars
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at ?http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at ?http://www.tux.org/lkml/
>