home *** CD-ROM | disk | FTP | other *** search
- static char sccs_id[] = "@(#) unix.c 4.0 " __DATE__ " HJR";
-
- /* unix.c (c) Copyright 1990 H.Rogers */
-
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <ctype.h>
-
- #include "fcntl.h"
- #include "termio.h"
-
- #include "sys/types.h"
- #include "sys/dev.h"
- #include "sys/os.h"
- #include "sys/unix.h"
- #include "sys/syslib.h"
- #include "sys/param.h"
- #include "sys/debug.h"
-
- struct sdir __sdir[MAXSDIR]; /* special directory list */
-
- char *__sfix[MAXSFIX]; /* special suffix list */
-
- static void
- __sdirinit (void)
- {
- int r[10];
- char buf[256];
-
- os_error *e = 0;
- int i;
- int tmp_found = 0;
-
- r[1] = (int) buf;
- r[3] = 0;
-
- for (i = 0; i < MAXSDIR;)
- {
- register char *s1, *s2;
-
- r[0] = (int) "UnixFS$/*";
- r[2] = 256;
- r[4] = 3;
-
- if (e = os_swi (0x23, r))
- break;
-
- buf[r[2]] = 0;
-
- s1 = (char *) r[3] + 8;
- if (!*s1)
- continue;
-
- if (!(s2 = __permstr (s1)))
- break;
- __sdir[i].name = s2;
-
- if (!(s2 = __permstr (buf)))
- break;
- __sdir[i].riscos_name = s2;
-
- if (stricmp (__sdir[i].name, "tmp") == 0)
- tmp_found = 1;
-
- i++;
- }
-
- if (!tmp_found && (i < MAXSDIR))
- {
- __sdir[i].name = "tmp";
- __sdir[i].riscos_name = "<Wimp$ScrapDir>";
- i++;
- }
-
- if (i < MAXSDIR)
- __sdir[i].name = 0; /* terminate list */
- }
-
- static void
- __panic (char *s)
- {
- s = s ? s : sys_errlist[errno];
- os_print ("\r\n");
- os_print (s);
- os_print ("\r\n\n");
- _exit (1);
- }
-
- static void
- __badr (void)
- {
- __panic ("Bad redirection");
- }
-
- void
- __unixinit (void)
- {
- int i;
-
- {
- char cli[MAXCOMMANDLEN];
-
- __u = 0; /* trap early exits */
-
- __svccli (cli); /* copy __cli in SVC mode */
-
- {
- int r[10];
- if (!os_swi (0x42583, r) && (r[0] != 0)) /* if DDE Utils pres + extra command line */
- {
- int len = strlen (cli);
- if (len + r[0] + 2 > MAXCOMMANDLEN) /* command line too long */
- {
- errno = -1;
- __panic ("command line far too long");
- }
- else
- {
- /* command line will fit, append it to cli. */
- cli[len] = ' ';
- r[0] = (int) cli + len + 1;
- os_swi (0x42584, r);
- r[0] = 0;
- os_swi (0x42581, r); /* set length to zero */
- }
- }
- }
-
- __envcnt = 0, __envsiz = 1;
-
- if (!(environ = malloc (sizeof (char *))))
- __panic (0);
- *environ = 0;
-
- if (__u = (struct proc *) __intenv ("UnixLib$env", 0))
- {
- #ifdef DEBUG
- __debug ("__unixinit() (test)");
- #endif
- {
- register char *s1 = cli, *s2 = __u->argb;
-
- while (*s2)
- if (*s1++ != *s2++)
- {
- __u = 0;
- break;
- }
- }
- {
- int r[10];
-
- r[0] = (int) "UnixLib$env";
- r[1] = r[3] = r[4] = 0;
- r[2] = -1;
- os_swi (0x24, r);
- }
- }
-
- __sdirinit ();
-
- {
- register char *s1, *s2;
- register int j;
-
- if (!(s1 = s2 = __getenv ("UnixFS$sfix", 0)))
- s1 = s2 = "a:c:cc:f:h:i:l:o:p:s:y";
- for (i = 0; i < MAXSFIX;)
- {
- while ((j = *s2) && j != ':')
- s2++;
- if (j && s1 == s2)
- {
- s1++, s2++;
- continue;
- }
- __sfix[i++] = s1;
- if (!j)
- break;
- *s2++ = 0;
- s1 = s2;
- }
- if (i < MAXSFIX)
- __sfix[i] = 0; /* terminate list */
- }
-
- __pipe = 0;
-
- if (!__u)
- {
- int argc;
- char **argv;
- char *ifile = 0, *ofile = 0;
- int ioflag = 0;
-
- if (!(__u = malloc (sizeof (struct proc))))
- __panic (0);
- if (!(__u->tty = malloc (sizeof (struct tty) * MAXTTY)))
- __panic (0);
-
- if (!(argv = malloc (sizeof (int) * (MAXCOMMANDLEN >> 2))))
- __panic (0);
- if (!(*argv = malloc (MAXCOMMANDLEN)))
- __panic (0);
-
- {
- register char *s1, *s2;
-
- argc = 0;
- s1 = cli;
- s2 = *argv;
-
- while (*s1 && argc < (MAXCOMMANDLEN >> 2))
- {
- while ((i = *s1) && isspace (i))
- s1++;
-
- if (!i)
- break;
- if (argc && i == '>')
- {
- outfile:
- if (*++s1 == '>')
- ++s1, ioflag |= 1;
- if (*s1 == '&')
- ++s1, ioflag |= 2;
- while ((i = *s1) && isspace (i))
- s1++;
- if (i)
- ofile = s1;
- else
- __badr ();
- while ((i = *s1) && !isspace (i) && i != '>' && i != '<')
- s1++;
- if (i == '<')
- {
- *s1 = 0;
- goto infile;
- }
- if (i == '>')
- __badr ();
- if (i)
- *s1++ = 0;
- continue;
- }
- if (argc && i == '<')
- {
- infile:
- if (*++s1 == '<')
- __badr ();
- while ((i = *s1) && isspace (i))
- s1++;
- if (i)
- ifile = s1;
- else
- __badr ();
- while ((i = *s1) && !isspace (i) && i != '>' && i != '<')
- s1++;
- if (i == '>')
- {
- *s1 = 0;
- goto outfile;
- }
- if (i == '<')
- __badr ();
- if (i)
- *s1++ = 0;
- continue;
- }
- argv[argc] = s2;
- while ((i = *s1) && !isspace (i) && (!argc || (i != '>' && i != '<')))
- {
- if (i == '\\')
- {
- s1++;
- if (i = *s1)
- *s2++ = i, s1++;
- else
- break;
- }
- else if (i == '"')
- {
- s1++;
- while ((i = *s1) && i != '"')
- {
- if (i == '\\')
- {
- s1++;
- if (i = *s1)
- *s2++ = i, s1++;
- else
- break;
- }
- else
- *s2++ = i, s1++;
- }
- if (i == '"')
- s1++;
- }
- else
- *s2++ = i, s1++;
- }
- argc++, *s2++ = 0;
- }
- argv[argc] = 0;
- }
-
- __u->argc = argc;
- __u->argv = argv;
- __u->argb = *argv;
-
- /* default UNIX process */
-
- {
- register struct proc *u = __u;
-
- u->uid = u->euid = 1;
- u->gid = u->egid = 1;
- u->pgrp = 1;
- u->pid = 2;
- u->ppid = 1;
- u->pproc = 0;
- u->umask = 0;
- u->alarm = 0;
- u->wait = 0;
- u->flag = 0;
- }
-
- /* default UNIX I/O */
-
- {
- char *tty;
-
- for (i = 0; i < MAXFD; i++)
- __u->file[i].dup = 0;
-
- if (!(tty = __getenv ("Unix$tty", 0)))
- tty = "/dev/tty";
-
- if (open (ifile ? ifile : tty, O_RDONLY) < 0)
- __panic (0);
- if (ioflag & 1)
- {
- if (open (ofile ? ofile : tty, O_WRONLY | O_CREAT, 0666) < 0)
- __panic (0);
- lseek (1, 0, 2);
- }
- else
- {
- if (open (ofile ? ofile : tty, O_WRONLY | O_CREAT | O_TRUNC, 0666) < 0)
- __panic (0);
- }
- if (!ofile || (ioflag & 2))
- {
- if (dup (1) < 0)
- __panic (0);
- }
- else
- {
- if (open (tty, O_WRONLY) < 0)
- __panic (0);
- }
- }
- }
- }
-
- if (i = __intenv ("Unix$uid", 0))
- __u->uid = __u->euid = i;
-
- #ifdef DEBUG
- __debug ("__unixinit() (final)");
- #endif
- }
-
- void
- __unixexit (void)
- {
- register int i;
- register struct file *f = __u->file;
-
- #ifdef DEBUG
- __debug ("__unixexit()");
- #endif
-
- if (f)
- for (i = 0; i < MAXFD; i++)
- if (f[i].dup)
- close (i);
-
- #ifdef DEBUG
- __debug ("__unixexit() (close())");
- #endif
- }
-
- int
- __fdalloc (void)
- {
- register struct file *f = __u->file;
- register int i;
-
- for (i = 0; i < MAXFD; i++, f++)
- if (!(f->dup))
- return (i);
-
- errno = EMFILE;
- return (-1);
- }
-
- char *
- __permstr (register const char *s)
- {
- register int i;
- register char *r;
-
- if (!s)
- return (0);
-
- i = strlen (s) + 1;
- if (!(r = malloc (i)))
- return (0);
- memcpy (r, s, i);
- return (r);
- }
-