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: tnet.c,v 1.7 1999-02-08 14:47:13 danw Exp $ |
---|
11 | * |
---|
12 | * tnet.c -- procedures to have tfiles go over the net. |
---|
13 | * |
---|
14 | * |
---|
15 | */ |
---|
16 | #ifndef lint |
---|
17 | static char rcsid_tnet_c[] = |
---|
18 | "$Id: tnet.c,v 1.7 1999-02-08 14:47:13 danw Exp $"; |
---|
19 | #endif /* lint */ |
---|
20 | |
---|
21 | #define min(A, B) ((A) < (B) ? (A) : (B)) |
---|
22 | #define NIL 0 |
---|
23 | |
---|
24 | #define SUCCESS 1 |
---|
25 | #define TFILE_BLK 500 |
---|
26 | |
---|
27 | #include <stdio.h> |
---|
28 | #include "usp.h" |
---|
29 | #include <discuss/tfile.h> |
---|
30 | #include <errno.h> |
---|
31 | |
---|
32 | |
---|
33 | extern int errno; |
---|
34 | |
---|
35 | /* |
---|
36 | * |
---|
37 | * tnet () -- This is the handler procedure, that handles tfile requests. |
---|
38 | * |
---|
39 | */ |
---|
40 | int tnet (op, infop, info, argp, argn, result) |
---|
41 | int op, *info, argn, *result; |
---|
42 | char **infop, *argp; |
---|
43 | { |
---|
44 | USPStream *us; |
---|
45 | USPCardinal bt; |
---|
46 | unsigned numread; |
---|
47 | |
---|
48 | *result = 0; /* optimist */ |
---|
49 | us = (USPStream *) *infop; |
---|
50 | |
---|
51 | switch (op) { |
---|
52 | case TFOPEN: /* argp is pointer to modes */ |
---|
53 | if (*argp == 'r') { /* reading */ |
---|
54 | if (USP_rcv_blk(us, &bt) != SUCCESS) { |
---|
55 | *result = errno; |
---|
56 | return (-1); |
---|
57 | } |
---|
58 | if (bt != TFILE_BLK) { |
---|
59 | *result = EBADF; |
---|
60 | return (-1); |
---|
61 | } |
---|
62 | *info = 1; |
---|
63 | } else { |
---|
64 | if (USP_begin_block (us, TFILE_BLK) != SUCCESS) { |
---|
65 | *result = errno; |
---|
66 | return (-1); |
---|
67 | } |
---|
68 | *info = -1; |
---|
69 | } |
---|
70 | return (0); |
---|
71 | |
---|
72 | case TFCLOSE: |
---|
73 | if (*info > 0) |
---|
74 | USP_flush_block(us); |
---|
75 | else |
---|
76 | USP_end_block(us); |
---|
77 | return (0); |
---|
78 | |
---|
79 | case TFREAD: |
---|
80 | if (USP_get_byte_block(us, argp, argn, &numread) != SUCCESS) { |
---|
81 | *result = errno; |
---|
82 | return (-1); |
---|
83 | } |
---|
84 | return(numread); |
---|
85 | |
---|
86 | case TFWRITE: |
---|
87 | if (USP_put_byte_block(us, argp, argn) != SUCCESS) { |
---|
88 | *result = errno; |
---|
89 | return (-1); |
---|
90 | } |
---|
91 | return (argn); |
---|
92 | |
---|
93 | case TFDESTROY: |
---|
94 | return (0); |
---|
95 | |
---|
96 | default: |
---|
97 | *result = EBADF; |
---|
98 | return (-1); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | /* |
---|
103 | * |
---|
104 | * net_tfile (tfs, us) |
---|
105 | * |
---|
106 | */ |
---|
107 | tfile net_tfile (tfs, us) |
---|
108 | int tfs; |
---|
109 | USPStream *us; |
---|
110 | { |
---|
111 | return (tcreate (tfs, (char *)us, 0, tnet)); |
---|
112 | } |
---|