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 >
Wrap
C/C++ Source or Header
|
2009-11-06
|
6KB
|
305 lines
/* mtp.c, Version 1.3, Created 3/7/91 */
/* Programme to transfer modules to OS-9 system */
/* Dr Alan M. McIvor, BP Sunbury Research Centre */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <strings.h>
#include <errno.h>
#include <ctype.h>
#include <sys/param.h>
#include "mtp.h"
#define USAGE "USAGE: mtp remote-host\n"
#define BUF_LENGTH 256
main(argc, argv)
int argc;
char **argv;
{
int s; /* socket */
int open_socket(), close_socket();
int get_commands();
/* Process command line */
while ((--argc > 0) && ((*++argv)[0] == '-'))
switch((*argv)[1])
{
case 'U': /* usage */
case '?':
fprintf(stderr, USAGE);
exit(1);
}
if (argc != 1)
{
fprintf(stderr, USAGE);
exit(2);
}
if ((s = open_socket(argv[0])) == -1)
{
fprintf(stderr, "mtp: Couldn't open socket\n");
exit(3);
}
/* interact with user */
if (get_commands(s) == 0)
{
if (close_socket(s) == -1)
{
printf("Shutdown and close failed\n");
exit(2);
}
}
else
{
/* panic shutdown */
shutdown(s, 2);
close(s);
}
exit(0);
}
int open_socket(remote_host)
char *remote_host;
{
/* Opens up a socket connected to the MTP_PORT on remote_host. It
returns the socket descriptor if successful and -1 if error. */
int s; /* socket */
struct sockaddr_in sin; /* socket name */
struct sockaddr_in sname;
struct protoent *tcp;
struct hostent *hp;
char c;
int e;
if ((tcp = getprotobyname("tcp")) == NULL)
return(-1);
if ((hp = gethostbyname(remote_host)) == NULL)
return(-1);
bzero((char *)&sin, sizeof(sin));
bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
sin.sin_family = hp->h_addrtype;
sin.sin_port = MTP_PORT;
s = socket(AF_INET, SOCK_STREAM, tcp->p_proto);
if (s == -1)
return(-1);
sname.sin_family = AF_INET;
sname.sin_addr.s_addr = INADDR_ANY;
sname.sin_port = 0;
bind(s, (char *)&sname, sizeof(sname));
if (connect(s, (char *)&sin, sizeof(sin)) == -1)
{
shutdown(s, 2);
close(s);
return(-1);
}
if (s == -1)
{
fprintf(stderr, "mtp: Couldn't open socket to mtp port on %s\n",
remote_host);
return(-1);
}
else
printf("Connected to %s.\n", remote_host);
/* get acknowledge from server */
if (((e = read(s, &c, 1)) <= 0) || (c != ACKNOWLEDGE))
{
perror("read_ack");
fprintf(stderr, "mtp: e = %d\n", e);
fprintf(stderr, "mtp: c = %c\n", c);
fprintf(stderr, "mtp: Couldn't read ACKNOWLEDGE from server.\n");
return(-1);
}
else
printf("OS-9 mtp server ready\n");
return(s);
}
int close_socket(s)
int s;
{
/* shutdowns and closes socket s */
/* returns -1 on error and 0 on success */
/* Shut down socket */
if (shutdown(s, 2) == -1)
return(-1);
if (close(s) == -1)
return(-1);
return(0);
}
int get_commands(s)
int s;
{
char buf[BUF_LENGTH];
char oldbuf[BUF_LENGTH];
int buflen;
char command[BUF_LENGTH];
char *argument;
char *pc;
int nc;
int command_index();
int put_module();
int get_module();
int signal_bye();
int change_cwd();
int report_pwd();
int report_ls();
buf[0] = '\0';
do
{
/* write prompt */
printf("%s", MTP_PROMPT);
strcpy(oldbuf, buf); /* preserve last command */
gets(buf); /* get a command string from user */
if (strcmp(buf, "!!") == 0)
{
strcpy(buf, oldbuf);
printf("%s\n", buf);
}
buflen = strlen(buf);
/* skip over white space */
for (nc = 0; nc < buflen; nc++)
if (!isspace(buf[nc]))
break;
/* get command string */
for (pc = command; nc < buflen; nc++)
if (isspace(buf[nc]))
break;
else
*pc++ = buf[nc];
*pc = '\0';
/* skip over white space */
for (; nc < buflen; nc++)
if (!isspace(buf[nc]))
break;
/* get argument string */
argument = buf + nc;
/*
printf("\t\t\t command = %s\n", command);
printf("\t\t\t argument = %s\n", argument);
printf("\t\t\t index = %d\n", command_index(command));
*/
/* find command number and act appropiately */
switch(command_index(command))
{
case BYE:
nc = signal_bye(s);
printf("Goodbye.\n");
break;
case PUTMOD:
nc = put_module(s, argument, PUT_MODULE);
break;
case UPUTMOD:
nc = put_module(s, argument, UPUT_MODULE);
break;
case GETMOD:
nc = get_module(s, argument);
break;
case CHDIR:
nc = change_cwd(argument);
break;
case REPPWD:
nc = report_pwd();
break;
case REPLS:
nc = report_ls(argument);
break;
case UNKNOWN:
default:
fprintf(stderr, "? Unknown command\n");
nc = 1;
break;
}
}
while (nc > 0);
return(nc);
}
int change_cwd(new_path)
char *new_path;
{
char pathname[MAXPATHLEN];
if (strlen(new_path) == 0)
strcpy(pathname, getenv("HOME"));
else if (new_path[0] == '~')
{
if (new_path[1] == '/')
{
strcpy(pathname, getenv("HOME"));
strcat(pathname, (new_path + 1));
}
else
{
strcpy(pathname, getenv("HOME"));
strcat(pathname, "/../"); /* not quite right but near enough */
strcat(pathname, (new_path + 1));
}
}
else
strcpy(pathname, new_path);
if (chdir(pathname) == -1)
fprintf(stderr, "mtp: Could not cd to %s\n", pathname);
else
printf("CWD now %s\n", getwd(pathname));
return(1);
}
int report_pwd()
{
char pathname[MAXPATHLEN];
printf("%s\n", getwd(pathname));
return(1);
}
int report_ls(argument)
char *argument;
{
char string[BUF_LENGTH];
sprintf(string, "ls %s", argument);
system(string);
return(1);
}