home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
MISC
/
mtp.shar
/
mtpcmds.c
< prev
next >
Wrap
C/C++ Source or Header
|
2009-11-06
|
10KB
|
403 lines
/* mtpcmds.c, Version 1.4, Created 8/9/91 */
/* Subroutines called by mtp */
/* Dr Alan M. McIvor, BP Sunbury Research Centre */
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <ctype.h>
#include <malloc.h>
#include <memory.h>
#include "/chuckapps/os9/defs/module.h"
#include "mtp.h"
int command_index(usr_cmd)
char *usr_cmd;
{
if (strcmp(usr_cmd, "quit") == 0)
return(BYE);
else if (strcmp(usr_cmd, "bye") == 0)
return(BYE);
else if (strcmp(usr_cmd, "put") == 0)
return(PUTMOD);
else if (strcmp(usr_cmd, "get") == 0)
return(GETMOD);
else if (strcmp(usr_cmd, "cd") == 0)
return(CHDIR);
else if (strcmp(usr_cmd, "pwd") == 0)
return(REPPWD);
else if (strcmp(usr_cmd, "ls") == 0)
return(REPLS);
else if (strcmp(usr_cmd, "uput") == 0)
return(UPUTMOD);
else
return(UNKNOWN);
}
/* command handlers - return -1 if error, 0 if caller should hangup normally
or >0 if caller should continue. */
int signal_bye(s)
int s;
{
char c;
/* send host a quit command */
c = QUIT;
if (write(s, &c, 1) == -1)
return(-1);
else
return(0);
}
int put_module(s, argument, control)
int s; /* socket */
char *argument;
char control; /* either PUT_MODULE or UPUT_MODULE */
{
FILE *fp;
FILE *file_open();
struct modhcom os9modhead;
char *module, *ptr;
int i, j;
int handle_error();
/* assume that the argument is one file name only and open this file */
if ((fp = file_open(argument)) == NULL)
{
fprintf(stderr, "put_module: Couldn't open module file %s\n", argument);
return(1);
}
/* read module header from file */
if (fread((char *)&os9modhead, sizeof(struct modhcom), 1, fp) != 1)
{
fprintf(stderr, "put_module: Couldn't read module header from file %s\n",
argument);
fclose(fp);
return(1);
}
/* check that the module sync bytes are correct */
if (os9modhead._msync != MODSYNC)
{
fprintf(stderr,
"put_module: File %s doesn't contain a valid module header\n",
argument);
fclose(fp);
return(1);
}
/* read in the module */
if ((module = malloc(os9modhead._msize)) == NULL)
{
fprintf(stderr, "put_module: Error mallocing\n");
fclose(fp);
return(1);
}
memcpy(module, (char *)&os9modhead, sizeof(struct modhcom));
if (fread((module + sizeof(struct modhcom)), 1,
(os9modhead._msize - sizeof(struct modhcom)), fp) !=
(os9modhead._msize - sizeof(struct modhcom)))
{
fprintf(stderr, "put_module: Error reading file %s\n", argument);
fclose(fp);
return(1);
}
fclose(fp);
/*
printf("\t\t\tmodule name is %s\n", (module + os9modhead._mname));
printf("\t\t\tmodule size is %d\n", os9modhead._msize);
*/
/* send the server the data */
if (write(s, &control, 1) != 1) /* put mod code */
{
fprintf(stderr, "put_module: Error on socket I/O\n");
return(-1);
}
i = strlen((module + os9modhead._mname));
i = htonl(i);
if (write(s, &i, 4) != 4) /* length of name */
{
fprintf(stderr, "put_module: Error on socket I/O\n");
return(-1);
}
i = strlen((module + os9modhead._mname));
if (write(s, (module + os9modhead._mname), i) != i) /*name */
{
fprintf(stderr, "put_module: Error on socket I/O\n");
return(-1);
}
i = htonl(os9modhead._msize);
if (write(s, &i, 4) != 4) /* size of module */
{
fprintf(stderr, "put_module: Error on socket I/O\n");
return(-1);
}
/* wait for acknowledge */
if (read(s, &control, 1) <= 0)
{
fprintf(stderr, "put_module: Error on socket I/O\n");
return(-1);
}
/* printf("Server acknowledged with code %c\n", control);*/
switch (control)
{
case ACKNOWLEDGE:
/* printf("Server acknowledged\n");*/
break;
case ERROR_CONTROL:
return(handle_error(s));
default:
fprintf(stderr, "put_module: Unknown control from server\n");
return(-1);
}
/* printf("Writing module ...\n");*/
/* send the module data */
if (write_block(s, module, os9modhead._msize) == -1)
{
fprintf(stderr, "put_module: Error on socket I/O\n");
return(-1);
}
/*
else
printf("%d ", j);
printf("\n");
printf("\t\t ... finished\n");
*/
free(module);
/* wait for acknowledge */
if (read(s, &control, 1) <= 0)
{
fprintf(stderr, "put_module: Error on socket I/O\n");
return(-1);
}
switch (control)
{
case ACKNOWLEDGE:
/* printf("Server acknowledged\n");*/
break;
case ERROR_CONTROL:
fclose(fp);
return(handle_error(s));
default:
fprintf(stderr, "put_module: Unknown control from server\n");
return(-1);
}
return(1);
}
int handle_error(socket)
int socket;
{
/* return -1 if fatal error, 0 nonfatal but stop, >0 if continue */
int error_number;
/* get error number from socket */
if (read(socket, (char *)&error_number, 4) == -1)
{
fprintf(stderr, "put_module: Error on socket I/O\n");
return(-1);
}
error_number = ntohl(error_number);
/* process error */
switch(error_number)
{
case E_FATAL:
fprintf(stderr, "Server indicated FATAL error.\n");
return(-1);
case E_UNLOAD:
fprintf(stderr, "Server indicated UNLOAD error.\n");
return(E_UNLOAD);
case E_MEMALLOC:
fprintf(stderr, "Server indicated ALLOC error.\n");
return(E_MEMALLOC);
case E_FORK:
fprintf(stderr, "Server indicated FORK error.\n");
return(E_FORK);
case E_VALIDATE:
fprintf(stderr, "Server indicated validation error.\n");
return(E_VALIDATE);
case E_NOMODULE:
fprintf(stderr, "Server indicated no such module.\n");
return(E_NOMODULE);
default:
fprintf(stderr, "Server indicated unknown error %d\n", error_number);
return(-1);
}
}
int get_module(s, argument)
int s; /* socket */
char *argument;
{
FILE *fp;
char *module;
char control;
int i, j;
int handle_error();
char module_name[256];
char file_name[256];
/* get module name and file name */
/* skip over white space */
for (i = 0; argument[i] != '\0'; i++)
if (!isspace(argument[i]))
break;
/* get module name */
for (j = 0; argument[i] != '\0'; i++)
if (isspace(argument[i]))
break;
else
module_name[j++] = argument[i];
module_name[j] = '\0';
/* skip over white space */
for (; argument[i] != '\0'; i++)
if (!isspace(argument[i]))
break;
/* get file name */
for (j = 0; argument[i] != '\0'; i++)
if (isspace(argument[i]))
break;
else
file_name[j++] = argument[i];
file_name[j] = '\0';
if (strlen(file_name) == 0)
strcpy(file_name, module_name);
/* printf("Getting module %s into file %s\n", module_name, file_name);*/
if (strlen(module_name) == 0)
{
fprintf(stderr, "? get mod-name [file-name]\n");
return(2);
}
/* open the file to write the data into */
if ((fp = fopen(file_name, "w")) == NULL)
{
fprintf(stderr, "get_module: Couldn't open file %s\n", file_name);
return(3);
}
/* send server GETMOD command, module name size and module name */
control = GET_MODULE;
if (write(s, &control, 1) != 1) /* get mod code */
{
fprintf(stderr, "get_module: Error on socket I/O\n");
return(-1);
}
i = strlen(module_name);
i = htonl(i);
if (write(s, &i, 4) != 4) /* length of name */
{
fprintf(stderr, "get_module: Error on socket I/O\n");
return(-1);
}
i = strlen(module_name);
if (write(s, module_name, i) != i) /*name */
{
fprintf(stderr, "get_module: Error on socket I/O\n");
return(-1);
}
/* wait for acknowledge */
if (read(s, &control, 1) <= 0)
{
fprintf(stderr, "get_module: Error on socket I/O\n");
return(-1);
}
switch (control)
{
case ACKNOWLEDGE:
/* printf("Server acknowledged\n");*/
break;
case ERROR_CONTROL:
fclose(fp);
return(handle_error(s));
break;
default:
fprintf(stderr, "get_module: Unknown control from server\n");
return(-1);
}
if (read(s, &i, 4) != 4) /* size of module */
{
fprintf(stderr, "get_module: Error on socket I/O\n");
return(-1);
}
i = ntohl(i);
/* printf("Module of size %d\n", i);*/
/* read module */
module = (char *)malloc(i);
if (read_block(s, module, i) != i)
{
fprintf(stderr, "get_module: Error on socket I/O\n");
return(-1);
}
/* write to file */
if (fwrite(module, 1, i, fp) != i)
{
fprintf(stderr, "get_module: Error writing module to file\n");
return(4);
}
free(module);
return(1);
}
FILE *file_open(fname)
char *fname;
{
FILE *fp;
char *mtppath; /* paths to search for modules */
extern char *getenv();
char pathname[1024];
int i;
if ((fp = fopen(fname, "r")) != NULL)
return(fp);
/* so file not in directory, look in path for it */
/* if no path defined return NULL */
if ((mtppath = getenv("MTPPATH")) == NULL)
return(NULL);
/* printf("MTPPATH = %s\n");*/
do
{
/* copy the next path out of MTPPATH:- paths being separated by : */
i = 0;
while ((*mtppath != ':') && (*mtppath != '\0'))
pathname[i++] = *mtppath++;
/* complete the full possible path name */
pathname[i] = '\0';
strcat(pathname, "/");
strcat(pathname, fname);
if ((fp = fopen(pathname, "r")) != NULL)
return(fp);
/* so not in that directory, repeat if further paths */
}
while (*mtppath++ != '\0');
return(NULL);
}