home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / code / unixlib / !UnixLib / src / unix / c / unix < prev    next >
Encoding:
Text File  |  1994-09-30  |  6.8 KB  |  425 lines

  1. static char sccs_id[] = "@(#) unix.c 4.0 " __DATE__ " HJR";
  2.  
  3. /* unix.c (c) Copyright 1990 H.Rogers */
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <ctype.h>
  9.  
  10. #include "fcntl.h"
  11. #include "termio.h"
  12.  
  13. #include "sys/types.h"
  14. #include "sys/dev.h"
  15. #include "sys/os.h"
  16. #include "sys/unix.h"
  17. #include "sys/syslib.h"
  18. #include "sys/param.h"
  19. #include "sys/debug.h"
  20.  
  21. struct sdir __sdir[MAXSDIR];    /* special directory list */
  22.  
  23. char *__sfix[MAXSFIX];        /* special suffix list */
  24.  
  25. static void
  26. __sdirinit (void)
  27. {
  28.   int r[10];
  29.   char buf[256];
  30.  
  31.   os_error *e = 0;
  32.   int i;
  33.   int tmp_found = 0;
  34.  
  35.   r[1] = (int) buf;
  36.   r[3] = 0;
  37.  
  38.   for (i = 0; i < MAXSDIR;)
  39.     {
  40.       register char *s1, *s2;
  41.  
  42.       r[0] = (int) "UnixFS$/*";
  43.       r[2] = 256;
  44.       r[4] = 3;
  45.  
  46.       if (e = os_swi (0x23, r))
  47.     break;
  48.  
  49.       buf[r[2]] = 0;
  50.  
  51.       s1 = (char *) r[3] + 8;
  52.       if (!*s1)
  53.     continue;
  54.  
  55.       if (!(s2 = __permstr (s1)))
  56.     break;
  57.       __sdir[i].name = s2;
  58.  
  59.       if (!(s2 = __permstr (buf)))
  60.     break;
  61.       __sdir[i].riscos_name = s2;
  62.  
  63.       if (stricmp (__sdir[i].name, "tmp") == 0)
  64.     tmp_found = 1;
  65.  
  66.       i++;
  67.     }
  68.  
  69.   if (!tmp_found && (i < MAXSDIR))
  70.     {
  71.       __sdir[i].name = "tmp";
  72.       __sdir[i].riscos_name = "<Wimp$ScrapDir>";
  73.       i++;
  74.     }
  75.  
  76.   if (i < MAXSDIR)
  77.     __sdir[i].name = 0;        /* terminate list */
  78. }
  79.  
  80. static void
  81. __panic (char *s)
  82. {
  83.   s = s ? s : sys_errlist[errno];
  84.   os_print ("\r\n");
  85.   os_print (s);
  86.   os_print ("\r\n\n");
  87.   _exit (1);
  88. }
  89.  
  90. static void
  91. __badr (void)
  92. {
  93.   __panic ("Bad redirection");
  94. }
  95.  
  96. void
  97. __unixinit (void)
  98. {
  99.   int i;
  100.  
  101.   {
  102.     char cli[MAXCOMMANDLEN];
  103.  
  104.     __u = 0;            /* trap early exits */
  105.  
  106.     __svccli (cli);        /* copy __cli in SVC mode */
  107.  
  108.     {
  109.       int r[10];
  110.       if (!os_swi (0x42583, r) && (r[0] != 0))    /* if DDE Utils pres + extra command line */
  111.     {
  112.       int len = strlen (cli);
  113.       if (len + r[0] + 2 > MAXCOMMANDLEN)    /* command line too long */
  114.         {
  115.           errno = -1;
  116.           __panic ("command line far too long");
  117.         }
  118.       else
  119.         {
  120.           /* command line will fit, append it to cli. */
  121.           cli[len] = ' ';
  122.           r[0] = (int) cli + len + 1;
  123.           os_swi (0x42584, r);
  124.           r[0] = 0;
  125.           os_swi (0x42581, r);    /* set length to zero */
  126.         }
  127.     }
  128.     }
  129.  
  130.     __envcnt = 0, __envsiz = 1;
  131.  
  132.     if (!(environ = malloc (sizeof (char *))))
  133.         __panic (0);
  134.     *environ = 0;
  135.  
  136.     if (__u = (struct proc *) __intenv ("UnixLib$env", 0))
  137.       {
  138. #ifdef DEBUG
  139.     __debug ("__unixinit() (test)");
  140. #endif
  141.     {
  142.       register char *s1 = cli, *s2 = __u->argb;
  143.  
  144.       while (*s2)
  145.         if (*s1++ != *s2++)
  146.           {
  147.         __u = 0;
  148.         break;
  149.           }
  150.     }
  151.     {
  152.       int r[10];
  153.  
  154.       r[0] = (int) "UnixLib$env";
  155.       r[1] = r[3] = r[4] = 0;
  156.       r[2] = -1;
  157.       os_swi (0x24, r);
  158.     }
  159.       }
  160.  
  161.     __sdirinit ();
  162.  
  163.     {
  164.       register char *s1, *s2;
  165.       register int j;
  166.  
  167.       if (!(s1 = s2 = __getenv ("UnixFS$sfix", 0)))
  168.     s1 = s2 = "a:c:cc:f:h:i:l:o:p:s:y";
  169.       for (i = 0; i < MAXSFIX;)
  170.     {
  171.       while ((j = *s2) && j != ':')
  172.         s2++;
  173.       if (j && s1 == s2)
  174.         {
  175.           s1++, s2++;
  176.           continue;
  177.         }
  178.       __sfix[i++] = s1;
  179.       if (!j)
  180.         break;
  181.       *s2++ = 0;
  182.       s1 = s2;
  183.     }
  184.       if (i < MAXSFIX)
  185.     __sfix[i] = 0;        /* terminate list */
  186.     }
  187.  
  188.     __pipe = 0;
  189.  
  190.     if (!__u)
  191.       {
  192.     int argc;
  193.     char **argv;
  194.     char *ifile = 0, *ofile = 0;
  195.     int ioflag = 0;
  196.  
  197.     if (!(__u = malloc (sizeof (struct proc))))
  198.         __panic (0);
  199.     if (!(__u->tty = malloc (sizeof (struct tty) * MAXTTY)))
  200.         __panic (0);
  201.  
  202.     if (!(argv = malloc (sizeof (int) * (MAXCOMMANDLEN >> 2))))
  203.         __panic (0);
  204.     if (!(*argv = malloc (MAXCOMMANDLEN)))
  205.       __panic (0);
  206.  
  207.     {
  208.       register char *s1, *s2;
  209.  
  210.       argc = 0;
  211.       s1 = cli;
  212.       s2 = *argv;
  213.  
  214.       while (*s1 && argc < (MAXCOMMANDLEN >> 2))
  215.         {
  216.           while ((i = *s1) && isspace (i))
  217.         s1++;
  218.  
  219.           if (!i)
  220.         break;
  221.           if (argc && i == '>')
  222.         {
  223.         outfile:
  224.           if (*++s1 == '>')
  225.             ++s1, ioflag |= 1;
  226.           if (*s1 == '&')
  227.             ++s1, ioflag |= 2;
  228.           while ((i = *s1) && isspace (i))
  229.             s1++;
  230.           if (i)
  231.             ofile = s1;
  232.           else
  233.             __badr ();
  234.           while ((i = *s1) && !isspace (i) && i != '>' && i != '<')
  235.             s1++;
  236.           if (i == '<')
  237.             {
  238.               *s1 = 0;
  239.               goto infile;
  240.             }
  241.           if (i == '>')
  242.             __badr ();
  243.           if (i)
  244.             *s1++ = 0;
  245.           continue;
  246.         }
  247.           if (argc && i == '<')
  248.         {
  249.         infile:
  250.           if (*++s1 == '<')
  251.             __badr ();
  252.           while ((i = *s1) && isspace (i))
  253.             s1++;
  254.           if (i)
  255.             ifile = s1;
  256.           else
  257.             __badr ();
  258.           while ((i = *s1) && !isspace (i) && i != '>' && i != '<')
  259.             s1++;
  260.           if (i == '>')
  261.             {
  262.               *s1 = 0;
  263.               goto outfile;
  264.             }
  265.           if (i == '<')
  266.             __badr ();
  267.           if (i)
  268.             *s1++ = 0;
  269.           continue;
  270.         }
  271.           argv[argc] = s2;
  272.           while ((i = *s1) && !isspace (i) && (!argc || (i != '>' && i != '<')))
  273.         {
  274.           if (i == '\\')
  275.             {
  276.               s1++;
  277.               if (i = *s1)
  278.             *s2++ = i, s1++;
  279.               else
  280.             break;
  281.             }
  282.           else if (i == '"')
  283.             {
  284.               s1++;
  285.               while ((i = *s1) && i != '"')
  286.             {
  287.               if (i == '\\')
  288.                 {
  289.                   s1++;
  290.                   if (i = *s1)
  291.                 *s2++ = i, s1++;
  292.                   else
  293.                 break;
  294.                 }
  295.               else
  296.                 *s2++ = i, s1++;
  297.             }
  298.               if (i == '"')
  299.             s1++;
  300.             }
  301.           else
  302.             *s2++ = i, s1++;
  303.         }
  304.           argc++, *s2++ = 0;
  305.         }
  306.       argv[argc] = 0;
  307.     }
  308.  
  309.     __u->argc = argc;
  310.     __u->argv = argv;
  311.     __u->argb = *argv;
  312.  
  313. /* default UNIX process */
  314.  
  315.     {
  316.       register struct proc *u = __u;
  317.  
  318.       u->uid = u->euid = 1;
  319.       u->gid = u->egid = 1;
  320.       u->pgrp = 1;
  321.       u->pid = 2;
  322.       u->ppid = 1;
  323.       u->pproc = 0;
  324.       u->umask = 0;
  325.       u->alarm = 0;
  326.       u->wait = 0;
  327.       u->flag = 0;
  328.     }
  329.  
  330. /* default UNIX I/O */
  331.  
  332.     {
  333.       char *tty;
  334.  
  335.       for (i = 0; i < MAXFD; i++)
  336.         __u->file[i].dup = 0;
  337.  
  338.       if (!(tty = __getenv ("Unix$tty", 0)))
  339.         tty = "/dev/tty";
  340.  
  341.       if (open (ifile ? ifile : tty, O_RDONLY) < 0)
  342.         __panic (0);
  343.       if (ioflag & 1)
  344.         {
  345.           if (open (ofile ? ofile : tty, O_WRONLY | O_CREAT, 0666) < 0)
  346.         __panic (0);
  347.           lseek (1, 0, 2);
  348.         }
  349.       else
  350.         {
  351.           if (open (ofile ? ofile : tty, O_WRONLY | O_CREAT | O_TRUNC, 0666) < 0)
  352.         __panic (0);
  353.         }
  354.       if (!ofile || (ioflag & 2))
  355.         {
  356.           if (dup (1) < 0)
  357.         __panic (0);
  358.         }
  359.       else
  360.         {
  361.           if (open (tty, O_WRONLY) < 0)
  362.         __panic (0);
  363.         }
  364.     }
  365.       }
  366.   }
  367.  
  368.   if (i = __intenv ("Unix$uid", 0))
  369.     __u->uid = __u->euid = i;
  370.  
  371. #ifdef DEBUG
  372.   __debug ("__unixinit() (final)");
  373. #endif
  374. }
  375.  
  376. void
  377. __unixexit (void)
  378. {
  379.   register int i;
  380.   register struct file *f = __u->file;
  381.  
  382. #ifdef DEBUG
  383.   __debug ("__unixexit()");
  384. #endif
  385.  
  386.   if (f)
  387.     for (i = 0; i < MAXFD; i++)
  388.       if (f[i].dup)
  389.     close (i);
  390.  
  391. #ifdef DEBUG
  392.   __debug ("__unixexit() (close())");
  393. #endif
  394. }
  395.  
  396. int
  397. __fdalloc (void)
  398. {
  399.   register struct file *f = __u->file;
  400.   register int i;
  401.  
  402.   for (i = 0; i < MAXFD; i++, f++)
  403.     if (!(f->dup))
  404.       return (i);
  405.  
  406.   errno = EMFILE;
  407.   return (-1);
  408. }
  409.  
  410. char *
  411. __permstr (register const char *s)
  412. {
  413.   register int i;
  414.   register char *r;
  415.  
  416.   if (!s)
  417.     return (0);
  418.  
  419.   i = strlen (s) + 1;
  420.   if (!(r = malloc (i)))
  421.     return (0);
  422.   memcpy (r, s, i);
  423.   return (r);
  424. }
  425.