dgaudet 97/09/14 04:13:54
Modified: src INDENT src/main buff.c buff.h Log: indent Revision Changes Path 1.7 +4 -4 apachen/src/INDENT Index: INDENT =================================================================== RCS file: /export/home/cvs/apachen/src/INDENT,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- INDENT 1997/09/14 11:09:32 1.6 +++ INDENT 1997/09/14 11:13:51 1.7 @@ -3,10 +3,10 @@ If a file has already been indent'd, or doesn't need it, mark it as DONE ./main: STATUS - alloc.c - alloc.h - buff.c RESERVED by Dean - buff.h RESERVED by Dean + alloc.c RESERVED by Dean + alloc.h RESERVED by Dean + buff.c DONE by Dean + buff.h DONE by Dean conf.h dummy.c explain.c 1.45 +318 -300 apachen/src/main/buff.c Index: buff.c =================================================================== RCS file: /export/home/cvs/apachen/src/main/buff.c,v retrieving revision 1.44 retrieving revision 1.45 diff -u -r1.44 -r1.45 --- buff.c 1997/08/17 11:40:11 1.44 +++ buff.c 1997/09/14 11:13:52 1.45 @@ -97,8 +97,7 @@ #ifdef WIN32 -int -sendwithtimeout(int sock, const char *buf, int len, int flags) +int sendwithtimeout(int sock, const char *buf, int len, int flags) { int iostate = 1; fd_set fdset; @@ -106,47 +105,42 @@ int err = WSAEWOULDBLOCK; int rv; - if(!(tv.tv_sec = check_alarm())) - return(send(sock, buf, len, flags)); - + if (!(tv.tv_sec = check_alarm())) + return (send(sock, buf, len, flags)); + rv = ioctlsocket(sock, FIONBIO, &iostate); iostate = 0; - if(rv) - { - err = WSAGetLastError(); - ap_assert(0); + if (rv) { + err = WSAGetLastError(); + ap_assert(0); } rv = send(sock, buf, len, flags); - if(rv == SOCKET_ERROR) - { - err = WSAGetLastError(); - if(err == WSAEWOULDBLOCK) - { - FD_ZERO(&fdset); - FD_SET(sock, &fdset); - tv.tv_usec = 0; - rv = select(FD_SETSIZE, NULL, &fdset, NULL, &tv); - if(rv == 0) - { - ioctlsocket(sock, FIONBIO, &iostate); - check_alarm(); - WSASetLastError(WSAEWOULDBLOCK); - return(SOCKET_ERROR); - } - rv = send(sock, buf, len, flags); - if(rv == SOCKET_ERROR) - err = WSAGetLastError(); - } + if (rv == SOCKET_ERROR) { + err = WSAGetLastError(); + if (err == WSAEWOULDBLOCK) { + FD_ZERO(&fdset); + FD_SET(sock, &fdset); + tv.tv_usec = 0; + rv = select(FD_SETSIZE, NULL, &fdset, NULL, &tv); + if (rv == 0) { + ioctlsocket(sock, FIONBIO, &iostate); + check_alarm(); + WSASetLastError(WSAEWOULDBLOCK); + return (SOCKET_ERROR); + } + rv = send(sock, buf, len, flags); + if (rv == SOCKET_ERROR) + err = WSAGetLastError(); + } } ioctlsocket(sock, FIONBIO, &iostate); - if(rv == SOCKET_ERROR) - WSASetLastError(err); - return(rv); + if (rv == SOCKET_ERROR) + WSASetLastError(err); + return (rv); } -int -recvwithtimeout(int sock, char *buf, int len, int flags) +int recvwithtimeout(int sock, char *buf, int len, int flags) { int iostate = 1; fd_set fdset; @@ -154,74 +148,71 @@ int err = WSAEWOULDBLOCK; int rv; - if(!(tv.tv_sec = check_alarm())) - return(recv(sock, buf, len, flags)); - + if (!(tv.tv_sec = check_alarm())) + return (recv(sock, buf, len, flags)); + rv = ioctlsocket(sock, FIONBIO, &iostate); iostate = 0; ap_assert(!rv); rv = recv(sock, buf, len, flags); - if(rv == SOCKET_ERROR) - { - err = WSAGetLastError(); - if(err == WSAEWOULDBLOCK) - { - FD_ZERO(&fdset); - FD_SET(sock, &fdset); - tv.tv_usec = 0; - rv = select(FD_SETSIZE, &fdset, NULL, NULL, &tv); - if(rv == 0) - { - ioctlsocket(sock, FIONBIO, &iostate); - check_alarm(); - WSASetLastError(WSAEWOULDBLOCK); - return(SOCKET_ERROR); - } - rv = recv(sock, buf, len, flags); - if(rv == SOCKET_ERROR) - err = WSAGetLastError(); - } + if (rv == SOCKET_ERROR) { + err = WSAGetLastError(); + if (err == WSAEWOULDBLOCK) { + FD_ZERO(&fdset); + FD_SET(sock, &fdset); + tv.tv_usec = 0; + rv = select(FD_SETSIZE, &fdset, NULL, NULL, &tv); + if (rv == 0) { + ioctlsocket(sock, FIONBIO, &iostate); + check_alarm(); + WSASetLastError(WSAEWOULDBLOCK); + return (SOCKET_ERROR); + } + rv = recv(sock, buf, len, flags); + if (rv == SOCKET_ERROR) + err = WSAGetLastError(); + } } ioctlsocket(sock, FIONBIO, &iostate); - if(rv == SOCKET_ERROR) - WSASetLastError(err); - return(rv); + if (rv == SOCKET_ERROR) + WSASetLastError(err); + return (rv); } -#endif /* WIN32 */ +#endif /* WIN32 */ /* the lowest level reading primitive */ -static inline int buff_read (BUFF *fb, void *buf, int nbyte) +static inline int buff_read(BUFF *fb, void *buf, int nbyte) { int rv; #if defined (WIN32) if (fb->flags & B_SOCKET) { - rv = recvwithtimeout( fb->fd_in, buf, nbyte, 0 ); + rv = recvwithtimeout(fb->fd_in, buf, nbyte, 0); if (rv == SOCKET_ERROR) errno = WSAGetLastError() - WSABASEERR; } else - rv = read( fb->fd_in, buf, nbyte ); + rv = read(fb->fd_in, buf, nbyte); #else - rv = read( fb->fd_in, buf, nbyte ); + rv = read(fb->fd_in, buf, nbyte); #endif /* WIN32 */ return rv; } /* the lowest level writing primitive */ -static inline int buff_write (BUFF *fb, const void *buf, int nbyte) +static inline int buff_write(BUFF *fb, const void *buf, int nbyte) { int rv; #if defined(WIN32) if (fb->flags & B_SOCKET) { - rv = sendwithtimeout( fb->fd, buf, nbyte, 0); + rv = sendwithtimeout(fb->fd, buf, nbyte, 0); if (rv == SOCKET_ERROR) errno = WSAGetLastError() - WSABASEERR; } else - rv = write( fb->fd, buf, nbyte ); + rv = write(fb->fd, buf, nbyte); #elif defined (B_SFIO) rv = sfwrite(fb->sf_out, buf, nbyte); #else @@ -230,16 +221,16 @@ return rv; } -static void -doerror(BUFF *fb, int err) +static void doerror(BUFF *fb, int err) { - int errsave = errno; /* Save errno to prevent overwriting it below */ + int errsave = errno; /* Save errno to prevent overwriting it below */ if (err == B_RD) fb->flags |= B_RDERR; else fb->flags |= B_WRERR; - if (fb->error != NULL) (*fb->error)(fb, err, fb->error_data); + if (fb->error != NULL) + (*fb->error) (fb, err, fb->error_data); errno = errsave; } @@ -253,17 +244,21 @@ BUFF *fb; fb = palloc(p, sizeof(BUFF)); - fb->pool=p; + fb->pool = p; fb->bufsiz = DEFAULT_BUFSIZE; - fb->flags = flags & (B_RDWR|B_SOCKET); + fb->flags = flags & (B_RDWR | B_SOCKET); - if (flags & B_RD) fb->inbase = palloc(p, fb->bufsiz); - else fb->inbase = NULL; + if (flags & B_RD) + fb->inbase = palloc(p, fb->bufsiz); + else + fb->inbase = NULL; /* overallocate so that we can put a chunk trailer of CRLF into this * buffer */ - if (flags & B_WR) fb->outbase = palloc(p, fb->bufsiz + 2); - else fb->outbase = NULL; + if (flags & B_WR) + fb->outbase = palloc(p, fb->bufsiz + 2); + else + fb->outbase = NULL; fb->inptr = fb->inbase; @@ -277,12 +272,12 @@ fb->fd_in = -1; #ifdef B_SFIO - fb->sf_in = NULL; + fb->sf_in = NULL; fb->sf_out = NULL; - fb->sf_in = sfnew(fb->sf_in, NIL(Void_t*), - (size_t)SF_UNBOUND, 0, SF_READ); - fb->sf_out = sfnew(fb->sf_out, NIL(Void_t*), - (size_t)SF_UNBOUND, 1, SF_WRITE); + fb->sf_in = sfnew(fb->sf_in, NIL(Void_t *), + (size_t) SF_UNBOUND, 0, SF_READ); + fb->sf_out = sfnew(fb->sf_out, NIL(Void_t *), + (size_t) SF_UNBOUND, 1, SF_WRITE); #endif return fb; @@ -299,12 +294,11 @@ API_EXPORT(int) bsetopt(BUFF *fb, int optname, const void *optval) { - if (optname == BO_BYTECT) - { - fb->bytes_sent = *(const long int *)optval - (long int)fb->outcnt;; + if (optname == BO_BYTECT) { + fb->bytes_sent = *(const long int *) optval - (long int) fb->outcnt;; return 0; - } else - { + } + else { errno = EINVAL; return -1; } @@ -312,14 +306,14 @@ API_EXPORT(int) bgetopt(BUFF *fb, int optname, void *optval) { - if (optname == BO_BYTECT) - { - long int bs=fb->bytes_sent + fb->outcnt; - if (bs < 0L) bs = 0L; - *(long int *)optval = bs; + if (optname == BO_BYTECT) { + long int bs = fb->bytes_sent + fb->outcnt; + if (bs < 0L) + bs = 0L; + *(long int *) optval = bs; return 0; - } else - { + } + else { errno = EINVAL; return -1; } @@ -328,33 +322,32 @@ /* * start chunked encoding */ -static void -start_chunk( BUFF *fb ) +static void start_chunk(BUFF *fb) { - char chunksize[16]; /* Big enough for practically anything */ + char chunksize[16]; /* Big enough for practically anything */ int chunk_header_size; if (fb->outchunk != -1) { /* already chunking */ return; } - if (!(fb->flags & B_WR) || (fb->flags & (B_WRERR|B_EOUT))) { + if (!(fb->flags & B_WR) || (fb->flags & (B_WRERR | B_EOUT))) { /* unbuffered writes */ return; } /* we know that the chunk header is going to take at least 3 bytes... */ - chunk_header_size = ap_snprintf( chunksize, sizeof(chunksize), - "%x\015\012", fb->bufsiz - fb->outcnt - 3 ); + chunk_header_size = ap_snprintf(chunksize, sizeof(chunksize), + "%x\015\012", fb->bufsiz - fb->outcnt - 3); /* we need at least the header_len + at least 1 data byte * remember that we've overallocated fb->outbase so that we can always * fit the two byte CRLF trailer */ - if( fb->bufsiz - fb->outcnt < chunk_header_size + 1 ) { + if (fb->bufsiz - fb->outcnt < chunk_header_size + 1) { bflush(fb); } /* assume there's enough space now */ - memcpy( &fb->outbase[fb->outcnt], chunksize, chunk_header_size ); + memcpy(&fb->outbase[fb->outcnt], chunksize, chunk_header_size); fb->outchunk = fb->outcnt; fb->outcnt += chunk_header_size; fb->outchunk_header_size = chunk_header_size; @@ -364,17 +357,16 @@ /* * end a chunk -- tweak the chunk_header from start_chunk, and add a trailer */ -static void -end_chunk( BUFF *fb ) +static void end_chunk(BUFF *fb) { int i; - if( fb->outchunk == -1 ) { + if (fb->outchunk == -1) { /* not chunking */ return; } - if( fb->outchunk + fb->outchunk_header_size == fb->outcnt ) { + if (fb->outchunk + fb->outchunk_header_size == fb->outcnt) { /* nothing was written into this chunk, and we can't write a 0 size * chunk because that signifies EOF, so just erase it */ @@ -384,9 +376,9 @@ } /* we know this will fit because of how we wrote it in start_chunk() */ - i = ap_snprintf( (char *)&fb->outbase[fb->outchunk], - fb->outchunk_header_size, - "%x", fb->outcnt - fb->outchunk - fb->outchunk_header_size ); + i = ap_snprintf((char *) &fb->outbase[fb->outchunk], + fb->outchunk_header_size, + "%x", fb->outcnt - fb->outchunk - fb->outchunk_header_size); /* we may have to tack some trailing spaces onto the number we just wrote * in case it was smaller than our estimated size. We've also written @@ -394,12 +386,12 @@ * \r back in. */ i += fb->outchunk; - while( fb->outbase[i] != '\015' && fb->outbase[i] != '\012' ) { + while (fb->outbase[i] != '\015' && fb->outbase[i] != '\012') { fb->outbase[i++] = ' '; } - if( fb->outbase[i] == '\012' ) { + if (fb->outbase[i] == '\012') { /* we overwrote the \r, so put it back */ - fb->outbase[i-1] = '\015'; + fb->outbase[i - 1] = '\015'; } /* tack on the trailing CRLF, we've reserved room for this */ @@ -420,7 +412,8 @@ if (flag & B_CHUNK) { start_chunk(fb); } - } else { + } + else { fb->flags &= ~flag; if (flag & B_CHUNK) { end_chunk(fb); @@ -434,11 +427,11 @@ { int fd; - fd = ( direction == B_RD ) ? fb->fd_in : fb->fd; + fd = (direction == B_RD) ? fb->fd_in : fb->fd; #if defined(O_NONBLOCK) - return fcntl (fd, F_SETFL, O_NONBLOCK); + return fcntl(fd, F_SETFL, O_NONBLOCK); #elif defined(F_NDELAY) - return fcntl (fd, F_SETFL, F_NDELAY); + return fcntl(fd, F_SETFL, F_NDELAY); #else return 0; #endif @@ -460,15 +453,14 @@ #if !defined (B_SFIO) || defined (WIN32) #define saferead saferead_guts #else -static int -saferead(BUFF *fb, char *buf, int nbyte) +static int saferead(BUFF *fb, char *buf, int nbyte) { return sfread(fb->sf_in, buf, nbyte); } #endif -API_EXPORT(void) bhalfduplex (BUFF *fb) +API_EXPORT(void) bhalfduplex(BUFF *fb) { int rv; fd_set fds; @@ -479,65 +471,64 @@ } /* test for a block */ do { - FD_ZERO( &fds ); - FD_SET( fb->fd_in, &fds ); + FD_ZERO(&fds); + FD_SET(fb->fd_in, &fds); tv.tv_sec = 0; tv.tv_usec = 0; - rv = ap_select( fb->fd_in + 1, &fds, NULL, NULL, &tv ); - } while( rv < 0 && errno == EINTR ); + rv = ap_select(fb->fd_in + 1, &fds, NULL, NULL, &tv); + } while (rv < 0 && errno == EINTR); /* treat any error as if it would block as well */ - if( rv != 1 ) { + if (rv != 1) { bflush(fb); } } -static inline int -saferead_guts(BUFF *fb, void *buf, int nbyte) +static inline int saferead_guts(BUFF *fb, void *buf, int nbyte) { int rv; - if( fb->flags & B_SAFEREAD ) { - bhalfduplex (fb); + if (fb->flags & B_SAFEREAD) { + bhalfduplex(fb); } do { - rv = buff_read (fb, buf, nbyte); + rv = buff_read(fb, buf, nbyte); } while (rv == -1 && errno == EINTR && !(fb->flags & B_EOUT)); - return( rv ); + return (rv); } #ifdef B_SFIO -int bsfio_read(Sfio_t *f, char *buf, int nbyte, apache_sfio *disc) +int bsfio_read(Sfio_t * f, char *buf, int nbyte, apache_sfio *disc) { int rv; BUFF *fb = disc->buff; - + rv = saferead_guts(fb, buf, nbyte); buf[rv] = '\0'; f->next = 0; - return(rv); + return (rv); } -int bsfio_write(Sfio_t *f, char *buf, int nbyte, apache_sfio *disc) +int bsfio_write(Sfio_t * f, char *buf, int nbyte, apache_sfio *disc) { return write(disc->buff->fd, buf, nbyte); } Sfdisc_t *bsfio_new(pool *p, BUFF *b) { - apache_sfio* disc; - - if(!(disc = (apache_sfio*)palloc(p, sizeof(apache_sfio))) ) - return (Sfdisc_t *)disc; - - disc->disc.readf = (Sfread_f)bsfio_read; - disc->disc.writef = (Sfwrite_f)bsfio_write; - disc->disc.seekf = (Sfseek_f)NULL; - disc->disc.exceptf = (Sfexcept_f)NULL; + apache_sfio *disc; + + if (!(disc = (apache_sfio *) palloc(p, sizeof(apache_sfio)))) + return (Sfdisc_t *) disc; + + disc->disc.readf = (Sfread_f) bsfio_read; + disc->disc.writef = (Sfwrite_f) bsfio_write; + disc->disc.seekf = (Sfseek_f) NULL; + disc->disc.exceptf = (Sfexcept_f) NULL; disc->buff = b; - return (Sfdisc_t *)disc; + return (Sfdisc_t *) disc; } #endif @@ -547,15 +538,16 @@ * and then there's the SFIO case. Note that saferead takes care * of EINTR. */ -static int read_with_errors (BUFF *fb, void *buf, int nbyte) +static int read_with_errors(BUFF *fb, void *buf, int nbyte) { int rv; - rv = saferead (fb, buf, nbyte); + rv = saferead(fb, buf, nbyte); if (rv == 0) { fb->flags |= B_EOF; - } else if (rv == -1 && errno != EAGAIN) { - doerror (fb, B_RD); + } + else if (rv == -1 && errno != EAGAIN) { + doerror(fb, B_RD); } return rv; } @@ -569,46 +561,47 @@ { int i, nrd; - if (fb->flags & B_RDERR) return -1; - if (nbyte == 0) return 0; + if (fb->flags & B_RDERR) + return -1; + if (nbyte == 0) + return 0; if (!(fb->flags & B_RD)) { /* Unbuffered reading. First check if there was something in the * buffer from before we went unbuffered. */ if (fb->incnt) { i = (fb->incnt > nbyte) ? nbyte : fb->incnt; - memcpy (buf, fb->inptr, i); + memcpy(buf, fb->inptr, i); fb->incnt -= i; fb->inptr += i; return i; } - i = read_with_errors (fb, buf, nbyte); + i = read_with_errors(fb, buf, nbyte); return i; } nrd = fb->incnt; /* can we fill the buffer */ - if (nrd >= nbyte) - { + if (nrd >= nbyte) { memcpy(buf, fb->inptr, nbyte); fb->incnt = nrd - nbyte; fb->inptr += nbyte; return nbyte; } - - if (nrd > 0) - { + + if (nrd > 0) { memcpy(buf, fb->inptr, nrd); nbyte -= nrd; - buf = nrd + (char *)buf; + buf = nrd + (char *) buf; fb->incnt = 0; } - if (fb->flags & B_EOF) return nrd; + if (fb->flags & B_EOF) + return nrd; /* do a single read */ if (nbyte >= fb->bufsiz) { /* read directly into buffer */ - i = read_with_errors (fb, buf, nbyte); + i = read_with_errors(fb, buf, nbyte); if (i == -1) { return nrd ? nrd : -1; } @@ -616,12 +609,13 @@ else { /* read into hold buffer, then memcpy */ fb->inptr = fb->inbase; - i = read_with_errors (fb, fb->inptr, fb->bufsiz); + i = read_with_errors(fb, fb->inptr, fb->bufsiz); if (i == -1) { return nrd ? nrd : -1; } fb->incnt = i; - if (i > nbyte) i = nbyte; + if (i > nbyte) + i = nbyte; memcpy(buf, fb->inptr, i); fb->incnt -= i; fb->inptr += i; @@ -653,50 +647,52 @@ int i, ch, ct; /* Can't do bgets on an unbuffered stream */ - if (!(fb->flags & B_RD)) - { + if (!(fb->flags & B_RD)) { errno = EINVAL; return -1; } - if (fb->flags & B_RDERR) return -1; + if (fb->flags & B_RDERR) + return -1; ct = 0; i = 0; - for (;;) - { - if (i == fb->incnt) - { + for (;;) { + if (i == fb->incnt) { /* no characters left */ fb->inptr = fb->inbase; fb->incnt = 0; - if (fb->flags & B_EOF) break; - i = read_with_errors (fb, fb->inptr, fb->bufsiz); + if (fb->flags & B_EOF) + break; + i = read_with_errors(fb, fb->inptr, fb->bufsiz); if (i == -1) { buff[ct] = '\0'; return ct ? ct : -1; } fb->incnt = i; - if (i == 0) break; /* EOF */ + if (i == 0) + break; /* EOF */ i = 0; - continue; /* restart with the new data */ + continue; /* restart with the new data */ } ch = fb->inptr[i++]; - if (ch == '\012') /* got LF */ - { - if (ct == 0) buff[ct++] = '\n'; + if (ch == '\012') { /* got LF */ + if (ct == 0) + buff[ct++] = '\n'; /* if just preceeded by CR, replace CR with LF */ - else if (buff[ct-1] == '\015') buff[ct-1] = '\n'; - else if (ct < n-1) buff[ct++] = '\n'; - else i--; /* no room for LF */ + else if (buff[ct - 1] == '\015') + buff[ct - 1] = '\n'; + else if (ct < n - 1) + buff[ct++] = '\n'; + else + i--; /* no room for LF */ break; } - if (ct == n-1) - { - i--; /* push back ch */ + if (ct == n - 1) { + i--; /* push back ch */ break; } - + buff[ct++] = ch; } fb->incnt -= i; @@ -718,23 +714,24 @@ int i; *buff = '\0'; - - if (!(fb->flags & B_RD)) { /* Can't do blookc on an unbuffered stream */ - errno = EINVAL; - return -1; - } - if (fb->flags & B_RDERR) return -1; - - if (fb->incnt == 0) { /* no characters left in stream buffer */ - fb->inptr = fb->inbase; - if (fb->flags & B_EOF) - return 0; - i = read_with_errors (fb, fb->inptr, fb->bufsiz); + if (!(fb->flags & B_RD)) { /* Can't do blookc on an unbuffered stream */ + errno = EINVAL; + return -1; + } + if (fb->flags & B_RDERR) + return -1; + + if (fb->incnt == 0) { /* no characters left in stream buffer */ + fb->inptr = fb->inbase; + if (fb->flags & B_EOF) + return 0; + + i = read_with_errors(fb, fb->inptr, fb->bufsiz); if (i <= 0) { return i; } - fb->incnt = i; + fb->incnt = i; } *buff = fb->inptr[0]; @@ -751,18 +748,16 @@ int i; /* Can't do bskiplf on an unbuffered stream */ - if (!(fb->flags & B_RD)) - { + if (!(fb->flags & B_RD)) { errno = EINVAL; return -1; } - if (fb->flags & B_RDERR) return -1; + if (fb->flags & B_RDERR) + return -1; - for (;;) - { - x = (unsigned char *)memchr(fb->inptr, '\012', fb->incnt); - if (x != NULL) - { + for (;;) { + x = (unsigned char *) memchr(fb->inptr, '\012', fb->incnt); + if (x != NULL) { x++; fb->incnt -= x - fb->inptr; fb->inptr = x; @@ -771,9 +766,11 @@ fb->inptr = fb->inbase; fb->incnt = 0; - if (fb->flags & B_EOF) return 0; - i = read_with_errors (fb, fb->inptr, fb->bufsiz); - if (i <= 0) return i; + if (fb->flags & B_EOF) + return 0; + i = read_with_errors(fb, fb->inptr, fb->bufsiz); + if (i <= 0) + return i; fb->incnt = i; } } @@ -798,9 +795,12 @@ char buf[1]; i = bread(fb, buf, 1); - if (i == 0) errno = 0; /* no error; EOF */ - if (i != 1) return EOF; - else return buf[0]; + if (i == 0) + errno = 0; /* no error; EOF */ + if (i != 1) + return EOF; + else + return buf[0]; } @@ -813,25 +813,24 @@ * * Deals with calling doerror and setting bytes_sent. */ -static int -write_it_all(BUFF *fb, const void *buf, int nbyte) +static int write_it_all(BUFF *fb, const void *buf, int nbyte) { int i; - if (fb->flags & (B_WRERR|B_EOUT)) + if (fb->flags & (B_WRERR | B_EOUT)) return -1; while (nbyte > 0) { - i = buff_write (fb, buf, nbyte); + i = buff_write(fb, buf, nbyte); if (i < 0) { if (errno != EAGAIN && errno != EINTR) { - doerror (fb, B_WR); + doerror(fb, B_WR); return -1; } } else { nbyte -= i; - buf = i + (const char *)buf; + buf = i + (const char *) buf; fb->bytes_sent += i; } if (fb->flags & B_EOUT) @@ -847,7 +846,7 @@ * * Deals with doerror() and bytes_sent. */ -static int writev_it_all (BUFF *fb, struct iovec *vec, int nvec) +static int writev_it_all(BUFF *fb, struct iovec *vec, int nvec) { int i, rv; @@ -856,12 +855,13 @@ */ i = 0; while (i < nvec) { - do rv = writev( fb->fd, &vec[i], nvec - i ); + do + rv = writev(fb->fd, &vec[i], nvec - i); while (rv == -1 && (errno == EINTR || errno == EAGAIN) - && !(fb->flags & B_EOUT)); + && !(fb->flags & B_EOUT)); if (rv == -1) { if (errno != EINTR && errno != EAGAIN) { - doerror (fb, B_WR); + doerror(fb, B_WR); } return -1; } @@ -869,13 +869,14 @@ /* recalculate vec to deal with partial writes */ while (rv > 0) { if (rv < vec[i].iov_len) { - vec[i].iov_base = (char *)vec[i].iov_base + rv; + vec[i].iov_base = (char *) vec[i].iov_base + rv; vec[i].iov_len -= rv; rv = 0; if (vec[i].iov_len == 0) { ++i; } - } else { + } + else { rv -= vec[i].iov_len; ++i; } @@ -891,18 +892,20 @@ /* A wrapper for buff_write which deals with error conditions and * bytes_sent. Also handles non-blocking writes. */ -static int write_with_errors (BUFF *fb, const void *buf, int nbyte) +static int write_with_errors(BUFF *fb, const void *buf, int nbyte) { int rv; - do rv = buff_write(fb, buf, nbyte); + do + rv = buff_write(fb, buf, nbyte); while (rv == -1 && errno == EINTR && !(fb->flags & B_EOUT)); if (rv == -1) { if (errno != EAGAIN) { - doerror (fb, B_WR); + doerror(fb, B_WR); } return -1; - } else if (rv == 0) { + } + else if (rv == 0) { errno = EAGAIN; return -1; } @@ -920,19 +923,18 @@ * Can be used on non-blocking descriptors, but only if they're not chunked. * Deals with doerror() and bytes_sent. */ -static int -bcwrite(BUFF *fb, const void *buf, int nbyte) +static int bcwrite(BUFF *fb, const void *buf, int nbyte) { - char chunksize[16]; /* Big enough for practically anything */ + char chunksize[16]; /* Big enough for practically anything */ #ifndef NO_WRITEV struct iovec vec[3]; #endif - if (fb->flags & (B_WRERR|B_EOUT)) + if (fb->flags & (B_WRERR | B_EOUT)) return -1; if (!(fb->flags & B_CHUNK)) { - return write_with_errors (fb, buf, nbyte); + return write_with_errors(fb, buf, nbyte); } #ifdef NO_WRITEV @@ -949,13 +951,13 @@ #else vec[0].iov_base = chunksize; vec[0].iov_len = ap_snprintf(chunksize, sizeof(chunksize), "%x\015\012", - nbyte); - vec[1].iov_base = (void *)buf; /* cast is to avoid const warning */ + nbyte); + vec[1].iov_base = (void *) buf; /* cast is to avoid const warning */ vec[1].iov_len = nbyte; vec[2].iov_base = "\r\n"; vec[2].iov_len = 2; - return writev_it_all (fb, vec, (sizeof(vec)/sizeof(vec[0]))) ? -1 : nbyte; + return writev_it_all(fb, vec, (sizeof(vec) / sizeof(vec[0]))) ? -1 : nbyte; #endif } @@ -965,7 +967,7 @@ * Used to combine the contents of the fb buffer, and a large buffer * passed in. */ -static int large_write (BUFF *fb, const void *buf, int nbyte) +static int large_write(BUFF *fb, const void *buf, int nbyte) { struct iovec vec[4]; int nvec; @@ -978,29 +980,30 @@ } nvec = 0; if (fb->outcnt > 0) { - vec[nvec].iov_base = (void *)fb->outbase; + vec[nvec].iov_base = (void *) fb->outbase; vec[nvec].iov_len = fb->outcnt; ++nvec; } if (fb->flags & B_CHUNK) { vec[nvec].iov_base = chunksize; - vec[nvec].iov_len = ap_snprintf (chunksize, sizeof(chunksize), - "%x\015\012", nbyte); + vec[nvec].iov_len = ap_snprintf(chunksize, sizeof(chunksize), + "%x\015\012", nbyte); ++nvec; - vec[nvec].iov_base = (void *)buf; + vec[nvec].iov_base = (void *) buf; vec[nvec].iov_len = nbyte; ++nvec; vec[nvec].iov_base = "\r\n"; vec[nvec].iov_len = 2; ++nvec; - } else { - vec[nvec].iov_base = (void *)buf; + } + else { + vec[nvec].iov_base = (void *) buf; vec[nvec].iov_len = nbyte; ++nvec; } fb->outcnt = 0; - return writev_it_all (fb, vec, nvec) ? -1 : nbyte; + return writev_it_all(fb, vec, nvec) ? -1 : nbyte; } #endif @@ -1016,8 +1019,10 @@ { int i, nwr; - if (fb->flags & (B_WRERR|B_EOUT)) return -1; - if (nbyte == 0) return 0; + if (fb->flags & (B_WRERR | B_EOUT)) + return -1; + if (nbyte == 0) + return 0; if (!(fb->flags & B_WR)) { /* unbuffered write -- have to use bcwrite since we aren't taking care @@ -1031,7 +1036,7 @@ * current buffer with it in a single writev() */ if (fb->outcnt > 0 && nbyte + fb->outcnt >= fb->bufsiz) { - return large_write (fb, buf, nbyte); + return large_write(fb, buf, nbyte); } #endif @@ -1040,19 +1045,19 @@ * out */ nwr = 0; - while (fb->outcnt > 0) - { + while (fb->outcnt > 0) { /* can we accept some data? */ i = fb->bufsiz - fb->outcnt; - if (i > 0) - { - if (i > nbyte) i = nbyte; + if (i > 0) { + if (i > nbyte) + i = nbyte; memcpy(fb->outbase + fb->outcnt, buf, i); fb->outcnt += i; nbyte -= i; - buf = i + (const char *)buf; + buf = i + (const char *) buf; nwr += i; - if (nbyte == 0) return nwr; /* return if none left */ + if (nbyte == 0) + return nwr; /* return if none left */ } /* the buffer must be full */ @@ -1068,17 +1073,17 @@ fb->outcnt = 0; break; } - i = write_with_errors (fb, fb->outbase, fb->outcnt); + i = write_with_errors(fb, fb->outbase, fb->outcnt); if (i <= 0) { return nwr ? nwr : -1; } /* deal with a partial write */ - if (i < fb->outcnt) - { - int j, n=fb->outcnt; - unsigned char *x=fb->outbase; - for (j=i; j < n; j++) x[j-i] = x[j]; + if (i < fb->outcnt) { + int j, n = fb->outcnt; + unsigned char *x = fb->outbase; + for (j = i; j < n; j++) + x[j - i] = x[j]; fb->outcnt -= i; } else @@ -1092,14 +1097,13 @@ * use bcwrite() to do this for us, it will do the chunking so that * we don't have to dink around building a chunk in our own buffer. */ - while (nbyte >= fb->bufsiz) - { + while (nbyte >= fb->bufsiz) { i = bcwrite(fb, buf, nbyte); if (i <= 0) { return nwr ? nwr : -1; } - buf = i + (const char *)buf; + buf = i + (const char *) buf; nwr += i; nbyte -= i; @@ -1108,8 +1112,10 @@ } /* copy what's left to the file buffer */ fb->outcnt = 0; - if( fb->flags & B_CHUNK ) start_chunk( fb ); - if (nbyte > 0) memcpy(fb->outbase + fb->outcnt, buf, nbyte); + if (fb->flags & B_CHUNK) + start_chunk(fb); + if (nbyte > 0) + memcpy(fb->outbase + fb->outcnt, buf, nbyte); fb->outcnt += nbyte; nwr += nbyte; return nwr; @@ -1123,26 +1129,29 @@ { int i; - if (!(fb->flags & B_WR) || (fb->flags & B_EOUT)) return 0; + if (!(fb->flags & B_WR) || (fb->flags & B_EOUT)) + return 0; + + if (fb->flags & B_WRERR) + return -1; - if (fb->flags & B_WRERR) return -1; - - if (fb->flags & B_CHUNK) end_chunk(fb); - - while (fb->outcnt > 0) - { - i = write_with_errors (fb, fb->outbase, fb->outcnt); - if (i <= 0) return -1; + if (fb->flags & B_CHUNK) + end_chunk(fb); + + while (fb->outcnt > 0) { + i = write_with_errors(fb, fb->outbase, fb->outcnt); + if (i <= 0) + return -1; /* - * We should have written all the data, but if the fd was in a - * strange (non-blocking) mode, then we might not have done so. - */ - if (i < fb->outcnt) - { - int j, n=fb->outcnt; - unsigned char *x=fb->outbase; - for (j=i; j < n; j++) x[j-i] = x[j]; + * We should have written all the data, but if the fd was in a + * strange (non-blocking) mode, then we might not have done so. + */ + if (i < fb->outcnt) { + int j, n = fb->outcnt; + unsigned char *x = fb->outbase; + for (j = i; j < n; j++) + x[j - i] = x[j]; } fb->outcnt -= i; @@ -1166,22 +1175,27 @@ { int rc1, rc2, rc3; - if (fb->flags & B_WR) rc1 = bflush(fb); - else rc1 = 0; + if (fb->flags & B_WR) + rc1 = bflush(fb); + else + rc1 = 0; #ifdef WIN32 if (fb->flags & B_SOCKET) { rc2 = pclosesocket(fb->pool, fb->fd); if (fb->fd_in != fb->fd) { rc3 = pclosesocket(fb->pool, fb->fd_in); - } else { + } + else { rc3 = 0; } - } else { + } + else { #endif rc2 = pclosef(fb->pool, fb->fd); if (fb->fd_in != fb->fd) { rc3 = pclosef(fb->pool, fb->fd_in); - } else { + } + else { rc3 = 0; } #ifdef WIN32 @@ -1199,11 +1213,14 @@ #ifdef B_SFIO sfclose(fb->sf_in); sfclose(fb->sf_out); -#endif +#endif - if (rc1 != 0) return rc1; - else if (rc2 != 0) return rc2; - else return rc3; + if (rc1 != 0) + return rc1; + else if (rc2 != 0) + return rc2; + else + return rc3; } /* @@ -1211,30 +1228,31 @@ */ API_EXPORT(int) bputs(const char *x, BUFF *fb) { - int i, j=strlen(x); + int i, j = strlen(x); i = bwrite(fb, x, j); - if (i != j) return -1; - else return j; + if (i != j) + return -1; + else + return j; } /* * returns the number of bytes written or -1 on error */ -API_EXPORT_NONSTD(int) bvputs(BUFF *fb, ...) +API_EXPORT_NONSTD(int) bvputs(BUFF *fb,...) { int i, j, k; va_list v; const char *x; va_start(v, fb); - for (k=0;;) - { + for (k = 0;;) { x = va_arg(v, const char *); - if (x == NULL) break; + if (x == NULL) + break; j = strlen(x); i = bwrite(fb, x, j); - if (i != j) - { + if (i != j) { va_end(v); return -1; } @@ -1246,7 +1264,7 @@ return k; } -API_EXPORT(void) bonerror(BUFF *fb, void (*error)(BUFF *, int, void *), +API_EXPORT(void) bonerror(BUFF *fb, void (*error) (BUFF *, int, void *), void *data) { fb->error = error; 1.24 +25 -26 apachen/src/main/buff.h Index: buff.h =================================================================== RCS file: /export/home/cvs/apachen/src/main/buff.h,v retrieving revision 1.23 retrieving revision 1.24 diff -u -r1.23 -r1.24 --- buff.h 1997/08/08 08:00:19 1.23 +++ buff.h 1997/09/14 11:13:53 1.24 @@ -79,34 +79,33 @@ typedef struct buff_struct BUFF; -struct buff_struct -{ - int flags; /* flags */ - unsigned char *inptr; /* pointer to next location to read */ - int incnt; /* number of bytes left to read from input buffer; - * always 0 if had a read error */ - int outchunk; /* location of chunk header when chunking */ - int outchunk_header_size; /* how long the header is */ - int outcnt; /* number of byte put in output buffer */ +struct buff_struct { + int flags; /* flags */ + unsigned char *inptr; /* pointer to next location to read */ + int incnt; /* number of bytes left to read from input buffer; + * always 0 if had a read error */ + int outchunk; /* location of chunk header when chunking */ + int outchunk_header_size; /* how long the header is */ + int outcnt; /* number of byte put in output buffer */ unsigned char *inbase; unsigned char *outbase; int bufsiz; - void (*error)(BUFF *fb, int op, void *data); + void (*error) (BUFF *fb, int op, void *data); void *error_data; - long int bytes_sent; /* number of bytes actually written */ + long int bytes_sent; /* number of bytes actually written */ pool *pool; /* could also put pointers to the basic I/O routines here */ - int fd; /* the file descriptor */ - int fd_in; /* input file descriptor, if different */ + int fd; /* the file descriptor */ + int fd_in; /* input file descriptor, if different */ - /* transport handle, for RPC binding handle or some such */ - void *t_handle; + /* transport handle, for RPC binding handle or some such */ + void *t_handle; #ifdef B_SFIO - Sfio_t *sf_in; - Sfio_t *sf_out; + Sfio_t *sf_in; + Sfio_t *sf_out; #endif }; @@ -133,8 +132,8 @@ #define bgetflag(fb, flag) ((fb)->flags & (flag)) /* Error handling */ -API_EXPORT(void) bonerror(BUFF *fb, void (*error)(BUFF *, int, void *), - void *data); +API_EXPORT(void) bonerror(BUFF *fb, void (*error) (BUFF *, int, void *), + void *data); /* I/O */ API_EXPORT(int) bread(BUFF *fb, void *buf, int nbyte); @@ -144,9 +143,9 @@ API_EXPORT(int) bwrite(BUFF *fb, const void *buf, int nbyte); API_EXPORT(int) bflush(BUFF *fb); API_EXPORT(int) bputs(const char *x, BUFF *fb); -API_EXPORT(int) bvputs(BUFF *fb, ...); -API_EXPORT_NONSTD(int) bprintf(BUFF *fb,const char *fmt,...); -API_EXPORT_NONSTD(int) vbprintf(BUFF *fb,const char *fmt,va_list vlist); +API_EXPORT(int) bvputs(BUFF *fb,...); +API_EXPORT_NONSTD(int) bprintf(BUFF *fb, const char *fmt,...); +API_EXPORT_NONSTD(int) vbprintf(BUFF *fb, const char *fmt, va_list vlist); /* Internal routines */ API_EXPORT(int) bflsbuf(int c, BUFF *fb); @@ -159,9 +158,9 @@ (fb)->outcnt == (fb)->bufsiz) ? bflsbuf(c, (fb)) : \ ((fb)->outbase[(fb)->outcnt++] = (c), 0)) -API_EXPORT(int) spawn_child_err_buff (pool *, int (*)(void *), void *, - enum kill_conditions, BUFF **pipe_in, BUFF **pipe_out, - BUFF **pipe_err); +API_EXPORT(int) spawn_child_err_buff(pool *, int (*)(void *), void *, + enum kill_conditions, BUFF **pipe_in, BUFF **pipe_out, + BUFF **pipe_err); /* enable non-blocking operations */ API_EXPORT(int) bnonblock(BUFF *fb, int direction); @@ -169,4 +168,4 @@ API_EXPORT(int) bfileno(BUFF *fb, int direction); /* bflush() if a read now would block, but don't actually read anything */ -API_EXPORT(void) bhalfduplex (BUFF *fb); +API_EXPORT(void) bhalfduplex(BUFF *fb);