mars_nwe-0.98.pl03
This commit is contained in:
61
examples/comm.c
Normal file
61
examples/comm.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* simple demo for a command programm which do a
|
||||
* DOS <-> UNX command handling using PIPE filesystem.
|
||||
* can be used with unxcomm for UNX.
|
||||
*
|
||||
* Can also be used under Linux for ncpfs <-> mars_nwe.
|
||||
*
|
||||
*/
|
||||
|
||||
#define ENV_UNXCOMM "UNXCOMM"
|
||||
#ifdef LINUX
|
||||
# define DEFAULT_COMM "/pipes/unxcomm"
|
||||
# else
|
||||
# define DEFAULT_COMM "p:/unxcomm"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#ifndef LINUX
|
||||
# include <io.h>
|
||||
#endif
|
||||
#include <fcntl.h>
|
||||
|
||||
static int usage(char *progname)
|
||||
{
|
||||
fprintf(stderr, "Usage:\t%s prog [paras]\n", progname);
|
||||
return(1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char *unxcomm=getenv("UNXCOMM");
|
||||
if (NULL == unxcomm) unxcomm=DEFAULT_COMM;
|
||||
if (argc > 1) {
|
||||
int fdout = open(unxcomm, O_RDWR);
|
||||
int fdin = dup(fdout);
|
||||
if (fdout > -1 && fdin > -1) {
|
||||
char **pp=argv+1;
|
||||
unsigned char b=32;
|
||||
int size;
|
||||
int buf[512];
|
||||
while(--argc) {
|
||||
write(fdout, *pp, strlen(*pp));
|
||||
++pp;
|
||||
write(fdout, &b, 1);
|
||||
}
|
||||
b=0;
|
||||
write(fdout, &b, 1);
|
||||
close(fdout);
|
||||
|
||||
while (0 < (size = read(fdin, buf, sizeof(buf)))) {
|
||||
write(1, buf, size);
|
||||
}
|
||||
close(fdin);
|
||||
return(0);
|
||||
} else
|
||||
fprintf(stderr, "Cannot open PIPECOMMAND '%s'\n", unxcomm);
|
||||
}
|
||||
return(usage(argv[0]));
|
||||
}
|
||||
BIN
examples/comm.exe
Executable file
BIN
examples/comm.exe
Executable file
Binary file not shown.
68
examples/unxcomm.c
Normal file
68
examples/unxcomm.c
Normal file
@@ -0,0 +1,68 @@
|
||||
/* simple UNX program to work together with 'comm' */
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
static char **build_argv(int bufsize, char *command, int len)
|
||||
/* routine returns **argv for use with execv routines */
|
||||
{
|
||||
int offset = ((len+4) / 4) * 4; /* aligned offset for **argv */
|
||||
int components = (bufsize - offset) / 4;
|
||||
if (components-- > 1) { /* minimal argv[0] + NULL */
|
||||
char **argv = (char **)(command+offset);
|
||||
char **pp = argv;
|
||||
char *p = command;
|
||||
char c;
|
||||
int i=0;
|
||||
*pp = p;
|
||||
*(p+len) = 0;
|
||||
while ((0 != (c = *p++)) && i < components) {
|
||||
if (c == 10 || c == 13) {
|
||||
*(p-1) = '\0';
|
||||
break;
|
||||
} else if (c == 32 || c == '\t') {
|
||||
*(p-1) = '\0';
|
||||
if (*p != 32 && *p != '\t' && *p != 10 && *p != 13) {
|
||||
*(++pp)=p;
|
||||
i++;
|
||||
}
|
||||
} else if (!i && c == '/') { /* here i must get argv[0] */
|
||||
*pp=p;
|
||||
}
|
||||
}
|
||||
if (*pp && !**pp) *pp=NULL;
|
||||
else
|
||||
*(++pp)=NULL;
|
||||
return(argv);
|
||||
}
|
||||
return(NULL);
|
||||
}
|
||||
#define MAXARGLEN 1024
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int status=fcntl(0, F_GETFL);
|
||||
int size;
|
||||
char buf[MAXARGLEN+1024];
|
||||
if (status != -1) fcntl(0, F_SETFL, status|O_NONBLOCK);
|
||||
close(2);
|
||||
dup2(1,2);
|
||||
if (-1 < (size=read(0, buf, MAXARGLEN))){
|
||||
char **argvv=build_argv(sizeof(buf), buf, size);
|
||||
if (argvv) {
|
||||
char path[300];
|
||||
execv(buf, argvv);
|
||||
sprintf(path, "/usr/bin/%s", *argvv);
|
||||
execv(path, argvv);
|
||||
sprintf(path, "/bin/%s", *argvv);
|
||||
execv(path, argvv);
|
||||
sprintf(path, "/usr/sbin/%s", *argvv);
|
||||
execv(path, argvv);
|
||||
sprintf(path, "/sbin/%s", *argvv);
|
||||
execv(path, argvv);
|
||||
fprintf(stderr, "%s:\tCould not find program '%s'\n", *argv, buf);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "%s:\tGot no paras\n", *argv);
|
||||
exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user