home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / runtime / rsys.r < prev    next >
Text File  |  2002-01-18  |  9KB  |  378 lines

  1. /*
  2.  * File: rsys.r
  3.  *  Contents: getstrg, host, longread, putstr
  4.  */
  5.  
  6. /*
  7.  * getstrg - read a line into buf from file fbp.  At most maxi characters
  8.  *  are read.  getstrg returns the length of the line, not counting the
  9.  *  newline.  Returns -1 if EOF and -2 if length was limited by maxi.
  10.  *  Discards \r before \n in translated mode.  [[ Needs ferror() check. ]]
  11.  */
  12.  
  13. int getstrg(buf, maxi, fbp)
  14. register char *buf;
  15. int maxi;
  16. struct b_file *fbp;
  17.    {
  18.    register int c, l;
  19.    FILE *fd;
  20.  
  21.    fd = fbp->fd;
  22.  
  23. #if AMIGA
  24. #if LATTICE
  25.    /* This code is special for Lattice 4.0.  It was different for
  26.     *  Lattice 3.10 and probably won't work for other C compilers.
  27.     */
  28.    extern struct UFB _ufbs[];
  29.  
  30.    if (IsInteractive(_ufbs[fileno(fd)].ufbfh))
  31.       return read(fileno(fd),buf,maxi);
  32. #endif                    /* LATTICE */
  33. #endif                    /* AMIGA */
  34.  
  35. #ifdef XWindows
  36.    if (isatty(fileno(fd))) wflushall();
  37. #endif                    /* XWindows */
  38.  
  39.    l = 0;
  40.    while (1) {
  41.  
  42. #ifdef Graphics
  43.       /* insert non-blocking read/code to service windows here */
  44. #endif                    /* Graphics */
  45.  
  46.       if ((c = fgetc(fd)) == '\n')    /* \n terminates line */
  47.      break;
  48.       if (c == '\r' && (fbp->status & Fs_Untrans) == 0) {
  49.      /* \r terminates line in translated mode */
  50.      if ((c = fgetc(fd)) != '\n')    /* consume following \n */
  51.          ungetc(c, fd);        /* (put back if not \n) */
  52.      break;
  53.      }
  54.       if (c == EOF)
  55.      if (l > 0) return l;
  56.      else return -1;
  57.       if (++l > maxi) {
  58.      ungetc(c, fd);
  59.      return -2;
  60.      }
  61.       *buf++ = c;
  62.       }
  63.    return l;
  64.    }
  65.  
  66. /*
  67.  * iconhost - return some sort of host name into the buffer pointed at
  68.  *  by hostname.  This code accommodates several different host name
  69.  *  fetching schemes.
  70.  */
  71. void iconhost(hostname)
  72. char *hostname;
  73.    {
  74.  
  75. #ifdef HostStr
  76.    /*
  77.     * The string constant HostStr contains the host name.
  78.     */
  79.    strcpy(hostname,HostStr);
  80. #elif VMS                /* HostStr */
  81.    /*
  82.     * VMS has its own special logic.
  83.     */
  84.    char *h;
  85.    if (!(h = getenv("ICON_HOST")) && !(h = getenv("SYS$NODE")))
  86.       h = "VAX/VMS";
  87.    strcpy(hostname,h);
  88. #else                    /* HostStr */
  89.    {
  90.    /*
  91.     * Use the uname system call.  (POSIX)
  92.     */
  93.    struct utsname utsn;
  94.    uname(&utsn);
  95.    strcpy(hostname,utsn.nodename);
  96.    }
  97. #endif                    /* HostStr */
  98.  
  99.    }
  100.  
  101. /*
  102.  * Read a long string in shorter parts. (Standard read may not handle long
  103.  *  strings.)
  104.  */
  105. word longread(s,width,len,fd)
  106. FILE *fd;
  107. int width;
  108. char *s;
  109. long len;
  110. {
  111.    tended char *ts = s;
  112.    long tally = 0;
  113.    long n = 0;
  114.  
  115. #if NT
  116.    /*
  117.     * Under NT/MSVC++, ftell() used in Icon where() returns bad answers
  118.     * after a wlongread().  We work around it here by fseeking after fread.
  119.     */
  120.    long pos = ftell(fd);
  121. #endif                    /* NT */
  122.  
  123. #ifdef XWindows
  124.    if (isatty(fileno(fd))) wflushall();
  125. #endif                    /* XWindows */
  126.  
  127.    while (len > 0) {
  128.       n = fread(ts, width, (int)((len < MaxIn) ? len : MaxIn), fd);
  129.       if (n <= 0) {
  130. #if NT
  131.          fseek(fd, pos + tally, SEEK_SET);
  132. #endif                    /* NT */
  133.          return tally;
  134.      }
  135.       tally += n;
  136.       ts += n;
  137.       len -= n;
  138.       }
  139. #if NT
  140.    fseek(fd, pos + tally, SEEK_SET);
  141. #endif                    /* NT */
  142.    return tally;
  143.    }
  144.  
  145. /*
  146.  * Print string referenced by descriptor d. Note, d must not move during
  147.  *   a garbage collection.
  148.  */
  149.  
  150. int putstr(f, d)
  151. register FILE *f;
  152. dptr d;
  153.    {
  154.    register char *s;
  155.    register word l;
  156.  
  157.    l = StrLen(*d);
  158.    if (l == 0)
  159.       return  Succeeded;
  160.    s = StrLoc(*d);
  161.  
  162. #ifdef MSWindows
  163. #ifdef ConsoleWindow
  164.    if ((f == stdout && !(ConsoleFlags & StdOutRedirect)) ||
  165.     (f == stderr && !(ConsoleFlags & StdErrRedirect))) {
  166.       if (ConsoleBinding == NULL)
  167.          ConsoleBinding = OpenConsole();
  168. #if BORLAND_286
  169.       goto Lab1;
  170. #else
  171.       { int i; for(i=0;i<l;i++) Consoleputc(s[i], f); }
  172. #endif
  173.       return Succeeded;
  174.       }
  175. Lab1:
  176. #endif                    /* ConsoleWindow */
  177. #endif                    /* MSWindows */
  178. #ifdef PresentationManager
  179.    if (ConsoleFlags & OutputToBuf) {
  180.       /* check for overflow */
  181.       if (MaxReadStr * 4 - ((int)ConsoleStringBufPtr - (int)ConsoleStringBuf) < l + 1)
  182.      return Failed;
  183.       /* big enough */
  184.       memcpy(ConsoleStringBufPtr, s, l);
  185.       ConsoleStringBufPtr += l;
  186.       *ConsoleStringBufPtr = '\0';
  187.       } /* End of if - push to buffer */
  188.    else if ((f == stdout && !(ConsoleFlags & StdOutRedirect)) ||
  189.         (f == stderr && !(ConsoleFlags & StdErrRedirect)))
  190.       wputstr((wbinding *)PMOpenConsole(), s, l);
  191.    return Succeeded;
  192. #endif                    /* PresentationManager */
  193. #if VMS
  194.    /*
  195.     * This is to get around a bug in VMS C's fwrite routine.
  196.     */
  197.    {
  198.       int i;
  199.       for (i = 0; i < l; i++)
  200.          if (putc(s[i], f) == EOF)
  201.             break;
  202.       if (i == l)
  203.          return Succeeded;
  204.       else
  205.          return Failed;
  206.    }
  207. #else                    /* VMS */
  208.    if (longwrite(s,l,f) < 0)
  209.       return Failed;
  210.    else
  211.       return Succeeded;
  212. #endif                    /* VMS */
  213.    }
  214.  
  215. /*
  216.  * idelay(n) - delay for n milliseconds
  217.  */
  218. int idelay(n)
  219. int n;
  220.    {
  221.  
  222. /*
  223.  * The following code is operating-system dependent [@fsys.01].
  224.  */
  225. #if OS2
  226. #if OS2_32
  227.    DosSleep(n);
  228.    return Succeeded;
  229. #else                    /* OS2_32 */
  230.    return Failed;
  231. #endif                    /* OS2_32 */
  232. #endif                    /* OS2 */
  233.  
  234. #if VMS
  235.    delay_vms(n);
  236.    return Succeeded;
  237. #endif                    /* VMS */
  238.  
  239. #if UNIX
  240.    struct timeval t;
  241.    t.tv_sec = n / 1000;
  242.    t.tv_usec = (n % 1000) * 1000;
  243.    select(1, NULL, NULL, NULL, &t);
  244.    return Succeeded;
  245. #endif                    /* UNIX */
  246.  
  247. #if MSDOS
  248. #if SCCX_MX
  249.    msleep(n);
  250.    return Succeeded;
  251. #else                    /* SCCX_MX */
  252. #if NT
  253. #ifdef MSWindows
  254.    Sleep(n);
  255. #else                    /* MSWindows */
  256.    /* ? should be a way for NT console apps to sleep... */
  257.    return Failed;
  258. #endif                    /* MSWindows */
  259.    return Succeeded;
  260. #else                    /* NT */
  261. #if BORLAND_286
  262.    /* evil busy wait */
  263.     clock_t start = clock();
  264.     while ((double)((clock() - start) / CLK_TCK) / 1000 < n);
  265.     return Succeeded;
  266. #else                    /* BORLAND_286 */
  267.    return Failed;
  268. #endif                    /* BORLAND_286 */
  269. #endif                    /* NT */
  270. #endif                    /* SCCX_MX */
  271. #endif                    /* MSDOS */
  272.  
  273. #if MACINTOSH
  274.    void MacDelay(int n);
  275.    MacDelay(n);
  276.    return Succeeded;
  277. #endif                    /* MACINTOSH */
  278.  
  279. #if AMIGA
  280. #if __SASC
  281.    Delay(n/20);
  282.    return Succeeded;
  283. #else                    /* __SASC */
  284.    return Failed
  285. #endif                                  /* __SASC */
  286. #endif                    /* AMIGA */
  287.  
  288. #if PORT || ARM
  289.    return Failed;
  290. #endif                    /* PORT || ARM */
  291.  
  292.    /*
  293.     * End of operating-system dependent code.
  294.     */
  295.    }
  296.  
  297. #ifdef MSWindows
  298. #ifdef FAttrib
  299. /*
  300.  * make_mode takes mode_t type (an integer) input and returns the file permission
  301.  * in the format of a string.
  302. */
  303. #if UNIX
  304. char *make_mode (mode_t st_mode)
  305. #endif                    /* UNIX */
  306. #if MSDOS
  307. char *make_mode (unsigned short st_mode)
  308. #endif                    /* MSDOS */
  309. {
  310.    char *buf;
  311.  
  312.    if ( (buf = (char *) malloc(sizeof(char)*11)) == NULL ) {
  313.       fprintf(stderr,"fatal malloc error\n");
  314.       return NULL;
  315.    }
  316.  
  317. #if UNIX
  318.    if ( st_mode & S_IFIFO )      buf[0] = 'f';
  319.    else if ( st_mode & S_IFCHR ) buf[0] = 'c';
  320.    else if ( st_mode & S_IFDIR ) buf[0] = 'd';
  321.    else if ( st_mode & S_IFBLK ) buf[0] = 'b';
  322.    else if ( st_mode & S_IFREG ) buf[0] = '-';
  323.    else                     buf[0] = '\?';
  324.  
  325.    if (st_mode & S_IRUSR) buf[1] = 'r';
  326.    else    buf[1] = '-';
  327.    if (st_mode & S_IWUSR) buf[2] = 'w';
  328.    else    buf[2] = '-';
  329.    if (st_mode & S_IXUSR) buf[3] = 'x';
  330.    else    buf[3] = '-';
  331.    if (st_mode & S_IRGRP) buf[4] = 'r';
  332.    else    buf[4] = '-';
  333.    if (st_mode & S_IWGRP) buf[5] = 'w';
  334.    else    buf[5] = '-';
  335.    if (st_mode & S_IXGRP) buf[6] = 'x';
  336.    else    buf[6] = '-';
  337.    if (st_mode & S_IROTH) buf[7] = 'r';
  338.    else    buf[7] = '-';
  339.    if (st_mode & S_IWOTH) buf[8] = 'w';
  340.    else    buf[8] = '-';
  341.    if (st_mode & S_IXOTH) buf[9] = 'x';
  342.    else    buf[9] = '-';
  343. #endif                    /* UNIX */
  344. #if MSDOS
  345.    if ( st_mode & _S_IFIFO )      buf[0] = 'f';
  346.    else if ( st_mode & _S_IFCHR ) buf[0] = 'c';
  347.    else if ( st_mode & _S_IFDIR ) buf[0] = 'd';
  348.    else if ( st_mode & _S_IFREG ) buf[0] = '-';
  349.    else                     buf[0] = '\?';
  350.  
  351.    if (st_mode & S_IREAD) buf[1] = 'r';
  352.    else    buf[1] = '-';
  353.    if (st_mode & S_IWRITE) buf[2] = 'w';
  354.    else    buf[2] = '-';
  355.    if (st_mode & S_IEXEC) buf[3] = 'x';
  356.    else    buf[3] = '-';
  357.    if (st_mode & S_IREAD) buf[4] = 'r';
  358.    else    buf[4] = '-';
  359.    if (st_mode & S_IWRITE) buf[5] = 'w';
  360.    else    buf[5] = '-';
  361.    if (st_mode & S_IEXEC) buf[6] = 'x';
  362.    else    buf[6] = '-';
  363.    if (st_mode & S_IREAD) buf[7] = 'r';
  364.    else    buf[7] = '-';
  365.    if (st_mode & S_IWRITE) buf[8] = 'w';
  366.    else    buf[8] = '-';
  367.    if (st_mode & S_IEXEC) buf[9] = 'x';
  368.    else    buf[9] = '-';
  369. #endif                    /* MSDOS */
  370.  
  371.    buf[10] = '\0';
  372.  
  373.    return buf;
  374. }
  375.  
  376. #endif                    /* FAttrib */
  377. #endif                    /* MSWindows */
  378.