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.9 2006-03-10 07:11:38 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.9 2006-03-10 07:11:38 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 <stdlib.h> |
---|
28 | #include <discuss/tfile.h> |
---|
29 | #include <errno.h> |
---|
30 | #include <sys/types.h> |
---|
31 | #include <sys/stat.h> |
---|
32 | |
---|
33 | #define NL '\n' |
---|
34 | |
---|
35 | struct tunix_state { |
---|
36 | char last_char; |
---|
37 | }; |
---|
38 | |
---|
39 | /* |
---|
40 | * |
---|
41 | * tunix () -- This is the handler procedure, that handles tfile requests. |
---|
42 | * |
---|
43 | */ |
---|
44 | int tunix (op, infop, info, argp, argn, result) |
---|
45 | int op, *info, argn, *result; |
---|
46 | char **infop, *argp; |
---|
47 | { |
---|
48 | int numread,numwrite; |
---|
49 | struct tunix_state *ts; |
---|
50 | |
---|
51 | *result = 0; /* optimist */ |
---|
52 | ts = (struct tunix_state *) *infop; |
---|
53 | |
---|
54 | switch (op) { |
---|
55 | case TFOPEN: /* argp is pointer to modes */ |
---|
56 | return (0); |
---|
57 | |
---|
58 | case TFCLOSE: |
---|
59 | return (0); |
---|
60 | |
---|
61 | case TFREAD: |
---|
62 | numread = read (*info, argp, argn); |
---|
63 | if (numread < 0) { |
---|
64 | *result = errno; |
---|
65 | return (-1); |
---|
66 | } |
---|
67 | return(numread); |
---|
68 | |
---|
69 | case TFWRITE: |
---|
70 | numwrite = write (*info, argp, argn); |
---|
71 | if (numwrite < 0) { |
---|
72 | *result = errno; |
---|
73 | return (-1); |
---|
74 | } |
---|
75 | /* save last character written to file, so we can force NL */ |
---|
76 | ts -> last_char = argp [numwrite-1]; |
---|
77 | return(numwrite); |
---|
78 | |
---|
79 | case TFDESTROY: |
---|
80 | free (*infop); |
---|
81 | return (0); |
---|
82 | |
---|
83 | case TFCONTROL: |
---|
84 | if (argn == TFC_FORCE_NL) { /* force a NL at this point */ |
---|
85 | if (ts -> last_char != NL) { |
---|
86 | ts -> last_char = NL; |
---|
87 | write (*info, &(ts -> last_char), 1); |
---|
88 | } |
---|
89 | } |
---|
90 | return (0); |
---|
91 | |
---|
92 | default: |
---|
93 | *result = EBADF; |
---|
94 | return (-1); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | /* |
---|
99 | * |
---|
100 | * unix_tfile (tfs, us) |
---|
101 | * |
---|
102 | */ |
---|
103 | tfile unix_tfile (desc) |
---|
104 | int desc; |
---|
105 | { |
---|
106 | struct stat buf; |
---|
107 | struct tunix_state *ts; |
---|
108 | |
---|
109 | if (fstat (desc, &buf) < 0) |
---|
110 | return (NIL); |
---|
111 | |
---|
112 | ts = (struct tunix_state *) malloc (sizeof (struct tunix_state)); |
---|
113 | ts -> last_char = 0; |
---|
114 | |
---|
115 | return (tcreate ((int) buf.st_size, (char *) ts, desc, tunix)); |
---|
116 | } |
---|