2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: tclFHandle.c /main/2 1996/08/08 14:43:54 cde-hp $ */
27 * This file contains functions for manipulating Tcl file handles.
29 * Copyright (c) 1995 Sun Microsystems, Inc.
31 * See the file "license.terms" for information on usage and redistribution
32 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
34 * SCCS: @(#) tclFHandle.c 1.6 96/02/13 16:29:55
41 * The FileHashKey structure is used to associate the OS file handle and type
42 * with the corresponding notifier data in a FileHandle.
45 typedef struct FileHashKey {
46 int type; /* File handle type. */
47 ClientData osHandle; /* Platform specific OS file handle. */
50 typedef struct FileHandle {
51 FileHashKey key; /* Hash key for a given file. */
52 ClientData data; /* Platform specific notifier data. */
53 Tcl_FileFreeProc *proc; /* Callback to invoke when file is freed. */
57 * Static variables used in this file:
60 static Tcl_HashTable fileTable; /* Hash table containing file handles. */
61 static int initialized = 0; /* 1 if this module has been initialized. */
64 * Static procedures used in this file:
67 static void FileExitProc _ANSI_ARGS_((ClientData clientData));
70 *----------------------------------------------------------------------
74 * This function retrieves the file handle associated with a
75 * platform specific file handle of the given type. It creates
76 * a new file handle if needed.
79 * Returns the file handle associated with the file descriptor.
82 * Initializes the file handle table if necessary.
84 *----------------------------------------------------------------------
88 Tcl_GetFile(osHandle, type)
89 ClientData osHandle; /* Platform specific file handle. */
90 int type; /* Type of file handle. */
93 Tcl_HashEntry *entryPtr;
97 Tcl_InitHashTable(&fileTable, sizeof(FileHashKey)/sizeof(int));
98 Tcl_CreateExitHandler(FileExitProc, 0);
101 key.osHandle = osHandle;
103 entryPtr = Tcl_CreateHashEntry(&fileTable, (char *) &key, &new);
105 FileHandle *newHandlePtr;
106 newHandlePtr = (FileHandle *) ckalloc(sizeof(FileHandle));
107 newHandlePtr->key = key;
108 newHandlePtr->data = NULL;
109 newHandlePtr->proc = NULL;
110 Tcl_SetHashValue(entryPtr, newHandlePtr);
113 return (Tcl_File) Tcl_GetHashValue(entryPtr);
117 *----------------------------------------------------------------------
121 * Deallocates an entry in the file handle table.
129 *----------------------------------------------------------------------
136 Tcl_HashEntry *entryPtr;
137 FileHandle *handlePtr = (FileHandle *) handle;
140 * Invoke free procedure, then delete the handle.
143 if (handlePtr->proc) {
144 (*handlePtr->proc)(handlePtr->data);
147 entryPtr = Tcl_FindHashEntry(&fileTable, (char *) &handlePtr->key);
149 Tcl_DeleteHashEntry(entryPtr);
150 ckfree((char *) handlePtr);
155 *----------------------------------------------------------------------
159 * This function retrieves the platform specific file data and
160 * type from the file handle.
163 * If typePtr is not NULL, sets *typePtr to the type of the file.
164 * Returns the platform specific file data.
169 *----------------------------------------------------------------------
173 Tcl_GetFileInfo(handle, typePtr)
177 FileHandle *handlePtr = (FileHandle *) handle;
180 *typePtr = handlePtr->key.type;
182 return handlePtr->key.osHandle;
186 *----------------------------------------------------------------------
188 * Tcl_SetNotifierData --
190 * This function is used by the notifier to associate platform
191 * specific notifier information and a deletion procedure with
198 * Updates the data and delProc slots in the file handle.
200 *----------------------------------------------------------------------
204 Tcl_SetNotifierData(handle, proc, data)
206 Tcl_FileFreeProc *proc;
209 FileHandle *handlePtr = (FileHandle *) handle;
210 handlePtr->proc = proc;
211 handlePtr->data = data;
215 *----------------------------------------------------------------------
217 * Tcl_GetNotifierData --
219 * This function is used by the notifier to retrieve the platform
220 * specific notifier information associated with a file handle.
223 * Returns the data stored in a file handle by a previous call to
224 * Tcl_SetNotifierData, and places a pointer to the free proc
225 * in the location referred to by procPtr.
230 *----------------------------------------------------------------------
234 Tcl_GetNotifierData(handle, procPtr)
236 Tcl_FileFreeProc **procPtr;
238 FileHandle *handlePtr = (FileHandle *) handle;
239 if (procPtr != NULL) {
240 *procPtr = handlePtr->proc;
242 return handlePtr->data;
246 *----------------------------------------------------------------------
250 * This function an exit handler that frees any memory allocated
251 * for the file handle table.
257 * Cleans up the file handle table.
259 *----------------------------------------------------------------------
263 FileExitProc(clientData)
264 ClientData clientData; /* Not used. */
266 Tcl_HashSearch search;
267 Tcl_HashEntry *entryPtr;
269 entryPtr = Tcl_FirstHashEntry(&fileTable, &search);
272 ckfree(Tcl_GetHashValue(entryPtr));
273 entryPtr = Tcl_NextHashEntry(&search);
276 Tcl_DeleteHashTable(&fileTable);