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>
23 static Crit_Sec osi_fdCS
;
25 osi_fdType_t
*osi_allFDTypes
;
28 osi_fdOps_t osi_TypeFDOps
= {
34 /* called while holding osi_fdCS, returns named type or null pointer */
35 osi_fdType_t
*osi_FindFDType(char *namep
)
39 for(ftp
= osi_allFDTypes
; ftp
; ftp
= (osi_fdType_t
*) osi_QNext(&ftp
->q
)) {
40 if (strcmp(ftp
->namep
, namep
) == 0) break;
46 long osi_UnregisterFDType(char *namep
)
49 osi_fdTypeFormat_t
*ffp
;
50 osi_fdTypeFormat_t
*nffp
;
52 /* check for dup name */
53 thrd_EnterCrit(&osi_fdCS
);
54 ftp
= osi_FindFDType(namep
);
57 /* free subsidiary storage, and remove from list */
59 osi_QRemove((osi_queue_t
**) &osi_allFDTypes
, &ftp
->q
);
61 /* free format structs */
62 for(ffp
= ftp
->formatListp
; ffp
; ffp
=nffp
) {
68 /* free main storage */
72 thrd_LeaveCrit(&osi_fdCS
);
76 osi_fdType_t
*osi_RegisterFDType(char *namep
, osi_fdOps_t
*opsp
, void *datap
)
80 /* check for dup name */
81 thrd_EnterCrit(&osi_fdCS
);
82 osi_assertx(osi_FindFDType(namep
) == NULL
, "registering duplicate iteration type");
84 ftp
= (osi_fdType_t
*) malloc(sizeof(*ftp
));
86 ftp
->namep
= (char *) malloc(strlen(namep
)+1);
87 strcpy(ftp
->namep
, namep
);
93 ftp
->formatListp
= NULL
;
95 osi_QAdd((osi_queue_t
**) &osi_allFDTypes
, &ftp
->q
);
96 thrd_LeaveCrit(&osi_fdCS
);
101 osi_AddFDFormatInfo(osi_fdType_t
*typep
, long region
, long index
,
102 char *labelp
, long format
)
104 osi_fdTypeFormat_t
*formatp
;
106 formatp
= (osi_fdTypeFormat_t
*) malloc(sizeof(*formatp
));
108 formatp
->labelp
= (char *) malloc(strlen(labelp
) + 1);
109 strcpy(formatp
->labelp
, labelp
);
111 formatp
->format
= format
;
113 /* now copy index info */
114 formatp
->region
= region
;
115 formatp
->index
= index
;
117 /* thread on the list when done */
118 thrd_EnterCrit(&osi_fdCS
);
119 formatp
->nextp
= typep
->formatListp
;
120 typep
->formatListp
= formatp
;
121 thrd_LeaveCrit(&osi_fdCS
);
128 static osi_once_t once
;
130 if (!osi_Once(&once
)) return 0;
133 osi_allFDTypes
= NULL
;
134 thrd_InitCrit(&osi_fdCS
);
136 /* now, initialize the types system by adding a type
139 osi_RegisterFDType("type", &osi_TypeFDOps
, NULL
);
146 osi_fd_t
*osi_AllocFD(char *namep
)
149 osi_fdType_t
*fdTypep
;
152 /* initialize for failure */
155 thrd_EnterCrit(&osi_fdCS
);
156 fdTypep
= osi_FindFDType(namep
);
158 code
= (fdTypep
->opsp
->Create
)(fdTypep
, &fdp
);
160 fdp
->fd
= ++osi_nextFD
;
161 fdp
->opsp
= fdTypep
->opsp
;
162 osi_QAdd((osi_queue_t
**) &osi_allFDs
, &fdp
->q
);
166 thrd_LeaveCrit(&osi_fdCS
);
171 osi_fd_t
*osi_FindFD(long fd
)
175 thrd_EnterCrit(&osi_fdCS
);
176 for(fdp
= osi_allFDs
; fdp
; fdp
= (osi_fd_t
*) osi_QNext(&fdp
->q
)) {
177 if (fdp
->fd
== fd
) break;
179 thrd_LeaveCrit(&osi_fdCS
);
184 osi_CloseFD(osi_fd_t
*fdp
)
188 thrd_EnterCrit(&osi_fdCS
);
189 osi_QRemove((osi_queue_t
**) &osi_allFDs
, &fdp
->q
);
190 thrd_LeaveCrit(&osi_fdCS
);
192 /* this call frees the FD's storage, so make sure everything is unthreaded
195 code
= (fdp
->opsp
->Close
)(fdp
);
201 /* now we have the fd type operations */
202 long osi_FDTypeCreate(osi_fdType_t
*fdTypep
, osi_fd_t
**outpp
)
206 fdp
= (osi_typeFD_t
*) malloc(sizeof(*fdp
));
208 fdp
->curp
= osi_allFDTypes
;
215 long osi_FDTypeGetInfo(osi_fd_t
*ifdp
, osi_remGetInfoParms_t
*outp
)
220 fdp
= (osi_typeFD_t
*) ifdp
;
222 if (typep
= fdp
->curp
) {
223 /* still more stuff left, copy out name move to the next */
226 strcpy(outp
->sdata
[0], typep
->namep
);
227 thrd_EnterCrit(&osi_fdCS
);
228 fdp
->curp
= (osi_fdType_t
*) osi_QNext(&typep
->q
);
229 thrd_LeaveCrit(&osi_fdCS
);
233 /* otherwise we've hit EOF */
234 return OSI_DBRPC_EOF
;
238 long osi_FDTypeClose(osi_fd_t
*ifdp
)
242 fdp
= (osi_typeFD_t
*) ifdp
;