2 * Copyright (C) 1998, 1989 Transarc Corporation - All rights reserved
4 * (C) COPYRIGHT IBM CORPORATION 1987, 1988
5 * LICENSED MATERIALS - PROPERTY OF IBM
9 /* Copyright (C) 1994 Cazamar Systems, Inc. */
12 #include <afs/param.h>
22 static CRITICAL_SECTION osi_fdCS;
24 osi_fdType_t *osi_allFDTypes;
27 osi_fdOps_t osi_TypeFDOps = {
33 /* called while holding osi_fdCS, returns named type or null pointer */
34 osi_fdType_t *osi_FindFDType(char *namep)
38 for(ftp = osi_allFDTypes; ftp; ftp = (osi_fdType_t *) osi_QNext(&ftp->q)) {
39 if (strcmp(ftp->namep, namep) == 0) break;
45 long osi_UnregisterFDType(char *namep)
48 osi_fdTypeFormat_t *ffp;
49 osi_fdTypeFormat_t *nffp;
51 /* check for dup name */
52 EnterCriticalSection(&osi_fdCS);
53 ftp = osi_FindFDType(namep);
56 /* free subsidiary storage, and remove from list */
58 osi_QRemove((osi_queue_t **) &osi_allFDTypes, &ftp->q);
60 /* free format structs */
61 for(ffp = ftp->formatListp; ffp; ffp=nffp) {
67 /* free main storage */
71 LeaveCriticalSection(&osi_fdCS);
75 osi_fdType_t *osi_RegisterFDType(char *namep, osi_fdOps_t *opsp, void *datap)
79 /* check for dup name */
80 EnterCriticalSection(&osi_fdCS);
81 osi_assertx(osi_FindFDType(namep) == NULL, "registering duplicate iteration type");
83 ftp = (osi_fdType_t *) malloc(sizeof(*ftp));
85 ftp->namep = (char *) malloc(strlen(namep)+1);
86 strcpy(ftp->namep, namep);
92 ftp->formatListp = NULL;
94 osi_QAdd((osi_queue_t **) &osi_allFDTypes, &ftp->q);
95 LeaveCriticalSection(&osi_fdCS);
100 osi_AddFDFormatInfo(osi_fdType_t *typep, long region, long index,
101 char *labelp, long format)
103 osi_fdTypeFormat_t *formatp;
105 formatp = (osi_fdTypeFormat_t *) malloc(sizeof(*formatp));
107 formatp->labelp = (char *) malloc(strlen(labelp) + 1);
108 strcpy(formatp->labelp, labelp);
110 formatp->format = format;
112 /* now copy index info */
113 formatp->region = region;
114 formatp->index = index;
116 /* thread on the list when done */
117 EnterCriticalSection(&osi_fdCS);
118 formatp->nextp = typep->formatListp;
119 typep->formatListp = formatp;
120 LeaveCriticalSection(&osi_fdCS);
127 static osi_once_t once;
129 if (!osi_Once(&once)) return 0;
132 osi_allFDTypes = NULL;
133 InitializeCriticalSection(&osi_fdCS);
135 /* now, initialize the types system by adding a type
138 osi_RegisterFDType("type", &osi_TypeFDOps, NULL);
145 osi_fd_t *osi_AllocFD(char *namep)
148 osi_fdType_t *fdTypep;
151 /* initialize for failure */
154 EnterCriticalSection(&osi_fdCS);
155 fdTypep = osi_FindFDType(namep);
157 code = (fdTypep->opsp->Create)(fdTypep, &fdp);
159 fdp->fd = ++osi_nextFD;
160 fdp->opsp = fdTypep->opsp;
161 osi_QAdd((osi_queue_t **) &osi_allFDs, &fdp->q);
165 LeaveCriticalSection(&osi_fdCS);
170 osi_fd_t *osi_FindFD(long fd)
174 EnterCriticalSection(&osi_fdCS);
175 for(fdp = osi_allFDs; fdp; fdp = (osi_fd_t *) osi_QNext(&fdp->q)) {
176 if (fdp->fd == fd) break;
178 LeaveCriticalSection(&osi_fdCS);
183 osi_CloseFD(osi_fd_t *fdp)
187 EnterCriticalSection(&osi_fdCS);
188 osi_QRemove((osi_queue_t **) &osi_allFDs, &fdp->q);
189 LeaveCriticalSection(&osi_fdCS);
191 /* this call frees the FD's storage, so make sure everything is unthreaded
194 code = (fdp->opsp->Close)(fdp);
200 /* now we have the fd type operations */
201 long osi_FDTypeCreate(osi_fdType_t *fdTypep, osi_fd_t **outpp)
205 fdp = (osi_typeFD_t *) malloc(sizeof(*fdp));
207 fdp->curp = osi_allFDTypes;
214 long osi_FDTypeGetInfo(osi_fd_t *ifdp, osi_remGetInfoParms_t *outp)
219 fdp = (osi_typeFD_t *) ifdp;
221 if (typep = fdp->curp) {
222 /* still more stuff left, copy out name move to the next */
225 strcpy(outp->sdata[0], typep->namep);
226 EnterCriticalSection(&osi_fdCS);
227 fdp->curp = (osi_fdType_t *) osi_QNext(&typep->q);
228 LeaveCriticalSection(&osi_fdCS);
232 /* otherwise we've hit EOF */
233 return OSI_DBRPC_EOF;
237 long osi_FDTypeClose(osi_fd_t *ifdp)
241 fdp = (osi_typeFD_t *) ifdp;