17

mb_sio.c

  1. #include "mb_sio.h"
  2.  
  3. #include "mbdef.h"
  4.  
  5. #if 0
  6. #include "ctk/dump.h"
  7. #ifndef MIN
  8. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  9. #endif
  10. #ifndef MAX
  11. #define MAX(x, y) ((x) < (y) ? (y) : (x))
  12. #endif
  13. #endif
  14.  
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17.  
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21.  
  22. #include <ctype.h>
  23. #include <errno.h>
  24.  
  25. int s_read( int s, u_char *buf, int len ) {
  26.     register u_char *p;
  27.     register int r, l;
  28.  
  29.     for ( p = buf, l = len; l > 0; l -= r, p += r ) {
  30.         r = read( s, p, l );
  31. #if 0
  32.         fprintf( stdout, "s_read=%i\n", r );
  33. #endif
  34.         switch ( r ) {
  35.         case 0:
  36.             /* closed */
  37.             close( s );
  38.             return -1;
  39.  
  40.         case -1:
  41.             switch ( errno ) {
  42.             case EINTR:
  43.                 r = 0;
  44.                 break;
  45.             default:
  46.                 close( s );
  47.                 return -1;
  48.             }
  49.  
  50.         default:    /* ok */
  51. #if 0
  52.             dump( p, MIN( r, 16 ), stdout );
  53. #endif
  54.             break;
  55.         }
  56.     }
  57.  
  58.     return len;
  59. }
  60.  
  61. int s_write( int s, u_char *buf, int len ) {
  62.     register u_char *p;
  63.     register int w, l;
  64.  
  65.     for ( p = buf, l = len; l > 0; l -= w, p += w ) {
  66.         w = write( s, p, l );
  67. #if 0
  68.         fprintf( stdout, "s_write=%i\n", w );
  69. #endif
  70.         if ( w < 0 )
  71.             switch ( errno ) {
  72.             case EINTR:
  73.                 w = 0;
  74.                 break;
  75.             default:
  76.                 close( s );
  77.                 return -1;
  78.             }
  79. #if 0
  80.         dump( p, MIN( w, 16 ), stdout );
  81. #endif
  82.     }
  83.  
  84.     return len;
  85. }
  86.  
  87. int s_getc( int s ) {
  88.     u_char c;
  89.  
  90.     return s_read( s, &c, 1 ) < 0 ? -1 : c;
  91. }
  92.  
  93. int s_getuint( int s ) {
  94.     char buf[ 32 ];
  95.     u_int u = 0;
  96.  
  97.     char *p;
  98.     int c;
  99.  
  100.     for ( p = buf; p < buf + sizeof( buf ); p++ ) {
  101.         if ( (c = s_getc( s )) < 0 )
  102.             /* read error */
  103.             return -1;
  104.  
  105.         if ( !isdigit( *p = c )) {
  106.             if ( c == '\0' ) {
  107.                 sscanf( buf, "%u", &u );
  108.                 return u;
  109.             }
  110.             /* error */
  111.             break;
  112.         }
  113.     }
  114.     /* protocol error */
  115.     close( s );
  116.     return -1;
  117. }
  118.  
  119. char *s_getident( int s ) {
  120.     char *p;
  121.     int c;
  122.  
  123.     static char buf[ MB_MAXIDENTSIZE ];
  124.  
  125.     for ( p = buf; p < buf + sizeof( buf ); p++ ) {
  126.         if ( (c = s_getc( s )) < 0 )
  127.             /* read error */
  128.             return 0;
  129.  
  130.         if ( !isprint( *p = c )) {
  131.             if ( c == '\0' )
  132.                 return buf;
  133.             /* error */
  134.             break;
  135.         }
  136.     }
  137.     /* protocol error */
  138.     close( s );
  139.     return 0;
  140. }

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].