2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* Copyright (C) 1994 Cazamar Systems, Inc. */
13 #include <afs/param.h>
25 static Crit_Sec osi_fdCS;
27 osi_fdType_t *osi_allFDTypes;
30 osi_fdOps_t osi_TypeFDOps = {
38 /* called while holding osi_fdCS, returns named type or null pointer */
39 osi_fdType_t *osi_FindFDType(char *namep)
43 for(ftp = osi_allFDTypes; ftp; ftp = (osi_fdType_t *) osi_QNext(&ftp->q)) {
44 if (strcmp(ftp->namep, namep) == 0) break;
50 long osi_UnregisterFDType(char *namep)
53 osi_fdTypeFormat_t *ffp;
54 osi_fdTypeFormat_t *nffp;
56 /* check for dup name */
57 thrd_EnterCrit(&osi_fdCS);
58 ftp = osi_FindFDType(namep);
61 /* free subsidiary storage, and remove from list */
63 osi_QRemove((osi_queue_t **) &osi_allFDTypes, &ftp->q);
65 /* free format structs */
66 for(ffp = ftp->formatListp; ffp; ffp=nffp) {
72 /* free main storage */
76 thrd_LeaveCrit(&osi_fdCS);
80 osi_fdType_t *osi_RegisterFDType(char *namep, osi_fdOps_t *opsp, void *datap)
84 /* check for dup name */
85 thrd_EnterCrit(&osi_fdCS);
86 osi_assertx(osi_FindFDType(namep) == NULL, "registering duplicate iteration type");
88 ftp = (osi_fdType_t *) malloc(sizeof(*ftp));
90 ftp->namep = (char *) malloc(strlen(namep)+1);
91 strcpy(ftp->namep, namep);
97 ftp->formatListp = NULL;
99 osi_QAdd((osi_queue_t **) &osi_allFDTypes, &ftp->q);
100 thrd_LeaveCrit(&osi_fdCS);
105 osi_AddFDFormatInfo(osi_fdType_t *typep, long region, long index,
106 char *labelp, long format)
108 osi_fdTypeFormat_t *formatp;
110 formatp = (osi_fdTypeFormat_t *) malloc(sizeof(*formatp));
112 formatp->labelp = (char *) malloc(strlen(labelp) + 1);
113 strcpy(formatp->labelp, labelp);
115 formatp->format = format;
117 /* now copy index info */
118 formatp->region = region;
119 formatp->index = index;
121 /* thread on the list when done */
122 thrd_EnterCrit(&osi_fdCS);
123 formatp->nextp = typep->formatListp;
124 typep->formatListp = formatp;
125 thrd_LeaveCrit(&osi_fdCS);
132 static osi_once_t once;
134 if (!osi_Once(&once)) return 0;
137 osi_allFDTypes = NULL;
138 thrd_InitCrit(&osi_fdCS);
140 /* now, initialize the types system by adding a type
143 osi_RegisterFDType("type", &osi_TypeFDOps, NULL);
150 osi_fd_t *osi_AllocFD(char *namep)
153 osi_fdType_t *fdTypep;
156 /* initialize for failure */
159 thrd_EnterCrit(&osi_fdCS);
160 fdTypep = osi_FindFDType(namep);
162 code = (fdTypep->opsp->Create)(fdTypep, &fdp);
164 fdp->fd = ++osi_nextFD;
165 fdp->opsp = fdTypep->opsp;
166 osi_QAdd((osi_queue_t **) &osi_allFDs, &fdp->q);
170 thrd_LeaveCrit(&osi_fdCS);
175 osi_fd_t *osi_FindFD(long fd)
179 thrd_EnterCrit(&osi_fdCS);
180 for(fdp = osi_allFDs; fdp; fdp = (osi_fd_t *) osi_QNext(&fdp->q)) {
181 if (fdp->fd == fd) break;
183 thrd_LeaveCrit(&osi_fdCS);
188 osi_CloseFD(osi_fd_t *fdp)
192 thrd_EnterCrit(&osi_fdCS);
193 osi_QRemove((osi_queue_t **) &osi_allFDs, &fdp->q);
194 thrd_LeaveCrit(&osi_fdCS);
196 /* this call frees the FD's storage, so make sure everything is unthreaded
199 code = (fdp->opsp->Close)(fdp);
205 /* now we have the fd type operations */
206 long osi_FDTypeCreate(osi_fdType_t *fdTypep, osi_fd_t **outpp)
210 fdp = (osi_typeFD_t *) malloc(sizeof(*fdp));
212 fdp->curp = osi_allFDTypes;
220 long osi_FDTypeGetInfo(osi_fd_t *ifdp, osi_remGetInfoParms_t *outp)
225 fdp = (osi_typeFD_t *) ifdp;
227 if (typep = fdp->curp) {
228 /* still more stuff left, copy out name move to the next */
231 strcpy(outp->sdata[0], typep->namep);
232 thrd_EnterCrit(&osi_fdCS);
233 fdp->curp = (osi_fdType_t *) osi_QNext(&typep->q);
234 thrd_LeaveCrit(&osi_fdCS);
238 /* otherwise we've hit EOF */
239 return OSI_DBRPC_EOF;
244 long osi_FDTypeClose(osi_fd_t *ifdp)
248 fdp = (osi_typeFD_t *) ifdp;