home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume21 / coda / part01 / libxen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-08  |  3.9 KB  |  219 lines

  1. /*
  2. **  Copyright 1989 BBN Systems and Technologies Corporation.
  3. **  All Rights Reserved.
  4. **  This is free software, and may be distributed under the terms of the
  5. **  GNU Public License; see the file COPYING for more details.
  6. **
  7. **  Client library routines for XENIX with Excelan TCP.
  8. */
  9. #include "client.h"
  10. #include <sys/types.h>
  11. #include <sgtty.h>
  12. #include <netdb.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #ifdef    RCSID
  16. static char     RCS[] =
  17.     "$Header: libxen.c,v 2.0 90/03/23 14:41:47 rsalz Exp $";
  18. #endif    /* RCSID */
  19.  
  20.  
  21. STATIC FILE    *WriteStream;        /* Something to write to    */
  22. STATIC FILE    *ReadStream;        /* Something to read from    */
  23.  
  24.  
  25. /*
  26. **  Make a directory.
  27. */
  28. int
  29. mkdir(dir, mode)
  30.     char    *dir;
  31.     int         mode;
  32. {
  33.     char    buff[SIZE];
  34.  
  35.     (void)sprintf(buff, "exec mkdir %s\n", dir);
  36.     if (system(buff))
  37.     return -1;
  38.     (void)sprintf(buff, "exec chmod %d %s", mode, dir);
  39.     return system(buff) ? -1 : 0;
  40. }
  41.  
  42.  
  43. /*
  44. **  Rename a file.
  45. */
  46. int
  47. rename(from, to)
  48.     char    *from;
  49.     char    *to;
  50. {
  51.     (void)unlink(to);
  52.     return link(from, to) < 0 ? -1 : unlink(from);
  53. }
  54.  
  55.  
  56. /*
  57. **  Do the grundge work of getting us a socket.
  58. */
  59. STATIC int
  60. GetSocket(machine, port)
  61.     char         *machine;
  62.     int              port;
  63. {
  64.     REGISTER int      s;
  65.     struct hostent     *hp;
  66.     struct sockaddr_in      sin;
  67.  
  68.     /* Set up the socket. */
  69.     (void)memset((char *)&sin, '\0', sizeof sin);
  70.     sin.sin_family = AF_INET;
  71.     sin.sin_port = htons(port);
  72.     if ((s = socket(SOCK_STREAM, (struct sockproto *)NULL, &sin, 0)) < 0) {
  73.     experror("socket");
  74.     return -1;
  75.     }
  76.  
  77.     /* Get the address of the server. */
  78.     if ((hp = gethostbyname(machine)) == NULL) {
  79.     (void)fprintf(stderr, "%s: Unknown host.\n", machine);
  80.     return -1;
  81.     }
  82.     (void)memcpy((char *)&sin.sin_addr, hp->h_addr, hp->h_length);
  83.  
  84.     /* Connect to the server. */
  85.     if (connect(s, (struct sockaddr *)&sin) < 0) {
  86.     experror("connect");
  87.     (void)close(s);
  88.     return -1;
  89.     }
  90.  
  91.     return s;
  92. }
  93.  
  94.  
  95. /*
  96. **  Open connection to server, return FALSE on error.
  97. */
  98. int
  99. SRVopen(machine, port)
  100.     char    *machine;
  101.     int         port;
  102. {
  103.     int         i;
  104.  
  105.     if ((i = GetSocket(machine, port)) < 0)
  106.     return FALSE;
  107.  
  108.     if ((ReadStream = fdopen(i, "r")) == NULL) {
  109.     perror("OpenServerChannel: fdopen #1");
  110.     return FALSE;
  111.     }
  112.  
  113.     if ((i = dup(i)) < 0) {
  114.     perror("OpenServerChannel: dup");
  115.     return FALSE;
  116.     }
  117.     if ((WriteStream = fdopen(i, "w")) == NULL) {
  118.     perror("OpenServerChannel: fdopen #2");
  119.     (void)fclose(ReadStream);
  120.     return FALSE;
  121.     }
  122.     return TRUE;
  123. }
  124.  
  125.  
  126. /*
  127. **  Send a QUIT and shut down.
  128. */
  129. void
  130. SRVclose()
  131. {
  132.     if (WriteStream == NULL || ReadStream == NULL)
  133.     return;
  134.  
  135.     SRVput("QUIT");
  136.     (void)fclose(WriteStream);
  137.     (void)fclose(ReadStream);
  138. }
  139.  
  140.  
  141. /*
  142. **  Send a line to the server.
  143. */
  144. void
  145. SRVput(p)
  146.     char    *p;
  147. {
  148.     if (SRVtrace)
  149.     (void)printf(">>>%s\n", p);
  150.     (void)fprintf(WriteStream, "%s\r\n", p);
  151.     (void)fflush(WriteStream);
  152. }
  153.  
  154.  
  155. /*
  156. **  Get a line of text from the server.  Strip end-of-line characters.
  157. */
  158. int
  159. SRVget(buff, size)
  160.     char        *buff;
  161.     int             size;
  162. {
  163.     REGISTER char    *p;
  164.  
  165.     while (fgets(buff, size, ReadStream)) {
  166.     if ((p = strchr(buff, '\r')) || (p = strchr(buff, '\n')))
  167.         *p = '\0';
  168.     if (SRVtrace)
  169.         (void)printf("<<<%s\n", buff);
  170.     if (strncmp(buff, "INF ", 4))
  171.         return TRUE;
  172.     (void)printf("Server message:\n\t%s\n", &buff[4]);
  173.     (void)fflush(stdout);
  174.     }
  175.     return FALSE;
  176. }
  177.  
  178.  
  179. /*
  180. **  Get a character from the server.
  181. */
  182. int
  183. SRVcget()
  184. {
  185.     return getc(ReadStream);
  186. }
  187.  
  188.  
  189. /*
  190. **  Get a password, without echoing it.
  191. */
  192. void
  193. GetPassword(buff, size)
  194.     char        *buff;
  195.     int             size;
  196. {
  197.     struct sgttyb     Modes;
  198.     char        *p;
  199.     int             ok;
  200.     int             flags;
  201.  
  202.     if (ok = ioctl(fileno(stdin), TIOCGETP, &Modes) >= 0) {
  203.     flags = Modes.sg_flags;
  204.     Modes.sg_flags &= ~ECHO;
  205.     (void)ioctl(fileno(stdin), TIOCSETP, &Modes);
  206.     }
  207.  
  208.     if (fgets(buff, size, stdin) == NULL)
  209.     Fatal("No password!?");
  210.     if (p = strchr(buff, '\n'))
  211.     *p = '\0';
  212.  
  213.     if (ok) {
  214.     Modes.sg_flags = flags;
  215.     (void)ioctl(fileno(stdin), TIOCSETP, &Modes);
  216.     (void)printf("\n");
  217.     }
  218. }
  219.