1 | /* |
---|
2 | * |
---|
3 | * Copyright (C) 1988, 1989 by the Massachusetts Institute of Technology |
---|
4 | * Developed by the MIT Student Information Processing Board (SIPB). |
---|
5 | * For copying information, see the file mit-copyright.h in this release. |
---|
6 | * |
---|
7 | */ |
---|
8 | /* |
---|
9 | * |
---|
10 | * $Id: tfile.c,v 1.8 2006-03-10 07:11:38 ghudson Exp $ |
---|
11 | * |
---|
12 | * tfile.c -- a new implementation of tfile's. |
---|
13 | * |
---|
14 | * |
---|
15 | */ |
---|
16 | #ifndef lint |
---|
17 | static char rcsid_tfile_c[] = |
---|
18 | "$Id: tfile.c,v 1.8 2006-03-10 07:11:38 ghudson Exp $"; |
---|
19 | #endif /* lint */ |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <errno.h> |
---|
24 | #include <discuss/tfile.h> |
---|
25 | #include <sys/types.h> |
---|
26 | #include <sys/stat.h> |
---|
27 | |
---|
28 | #define min(A, B) ((A) < (B) ? (A) : (B)) |
---|
29 | #define NIL 0 |
---|
30 | #define SUCCESS 1 |
---|
31 | |
---|
32 | tfile tcreate (tfs, infop, info, proc) |
---|
33 | int tfs; |
---|
34 | char *infop; |
---|
35 | int info; |
---|
36 | int (*proc)(); |
---|
37 | { |
---|
38 | tfile tf; |
---|
39 | |
---|
40 | tf = (tfile) malloc (sizeof (struct _tfile)); |
---|
41 | if (tf == NIL) |
---|
42 | return (NIL); |
---|
43 | |
---|
44 | tf -> proc = proc; |
---|
45 | tf -> size = tfs; |
---|
46 | tf -> info = info; |
---|
47 | tf -> infop = infop; |
---|
48 | |
---|
49 | return (tf); |
---|
50 | } |
---|
51 | |
---|
52 | /* |
---|
53 | * |
---|
54 | * tfsize (tf) -- return size of a file. |
---|
55 | * |
---|
56 | */ |
---|
57 | int tfsize (tf) |
---|
58 | tfile tf; |
---|
59 | { |
---|
60 | return (tf -> size); |
---|
61 | } |
---|
62 | |
---|
63 | topen(tf,mode,result) |
---|
64 | tfile tf; |
---|
65 | char *mode; |
---|
66 | int *result; |
---|
67 | { |
---|
68 | if (tf == NIL || tf -> proc == NIL) { |
---|
69 | *result = EINVAL; |
---|
70 | return (-1); |
---|
71 | } |
---|
72 | |
---|
73 | return ((*(tf -> proc)) (TFOPEN, &(tf -> infop), &(tf -> info), mode, 0, result)); |
---|
74 | |
---|
75 | } |
---|
76 | |
---|
77 | tclose(tf,result) |
---|
78 | tfile tf; |
---|
79 | int *result; |
---|
80 | { |
---|
81 | if (tf == NIL || tf -> proc == NIL) { |
---|
82 | *result = EINVAL; |
---|
83 | return (-1); |
---|
84 | } |
---|
85 | |
---|
86 | return ((*(tf -> proc)) (TFCLOSE, &(tf -> infop), &(tf -> info), 0, 0, result)); |
---|
87 | } |
---|
88 | |
---|
89 | int tread(tf,bufp,wanted,result) |
---|
90 | tfile tf; |
---|
91 | char *bufp; |
---|
92 | int wanted; |
---|
93 | int *result; |
---|
94 | { |
---|
95 | if (tf == NIL || tf -> proc == NIL) { |
---|
96 | *result = EINVAL; |
---|
97 | return (-1); |
---|
98 | } |
---|
99 | |
---|
100 | return ((*(tf -> proc)) (TFREAD, &(tf -> infop), &(tf -> info), bufp, wanted,result)); |
---|
101 | } |
---|
102 | |
---|
103 | int twrite(tf,bufp,wanted,result) |
---|
104 | tfile tf; |
---|
105 | char *bufp; |
---|
106 | int wanted; |
---|
107 | int *result; |
---|
108 | { |
---|
109 | if (tf == NIL || tf -> proc == NIL) { |
---|
110 | *result = EINVAL; |
---|
111 | return (-1); |
---|
112 | } |
---|
113 | |
---|
114 | return ((*(tf -> proc)) (TFWRITE, &(tf -> infop), &(tf -> info), bufp, wanted, result)); |
---|
115 | } |
---|
116 | |
---|
117 | int tcontrol(tf,op,cinfop,result) |
---|
118 | tfile tf; |
---|
119 | int op; |
---|
120 | char *cinfop; |
---|
121 | int *result; |
---|
122 | { |
---|
123 | if (tf == NIL || tf -> proc == NIL) { |
---|
124 | *result = EINVAL; |
---|
125 | return (-1); |
---|
126 | } |
---|
127 | |
---|
128 | return ((*(tf -> proc)) (TFCONTROL, &(tf -> infop), &(tf -> info), cinfop, op, result)); |
---|
129 | } |
---|
130 | |
---|
131 | int tdestroy (tf) |
---|
132 | tfile tf; |
---|
133 | { |
---|
134 | int dummy; |
---|
135 | |
---|
136 | if (tf == NIL) { |
---|
137 | return (-1); |
---|
138 | } |
---|
139 | |
---|
140 | if (tf -> proc != NIL) |
---|
141 | (void) (*(tf -> proc)) (TFDESTROY, &(tf -> infop), &(tf -> info), 0, 0, &dummy); |
---|
142 | tf -> proc = NIL; |
---|
143 | free (tf); |
---|
144 | |
---|
145 | return(0); |
---|
146 | } |
---|