home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / icon / dos / src / icont / tlocal.c < prev    next >
C/C++ Source or Header  |  1992-02-10  |  8KB  |  384 lines

  1. /*
  2.  *  tlocal.c -- functions needed for different systems.
  3.  */
  4.  
  5. #include "../h/gsupport.h"
  6.  
  7. /*
  8.  * The following code is operating-system dependent [@tlocal.01].
  9.  *  Routines needed by different systems.
  10.  */
  11.  
  12. #if PORT
  13. /* place to put anything system specific */
  14. Deliberate Syntax Error
  15. #endif                    /* PORT */
  16.  
  17. #if AMIGA
  18. #if LATTICE
  19. unsigned long _STACK = 20000;   /*   MNEED ALSO, PLEASE */
  20. #endif                    /* LATTICE */
  21. #if AZTEC_C
  22. /*
  23.  * abs
  24.  */
  25. abs(i)
  26. int i;
  27. {
  28.    return ((i<0)? (-i) : i);
  29. }
  30.  
  31. /*
  32.  * getfa - get file attribute -1 == OK, 0 == ERROR, 1 == DIRECTORY
  33.  */
  34. getfa()
  35. {
  36.    return -1;
  37. }
  38. #endif                    /* AZTEC_C */
  39. #endif                    /* AMIGA */
  40.  
  41. #if ARM
  42. #include "kernel.h"
  43.  
  44. int unlink (const char *name)
  45. {
  46.     _kernel_osfile_block blk;
  47.  
  48.     return (_kernel_osfile(6,name,&blk) <= 0);
  49. }
  50.  
  51. /* **** The following line causes a fatal error in some C preprocessors
  52.    **** even if ARM is 0.  Remove the comment characters for ARM.
  53.    ****
  54. */
  55. /*#define QUOTE " \"\t"*/
  56.  
  57. int armquote (char *str, char **ret)
  58. {
  59.     char *p;
  60.     static char buf[255];
  61.  
  62.     if (strpbrk(str,QUOTE) == NULL)
  63.     {
  64.         *ret = str;
  65.         return strlen(str);
  66.     }
  67.  
  68.     p = buf;
  69.  
  70.     while (*str && p < &buf[255])
  71.     {
  72.         if (strchr(QUOTE,*str))
  73.         {
  74.             if (p > &buf[252])
  75.                 return -1;
  76.  
  77.             *p++ = '\\';
  78.             *p++ = *str;
  79.         }
  80.         else
  81.             *p++ = *str;
  82.  
  83.         ++str;
  84.     }
  85.  
  86.     if (p >= &buf[255])
  87.         return -1;
  88.  
  89.     *p = 0;
  90.     *ret = buf;
  91.     return (p - buf);
  92. }
  93.  
  94. /* Takes a filename, with a ".u1" suffix, and swaps it, IN PLACE, to
  95.  * conform to Archimedes conventions (u1 as a directory).
  96.  * Note that this is a very simplified version. It relies on the following
  97.  * facts:
  98.  *
  99.  *    1. In the ucode link directives, files ALWAYS end in .u1
  100.  *    2. The input filename is writeable.
  101.  *    3. Files which include directory parts conform to Archimedes
  102.  *       format (FS:dir.dir.file). Note that Unix formats such as
  103.  *       "/usr/icon/lib/time" are inherently non-portable, and NOT
  104.  *       supported.
  105.  *
  106.  * This function is only called from readglob() in C.Lglob.
  107.  */
  108. char *flipname(char *name)
  109. {
  110.     char *p = name + strlen(name) - 1;
  111.     char *q = p - 3;
  112.  
  113.     /* Copy the leafname to the end */
  114.     while (q >= name && *q != '.' && *q != ':')
  115.         *p-- = *q--;
  116.  
  117.     /* Insert the "U1." before the leafname */
  118.     *p-- = '.';
  119.     *p-- = '1';
  120.     *p-- = 'U';
  121.  
  122.     return name;
  123. }
  124. #endif                    /* ARM */
  125.  
  126. #if ATARI_ST
  127.  
  128. unsigned long _STACK = 10240;   /*   MNEED ALSO, PLEASE */
  129.  
  130. #endif                    /* ATARI_ST */
  131.  
  132. #if MACINTOSH
  133. #if MPW
  134. /* Floating Point Conversion Routine Stubs
  135.  
  136.    These routines, called by printf, are only necessary if floating point
  137.    formatting is used.
  138. */
  139.  
  140. char *ecvt(value,count,dec,sign)
  141. double value;
  142. int count,*dec,*sign;
  143. {
  144. /* #pragma unused(value,count,dec,sign) */
  145. return NULL;
  146. }
  147. fcvt() {}
  148.  
  149.  
  150. /* Routine to set file type and creator.
  151. */
  152.  
  153. #include <Files.h>
  154.  
  155. void
  156. setfile(filename,type,creator)
  157. char *filename;
  158. OSType type,creator;
  159.    {
  160.    FInfo info;
  161.  
  162.    if (getfinfo(filename,0,&info) == 0) {
  163.       info.fdType = type;
  164.       info.fdCreator = creator;
  165.       setfinfo(filename,0,&info);
  166.       }
  167.    return;
  168.    }
  169.  
  170.  
  171. /* Routine to quote strings for MPW
  172. */
  173.  
  174. char *
  175. mpwquote(s)
  176. char *s;
  177.    {
  178.    static char quotechar[] =
  179.      " \t\n\r#;&|()6'\"/\\{}`?E[]+*GH(<>3I7";
  180.    static char *endq = quotechar + sizeof(quotechar);
  181.    int quote = 0;
  182.    char c,d,*sp,*qp,*cp,*q;
  183.    char *malloc();
  184.  
  185.    sp = s;
  186.    while (c = *sp++) {
  187.       cp = quotechar;
  188.       while ((d = *cp++) && c != d)
  189.      ;
  190.       if (cp != endq) {
  191.          quote = 1;
  192.      break;
  193.      }
  194.       }
  195.    if (quote) {
  196.       qp = q = malloc(4 * strlen(s) + 1);
  197.       *qp++ = '\'';
  198.       sp = s;
  199.       while (c = *sp++) {
  200.      if (c == '\'') {
  201.         *qp++ = '\'';
  202.         *qp++ = '6';
  203.         *qp++ = '\'';
  204.         *qp++ = '\'';
  205.         quote = 1;
  206.         }
  207.      else *qp++ = c;
  208.      }
  209.       *qp++ = '\'';
  210.       *qp++ = '\0';
  211.       }
  212.    else {
  213.       q = malloc(strlen(s) + 1);
  214.       strcpy(q,s);
  215.       }
  216.    return q;
  217.    }
  218.  
  219.  
  220. /*
  221.  * SortOptions -- sorts icont options so that options and file names can
  222.  * appear in any order.
  223.  */
  224. void
  225. SortOptions(argv)
  226. char *argv[];
  227.    {
  228.    char **last,**p,*q,**op,**fp,**optlist,**filelist,opt,*s,*malloc();
  229.    int size,error = 0;;
  230.  
  231.    /*
  232.     * Count parameters before -x.
  233.     */
  234.    ++argv;
  235.    for (last = argv; *last != NULL && strcmp(*last,"-x") != 0; ++last)
  236.       ;
  237.    /*
  238.     * Allocate a work area to build separate lists of options
  239.     * and filenames.
  240.     */
  241.    size = (last - argv + 1) * sizeof(char*);
  242.    optlist = filelist = NULL;
  243.    op = optlist = (char **)malloc(size);
  244.    fp = filelist = (char **)malloc(size);
  245.    if (optlist && filelist) {            /* if allocations ok */
  246.       for (p = argv; (s = *p); ++p) {        /* loop thru args */
  247.          if (error) break;
  248.      if (s[0] == '-' && (opt = s[1]) != '\0') { /* if an option */
  249.         if (q = strchr(Options,opt)) {    /* if valid option */
  250.            *op++ = s;
  251.            if (q[1] == ':') {        /* if has a value */
  252.           if (s[2] != '\0') s += 2;    /* if value in this word */
  253.           else s = *op++ = *++p;    /* else value in next word */
  254.           if (s) {            /* if next word exists */
  255.              if (opt == 'S') {        /* if S option */
  256.             if (s[0] == 'h') ++s;    /* bump past h (??) */
  257.             if (s[0]) ++s;        /* bump past letter */
  258.             else error = 3;        /* error -- no letter */
  259.             if (s[0] == '\0') {    /* if value in next word */
  260.                if ((*op++ = *++p) == NULL)
  261.                      error = 4;    /* error -- no next word */
  262.                }
  263.             }
  264.              }
  265.           else error = 1;    /* error -- missing value */
  266.           }
  267.            }
  268.            else error = 2;        /* error -- invalid option */
  269.         }
  270.      else {                    /* else a file */
  271.         *fp++ = s;
  272.         }
  273.      }
  274.       *op = NULL;
  275.       *fp = NULL;
  276.       if (!error) {
  277.      p = argv;
  278.      for (op = optlist; *op; ++op) *p++ = *op;
  279.      for (fp = filelist; *fp; ++fp) *p++ = *fp;
  280.      }
  281.       }
  282.    if (optlist) free(optlist);
  283.    if (filelist) free(filelist);
  284.    return;
  285.    }
  286. #endif                    /* MPW */
  287. #endif                    /* MACINTOSH */
  288.  
  289. #if MSDOS
  290.  
  291. #if MICROSOFT
  292.  
  293. pointer xmalloc(n)
  294.    long n;
  295.    {
  296.    return calloc((size_t)n,sizeof(char));
  297.    }
  298. #endif                    /* MICROSOFT */
  299.  
  300. #if MICROSOFT || LATTICE
  301. int _stack = (8 * 1024);
  302. #endif                    /* MICROSOFT || LATTICE */
  303.  
  304. #if TURBO
  305. extern unsigned _stklen = 12 * 1024;
  306. #endif                    /* TURBO */
  307. #endif                    /* MSDOS */
  308.  
  309. #if MVS || VM
  310. #if SASC
  311. #include <options.h>
  312. char _linkage = _OPTIMIZE;
  313.  
  314. #if MVS                 /* expect dsnames, not DDnames, as file names */
  315. char *_style = "tso:";
  316. #define SYS_OSVS
  317. #else                    /* MVS */
  318. #define SYS_CMS
  319. #endif                    /* MVS */
  320.  
  321. #define RES_IOUTIL
  322. #define RES_DSNAME
  323.  
  324. #include <resident.h>
  325.  
  326. #if VM
  327. #include <cmsexec.h>
  328. #endif                    /* VM */
  329. /*
  330.  * No execvp, so turn it into a call to system.  (Then caller can exit.)
  331.  * In VM, put the ICONX command on the CMS stack, and someone else will
  332.  * do it after we're gone.  (system would clobber the user area.)
  333.  */
  334. int sysexec(cmd, argv)
  335.    char *cmd;
  336.    char **argv;
  337.    {
  338. #if MVS
  339.       char *prefix = "tso:";
  340. #else                    /* MVS */
  341.       char *prefix = "";
  342. #endif                    /* MVS */
  343.       int cmdlen = strlen(cmd) + strlen(prefix) + 1;
  344.       char **p;
  345.       char *cmdstr, *next;
  346.  
  347.       for(p = argv+1; *p; ++p)
  348.          cmdlen += strlen(*p) + 1;
  349.       cmdstr = malloc(cmdlen);      /* blithely ignoring failure...  */
  350.       strcpy(cmdstr, prefix);
  351.       strcat(cmdstr, cmd);
  352.       next = cmdstr + strlen(prefix) + strlen(cmd);
  353.       for (p = argv+1; *p; ++p)
  354.          {
  355.              *next = ' ';
  356.              strcpy(next+1, *p);
  357.              next += strlen(*p) + 1;
  358.           }
  359.       *next = '\0';
  360. #if MVS
  361.       return(system(cmdstr));
  362. #else                    /* MVS */
  363.       cmspush(cmdstr);
  364.       return NormalExit;
  365. #endif                    /* MVS */
  366.    }
  367. #endif                    /* SASC */
  368. #endif                    /* MVS || VM */
  369.  
  370. #if OS2
  371. #endif                    /* OS2 */
  372.  
  373. #if UNIX
  374. #endif                    /* UNIX */
  375.  
  376. #if VMS
  377. #endif                    /* VMS */
  378.  
  379. /*
  380.  * End of operating-system specific code.
  381.  */
  382.  
  383. static char *tjunk;            /* avoid empty module */
  384.