18
mb_sio.c
- #include "mb_sio.h"
- #include "mbdef.h"
- #if 0
- #include "ctk/dump.h"
- #ifndef MIN
- #define MIN(x, y) ((x) < (y) ? (x) : (y))
- #endif
- #ifndef MAX
- #define MAX(x, y) ((x) < (y) ? (y) : (x))
- #endif
- #endif
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <errno.h>
- int s_read( int s, u_char *buf, int len ) {
- register u_char *p;
- register int r, l;
- for ( p = buf, l = len; l > 0; l -= r, p += r ) {
- r = read( s, p, l );
- #if 0
- fprintf( stdout, "s_read=%i\n", r );
- #endif
- switch ( r ) {
- case 0:
- /* closed */
- close( s );
- return -1;
- case -1:
- switch ( errno ) {
- case EINTR:
- r = 0;
- break;
- default:
- close( s );
- return -1;
- }
- default: /* ok */
- #if 0
- dump( p, MIN( r, 16 ), stdout );
- #endif
- break;
- }
- }
- return len;
- }
- int s_write( int s, u_char *buf, int len ) {
- register u_char *p;
- register int w, l;
- for ( p = buf, l = len; l > 0; l -= w, p += w ) {
- w = write( s, p, l );
- #if 0
- fprintf( stdout, "s_write=%i\n", w );
- #endif
- if ( w < 0 )
- switch ( errno ) {
- case EINTR:
- w = 0;
- break;
- default:
- close( s );
- return -1;
- }
- #if 0
- dump( p, MIN( w, 16 ), stdout );
- #endif
- }
- return len;
- }
- int s_getc( int s ) {
- u_char c;
- return s_read( s, &c, 1 ) < 0 ? -1 : c;
- }
- int s_getuint( int s ) {
- char buf[ 32 ];
- u_int u = 0;
- char *p;
- int c;
- for ( p = buf; p < buf + sizeof( buf ); p++ ) {
- if ( (c = s_getc( s )) < 0 )
- /* read error */
- return -1;
- if ( !isdigit( *p = c )) {
- if ( c == '\0' ) {
- sscanf( buf, "%u", &u );
- return u;
- }
- /* error */
- break;
- }
- }
- /* protocol error */
- close( s );
- return -1;
- }
- char *s_getident( int s ) {
- char *p;
- int c;
- static char buf[ MB_MAXIDENTSIZE ];
- for ( p = buf; p < buf + sizeof( buf ); p++ ) {
- if ( (c = s_getc( s )) < 0 )
- /* read error */
- return 0;
- if ( !isprint( *p = c )) {
- if ( c == '\0' )
- return buf;
- /* error */
- break;
- }
- }
- /* protocol error */
- close( s );
- return 0;
- }
Commentaires