[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 | * |
---|
[22404] | 10 | * $Id: tunix.c,v 1.9 2006-03-10 07:11:38 ghudson 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[] = |
---|
[22404] | 18 | "$Id: tunix.c,v 1.9 2006-03-10 07:11:38 ghudson Exp $"; |
---|
[12459] | 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> |
---|
[22404] | 27 | #include <stdlib.h> |
---|
[1620] | 28 | #include <discuss/tfile.h> |
---|
[336] | 29 | #include <errno.h> |
---|
| 30 | #include <sys/types.h> |
---|
| 31 | #include <sys/stat.h> |
---|
| 32 | |
---|
[618] | 33 | #define NL '\n' |
---|
[336] | 34 | |
---|
[618] | 35 | struct tunix_state { |
---|
| 36 | char last_char; |
---|
| 37 | }; |
---|
| 38 | |
---|
[336] | 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; |
---|
[618] | 49 | struct tunix_state *ts; |
---|
[336] | 50 | |
---|
| 51 | *result = 0; /* optimist */ |
---|
[618] | 52 | ts = (struct tunix_state *) *infop; |
---|
[336] | 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 | } |
---|
[618] | 75 | /* save last character written to file, so we can force NL */ |
---|
| 76 | ts -> last_char = argp [numwrite-1]; |
---|
[336] | 77 | return(numwrite); |
---|
| 78 | |
---|
| 79 | case TFDESTROY: |
---|
[618] | 80 | free (*infop); |
---|
[336] | 81 | return (0); |
---|
| 82 | |
---|
[618] | 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 | |
---|
[336] | 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; |
---|
[618] | 107 | struct tunix_state *ts; |
---|
[336] | 108 | |
---|
| 109 | if (fstat (desc, &buf) < 0) |
---|
| 110 | return (NIL); |
---|
| 111 | |
---|
[618] | 112 | ts = (struct tunix_state *) malloc (sizeof (struct tunix_state)); |
---|
| 113 | ts -> last_char = 0; |
---|
| 114 | |
---|
[11051] | 115 | return (tcreate ((int) buf.st_size, (char *) ts, desc, tunix)); |
---|
[336] | 116 | } |
---|