home *** CD-ROM | disk | FTP | other *** search
/ Oracle Video Server 3.0.3.1 / OVS_3031_NT.iso / win32 / sqlnet / net23 / client / tnsapifc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-08  |  4.8 KB  |  201 lines

  1. /*
  2.    $Header: /netrcs/RCS/oracle/network/tns/tnsapi/RCS/tnsapifc.c,v 1.2 1995/09/11 21:49:17 mhill Exp $
  3. */
  4.  
  5. /* Copyright (c) 1995 by Oracle Corporation.  All rights reserved. */
  6.  
  7. /*
  8. NAME
  9.   tnsapifc - SQL*Net Open - finger client demo
  10. */
  11.  
  12. /* This demo shows how the typical BSD "finger" program could be 
  13.    converted to work with the SQL*Net Open API.  This client program has
  14.    been tested on Solaris and Windows NT, and is probably portable to 
  15.    other platforms as well.
  16.  
  17.    This is meant to be used with tnsapifd, the finger server ("daemon") */
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <tnsapi.h>
  23.  
  24. static int help_wanted();
  25. static char *build_arg_string();
  26. static void free_arg_string();
  27.  
  28. int main(argc, argv)
  29. int argc;
  30. char **argv;
  31. {
  32.     void *handle = (void *)0;
  33.     const char *name;
  34.     int rc = 0;
  35.     char *command;
  36.     int length, sent, rcvd;
  37.     char reply[256];
  38.     char *work;
  39.     
  40.     if (help_wanted(argc, argv))
  41.     {
  42.         (void) printf("sfinger - SQL*Net Open/finger client\n");
  43.     (void) printf("usage: sfinger <name> <args>...\n");
  44.     (void) printf("       <name> is the TNS service name of the finger ");
  45.     (void) printf("daemon\n");
  46.     (void) printf("       <args> are additional arguments to send to ");
  47.     (void) printf("the remote finger command\n");
  48.     exit(1);
  49.     }
  50.     else
  51.     {
  52.     /* argv[1] must be the name of the finger daemon */
  53.     name = argv[1];
  54.     
  55.     rc = tnsopen(&handle, name);
  56.     if (rc) goto error;
  57.  
  58.     /* primitive marshalling of arguments.  cram all the arguments
  59.        separated by spaces into a long string, and send it to the 
  60.        other side.  First two bytes of the string will be the number
  61.        1, followed by the number of bytes in the string */
  62.     command = build_arg_string(argc, argv);
  63.  
  64.     length = strlen(command + 4) + 4 + 1; /* writing the null */
  65.     work = command;
  66.     
  67.     while(length)
  68.     {
  69.         sent = length;
  70.         rc = tnssend(handle, (void *)work, &sent);
  71.         if (rc) goto error;
  72.         
  73.         length -= sent;
  74.         work += sent;
  75.     }
  76.     
  77.     free_arg_string(command);
  78.     
  79.     /* now read until end of file, and write whatever I get to output.
  80.        Note that if the finger program on the server side sends output
  81.        that has, for instance, only newlines, where my client expects
  82.        carriage returns also, this will look weird (but this is only an 
  83.        example.) */
  84.     
  85.     length = sizeof(reply);
  86.     
  87.     while(!rc)
  88.     {
  89.         rcvd = length;
  90.         rc = tnsrecv(handle, (void *)reply, &rcvd);
  91.         write(1, reply, rcvd);
  92.     }
  93.  
  94.     if (rc == RECVFAIL_TNSAPIE) /* receive failed - presumably eof */
  95.     {
  96.         rc = tnsclose(&handle);
  97.         if (!rc) exit(0);
  98.     }
  99.         
  100.       error:
  101.     printf("error code %d\n", rc);
  102.     rc = 0;
  103.     
  104.     rc = tnsclose(&handle);
  105.     if (rc)
  106.     {
  107.         printf("error on tnsclose is %d\n", rc);
  108.     }
  109.     
  110.     exit(0);
  111.     }
  112. }
  113.  
  114.  
  115. /* This function returns 0 if no help is wanted, 1 otherwise - judging from 
  116.    the contents of the argument list (user either provided -? or /? as 
  117.    one of the arguments, or didn't provide any arguments) */
  118. static int help_wanted(hargc, hargv)
  119. int hargc;
  120. char **hargv;
  121. {
  122.     int a = hargc;
  123.     
  124.     if (hargc < 2) return(1);
  125.     
  126.     for (; a > 1; a--)        /* ignore the first argument (program name) */
  127.     {
  128.     if (!strcmp(hargv[a-1], "/?") || !strcmp(hargv[a-1], "-?")) return(1);
  129.     }
  130.     
  131.     return(0);
  132. }
  133.  
  134. /* build an argument string in the following format:
  135.    2 bytes - local representation of the number 1
  136.    2 bytes - number of bytes in the argument string
  137.    remaining bytes - argument list separated by spaces
  138.    start with whatever is in argv[2] (beginning of "additional arguments") */
  139. static char *build_arg_string(bargc, bargv)
  140. int bargc;
  141. char **bargv;
  142. {
  143.     short alloc_size = 0;
  144.     char *result;
  145.     short one = 1;
  146.     char *work;
  147.     int i;
  148.     
  149.     /* calculate size of buffer to allocate */
  150.     if (bargc > 2)
  151.     {
  152.     i = bargc;
  153.     for (; i > 2; i--)
  154.     {
  155.         /* size of argument plus a space (or null for the end) */
  156.         alloc_size += (strlen(bargv[i-1]) + 1);
  157.     }
  158.     }
  159.     else
  160.     {
  161.     alloc_size = 1;        /* space for null */
  162.     }
  163.     
  164.     result = (char *)malloc(alloc_size + 4);
  165.     
  166.     memcpy(result, &one, 2);
  167.     memcpy(result + 2, &alloc_size, 2);
  168.     
  169.     if (alloc_size == 1) 
  170.     {
  171.     *(result + 4) = '\0';
  172.     return(result); /* no arguments, just return */
  173.     }
  174.     
  175.     /* now copy in the arguments */
  176.     work = result + 4;
  177.     
  178.     for (i = 2; i < bargc; i++)
  179.     {
  180.     int copied;
  181.     
  182.     copied = strlen(bargv[i]);
  183.     memcpy(work, bargv[i], copied);
  184.     *(work + copied) = ' ';
  185.     work += (copied + 1);
  186.     }
  187.     
  188.     /* last time through, convert the previous space to a null */
  189.     *(--work) = '\0';
  190.     return(result);
  191. }
  192.  
  193. static void free_arg_string(string)
  194. char *string;
  195. {
  196.     free(string);
  197.     return;
  198. }
  199.  
  200.     
  201.