[336] | 1 | /* |
---|
| 2 | * |
---|
[1934] | 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 | * |
---|
[12459] | 10 | * $Id: tunix.c,v 1.8 1999-02-08 14:47:13 danw Exp $ |
---|
[341] | 11 | * |
---|
[336] | 12 | * tunix.c -- procedures to have tfiles work from unix files. |
---|
| 13 | * |
---|
[11051] | 14 | * |
---|
[336] | 15 | */ |
---|
[341] | 16 | #ifndef lint |
---|
[1620] | 17 | static char rcsid_tunix_c[] = |
---|
[12459] | 18 | "$Id: tunix.c,v 1.8 1999-02-08 14:47:13 danw Exp $"; |
---|
| 19 | #endif /* lint */ |
---|
[336] | 20 | |
---|
| 21 | #define min(A, B) ((A) < (B) ? (A) : (B)) |
---|
| 22 | #define NIL 0 |
---|
| 23 | |
---|
| 24 | #define SUCCESS 1 |
---|
| 25 | |
---|
| 26 | #include <stdio.h> |
---|
[1620] | 27 | #include <discuss/tfile.h> |
---|
[336] | 28 | #include <errno.h> |
---|
| 29 | #include <sys/types.h> |
---|
| 30 | #include <sys/stat.h> |
---|
| 31 | |
---|
[618] | 32 | #define NL '\n' |
---|
[336] | 33 | |
---|
[618] | 34 | char *malloc(); |
---|
| 35 | |
---|
[336] | 36 | extern int errno; |
---|
| 37 | |
---|
[618] | 38 | struct tunix_state { |
---|
| 39 | char last_char; |
---|
| 40 | }; |
---|
| 41 | |
---|
[336] | 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; |
---|
[618] | 52 | struct tunix_state *ts; |
---|
[336] | 53 | |
---|
| 54 | *result = 0; /* optimist */ |
---|
[618] | 55 | ts = (struct tunix_state *) *infop; |
---|
[336] | 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 | } |
---|
[618] | 78 | /* save last character written to file, so we can force NL */ |
---|
| 79 | ts -> last_char = argp [numwrite-1]; |
---|
[336] | 80 | return(numwrite); |
---|
| 81 | |
---|
| 82 | case TFDESTROY: |
---|
[618] | 83 | free (*infop); |
---|
[336] | 84 | return (0); |
---|
| 85 | |
---|
[618] | 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 | |
---|
[336] | 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; |
---|
[618] | 110 | struct tunix_state *ts; |
---|
[336] | 111 | |
---|
| 112 | if (fstat (desc, &buf) < 0) |
---|
| 113 | return (NIL); |
---|
| 114 | |
---|
[618] | 115 | ts = (struct tunix_state *) malloc (sizeof (struct tunix_state)); |
---|
| 116 | ts -> last_char = 0; |
---|
| 117 | |
---|
[11051] | 118 | return (tcreate ((int) buf.st_size, (char *) ts, desc, tunix)); |
---|
[336] | 119 | } |
---|