file path question

46 views
Skip to first unread message

js

unread,
Jul 21, 2021, 6:21:24 PMJul 21
to
Hey experts,

When using open or exec, you supply the name of a program or a script
file. What is the recommended way to handle the file path separators
here? Should I convert the file names to their "native" names or leave
them as forward slashes ("normalized")?


Robert Heller

unread,
Jul 21, 2021, 7:27:37 PMJul 21
to
The file I/O functions are perfectly capable of handling "un-normalzed"
pathnames. The first argument to exec can also handle "un-normalzed"
pathnames. If you are opening a pipe ([open "|$pathname args..."] and the args
includes local pathnames OR if you if you are including local pathnames as
arguments to an exec command, then you need to use [file normalize ...].

Generally you don't need to use [file normalize]. There are a few special
cases you use [file normalize].


>
>
>
>

--
Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

js

unread,
Jul 24, 2021, 3:32:02 PMJul 24
to
On 7/21/21 7:27 PM, Robert Heller wrote:
>
> The file I/O functions are perfectly capable of handling "un-normalzed"
> pathnames. The first argument to exec can also handle "un-normalzed"
> pathnames. If you are opening a pipe ([open "|$pathname args..."] and the args
> includes local pathnames OR if you if you are including local pathnames as
> arguments to an exec command, then you need to use [file normalize ...].
>
> Generally you don't need to use [file normalize]. There are a few special
> cases you use [file normalize].
>
>

Thank you for the useful info.

I am not sure if this is documented or not but if there is an error with
the argument to "exec", Tcl returns the error message in which the file
path is in "native" format. I suppose this is perfectly OK with the only
caveat being that the backslashes are then in danger of disappearing
when writing/reading from a file.


% exec C:/Windows/nosuchnotepad.exe
couldn't execute "C:\Windows\nosuchnotepad.exe": no such file or directory

Rich

unread,
Jul 24, 2021, 3:50:50 PMJul 24
to
js <jsin...@outlook.com> wrote:
> I am not sure if this is documented or not but if there is an error with
> the argument to "exec", Tcl returns the error message in which the file
> path is in "native" format. I suppose this is perfectly OK with the only
> caveat being that the backslashes are then in danger of disappearing
> when writing/reading from a file.

Not at all, unless you do something that causes the return message to
be re-parsed by the Tcl parser:

Note:

$ rlwrap tclsh
% set backslashes "the\\quick\\brown\\fox\\jumped"
the\quick\brown\fox\jumped
% set fd [open no-eval {WRONLY CREAT TRUNC}]
file3
% puts $fd $backslashes
% close $fd
% set fd [open with-eval {WRONLY CREAT TRUNC}]
file3
% eval puts $fd $backslashes
% close $fd
% exit
$ cat with-eval
thequicrown
oxjumped
$ cat no-eval
the\quick\brown\fox\jumped

The file "no-eval" did not cause the contents of the "backslashes"
variable to be re-parsed by the interperter, and the backslashes are
all present.

The file "with-eval" caused the Tcl interpreter to re-parse the
contents of the string, and it did backslash replacements as it is
supposed to do.

So unless you write code that causes the string to be re-parsed, you
will not lose the backslashes.

Robert Heller

unread,
Jul 24, 2021, 4:23:26 PMJul 24
to
At Sat, 24 Jul 2021 15:31:48 -0400 js <jsin...@outlook.com> wrote:

>
> On 7/21/21 7:27 PM, Robert Heller wrote:
> >
> > The file I/O functions are perfectly capable of handling "un-normalzed"
> > pathnames. The first argument to exec can also handle "un-normalzed"
> > pathnames. If you are opening a pipe ([open "|$pathname args..."] and the args
> > includes local pathnames OR if you if you are including local pathnames as
> > arguments to an exec command, then you need to use [file normalize ...].
> >
> > Generally you don't need to use [file normalize]. There are a few special
> > cases you use [file normalize].
> >
> >
>
> Thank you for the useful info.
>
> I am not sure if this is documented or not but if there is an error with
> the argument to "exec", Tcl returns the error message in which the file
> path is in "native" format. I suppose this is perfectly OK with the only
> caveat being that the backslashes are then in danger of disappearing
> when writing/reading from a file.

I'm guessing that is because exec is itself calling [file normalize] before
passing the path off to what passes for execv() or shell() under MS-Windows
and the error is originating from the MS-Windows kernel call or something like
that. Whether or not the backslashes disappear depends on how you read/write
the file...

>
>
> % exec C:/Windows/nosuchnotepad.exe
> couldn't execute "C:\Windows\nosuchnotepad.exe": no such file or directory
>
>
>

Reply all
Reply to author
Forward
0 new messages