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: tunix.c,v 1.7 1999-01-22 23:10:03 ghudson Exp $ |
---|
11 | * |
---|
12 | * tunix.c -- procedures to have tfiles work from unix files. |
---|
13 | * |
---|
14 | * |
---|
15 | */ |
---|
16 | #ifndef lint |
---|
17 | static char rcsid_tunix_c[] = |
---|
18 | "$Id: tunix.c,v 1.7 1999-01-22 23:10:03 ghudson Exp $"; |
---|
19 | #endif lint |
---|
20 | |
---|
21 | #define min(A, B) ((A) < (B) ? (A) : (B)) |
---|
22 | #define NIL 0 |
---|
23 | |
---|
24 | #define SUCCESS 1 |
---|
25 | |
---|
26 | #include <stdio.h> |
---|
27 | #include <discuss/tfile.h> |
---|
28 | #include <errno.h> |
---|
29 | #include <sys/types.h> |
---|
30 | #include <sys/stat.h> |
---|
31 | |
---|
32 | #define NL '\n' |
---|
33 | |
---|
34 | char *malloc(); |
---|
35 | |
---|
36 | extern int errno; |
---|
37 | |
---|
38 | struct tunix_state { |
---|
39 | char last_char; |
---|
40 | }; |
---|
41 | |
---|
42 | /* |
---|
43 | * |
---|
44 | * tunix () -- This is the handler procedure, that handles tfile requests. |
---|
45 | * |
---|
46 | */ |
---|
47 | int tunix (op, infop, info, argp, argn, result) |
---|
48 | int op, *info, argn, *result; |
---|
49 | char **infop, *argp; |
---|
50 | { |
---|
51 | int numread,numwrite; |
---|
52 | struct tunix_state *ts; |
---|
53 | |
---|
54 | *result = 0; /* optimist */ |
---|
55 | ts = (struct tunix_state *) *infop; |
---|
56 | |
---|
57 | switch (op) { |
---|
58 | case TFOPEN: /* argp is pointer to modes */ |
---|
59 | return (0); |
---|
60 | |
---|
61 | case TFCLOSE: |
---|
62 | return (0); |
---|
63 | |
---|
64 | case TFREAD: |
---|
65 | numread = read (*info, argp, argn); |
---|
66 | if (numread < 0) { |
---|
67 | *result = errno; |
---|
68 | return (-1); |
---|
69 | } |
---|
70 | return(numread); |
---|
71 | |
---|
72 | case TFWRITE: |
---|
73 | numwrite = write (*info, argp, argn); |
---|
74 | if (numwrite < 0) { |
---|
75 | *result = errno; |
---|
76 | return (-1); |
---|
77 | } |
---|
78 | /* save last character written to file, so we can force NL */ |
---|
79 | ts -> last_char = argp [numwrite-1]; |
---|
80 | return(numwrite); |
---|
81 | |
---|
82 | case TFDESTROY: |
---|
83 | free (*infop); |
---|
84 | return (0); |
---|
85 | |
---|
86 | case TFCONTROL: |
---|
87 | if (argn == TFC_FORCE_NL) { /* force a NL at this point */ |
---|
88 | if (ts -> last_char != NL) { |
---|
89 | ts -> last_char = NL; |
---|
90 | write (*info, &(ts -> last_char), 1); |
---|
91 | } |
---|
92 | } |
---|
93 | return (0); |
---|
94 | |
---|
95 | default: |
---|
96 | *result = EBADF; |
---|
97 | return (-1); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | /* |
---|
102 | * |
---|
103 | * unix_tfile (tfs, us) |
---|
104 | * |
---|
105 | */ |
---|
106 | tfile unix_tfile (desc) |
---|
107 | int desc; |
---|
108 | { |
---|
109 | struct stat buf; |
---|
110 | struct tunix_state *ts; |
---|
111 | |
---|
112 | if (fstat (desc, &buf) < 0) |
---|
113 | return (NIL); |
---|
114 | |
---|
115 | ts = (struct tunix_state *) malloc (sizeof (struct tunix_state)); |
---|
116 | ts -> last_char = 0; |
---|
117 | |
---|
118 | return (tcreate ((int) buf.st_size, (char *) ts, desc, tunix)); |
---|
119 | } |
---|