home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / CEREBRUM / WAT9609.ZIP / APPS / COOKIE.C next >
Text File  |  1994-11-28  |  2KB  |  123 lines

  1. /******************************************************************************
  2.     COOKIE - read and print a witty saying from internet
  3.  
  4.     By: Jim Martin                      Internet: jim@dorm.rutgers.edu
  5.         Dormitory Networking Project    UUCP: {backbone}!rutgers!jim
  6.         Rutgers University              Phone: (908) 932-3719
  7.  
  8.     Uses the Waterloo TCP kernal.
  9. ******************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <tcp.h>
  14.  
  15.  
  16. #define COOKIE_PORT 17
  17.  
  18. int cookie( longword host )
  19. {
  20.     static udp_Socket sock;
  21.         udp_Socket *s;
  22.         char buffer[ 2049 ];
  23.         int status;
  24.     int i;
  25.     int fortunelen;
  26.  
  27.         s = &sock;
  28.         status = 0;
  29.  
  30.     if ( host != 0 )
  31.     {
  32.         status=udp_open( s, 0, host, COOKIE_PORT, NULL );
  33.     }
  34.     else
  35.     {
  36.         if (_last_cookie == 0)
  37.         {
  38.             puts("Sorry, I can't seem to remember where my cookie jars are. ");
  39.             puts("Could you tell me where one is? (Hint: host on the comand line)");
  40.             exit(3);
  41.         }
  42.         for (i=0; i < _last_cookie; i++)
  43.         {
  44.             if ( (status=udp_open( s, 0, _cookie[i], COOKIE_PORT, NULL )) != 0 )
  45.             {
  46.                 break;
  47.             }
  48.         }
  49.     }
  50.  
  51.     if ( status == 0 )
  52.     {
  53.         puts("None of the cookie jars are open!");
  54.         return( 1 );
  55.     }
  56.  
  57.     sock_write( s, "\n", 1 );
  58.  
  59.  
  60.         while ( 1 )
  61.     {
  62.         sock_tick( s, &status );
  63.  
  64.         if (sock_dataready( s ) )
  65.         {
  66.             fortunelen=sock_fastread( s, buffer, sizeof( buffer ));
  67.             buffer[fortunelen]='\0';
  68.             printf("%s\n", buffer);
  69.             sock_close(s);
  70.             return(0);
  71.         }
  72.         }
  73.  
  74. sock_err:
  75.     switch (status)
  76.     {
  77.         case 1 : /* foreign host closed */
  78.              return(0);
  79.         case -1: /* timeout */
  80.              printf("\nConnection timed out!");
  81.              printf("ERROR: %s\n\r", sockerr( s ));
  82.              return(1);
  83.         }
  84.         return (0);
  85. }
  86.  
  87.  
  88. int main(int argc, char **argv )
  89. {
  90.     int status;
  91.     longword host;
  92.  
  93.     if (argc > 2)
  94.     {
  95.         puts("Quote of the Day (Cookie) - retrieves a witty message");
  96.         puts("Usage: COOKIE [server]");
  97.         exit( 3 );
  98.     }
  99.  
  100.     sock_init();
  101.  
  102.     if ( argc == 1)
  103.     {
  104.         status = cookie ((longword) NULL);
  105.     }
  106.     else
  107.     {
  108.         if ( (host = resolve( argv[1])) != 0uL )
  109.         {
  110.             status = cookie( host );
  111.         }
  112.         else
  113.         {
  114.             printf("Could not resolve host '%s'\n", argv[1]);
  115.             status = 3;
  116.         }
  117.     }
  118.  
  119.     exit( status );
  120.         return (0);  /* not reached */
  121. }
  122.  
  123.