Annotation of libwww/Library/src/HTLocal.c, revision 2.12
2.1 frystyk 1: /* HTLocal.c
2: ** LOCAL FILE HANDLING
3: **
4: ** (c) COPYRIGHT MIT 1995.
5: ** Please first read the full copyright statement in the file COPYRIGH.
2.12 ! frystyk 6: ** @(#) $Id: HTLocal.c,v 2.11 1999/02/22 22:10:11 frystyk Exp $
2.1 frystyk 7: **
8: ** History:
9: ** HFN: Written
10: */
11:
12: /* Library include files */
2.10 frystyk 13: #include "wwwsys.h"
2.1 frystyk 14: #include "WWWUtil.h"
2.7 frystyk 15: #include "WWWCore.h"
2.1 frystyk 16: #include "HTLocal.h" /* Implemented here */
17:
18: /* ------------------------------------------------------------------------- */
19:
20: /*
2.2 frystyk 21: ** Opens a local file usien whatever means are available on the current
22: ** platform. If we have unix file descriptors then use that as we can use
23: ** select on them. Otherwise use ANSI C file descriptors.
2.1 frystyk 24: **
2.2 frystyk 25: ** returns HT_ERROR Error has occured or interrupted
26: ** HT_OK if connected
27: ** HT_WOULD_BLOCK if operation would have blocked
2.1 frystyk 28: */
29: PUBLIC int HTFileOpen (HTNet * net, char * local, HTLocalMode mode)
30: {
2.7 frystyk 31: HTRequest * request = HTNet_request(net);
32: HTHost * host = HTNet_host(net);
33: #ifdef NO_UNIX_IO
34: FILE * fp = NULL;
35: #else
2.4 frystyk 36: SOCKET sockfd = INVSOC;
2.7 frystyk 37: #endif /* NO_UNIX_IO */
38:
2.1 frystyk 39: #ifndef NO_UNIX_IO
2.2 frystyk 40: int status = -1; /* JTD:5/30/96 - must init status to -1 */
2.4 frystyk 41: if ((sockfd = open(local, mode)) == -1) {
2.1 frystyk 42: HTRequest_addSystemError(request, ERR_FATAL, errno, NO, "open");
43: return HT_ERROR;
44: }
2.12 ! frystyk 45: HTTRACE(PROT_TRACE, "Socket...... Opened %d\n" _ sockfd);
2.1 frystyk 46:
47: /* If non-blocking protocol then change socket status
48: ** I use fcntl() so that I can ask the status before I set it.
49: ** See W. Richard Stevens (Advan. Prog. in UNIX env, p.364)
50: ** Be CAREFULL with the old `O_NDELAY' - it wont work as read()
51: ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD
52: ** and does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
53: */
2.7 frystyk 54: if (!HTNet_preemptive(net)) {
2.1 frystyk 55: #ifdef HAVE_FCNTL
2.9 frystyk 56: if ((status = fcntl(sockfd, F_GETFL, 0)) != -1) {
2.1 frystyk 57: #ifdef O_NONBLOCK
2.2 frystyk 58: status |= O_NONBLOCK;/* POSIX */
2.1 frystyk 59: #else
60: #ifdef F_NDELAY
2.2 frystyk 61: status |= F_NDELAY; /* BSD */
2.1 frystyk 62: #endif /* F_NDELAY */
63: #endif /* O_NONBLOCK */
2.9 frystyk 64: status = fcntl(sockfd, F_SETFL, status);
2.1 frystyk 65: }
2.2 frystyk 66: #endif /* HAVE_FCNTL */
2.11 frystyk 67: HTTRACE(PROT_TRACE, "HTFileOpen.. `%s\' opened using %sblocking socket\n" _
68: local _ status == -1 ? "" : "NON-");
2.1 frystyk 69: }
2.2 frystyk 70: /* #endif - HAVE_FCNTL <- wrong location, moved up JTD:5/30/96 */
71: #else /* !NO_UNIX_IO */
2.1 frystyk 72: #ifdef VMS
2.7 frystyk 73: if ((fp = fopen(local, mode,"shr=put","shr=upd")) == NULL) {
2.1 frystyk 74: HTRequest_addSystemError(request, ERR_FATAL, errno, NO, "fopen");
75: return HT_ERROR;
76: }
77: #else
2.7 frystyk 78: if ((fp = fopen(local, mode)) == NULL) {
2.2 frystyk 79: HTRequest_addSystemError(request, ERR_FATAL, errno, NO, "fopen");
80: return HT_ERROR;
2.1 frystyk 81: }
82: #endif /* VMS */
2.11 frystyk 83: HTTRACE(PROT_TRACE, "HTDoOpen.... `%s\' opened using FILE %p\n" _ local _ fp);
2.1 frystyk 84: #endif /* !NO_UNIX_IO */
2.4 frystyk 85:
86: /*
2.7 frystyk 87: ** Associate the channel with the host and create
88: ** an input and and output stream
2.4 frystyk 89: */
2.7 frystyk 90: #ifdef NO_UNIX_IO
91: HTHost_setChannel(host, HTChannel_new(INVSOC, fp, YES));
92: #else
93: HTHost_setChannel(host, HTChannel_new(sockfd, NULL, YES));
94: #endif /* NO_UNIX_IO */
2.4 frystyk 95:
2.7 frystyk 96: HTHost_getInput(host, HTNet_transport(net), NULL, 0);
97: HTHost_getOutput(host, HTNet_transport(net), NULL, 0);
2.1 frystyk 98: return HT_OK;
99: }
100:
101: /*
102: ** Closes a file descriptor whatever means are available on the current
103: ** platform. If we have unix file descriptors then use this otherwise use
104: ** the ANSI C file descriptors
105: **
106: ** returns HT_ERROR Error has occured or interrupted
107: ** HT_OK if connected
108: ** HT_WOULD_BLOCK if operation would have blocked
109: */
2.3 frystyk 110: PUBLIC int HTFileClose (HTNet * net)
2.1 frystyk 111: {
2.7 frystyk 112: HTHost * host = HTNet_host(net);
113: HTChannel * ch = HTHost_channel(host);
2.1 frystyk 114: int status = -1;
2.7 frystyk 115: if (net && ch) {
2.5 eric 116: #ifdef NO_UNIX_IO
2.7 frystyk 117: FILE * fp = HTChannel_file(ch);
118: if (fp) {
2.11 frystyk 119: HTTRACE(PROT_TRACE, "Closing..... ANSI file %p\n" _ fp);
2.8 frystyk 120: status = fclose(fp);
2.7 frystyk 121: HTChannel_setFile(ch, NULL);
2.1 frystyk 122: }
123: #else
2.7 frystyk 124: SOCKET sockfd = HTChannel_socket(ch);
125: if (sockfd != INVSOC) {
2.11 frystyk 126: HTTRACE(PROT_TRACE, "Closing..... fd %d\n" _ sockfd);
2.7 frystyk 127: status = NETCLOSE(sockfd);
128: HTChannel_setSocket(ch, INVSOC);
2.1 frystyk 129: }
130: #endif /* NO_UNIX_IO */
131: }
132: return status < 0 ? HT_ERROR : HT_OK;
133: }
134:
Webmaster