home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / MISC / mtp.shar / mtp.c < prev    next >
C/C++ Source or Header  |  2009-11-06  |  6KB  |  305 lines

  1. /* mtp.c, Version 1.3, Created 3/7/91 */
  2. /* Programme to transfer modules to OS-9 system */
  3. /* Dr Alan M. McIvor, BP Sunbury Research Centre */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <strings.h>
  11. #include <errno.h>
  12. #include <ctype.h>
  13. #include <sys/param.h>
  14.  
  15. #include "mtp.h"
  16.  
  17. #define USAGE "USAGE: mtp remote-host\n"
  18. #define BUF_LENGTH 256
  19.  
  20. main(argc, argv)
  21.      int argc;
  22.      char **argv;
  23. {
  24.   int s;            /* socket */
  25.   int open_socket(), close_socket();
  26.   int get_commands();
  27.  
  28.   /* Process command line */
  29.   while ((--argc > 0) && ((*++argv)[0] == '-'))
  30.     switch((*argv)[1])
  31.       {
  32.     case 'U':        /* usage */
  33.     case '?':
  34.       fprintf(stderr, USAGE);
  35.       exit(1);
  36.       }
  37.   if (argc != 1)
  38.     {
  39.       fprintf(stderr, USAGE);
  40.       exit(2);
  41.     }
  42.   
  43.   if ((s = open_socket(argv[0])) == -1)
  44.     {
  45.       fprintf(stderr, "mtp: Couldn't open socket\n");
  46.       exit(3);
  47.     }
  48.  
  49.   /* interact with user */
  50.   if (get_commands(s) == 0)
  51.     {
  52.       if (close_socket(s) == -1)
  53.     {
  54.       printf("Shutdown and close failed\n");
  55.       exit(2);
  56.     }
  57.     }
  58.   else
  59.     {
  60.       /* panic shutdown */
  61.       shutdown(s, 2);
  62.       close(s);
  63.     }
  64.       
  65.   exit(0);
  66. }
  67.    
  68.  
  69. int open_socket(remote_host)
  70.      char *remote_host;
  71. {
  72.   /* Opens up a socket connected to the MTP_PORT on remote_host. It
  73.      returns the socket descriptor if successful and -1 if error. */
  74.  
  75.   int s;            /* socket */
  76.   struct sockaddr_in sin;    /* socket name */
  77.   struct sockaddr_in sname;
  78.   struct protoent *tcp;
  79.   struct hostent *hp;
  80.   char c;
  81.   int e;
  82.  
  83.   if ((tcp = getprotobyname("tcp")) == NULL)
  84.     return(-1);
  85.  
  86.   if ((hp = gethostbyname(remote_host)) == NULL)
  87.     return(-1);
  88.  
  89.   bzero((char *)&sin, sizeof(sin));
  90.   bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
  91.   sin.sin_family = hp->h_addrtype;
  92.   sin.sin_port = MTP_PORT;
  93.  
  94.   s = socket(AF_INET, SOCK_STREAM, tcp->p_proto);
  95.   if (s == -1)
  96.     return(-1);
  97.  
  98.   sname.sin_family = AF_INET;
  99.   sname.sin_addr.s_addr = INADDR_ANY;
  100.   sname.sin_port = 0;
  101.   bind(s, (char *)&sname, sizeof(sname));
  102.  
  103.   if (connect(s, (char *)&sin, sizeof(sin)) == -1)
  104.     {
  105.       shutdown(s, 2);
  106.       close(s);
  107.       return(-1);
  108.     }
  109.       
  110.   if (s == -1)
  111.     {
  112.       fprintf(stderr, "mtp: Couldn't open socket to mtp port on %s\n",
  113.           remote_host);
  114.       return(-1);
  115.     }
  116.   else
  117.     printf("Connected to %s.\n", remote_host);
  118.  
  119.   /* get acknowledge from server */
  120.   if (((e = read(s, &c, 1)) <= 0) || (c != ACKNOWLEDGE))
  121.     {
  122.       perror("read_ack");
  123.       fprintf(stderr, "mtp: e = %d\n", e);
  124.       fprintf(stderr, "mtp: c = %c\n", c);
  125.       fprintf(stderr, "mtp: Couldn't read ACKNOWLEDGE from server.\n");
  126.       return(-1);
  127.     }
  128.   else
  129.     printf("OS-9 mtp server ready\n");
  130.  
  131.   return(s);
  132. }
  133.  
  134.  
  135. int close_socket(s)
  136.      int s;
  137. {
  138.   /* shutdowns and closes socket s */
  139.   /* returns -1 on error and 0 on success */
  140.  
  141.   /* Shut down socket */
  142.   if (shutdown(s, 2) == -1)
  143.     return(-1);
  144.   
  145.   if (close(s) == -1)
  146.     return(-1);
  147.  
  148.   return(0);
  149. }
  150.  
  151.  
  152. int get_commands(s)
  153.      int s;
  154. {
  155.   char buf[BUF_LENGTH];
  156.   char oldbuf[BUF_LENGTH];
  157.   int buflen;
  158.   char command[BUF_LENGTH];
  159.   char *argument;
  160.   char *pc;
  161.   int nc;
  162.   int command_index();
  163.   int put_module();
  164.   int get_module();
  165.   int signal_bye();
  166.   int change_cwd();
  167.   int report_pwd();
  168.   int report_ls();
  169.  
  170.   buf[0] = '\0';
  171.  
  172.   do
  173.     {
  174.       /* write prompt */
  175.       printf("%s", MTP_PROMPT);
  176.  
  177.       strcpy(oldbuf, buf);    /* preserve last command */
  178.       gets(buf);        /* get a command string from user */
  179.       if (strcmp(buf, "!!") == 0)
  180.     {
  181.       strcpy(buf, oldbuf);
  182.       printf("%s\n", buf);
  183.     }
  184.  
  185.       buflen = strlen(buf);
  186.  
  187.       /* skip over white space */
  188.       for (nc = 0; nc < buflen; nc++)
  189.     if (!isspace(buf[nc]))
  190.       break;
  191.  
  192.       /* get command string */
  193.       for (pc = command; nc < buflen; nc++)
  194.     if (isspace(buf[nc]))
  195.       break;
  196.         else
  197.       *pc++ = buf[nc];
  198.       *pc = '\0';
  199.       
  200.       /* skip over white space */
  201.       for (; nc < buflen; nc++)
  202.     if (!isspace(buf[nc]))
  203.       break;
  204.  
  205.       /* get argument string */
  206.       argument = buf + nc;
  207.  
  208. /*
  209.       printf("\t\t\t command  = %s\n", command);
  210.       printf("\t\t\t argument = %s\n", argument);
  211.       printf("\t\t\t index    = %d\n", command_index(command));
  212. */
  213.       /* find command number and act appropiately */
  214.       switch(command_index(command))
  215.     {
  216.       case BYE:
  217.         nc = signal_bye(s);
  218.         printf("Goodbye.\n");
  219.         break;
  220.       case PUTMOD:
  221.         nc = put_module(s, argument, PUT_MODULE);
  222.         break;
  223.       case UPUTMOD:
  224.         nc = put_module(s, argument, UPUT_MODULE);
  225.         break;
  226.       case GETMOD:
  227.         nc = get_module(s, argument);
  228.         break;
  229.       case CHDIR:
  230.         nc = change_cwd(argument);
  231.         break;
  232.       case REPPWD:
  233.         nc = report_pwd();
  234.         break;
  235.       case REPLS:
  236.         nc = report_ls(argument);
  237.         break;
  238.       case UNKNOWN:
  239.       default:
  240.         fprintf(stderr, "? Unknown command\n");
  241.         nc = 1;
  242.         break;
  243.     }
  244.     }
  245.   while (nc > 0);
  246.  
  247.   return(nc);
  248. }  
  249.  
  250.  
  251.  
  252. int change_cwd(new_path)
  253.      char *new_path;
  254. {
  255.   char pathname[MAXPATHLEN];
  256.  
  257.   if (strlen(new_path) == 0)
  258.     strcpy(pathname, getenv("HOME"));
  259.   else if (new_path[0] == '~')
  260.     {
  261.       if (new_path[1] == '/')
  262.     {
  263.       strcpy(pathname, getenv("HOME"));
  264.       strcat(pathname, (new_path + 1));
  265.     }
  266.       else
  267.     {
  268.       strcpy(pathname, getenv("HOME"));
  269.       strcat(pathname, "/../"); /* not quite right but near enough */
  270.       strcat(pathname, (new_path + 1));
  271.     }
  272.     }
  273.   else
  274.     strcpy(pathname, new_path);
  275.  
  276.   if (chdir(pathname) == -1)
  277.     fprintf(stderr, "mtp: Could not cd to %s\n", pathname);
  278.   else
  279.     printf("CWD now %s\n", getwd(pathname));
  280.  
  281.   return(1);
  282. }
  283.  
  284.   
  285. int report_pwd()
  286. {
  287.   char pathname[MAXPATHLEN];
  288.  
  289.   printf("%s\n", getwd(pathname));
  290.  
  291.   return(1);
  292. }
  293.  
  294.   
  295. int report_ls(argument)
  296.      char *argument;
  297. {
  298.   char string[BUF_LENGTH];
  299.  
  300.   sprintf(string, "ls %s", argument);
  301.   system(string);
  302.  
  303.   return(1);
  304. }
  305.