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 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/discuss/libds/tunix.c,v $ |
---|
11 | * $Header: /afs/dev.mit.edu/source/repository/athena/bin/discuss/libds/tunix.c,v 1.5 1989-06-03 00:22:33 srz Exp $ |
---|
12 | * |
---|
13 | * tunix.c -- procedures to have tfiles work from unix files. |
---|
14 | * |
---|
15 | * $Log: not supported by cvs2svn $ |
---|
16 | * Revision 1.4 89/01/04 20:47:00 raeburn |
---|
17 | * Fixed include reference |
---|
18 | * |
---|
19 | * Revision 1.3 87/07/18 00:01:38 srz |
---|
20 | * Added control operation to tfile's. First control operation: FORCE_NL, |
---|
21 | * which forces a NL onto a tfile if it isn't already there. |
---|
22 | * |
---|
23 | * Revision 1.2 87/04/11 00:06:29 srz |
---|
24 | * Added RCS junk |
---|
25 | * |
---|
26 | * |
---|
27 | */ |
---|
28 | #ifndef lint |
---|
29 | static char rcsid_tunix_c[] = |
---|
30 | "$Header: /afs/dev.mit.edu/source/repository/athena/bin/discuss/libds/tunix.c,v 1.5 1989-06-03 00:22:33 srz Exp $"; |
---|
31 | #endif lint |
---|
32 | |
---|
33 | #define min(A, B) ((A) < (B) ? (A) : (B)) |
---|
34 | #define NIL 0 |
---|
35 | |
---|
36 | #define SUCCESS 1 |
---|
37 | |
---|
38 | #include <stdio.h> |
---|
39 | #include <discuss/tfile.h> |
---|
40 | #include <errno.h> |
---|
41 | #include <sys/types.h> |
---|
42 | #include <sys/stat.h> |
---|
43 | |
---|
44 | #define NL '\n' |
---|
45 | |
---|
46 | char *malloc(); |
---|
47 | |
---|
48 | extern int errno; |
---|
49 | |
---|
50 | struct tunix_state { |
---|
51 | char last_char; |
---|
52 | }; |
---|
53 | |
---|
54 | /* |
---|
55 | * |
---|
56 | * tunix () -- This is the handler procedure, that handles tfile requests. |
---|
57 | * |
---|
58 | */ |
---|
59 | int tunix (op, infop, info, argp, argn, result) |
---|
60 | int op, *info, argn, *result; |
---|
61 | char **infop, *argp; |
---|
62 | { |
---|
63 | int numread,numwrite; |
---|
64 | struct tunix_state *ts; |
---|
65 | |
---|
66 | *result = 0; /* optimist */ |
---|
67 | ts = (struct tunix_state *) *infop; |
---|
68 | |
---|
69 | switch (op) { |
---|
70 | case TFOPEN: /* argp is pointer to modes */ |
---|
71 | return (0); |
---|
72 | |
---|
73 | case TFCLOSE: |
---|
74 | return (0); |
---|
75 | |
---|
76 | case TFREAD: |
---|
77 | numread = read (*info, argp, argn); |
---|
78 | if (numread < 0) { |
---|
79 | *result = errno; |
---|
80 | return (-1); |
---|
81 | } |
---|
82 | return(numread); |
---|
83 | |
---|
84 | case TFWRITE: |
---|
85 | numwrite = write (*info, argp, argn); |
---|
86 | if (numwrite < 0) { |
---|
87 | *result = errno; |
---|
88 | return (-1); |
---|
89 | } |
---|
90 | /* save last character written to file, so we can force NL */ |
---|
91 | ts -> last_char = argp [numwrite-1]; |
---|
92 | return(numwrite); |
---|
93 | |
---|
94 | case TFDESTROY: |
---|
95 | free (*infop); |
---|
96 | return (0); |
---|
97 | |
---|
98 | case TFCONTROL: |
---|
99 | if (argn == TFC_FORCE_NL) { /* force a NL at this point */ |
---|
100 | if (ts -> last_char != NL) { |
---|
101 | ts -> last_char = NL; |
---|
102 | write (*info, &(ts -> last_char), 1); |
---|
103 | } |
---|
104 | } |
---|
105 | return (0); |
---|
106 | |
---|
107 | default: |
---|
108 | *result = EBADF; |
---|
109 | return (-1); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | /* |
---|
114 | * |
---|
115 | * unix_tfile (tfs, us) |
---|
116 | * |
---|
117 | */ |
---|
118 | tfile unix_tfile (desc) |
---|
119 | int desc; |
---|
120 | { |
---|
121 | struct stat buf; |
---|
122 | struct tunix_state *ts; |
---|
123 | |
---|
124 | if (fstat (desc, &buf) < 0) |
---|
125 | return (NIL); |
---|
126 | |
---|
127 | ts = (struct tunix_state *) malloc (sizeof (struct tunix_state)); |
---|
128 | ts -> last_char = 0; |
---|
129 | |
---|
130 | return (tcreate (buf.st_size, (char *) ts, desc, tunix)); |
---|
131 | } |
---|