Tcl_OpenFileChannel(3)
okay bestie, welcome to the Tcl_OpenFileChannel(3) page from the Tcl Library Procedures manual. It's giving buffered I/O facilities using channels. β¨ Like, the whole page is about handling channels, and it's a whole squad of functions. No cap.
π NAME π
So like, this page covers a whole lineup of functions, and every single one is iconic:
Tcl_OpenFileChannelTcl_OpenCommandChannelTcl_MakeFileChannelTcl_GetChannelTcl_GetChannelNamesTcl_GetChannelNamesExTcl_RegisterChannelTcl_UnregisterChannelTcl_DetachChannelTcl_IsStandardChannelTcl_CloseTcl_CloseExTcl_ReadCharsTcl_ReadTcl_GetsObjTcl_GetsTcl_WriteObjTcl_WriteCharsTcl_WriteTcl_FlushTcl_SeekTcl_TellTcl_TruncateChannelTcl_GetChannelOptionTcl_SetChannelOptionTcl_EofTcl_InputBlockedTcl_InputBufferedTcl_OutputBufferedTcl_UngetsTcl_ReadRawTcl_WriteRaw
and the whole vibe? buffered I/O facilities using channels. Periodt. π
β¨ SYNOPSIS β¨
First things first, you gotta include the header, fr fr:
#include <tcl.h>
Tcl_Channel
Tcl_OpenFileChannel(interp, fileName, mode, permissions)
Tcl_Channel
Tcl_OpenCommandChannel(interp, argc, argv, flags)
Tcl_Channel
Tcl_MakeFileChannel(handle, readOrWrite)
Tcl_Channel
Tcl_GetChannel(interp, channelName, modePtr)
int
Tcl_GetChannelNames(interp)
int
Tcl_GetChannelNamesEx(interp, pattern)
Tcl_RegisterChannel(interp, channel)
int
Tcl_UnregisterChannel(interp, channel)
int
Tcl_DetachChannel(interp, channel)
int
Tcl_IsStandardChannel(channel)
int
Tcl_Close(interp, channel)
int
Tcl_CloseEx(interp, channel, closeFlags)
Tcl_Size
Tcl_ReadChars(channel, readObjPtr, charsToRead, appendFlag)
Tcl_Size
Tcl_Read(channel, readBuf, bytesToRead)
Tcl_Size
Tcl_GetsObj(channel, lineObjPtr)
Tcl_Size
Tcl_Gets(channel, lineRead)
Tcl_Size
Tcl_Ungets(channel, input, inputLen, addAtEnd)
Tcl_Size
Tcl_WriteObj(channel, writeObjPtr)
Tcl_Size
Tcl_WriteChars(channel, charBuf, bytesToWrite)
Tcl_Size
Tcl_Write(channel, byteBuf, bytesToWrite)
Tcl_Size
Tcl_ReadRaw(channel, readBuf, bytesToRead)
Tcl_Size
Tcl_WriteRaw(channel, byteBuf, bytesToWrite)
int
Tcl_Eof(channel)
int
Tcl_Flush(channel)
int
Tcl_InputBlocked(channel)
int
Tcl_InputBuffered(channel)
int
Tcl_OutputBuffered(channel)
long long
Tcl_Seek(channel, offset, seekMode)
long long
Tcl_Tell(channel)
int
Tcl_TruncateChannel(channel, length)
int
Tcl_GetChannelOption(interp, channel, optionName, optionValue)
int
Tcl_SetChannelOption(interp, channel, optionName, newValue)
okay that's the synopsis bestie, part 2 is coming with DESCRIPTION and it's a whole journey π
β¨ ARGUMENTS β¨
okay bestie, here's the full cast of characters every one of these functions gets to work with π some of them are (in) (you hand them over), some are (out) (they get filled for you), and a few are (in/out) (you give a thing AND get it back changed). read carefully, no skimming π
Tcl_Interp *interp(in) β the interpreter. used for error reporting, and to look up a channel that's registered inside it.const char *fileName(in) β the name of a local or network file.const char *mode(in) β specifies how the file is to be accessed. may have any of the values allowed for the mode argument to the Tclopencommand.int permissions(in) β POSIX-style permission flags such as0644. if a new file gets created, these permissions will be set on the created file.Tcl_Size argc(in) β the number of elements inargv.const char **argv(in) β arguments for constructing a command pipeline. these values have the same meaning as the non-switch arguments to the Tclexeccommand.int flags(in) β specifies the disposition of the stdio handles in a pipeline: an OR-ed combination ofTCL_STDIN,TCL_STDOUT,TCL_STDERR, andTCL_ENFORCE_MODE. IfTCL_STDINis set, stdin for the first child in the pipe is the pipe channel, otherwise it is the same as the standard input of the invoking process; likewise forTCL_STDOUTandTCL_STDERR. IfTCL_ENFORCE_MODEis not set, then the pipe can redirect stdio handles to override the stdio handles for whichTCL_STDIN,TCL_STDOUTandTCL_STDERRhave been set. If it is set, then such redirections cause an error.void *handle(in) β operating system specific handle for I/O to a file. for Unix this is a file descriptor, for Windows it is aHANDLE.int readOrWrite(in) β OR-ed combination ofTCL_READABLEandTCL_WRITABLEto indicate what operations are valid onhandle.const char *channelName(in) β the name of the channel.int *modePtr(out) β points at an integer variable that will receive an OR-ed combination ofTCL_READABLEandTCL_WRITABLE, denoting whether the channel is open for reading and writing.const char *pattern(in) β the pattern to match on, passed toTcl_StringMatch, orNULL.Tcl_Channel channel(in) β a Tcl channel for input or output. must have been the return value from a procedure such asTcl_OpenFileChannel.Tcl_Obj *readObjPtr(in/out) β a pointer to a Tcl value in which to store the characters read from the channel.Tcl_Size charsToRead(in) β the number of characters to read from the channel. if the channel's encoding is binary, this is equivalent to the number of bytes to read from the channel.int closeFlags(in) β if0, the channel is closed in both directions. ifTCL_CLOSE_READ, the channel is only closed for reading. ifTCL_CLOSE_WRITE, the channel is only closed for writing. > these flags must not be combined. π«int appendFlag(in) β if non-zero, data read from the channel will be appended to the value. otherwise, the data will replace the existing contents of the value.char *readBuf(out) β a buffer in which to store the bytes read from the channel.Tcl_Size bytesToRead(in) β the number of bytes to read from the channel. the bufferreadBufmust be large enough to hold this many bytes.Tcl_Obj *lineObjPtr(in/out) β a pointer to a Tcl value in which to store the line read from the channel. the line read will be appended to the current value of the value.Tcl_DString *lineRead(in/out) β a pointer to a Tcl dynamic string in which to store the line read from the channel. must have been initialized by the caller. the line read will be appended to any data already in the dynamic string.const char *input(in) β the input to add to a channel buffer.Tcl_Size inputLen(in) β length of the inputint addAtEnd(in) β flag indicating whether the input should be added to the end or beginning of the channel buffer.Tcl_Obj *writeObjPtr(in) β a pointer to a Tcl value whose contents will be output to the channel.const char *charBuf(in) β a buffer containing the characters to output to the channel.const char *byteBuf(in) β a buffer containing the bytes to output to the channel.Tcl_Size bytesToWrite(in) β the number of bytes to consume fromcharBuforbyteBufand output to the channel.long long offset(in) β how far to move the access point in the channel at which the next input or output operation will be applied, measured in bytes from the position given byseekMode. may be either positive or negative.int seekMode(in) β relative to which point to seek; used withoffsetto calculate the new access point for the channel. legal values areSEEK_SET,SEEK_CUR, andSEEK_END.long long length(in) β the (non-negative) length to truncate the channel the channel to.const char *optionName(in) β the name of an option applicable to this channel, such as-blocking. may have any of the values accepted by thefconfigurecommand.Tcl_DString *optionValue(in) β where to store the value of an option or a list of all options and their values. must have been initialized by the caller.const char *newValue(in) β new value for the option given byoptionName.
π« DESCRIPTION π«
so like, the Tcl channel mechanism is genuinely that girl β it gives you a device-independent and platform-independent way to do buffered input and output on a whole buffet of file, socket, and device types ππ
and it's extensible too, which is the iconic part: you can support brand new channel types by providing a low-level channel driver for the new type. the channel driver interface itself is described over in the manual entry for Tcl_CreateChannel π
the channel mechanism provides a buffering scheme modeled after Unix's standard I/O, and it also allows for nonblocking I/O on channels. yes bestie, nonblocking. we love to see it β¨
the procedures described in this manual entry comprise the C APIs of the generic layer of the channel architecture. for a description of the channel driver architecture, and how to implement channel drivers for new types of channels, see the manual entry for Tcl_CreateChannel π¦
πΈ TCL_OPENFILECHANNEL πΈ
okay bestie, so Tcl_OpenFileChannel opens the file you name in fileName and hands you back a channel handle you can do input and output with. it's literally modeled after the fopen procedure from the Unix standard I/O library. the syntax and meaning of all the arguments is basically the same as what you'd give the Tcl open command when opening a file.
if an error happens while opening the channel, Tcl_OpenFileChannel returns NULL and records a POSIX error code you can grab with Tcl_GetErrno. also, if interp is not NULL, Tcl_OpenFileChannel leaves an error message in interp's result after any error.
as of Tcl 8.4, the value-based APITcl_FSOpenFileChannelshould be used in preference toTcl_OpenFileChannelwherever possible. iykyk, it's giving glow-up era.
the freshly created channel is NOT registered in the supplied interpreter. to register it, use Tcl_RegisterChannel, described below π. and if one of the standard channels β stdin, stdout or stderr β was previously closed, the act of creating the new channel also assigns it as a replacement for the standard channel. no cap.
π«Ά TCL_OPENCOMMANDCHANNEL π«Ά
Tcl_OpenCommandChannel gives you a C-level interface to the functions of the exec and open commands. it creates a sequence of subprocesses specified by the argv and argc arguments and returns a channel you can use to communicate with these subprocesses. the flags argument tells you what sort of communication will exist with the command pipeline.
- if the
TCL_STDINflag is set, then the standard input for the first subprocess will be tied to the channel: writing to the channel will provide input to the subprocess. ifTCL_STDINis not set, then standard input for the first subprocess will be the same as this application's standard input. - if
TCL_STDOUTis set, then standard output from the last subprocess can be read from the channel; otherwise it goes to this application's standard output. - if
TCL_STDERRis set, standard error output for all subprocesses is returned to the channel and results in an error when the channel is closed; otherwise it goes to this application's standard error. - if
TCL_ENFORCE_MODEis not set, thenargcandargvcan redirect the stdio handles to overrideTCL_STDIN,TCL_STDOUT, andTCL_STDERR; if it is set, then it is an error forargcandargvto override stdio channels for whichTCL_STDIN,TCL_STDOUT, andTCL_STDERRhave been set.
if an error occurs while opening the channel, Tcl_OpenCommandChannel returns NULL and records a POSIX error code you can retrieve with Tcl_GetErrno. plus, Tcl_OpenCommandChannel leaves an error message in the interpreter's result.
heads up bestie:interpcannot beNULL. π
the newly created channel is not registered in the supplied interpreter; to register it, use Tcl_RegisterChannel, described below. and if one of the standard channels, stdin, stdout or stderr, was previously closed, the act of creating the new channel also assigns it as a replacement for the standard channel. same vibe as before, you know the drill.
π TCL_MAKEFILECHANNEL π
Tcl_MakeFileChannel makes a Tcl_Channel from an existing, platform-specific, file handle. that's it, that's the whole thing. β¨
the newly created channel is not registered in the supplied interpreter; to register it, use Tcl_RegisterChannel, described below. and if one of the standard channels, stdin, stdout or stderr, was previously closed, the act of creating the new channel also assigns it as a replacement for the standard channel.
π¦ TCL_GETCHANNEL π¦
Tcl_GetChannel returns a channel given the channelName used to create it with Tcl_CreateChannel, plus a pointer to a Tcl interpreter in interp. if a channel by that name is not registered in that interpreter, the procedure returns NULL. like, gone, not found, bye. π
if the modePtr argument is not NULL, it points at an integer variable that will receive an OR-ed combination of TCL_READABLE and TCL_WRITABLE describing whether the channel is open for reading and writing. slay, now you know its whole deal.
Tcl_GetChannelNames and Tcl_GetChannelNamesEx write the names of the registered channels to the interpreter's result as a list value. Tcl_GetChannelNamesEx will filter these names according to the pattern. if pattern is NULL, then it will not do any filtering. the return value is TCL_OK if no errors occurred writing to the result, otherwise it is TCL_ERROR, and the error message is left in the interpreter's result.
π TCL_REGISTERCHANNEL π
Tcl_RegisterChannel adds a channel to the set of channels accessible in interp. after this call, Tcl programs executing in that interpreter can refer to the channel in input or output operations using the name given in the call to Tcl_CreateChannel.
after this call, the channel becomes the property of the interpreter, and the caller should NOT call Tcl_Close for the channel; the channel will be closed automatically when it is unregistered from the interpreter. periodt. π«Ά
code executing outside of any Tcl interpreter can call Tcl_RegisterChannel with interp as NULL, to indicate that it wishes to hold a reference to this channel. subsequently, the channel can be registered in a Tcl interpreter and it will only be closed when the matching number of calls to Tcl_UnregisterChannel have been made. this allows code executing outside of any interpreter to safely hold a reference to a channel that is also registered in a Tcl interpreter. bestie behavior, honestly. π
this procedure interacts with the code managing the standard channels. if no standard channels were initialized before the first call to Tcl_RegisterChannel, they will get initialized by that call. see Tcl_StandardChannels for a general treatise about standard channels and the behavior of the Tcl library with regard to them. go read that one too, it's lore. π
π TCL_UNREGISTERCHANNEL π
Tcl_UnregisterChannel removes a channel from the set of channels accessible in interp. after this call, Tcl programs will no longer be able to use the channel's name to refer to the channel in that interpreter. if this operation removed the last registration of the channel in any interpreter, the channel is also closed and destroyed. rip channel, you had a good run πβ¨
code not associated with a Tcl interpreter can call Tcl_UnregisterChannel with interp as NULL, to indicate to Tcl that it no longer holds a reference to that channel. if this is the last reference to the channel, it will now be closed.
Tcl_UnregisterChannel is very similar to Tcl_DetachChannel except that it will also close the channel if no further references to it exist. that's the one little difference, iconic.
β TCL_DETACHCHANNEL β
Tcl_DetachChannel removes a channel from the set of channels accessible in interp. after this call, Tcl programs will no longer be able to use the channel's name to refer to the channel in that interpreter. beyond that, this command has no further effect. lowkey chill of it.
it cannot be used on the standard channels (stdout,stderr,stdin), and will returnTCL_ERRORif passed one of those channels. so like, do not try it. π ββοΈ
code not associated with a Tcl interpreter can call Tcl_DetachChannel with interp as NULL, to indicate to Tcl that it no longer holds a reference to that channel. if this is the last reference to the channel, unlike Tcl_UnregisterChannel, it will NOT be closed. she stays open, she's independent now. π
πΈ TCL_ISSTANDARDCHANNEL πΈ
Tcl_IsStandardChannel tests whether a channel is one of the three standard channels β stdin, stdout or stderr. if so, it returns 1, otherwise 0. simple as that.
no attempt is made to check whether the given channel or the standard channels are initialized or otherwise valid. so like, it's not gonna fact-check you. it just vibes. π
π₯ TCL_CLOSE AND TCL_CLOSEEX π₯
Tcl_Close destroys the channel channel, which must denote a currently open channel. the channel should NOT be registered in any interpreter when Tcl_Close is called.
buffered output is flushed to the channel's output device prior to destroying the channel, and any buffered input is discarded. if this is a blocking channel, the call does not return until all buffered data is successfully sent to the channel's output device. if this is a nonblocking channel and there is buffered output that cannot be written without blocking, the call returns immediately; output is flushed in the background and the channel will be closed once all of the buffered data has been output. in this case, errors during flushing are not reported. sneaky, but okay. π
Tcl_CloseEx allows for both full closing and half-closing of channels depending on its closeFlags parameter. see the description of the parameter above. it is an error to attempt to close the channel for a direction for which it is not open. the channel is destroyed only when it has been closed for both reading and writing. only socket and command pipe channels support half-closing. so not every channel can do the split, iykyk. β¨
if the channel was closed successfully, Tcl_Close and Tcl_CloseEx return TCL_OK. if an error occurs, they return TCL_ERROR and record a POSIX error code that can be retrieved with Tcl_GetErrno. if the channel is being closed synchronously and an error occurs during closing of the channel and interp is not NULL, an error message is left in the interpreter's result.
note that it is not safe to call the channel closing functions on a channel that has been registered usingTcl_RegisterChannel; see the documentation forTcl_RegisterChannel, above, for details. if the channel has ever been given as thechanargument in a call toTcl_RegisterChannel, you should instead useTcl_UnregisterChannel, which will internally callTcl_Closewhen all calls toTcl_RegisterChannelhave been matched by corresponding calls toTcl_UnregisterChannel. matching energy only, bestie. π«Ά
π TCL_READCHARS AND TCL_READ π
okay bestie, storytime: Tcl_ReadChars eats bytes off channel and converts them to UTF-8 based on whatever the channel's encoding is, then drops the produced data into readObjPtr's string representation. What does it give you back? The number of characters β up to charsToRead β that got stored in readObjPtr. If something goes wrong while reading, you get -1 and Tcl_ReadChars records a POSIX error code you can grab with Tcl_GetErrno. And here's a lil niche one: if an encoding error happens while the channel is in blocking mode with -profile strict, the characters retrieved until the encoding error happened will be stored in readObjPtr. So you don't lose them, okay? π«Ά
Set charsToRead to -1 and it'll read all characters currently available (non-blocking) or literally everything until eof (blocking mode). no cap.
Your return value might be smaller than what you asked for β that's called a short read and it's not a glitch, it's a feature. In blocking mode this can <em>only</em> happen on an end-of-file. In nonblocking mode, a short read can also happen if an encoding error is encountered (with -profile strict) or if there isn't enough input currently available: Tcl_ReadChars returns a short count rather than waiting for more data. it's giving "we move when we can" π
- Blocking mode + return value of zero β that's an end-of-file condition.
- Nonblocking mode + return value of zero β either no input is currently available <em>or</em> an end-of-file condition.
Use Tcl_Eof and Tcl_InputBlocked to tell which of these conditions actually occurred. don't guess, bestie.
Tcl_ReadChars translates the various end-of-line representations into the canonical \n internal representation according to the current end-of-line recognition mode. End-of-line recognition and the various platform-specific modes are described in the manual entry for the Tcl fconfigure command. β¨
okay now the fun optimization bit, because it's actually iconic: when you're reading from a channel with the encoding binary, the bytes are not converted to UTF-8 as they are read. Instead, they're stored in readObjPtr's internal representation as a byte-array value. The string representation of this value only gets constructed if it's needed (e.g., because of a call to Tcl_GetStringFromObj). So byte-oriented data can be read from a channel, manipulated by calling Tcl_GetByteArrayFromObj and related functions, and then written to a channel without the expense of ever converting to or from UTF-8. efficiency queen π
Tcl_Read is like Tcl_ReadChars, except it does not do encoding conversions, regardless of the channel's encoding. Heads up: it is deprecated and exists for backwards compatibility with non-internationalized Tcl extensions. It consumes bytes from channel and stores them in readBuf, performing end-of-line translations on the way. The return value of Tcl_Read is the number of bytes, up to bytesToRead, written in readBuf. Important: the buffer produced by Tcl_Read is not null-terminated. Its contents are valid from the zeroth position up to and excluding the position indicated by the return value. so like, don't strlen it, i'm begging π
Tcl_ReadRaw is the same as Tcl_Read but does <em>not</em> compensate for stacking. While Tcl_Read (and the other functions in the API) always get their data from the topmost channel in the stack the supplied channel is part of, Tcl_ReadRaw does not. So this function is only usable for transformational channel drivers, i.e. drivers used in the middle of a stack of channels, to move data from the channel below into the transformation. niche, but make it fashion π
π TCL_GETSOBJ AND TCL_GETS π
Tcl_GetsObj consumes bytes from channel, converting the bytes to UTF-8 based on the channel's encoding, until a full line of input has been seen. If the channel's encoding is binary, each byte read from the channel is treated as an individual Unicode character. All of the characters of the line except for the terminating end-of-line character(s) are appended to lineObjPtr's string representation. The end-of-line character(s) are read and <em>discarded</em>. bye felicia π
- If a line was successfully read β return value is greater than or equal to zero and indicates the number of bytes stored in
lineObjPtr. - If an error occurs β
Tcl_GetsObjreturns-1and records a POSIX error code retrievable withTcl_GetErrno.
Tcl_GetsObj <em>also</em> returns -1 if the end of the file is reached; the Tcl_Eof procedure can be used to distinguish an error from an end-of-file condition. two very different vibes, same number, iykyk π
If the channel is in nonblocking mode, the return value can also be -1 if no data was available or the data that was available did not contain an end-of-line character. When -1 is returned, the Tcl_InputBlocked procedure may be invoked to determine if the channel is blocked because of input unavailability.
Tcl_Gets is the same as Tcl_GetsObj except the resulting characters are appended to the dynamic string given by lineRead rather than a Tcl value. same energy, different destination π¦
π TCL_UNGETS π«Ά
Tcl_Ungets is used to add data to the input queue of a channel, at either the head or tail of the queue. The pointer input points to the data that is to be added. The length of the input to add is given by inputLen. A non-zero value of addAtEnd indicates that the data is to be added at the end of queue; otherwise it will be added at the head of the queue. And β plot twist β if channel has a "sticky" EOF set, no data will be added to the input queue. Tcl_Ungets returns inputLen or -1 if an error occurs. put it back where you found it, queen π
π TCL_WRITECHARS, TCL_WRITEOBJ, AND TCL_WRITE β¨
Tcl_WriteChars accepts bytesToWrite bytes of character data at charBuf. The UTF-8 characters in the buffer are converted to the channel's encoding and queued for output to channel. If bytesToWrite is negative, Tcl_WriteChars expects charBuf to be null-terminated and it outputs everything up to the null. πΈ
Data queued for output may <em>not</em> appear on the output device immediately, due to internal buffering. If the data should appear immediately, call Tcl_Flush after the call to Tcl_WriteChars, or set the -buffering option on the channel to none. If you wish the data to appear as soon as a complete line is accepted for output, set the -buffering option on the channel to line mode.
The return value of Tcl_WriteChars is a count of how many bytes were accepted for output to the channel. This is either -1 to indicate that an error occurred, or another number greater than zero to indicate success. If an error occurs, Tcl_WriteChars records a POSIX error code that may be retrieved with Tcl_GetErrno.
Newline characters in the output data are translated to platform-specific end-of-line sequences according to the -translation option for the channel. This is done even if the channel has no encoding. periodt.
Tcl_WriteObj is similar to Tcl_WriteChars except it accepts a Tcl value whose contents will be output to the channel. The UTF-8 characters in writeObjPtr's string representation are converted to the channel's encoding and queued for output to channel. As a performance optimization, when writing to a channel with the encoding binary, UTF-8 characters are not converted as they are written. Instead, the bytes in writeObjPtr's internal representation as a byte-array value are written to the channel. The byte-array representation of the value will be constructed if it is needed. In this way, byte-oriented data can be read from a channel, manipulated by calling Tcl_GetByteArrayFromObj and related functions, and then written to a channel without the expense of ever converting to or from UTF-8. the glow-up is mutual bestie π«Ά
Tcl_Write is similar to Tcl_WriteChars except that it does not do encoding conversions, regardless of the channel's encoding. It is deprecated and exists for backwards compatibility with non-internationalized Tcl extensions. It accepts bytesToWrite bytes of data at byteBuf and queues them for output to channel. If bytesToWrite is negative, Tcl_Write expects byteBuf to be null-terminated and it outputs everything up to the null.
Tcl_WriteRaw is the same as Tcl_Write but does not compensate for stacking. While Tcl_Write (and the other functions in the API) always feed their input to the topmost channel in the stack the supplied channel is part of, Tcl_WriteRaw does not. Thus this function is only usable for transformational channel drivers, i.e. drivers used in the middle of a stack of channels, to move data from the transformation into the channel below it. middle-of-the-stack main character energy π
π TCL_FLUSH π
Tcl_Flush causes all of the buffered output data for channel to be written to its underlying file or device as soon as possible. If the channel is in blocking mode, the call does not return until all the buffered data has been sent to the channel or some error occurred. The call returns immediately if the channel is nonblocking; it starts a background flush that will write the buffered data to the channel eventually, as fast as the channel is able to absorb it. she'll get there when she gets there π
The return value is normally TCL_OK. If an error occurs, Tcl_Flush returns TCL_ERROR and records a POSIX error code that can be retrieved with Tcl_GetErrno.
π TCL_SEEK π¦
Tcl_Seek moves the access point in channel where subsequent data will be read or written. Buffered output is flushed to the channel and buffered input is <em>discarded</em>, prior to the seek operation. clean slate, we're starting over π
Tcl_Seek normally returns the new access point. If an error occurs, Tcl_Seek returns -1 and records a POSIX error code that can be retrieved with Tcl_GetErrno. And important caveat: after an error, the access point may or may not have been moved. so like, don't assume. π
π TCL_TELL β
Tcl_Tell returns the current access point for a channel. The returned value is -1 if the channel does not support seeking. that's it, that's the whole function, and honestly? respect π
π TCL_TRUNCATECHANNEL π
Tcl_TruncateChannel truncates the file underlying channel to a given length of bytes. It returns TCL_OK if the operation succeeded, and TCL_ERROR otherwise. chop chop βοΈβ¨
π TCL_GETCHANNELOPTION π
Tcl_GetChannelOption retrieves, in optionValue, the value of one of the options currently in effect for a channel β or a list of all options and their values. The channel argument identifies the channel for which to query an option or retrieve all options and their values. If optionName is not NULL, it is the name of the option to query; the option's value is copied to the Tcl dynamic string denoted by optionValue. If optionName is NULL, the function stores an alternating list of option names and their values in optionValue, using a series of calls to Tcl_DStringAppendElement. two modes, both iconic π«Ά
The various preexisting options and their possible values are described in the manual entry for the Tcl fconfigure command. Other options can be added by each channel type. These channel type specific options are described in the manual entry for the Tcl command that creates a channel of that type; for example, the additional options for TCP-based channels are described in the manual entry for the Tcl socket command. πΈ
The procedure normally returns TCL_OK. If an error occurs, it returns TCL_ERROR and calls Tcl_SetErrno to store an appropriate POSIX error code.
π TCL_SETCHANNELOPTION π
Tcl_SetChannelOption is the girl who sets a brand new value newValue for an option optionName on channel. She's like the makeover function. β¨
Normally she returns TCL_OK. But if something goes wrong, she returns TCL_ERROR β and bestie, if interp is non-NULL, Tcl_SetChannelOption will leave an error message in the interpreter's result too. So you'll know. π
πΈ TCL_EOF πΈ
Tcl_Eof returns a nonzero value if channel hit an end of file during the last input operation. That's it, that's the whole vibe. The end. π
π¦ TCL_INPUTBLOCKED π¦
Tcl_InputBlocked returns a nonzero value if channel is in nonblocking mode and the last input operation gave back less data than you asked for, because there simply wasn't enough data available yet. π
But if the channel is in blocking mode? The call always returns zero. No notes. Periodt. π
π TCL_INPUTBUFFERED π
Tcl_InputBuffered returns the number of bytes of input currently chillin' in the internal buffers for a channel.
And if the channel is not open for reading? This function always returns zero. Respectfully, nothing's there. π
β¨ TCL_OUTPUTBUFFERED β¨
Tcl_OutputBuffered returns the number of bytes of output currently sitting in the internal buffers for a channel.
If the channel is not open for writing, this function always returns zero. Iconic behavior honestly. π
π PLATFORM ISSUES π
The handles you get back from Tcl_GetChannelHandle? They depend entirely on the platform and the channel type. No cap.
- On Unix platforms, the handle is always a Unix file descriptor, exactly as returned from the
opensystem call. π§ - On Windows platforms, the handle is a file
HANDLEwhen the channel was created withTcl_OpenFileChannel,Tcl_OpenCommandChannel, orTcl_MakeFileChannel. πͺ - Other channel types may return a different type of handle on Windows platforms. Surprise! π
π REFERENCE COUNT MANAGEMENT π
Okay this section is the responsible adult of the page, so listen up bestie. π«Ά
The readObjPtr argument to Tcl_ReadChars must be an unshared value β this function will modify it. And using the interpreter result for this purpose is strongly not recommended. The preferred pattern? Use a new value from Tcl_NewObj to receive the data, and only pass it to Tcl_SetObjResult if this function succeeds. Do it this way, I'm begging. π
The lineObjPtr argument to Tcl_GetsObj must also be an unshared value β it will be modified by this function. Same deal: using the interpreter result for this purpose is strongly not recommended. The preferred pattern is to use a new value from Tcl_NewObj to receive the data and only to pass it to Tcl_SetObjResult if this function succeeds. Consistency, queen. β¨
The writeObjPtr argument to Tcl_WriteObj should be a value with any reference count. This function will not modify the reference count. But using the interpreter result without adding an additional reference to it is not recommended. Noted. π
π₯ SEE ALSO π₯
β KEYWORDS β
access point, blocking, buffered I/O, channel, channel driver, end of file, flush, input, nonblocking, output, read, seek, write
and that's a wrap on the whole page, bestie!! πβ¨π This iconic little manual page comes to you from Tcl 8.3, and its official name is Tcl_OpenFileChannel(3) β say it with your chest. Now go open some channels, slay your file descriptors, and remember: always close what you open. bye mwah π
π¦πΈ
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 1996-1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms