home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / icon / Source / Iconx / C / Rsys < prev   
Encoding:
Text File  |  1990-07-19  |  5.9 KB  |  290 lines

  1. /*
  2.  * File: rsys.c
  3.  *  Contents: [flushrec], [getrec], getstrg, host, longread, [putrec], putstr
  4.  */
  5.  
  6. #include "../h/config.h"
  7. #include "../h/rt.h"
  8. #include "rproto.h"
  9.  
  10. #if AMIGA
  11. #if LATTICE
  12. #include <ios1.h>
  13. #endif                    /* LATTICE */
  14. #endif                    /* AMIGA */
  15.  
  16. #ifdef RecordIO
  17. #if SASC
  18. #include <lcio.h>
  19. #endif                    /* SASC */
  20. #endif                    /* RecordIO */
  21.  
  22. #ifdef RecordIO
  23. /*
  24.  * flushrec - force buffered output to be written with a record break.
  25.  *  Applies only to files with mode "s".
  26.  */
  27.  
  28. novalue flushrec(fd)
  29. FILE *fd;
  30. {
  31. #if SASC
  32.    afwrite("", 1, 0, fd);
  33. #endif                    /* SASC */
  34. }
  35.  
  36. /*
  37.  * getrec - read a record into buf from file fd. At most maxi characters
  38.  *  are read.  getrec returns the length of the record.
  39.  *  Returns -1 if EOF and -2 if length was limited by
  40.  *  maxi. [[ Needs ferror() check. ]]
  41.  *  This function is meaningful only for files opened with mode "s".
  42.  */
  43.  
  44. int getrec(buf, maxi, fd)
  45. register char *buf;
  46. int maxi;
  47. FILE *fd;
  48.    {
  49. #ifdef SASC
  50.    register int l;
  51.  
  52.    l = afreadh(buf, 1, maxi+1, fd);     /* read record or maxi+1 chars */
  53.    if (l == 0) return -1;
  54.    if (l <= maxi) return l;
  55.    ungetc(buf[maxi], fd);               /* if record not used up, push
  56.                                            back last char read */
  57.    return -2;
  58. #endif                    /* SASC */
  59.    }
  60. #endif                    /* RecordIO */
  61.  
  62. /*
  63.  * getstrg - read a line into buf from file fd.  At most maxi characters
  64.  *  are read.  getstrg returns the length of the line, not counting
  65.  *  the newline.  Returns -1 if EOF and -2 if length was limited by
  66.  *  maxi. [[ Needs ferror() check. ]]
  67.  */
  68.  
  69. int getstrg(buf, maxi, fd)
  70. register char *buf;
  71. int maxi;
  72. FILE *fd;
  73.    {
  74.    register int c, l;
  75.  
  76.  
  77. #if AMIGA
  78. #if LATTICE
  79.    /* This code is special for Lattice 4.0.  It was different for
  80.     *  Lattice 3.10 and probably won't work for other C compilers.
  81.     */
  82.    extern struct UFB _ufbs[];
  83.  
  84.    if (IsInteractive(_ufbs[fileno(fd)].ufbfh))
  85.       return read(fileno(fd),buf,maxi);
  86. #endif                    /* LATTICE */
  87. #endif                    /* AMIGA */
  88.  
  89.  
  90.    l = 0;
  91.    while ((c = fgetc(fd)) != '\n') {
  92.       if (c == EOF)
  93.      if (l > 0) return l;
  94.      else return -1;
  95.       if (++l > maxi) {
  96.      ungetc(c, fd);
  97.      return -2;
  98.      }
  99.       *buf++ = c;
  100.       }
  101.    return l;
  102.    }
  103.  
  104. #ifdef UtsName
  105. #include <sys/utsname.h>
  106. #endif                    /* UtsName */
  107.  
  108. /*
  109.  * iconhost - return some sort of host name into the buffer pointed at
  110.  *  by hostname.  This code accommodates several different host name
  111.  *  fetching schemes.
  112.  */
  113. novalue iconhost(hostname)
  114. char *hostname;
  115.    {
  116.  
  117. #ifdef WhoHost
  118.    /*
  119.     * The host name is in /usr/include/whoami.h. (V7, 4.[01]bsd)
  120.     */
  121.    whohost(hostname);
  122. #endif                    /* WhoHost */
  123.  
  124. #ifdef UtsName
  125.    {
  126.    /*
  127.     * Use the uname system call.  (System III & V)
  128.     */
  129.    struct utsname utsn;
  130.    uname(&utsn);
  131.    strcpy(hostname,utsn.nodename);
  132.    }
  133. #endif                    /* UtsName */
  134.  
  135. #ifdef GetHost
  136.    /*
  137.     * Use the gethostname system call.  (4.2bsd)
  138.     */
  139.    gethostname(hostname,MaxCvtLen);
  140. #endif                    /* GetHost */
  141.  
  142. #if VMS
  143.    /*
  144.     * VMS has its own special logic.
  145.     */
  146.    char *h;
  147.    if (!(h = getenv("ICON$HOST")) && !(h = getenv("SYS$NODE")))
  148.       h = "VAX/VMS";
  149.    strcpy(hostname,h);
  150. #endif                    /* VMS */
  151.  
  152. #ifdef HostStr
  153.    /*
  154.     * The string constant HostStr contains the host name.
  155.     */
  156.    strcpy(hostname,HostStr);
  157. #endif                    /* HostStr */
  158.  
  159.    }
  160.  
  161. #ifdef WhoHost
  162. #define HdrFile "/usr/include/whoami.h"
  163.  
  164. /*
  165.  * whohost - look for a line of the form
  166.  *  #define sysname "name"
  167.  * in HdrFile and return the name.
  168.  */
  169. novalue whohost(hostname)
  170. char *hostname;
  171.    {
  172.    char buf[BUFSIZ];
  173.    FILE *fd;
  174.  
  175.    fd = fopen(HdrFile, ReadText);
  176.    if (fd == NULL) {
  177.       sprintf(buf, "Cannot open %s, no value for &host\n", HdrFile);
  178.       syserr(buf);
  179.    }
  180.  
  181.    for (;;) {   /* each line in the file */
  182.       if (fgets(buf, sizeof buf, fd) == NULL) {
  183.          sprintf(buf, "No #define for sysname in %s, no value for &host\n",
  184.             HdrFile);
  185.          syserr(buf);
  186.       }
  187.       if (sscanf(buf,"#define sysname \"%[^\"]\"", hostname) == 1) {
  188.          fclose(fd);
  189.          return;
  190.       }
  191.    }
  192.    }
  193. #endif                    /* WhoHost */
  194.  
  195. /*
  196.  * Read a long string in shorter parts. (Standard read may not handle long
  197.  *  strings.)
  198.  */
  199. word longread(s,width,len,fname)
  200. FILE *fname;
  201. int width;
  202. char *s;
  203. long len;
  204. {
  205.    long tally = 0;
  206.    long n = 0;
  207.  
  208.    while (len > 0) {
  209.       n = fread(s, width, (int)((len < MaxIn) ? len : MaxIn), fname);
  210.       if (n <= 0)
  211.          return tally;
  212.       tally += n;
  213.       s += n;
  214.       len -= n;
  215.       }  
  216.    return tally;
  217.    }
  218.  
  219. #ifdef RecordIO
  220. /*
  221.  * Write string referenced by descriptor d, avoiding a record break.
  222.  *  Applies only to files openend with mode "s".
  223.  */
  224.  
  225. int putrec(f, d)
  226. register FILE *f;
  227. dptr d;
  228.    {
  229. #if SASC
  230.    register char *s;
  231.    register word l;
  232.  
  233.    l = StrLen(*d);
  234.    if (l == 0)
  235.       return Success;
  236.    s = StrLoc(*d);
  237.  
  238.    if (afwriteh(s,1,l,f) < l)
  239.       return Failure;
  240.    else
  241.       return Success;
  242.    /*
  243.     * Note:  Because RecordIO depends on SASC, and because SASC
  244.     *  uses its own malloc rather than the Icon malloc, file usage
  245.     *  cannot cause a garbage collection.  This may require
  246.     *  reevaluation if RecordIO is supported for any other compiler.
  247.     */
  248. #endif                    /* SASC */
  249.    }
  250. #endif                    /* RecordIO */
  251.  
  252. /*
  253.  * Print string referenced by descriptor d. Note, d must not move during
  254.  *   a garbage collection.
  255.  */
  256.  
  257. int putstr(f, d)
  258. register FILE *f;
  259. dptr d;
  260.    {
  261.    register char *s;
  262.    register word l;
  263.  
  264.    l = StrLen(*d);
  265.    if (l == 0)
  266.       return  Success;
  267.    s = StrLoc(*d);
  268.  
  269. #ifdef FixedRegions
  270.    if (longwrite(s,l,f) < 0)
  271.       return Failure;
  272.    else
  273.       return Success;
  274. #else                    /* FixedRegions */
  275.    /*
  276.     * In expandable regions storage management, the first output to a file may
  277.     *  cause allocation, which in turn may cause a garbage collection, changing
  278.     *  where the string is.  So write one character and reload the address
  279.     *  of the string from the tended descriptor.
  280.     */
  281.  
  282.    putc(*s, f);
  283.    s = StrLoc(*d) + 1;
  284.    if (longwrite(s,--l,f) < 0)
  285.       return Failure;
  286.    else
  287.       return Success;
  288. #endif                    /* FixedRegions */
  289.    }
  290.