Annotation of libwww/Library/src/HTLocal.c, revision 2.11

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.11    ! frystyk     6: **     @(#) $Id: HTLocal.c,v 2.10 1998/05/04 19:36:50 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:     }
                     45: 
                     46:     /* If non-blocking protocol then change socket status
                     47:     ** I use fcntl() so that I can ask the status before I set it.
                     48:     ** See W. Richard Stevens (Advan. Prog. in UNIX env, p.364)
                     49:     ** Be CAREFULL with the old `O_NDELAY' - it wont work as read()
                     50:     ** returns 0 when blocking and NOT -1. FNDELAY is ONLY for BSD
                     51:     ** and does NOT work on SVR4 systems. O_NONBLOCK is POSIX.
                     52:     */
2.7       frystyk    53:     if (!HTNet_preemptive(net)) {
2.1       frystyk    54: #ifdef HAVE_FCNTL
2.9       frystyk    55:        if ((status = fcntl(sockfd, F_GETFL, 0)) != -1) {
2.1       frystyk    56: #ifdef O_NONBLOCK
2.2       frystyk    57:            status |= O_NONBLOCK;/* POSIX */
2.1       frystyk    58: #else
                     59: #ifdef F_NDELAY
2.2       frystyk    60:            status |= F_NDELAY; /* BSD */
2.1       frystyk    61: #endif /* F_NDELAY */
                     62: #endif /* O_NONBLOCK */
2.9       frystyk    63:            status = fcntl(sockfd, F_SETFL, status);
2.1       frystyk    64:        }
2.2       frystyk    65: #endif /* HAVE_FCNTL */
2.11    ! frystyk    66:        HTTRACE(PROT_TRACE, "HTFileOpen.. `%s\' opened using %sblocking socket\n" _ 
        !            67:                    local _ status == -1 ? "" : "NON-");
2.1       frystyk    68:     }
2.2       frystyk    69:     /* #endif - HAVE_FCNTL <- wrong location, moved up JTD:5/30/96 */
                     70: #else /* !NO_UNIX_IO */
2.1       frystyk    71: #ifdef VMS
2.7       frystyk    72:     if ((fp = fopen(local, mode,"shr=put","shr=upd")) == NULL) {
2.1       frystyk    73:        HTRequest_addSystemError(request, ERR_FATAL, errno, NO, "fopen");
                     74:        return HT_ERROR;
                     75:     }
                     76: #else
2.7       frystyk    77:     if ((fp = fopen(local, mode)) == NULL) {
2.2       frystyk    78:         HTRequest_addSystemError(request, ERR_FATAL, errno, NO, "fopen");
                     79:         return HT_ERROR;
2.1       frystyk    80:     }
                     81: #endif /* VMS */
2.11    ! frystyk    82:     HTTRACE(PROT_TRACE, "HTDoOpen.... `%s\' opened using FILE %p\n" _ local _ fp);
2.1       frystyk    83: #endif /* !NO_UNIX_IO */
2.4       frystyk    84: 
                     85:     /*
2.7       frystyk    86:     **  Associate the channel with the host and create
                     87:     **  an input and and output stream 
2.4       frystyk    88:     */
2.7       frystyk    89: #ifdef NO_UNIX_IO
                     90:     HTHost_setChannel(host, HTChannel_new(INVSOC, fp, YES));
                     91: #else
                     92:     HTHost_setChannel(host, HTChannel_new(sockfd, NULL, YES));
                     93: #endif /* NO_UNIX_IO */
2.4       frystyk    94: 
2.7       frystyk    95:     HTHost_getInput(host, HTNet_transport(net), NULL, 0);
                     96:     HTHost_getOutput(host, HTNet_transport(net), NULL, 0);
2.1       frystyk    97:     return HT_OK;
                     98: }
                     99: 
                    100: /*
                    101: **     Closes a file descriptor whatever means are available on the current
                    102: **     platform. If we have unix file descriptors then use this otherwise use
                    103: **     the ANSI C file descriptors
                    104: **
                    105: **     returns         HT_ERROR        Error has occured or interrupted
                    106: **                     HT_OK           if connected
                    107: **                     HT_WOULD_BLOCK  if operation would have blocked
                    108: */
2.3       frystyk   109: PUBLIC int HTFileClose (HTNet * net)
2.1       frystyk   110: {
2.7       frystyk   111:     HTHost * host = HTNet_host(net);
                    112:     HTChannel * ch = HTHost_channel(host);
2.1       frystyk   113:     int status = -1;
2.7       frystyk   114:     if (net && ch) {
2.5       eric      115: #ifdef NO_UNIX_IO
2.7       frystyk   116:        FILE * fp = HTChannel_file(ch);
                    117:        if (fp) {
2.11    ! frystyk   118:            HTTRACE(PROT_TRACE, "Closing..... ANSI file %p\n" _ fp);
2.8       frystyk   119:            status = fclose(fp);
2.7       frystyk   120:            HTChannel_setFile(ch, NULL);
2.1       frystyk   121:        }
                    122: #else
2.7       frystyk   123:        SOCKET sockfd = HTChannel_socket(ch);
                    124:        if (sockfd != INVSOC) {
2.11    ! frystyk   125:            HTTRACE(PROT_TRACE, "Closing..... fd %d\n" _ sockfd);
2.7       frystyk   126:            status = NETCLOSE(sockfd);
                    127:            HTChannel_setSocket(ch, INVSOC);
2.1       frystyk   128:        }
                    129: #endif /* NO_UNIX_IO */
                    130:     }
                    131:     return status < 0 ? HT_ERROR : HT_OK;
                    132: }
                    133: 

Webmaster