man
filename open
filename(n) Tcl Built-In Commands filename(n)
______________________________________________________________________________
NAME
filename - File name conventions supported by Tcl commands
_________________________________________________________________
INTRODUCTION
All Tcl commands and C procedures that take file names as arguments ex-
pect the file names to be in one of three forms, depending on the cur-
rent platform. On each platform, Tcl supports file names in the stan-
dard forms(s) for that platform. In addition, on all platforms, Tcl
supports a Unix-like syntax intended to provide a convenient way of
constructing simple file names. However, scripts that are intended to
be portable should not assume a particular form for file names. In-
stead, portable scripts must use the file split and file join commands
to manipulate file names (see the file manual entry for more details).
PATH TYPES
File names are grouped into three general types based on the starting
point for the path used to specify the file: absolute, relative, and
volume-relative. Absolute names are completely qualified, giving a
path to the file relative to a particular volume and the root directory
on that volume. Relative names are unqualified, giving a path to the
file relative to the current working directory. Volume-relative names
are partially qualified, either giving the path relative to the root
directory on the current volume, or relative to the current directory
of the specified volume. The file pathtype command can be used to de-
termine the type of a given path.
PATH SYNTAX
The rules for native names depend on the value reported in the Tcl ar-
ray element tcl_platform(platform):
mac On Apple Macintosh systems, Tcl supports two forms of path
names. The normal Mac style names use colons as path separa-
tors. Paths may be relative or absolute, and file names may
contain any character other than colon. A leading colon
causes the rest of the path to be interpreted relative to the
current directory. If a path contains a colon that is not at
the beginning, then the path is interpreted as an absolute
path. Sequences of two or more colons anywhere in the path
are used to construct relative paths where :: refers to the
parent of the current directory, ::: refers to the parent of
the parent, and so forth.
In addition to Macintosh style names, Tcl also supports a
subset of Unix-like names. If a path contains no colons,
then it is interpreted like a Unix path. Slash is used as
the path separator. The file name . refers to the current
directory, and .. refers to the parent of the current direc-
tory. However, some names like / or /.. have no mapping, and
are interpreted as Macintosh names. In general, commands
that generate file names will return Macintosh style names,
but commands that accept file names will take both Macintosh
and Unix-style names.
The following examples illustrate various forms of path
names:
: Relative path to the current folder.
MyFile Relative path to a file named MyFile in the
current folder.
MyDisk:MyFile Absolute path to a file named MyFile on the
device named MyDisk.
:MyDir:MyFile Relative path to a file name MyFile in a
folder named MyDir in the current folder.
::MyFile Relative path to a file named MyFile in the
folder above the current folder.
:::MyFile Relative path to a file named MyFile in the
folder two levels above the current folder.
/MyDisk/MyFile Absolute path to a file named MyFile on the
device named MyDisk.
../MyFile Relative path to a file named MyFile in the
folder above the current folder.
unix On Unix platforms, Tcl uses path names where the components
are separated by slashes. Path names may be relative or ab-
solute, and file names may contain any character other than
slash. The file names . and .. are special and refer to the
current directory and the parent of the current directory re-
spectively. Multiple adjacent slash characters are inter-
preted as a single separator. The following examples illus-
trate various forms of path names:
/ Absolute path to the root directory.
/etc/passwd Absolute path to the file named passwd in the
directory etc in the root directory.
. Relative path to the current directory.
foo Relative path to the file foo in the current
directory.
foo/bar Relative path to the file bar in the directory
foo in the current directory.
../foo Relative path to the file foo in the directory
above the current directory.
windows On Microsoft Windows platforms, Tcl supports both drive-rela-
tive and UNC style names. Both / and \ may be used as direc-
tory separators in either type of name. Drive-relative names
consist of an optional drive specifier followed by an abso-
lute or relative path. UNC paths follow the general form
\\servername\sharename\path\file. In both forms, the file
names . and .. are special and refer to the current directory
and the parent of the current directory respectively. The
following examples illustrate various forms of path names:
\\Host\share/file
Absolute UNC path to a file called file in the
root directory of the export point share on
the host Host.
c:foo Volume-relative path to a file foo in the cur-
rent directory on drive c.
c:/foo Absolute path to a file foo in the root direc-
tory of drive c.
foo\bar Relative path to a file bar in the foo direc-
tory in the current directory on the current
volume.
\foo Volume-relative path to a file foo in the root
directory of the current volume.
TILDE SUBSTITUTION
In addition to the file name rules described above, Tcl also supports
csh-style tilde substitution. If a file name starts with a tilde, then
the file name will be interpreted as if the first element is replaced
with the location of the home directory for the given user. If the
tilde is followed immediately by a separator, then the $HOME environ-
ment variable is substituted. Otherwise the characters between the
tilde and the next separator are taken as a user name, which is used to
retrieve the user's home directory for substitution.
The Macintosh and Windows platforms do not support tilde substitution
when a user name follows the tilde. On these platforms, attempts to
use a tilde followed by a user name will generate an error. File names
that have a tilde without a user name will be substituted using the
$HOME environment variable, just like for Unix.
PORTABILITY ISSUES
Not all file systems are case sensitive, so scripts should avoid code
that depends on the case of characters in a file name. In addition,
the character sets allowed on different devices may differ, so scripts
should choose file names that do not contain special characters like:
<>:"/\|. The safest approach is to use names consisting of alphanu-
meric characters only. Also Windows 3.1 only supports file names with
a root of no more than 8 characters and an extension of no more than 3
characters.
KEYWORDS
current directory, absolute file name, relative file name, volume-rela-
tive file name, portability
Tcl 7.5 filename(n)
open(n) Tcl Built-In Commands open(n)
______________________________________________________________________________
NAME
open - Open a file-based or command pipeline channel
SYNOPSIS
open fileName
open fileName access
open fileName access permissions
_________________________________________________________________
DESCRIPTION
This command opens a file, serial port, or command pipeline and returns |
a channel identifier that may be used in future invocations of commands
like read, puts, and close. If the first character of fileName is not
| then the command opens a file: fileName gives the name of the file to
open, and it must conform to the conventions described in the filename
manual entry.
The access argument, if present, indicates the way in which the file
(or command pipeline) is to be accessed. In the first form access may
have any of the following values:
r Open the file for reading only; the file must already
exist. This is the default value if access is not speci-
fied.
r+ Open the file for both reading and writing; the file
must already exist.
w Open the file for writing only. Truncate it if it ex-
ists. If it doesn't exist, create a new file.
w+ Open the file for reading and writing. Truncate it if
it exists. If it doesn't exist, create a new file.
a Open the file for writing only. The file must already
exist, and the file is positioned so that new data is
appended to the file.
a+ Open the file for reading and writing. If the file
doesn't exist, create a new empty file. Set the initial
access position to the end of the file.
In the second form, access consists of a list of any of the following
flags, all of which have the standard POSIX meanings. One of the flags
must be either RDONLY, WRONLY or RDWR.
RDONLY Open the file for reading only.
WRONLY Open the file for writing only.
RDWR Open the file for both reading and writing.
APPEND Set the file pointer to the end of the file prior to
each write.
CREAT Create the file if it doesn't already exist (without
this flag it is an error for the file not to exist).
EXCL If CREAT is also specified, an error is returned if the
file already exists.
NOCTTY If the file is a terminal device, this flag prevents the
file from becoming the controlling terminal of the
process.
NONBLOCK Prevents the process from blocking while opening the
file, and possibly in subsequent I/O operations. The
exact behavior of this flag is system- and device-depen-
dent; its use is discouraged (it is better to use the
fconfigure command to put a file in nonblocking mode).
For details refer to your system documentation on the
open system call's O_NONBLOCK flag.
TRUNC If the file exists it is truncated to zero length.
If a new file is created as part of opening it, permissions (an inte-
ger) is used to set the permissions for the new file in conjunction
with the process's file mode creation mask. Permissions defaults to
0666.
COMMAND PIPELINES
If the first character of fileName is ``|'' then the remaining charac-
ters of fileName are treated as a list of arguments that describe a
command pipeline to invoke, in the same style as the arguments for
exec. In this case, the channel identifier returned by open may be
used to write to the command's input pipe or read from its output pipe,
depending on the value of access. If write-only access is used (e.g.
access is w), then standard output for the pipeline is directed to the
current standard output unless overridden by the command. If read-only
access is used (e.g. access is r), standard input for the pipeline is
taken from the current standard input unless overridden by the command.
SERIAL COMMUNICATIONS
If fileName refers to a serial port, then the specified serial port is |
opened and initialized in a platform-dependent manner. Acceptable val- |
ues for the fileName to use to open a serial port are described in the |
PORTABILITY ISSUES section. |
CONFIGURATION OPTIONS |
The fconfigure command can be used to query and set the following con- |
figuration option for open serial ports: |
-mode baud,parity,data,stop |
This option is a set of 4 comma-separated values: the baud rate, |
parity, number of data bits, and number of stop bits for this |
serial port. The baud rate is a simple integer that specifies |
the connection speed. Parity is one of the following letters: |
n, o, e, m, s; respectively signifying the parity options of |
``none'', ``odd'', ``even'', ``mark'', or ``space''. Data is |
the number of data bits and should be an integer from 5 to 8, |
while stop is the number of stop bits and should be the integer |
1 or 2.
PORTABILITY ISSUES |
Windows (all versions) |
Valid values for fileName to open a serial port are of the form |
comX:, where X is a number, generally from 1 to 4. An attempt |
to open a serial port that does not exist will fail. |
Windows NT |
When running Tcl interactively, there may be some strange inter- |
actions between the real console, if one is present, and a com- |
mand pipeline that uses standard input or output. If a command |
pipeline is opened for reading, some of the lines entered at the |
console will be sent to the command pipeline and some will be |
sent to the Tcl evaluator. If a command pipeline is opened for |
writing, keystrokes entered into the console are not visible un- |
til the the pipe is closed. This behavior occurs whether the |
command pipeline is executing 16-bit or 32-bit applications. |
These problems only occur because both Tcl and the child appli- |
cation are competing for the console at the same time. If the |
command pipeline is started from a script, so that Tcl is not |
accessing the console, or if the command pipeline does not use |
standard input or output, but is redirected from or to a file, |
then the above problems do not occur. |
Windows 95 |
A command pipeline that executes a 16-bit DOS application cannot |
be opened for both reading and writing, since 16-bit DOS appli- |
cations that receive standard input from a pipe and send stan- |
dard output to a pipe run synchronously. Command pipelines that |
do not execute 16-bit DOS applications run asynchronously and |
can be opened for both reading and writing. |
When running Tcl interactively, there may be some strange inter- |
actions between the real console, if one is present, and a com- |
mand pipeline that uses standard input or output. If a command |
pipeline is opened for reading from a 32-bit application, some |
of the keystrokes entered at the console will be sent to the |
command pipeline and some will be sent to the Tcl evaluator. If |
a command pipeline is opened for writing to a 32-bit applica- |
tion, no output is visible on the console until the the pipe is |
closed. These problems only occur because both Tcl and the |
child application are competing for the console at the same |
time. If the command pipeline is started from a script, so that |
Tcl is not accessing the console, or if the command pipeline |
does not use standard input or output, but is redirected from or |
to a file, then the above problems do not occur. |
Whether or not Tcl is running interactively, if a command pipe- |
line is opened for reading from a 16-bit DOS application, the |
call to open will not return until end-of-file has been received |
from the command pipeline's standard output. If a command pipe- |
line is opened for writing to a 16-bit DOS application, no data |
will be sent to the command pipeline's standard output until the |
pipe is actually closed. This problem occurs because 16-bit DOS |
applications are run synchronously, as described above. |
Windows 3.X |
A command pipeline can execute 16-bit or 32-bit DOS or Windows |
applications, but the call to open will not return until the |
last program in the pipeline has finished executing; command |
pipelines run synchronously. If the pipeline is opened with |
write access (either just writing or both reading and writing) |
the first application in the pipeline will instead see an imme- |
diate end-of-file; any data the caller writes to the open pipe |
will instead be discarded. |
Since Tcl cannot be run with a real console under Windows 3.X, |
there are no interactions between command pipelines and the con- |
sole. |
Macintosh |
Opening a serial port is not currently implemented under Macin- |
tosh. |
Opening a command pipeline is not supported under Macintosh, |
since applications do not support the concept of standard input |
or output. |
Unix |
Valid values for fileName to open a serial port are generally of |
the form /dev/ttyX, where X is a or b, but the name of any |
pseudo-file that maps to a serial port may be used. |
When running Tcl interactively, there may be some strange inter- |
actions between the console, if one is present, and a command |
pipeline that uses standard input. If a command pipeline is |
opened for reading, some of the lines entered at the console |
will be sent to the command pipeline and some will be sent to |
the Tcl evaluator. This problem only occurs because both Tcl |
and the child application are competing for the console at the |
same time. If the command pipeline is started from a script, so |
that Tcl is not accessing the console, or if the command pipe- |
line does not use standard input, but is redirected from a file, |
then the above problem does not occur. |
See the PORTABILITY ISSUES section of the exec command for additional |
information not specific to command pipelines about executing applica- |
tions on the various platforms |
SEE ALSO |
close(n), filename(n), gets(n), read(n), puts(n), exec(n)
KEYWORDS
access mode, append, create, file, non-blocking, open, permissions,
pipeline, process, serial
Tcl 7.6 open(n)