home *** CD-ROM | disk | FTP | other *** search
/ Boldly Go Collection / version40.iso / TS / 25D / XSCHEM20.ZIP / UNIXSTUF.C < prev    next >
C/C++ Source or Header  |  1989-09-14  |  3KB  |  177 lines

  1. /* unixstuff.c - unix specific routines */
  2.  
  3. #include "xscheme.h"
  4.  
  5. #define LBSIZE 200
  6.  
  7. /* external variables */
  8. extern LVAL s_unbound,true;
  9. extern FILE *tfp;
  10. extern int errno;
  11.  
  12. /* local variables */
  13. static char lbuf[LBSIZE];
  14. static int lindex;
  15. static int lcount;
  16. static long rseed = 1L;
  17.  
  18. /* osinit - initialize */
  19. osinit(banner)
  20.   char *banner;
  21. {
  22.     printf("%s\n",banner);
  23.     lindex = 0;
  24.     lcount = 0;
  25. }
  26.  
  27. /* osfinish - clean up before returning to the operating system */
  28. osfinish()
  29. {
  30. }
  31.  
  32. /* oserror - print an error message */
  33. oserror(msg)
  34.   char *msg;
  35. {
  36.     printf("error: %s\n",msg);
  37. }
  38.  
  39. /* osrand - return a random number between 0 and n-1 */
  40. int osrand(n)
  41.   int n;
  42. {
  43.     long k1;
  44.  
  45.     /* make sure we don't get stuck at zero */
  46.     if (rseed == 0L) rseed = 1L;
  47.  
  48.     /* algorithm taken from Dr. Dobbs Journal, November 1985, page 91 */
  49.     k1 = rseed / 127773L;
  50.     if ((rseed = 16807L * (rseed - k1 * 127773L) - k1 * 2836L) < 0L)
  51.     rseed += 2147483647L;
  52.  
  53.     /* return a random number between 0 and n-1 */
  54.     return ((int)(rseed % (long)n));
  55. }
  56.  
  57. /* osaopen - open an ascii file */
  58. FILE *osaopen(name,mode)
  59.   char *name,*mode;
  60. {
  61.     return (fopen(name,mode));
  62. }
  63.  
  64. /* osbopen - open a binary file */
  65. FILE *osbopen(name,mode)
  66.   char *name,*mode;
  67. {
  68.     return (fopen(name,mode));
  69. }
  70.  
  71. /* osclose - close a file */
  72. int osclose(fp)
  73.   FILE *fp;
  74. {
  75.     return (fclose(fp));
  76. }
  77.  
  78. /* ostell - get the current file position */
  79. long ostell(fp)
  80.   FILE *fp;
  81. {
  82.     return (ftell(fp));
  83. }
  84.  
  85. /* osseek - set the current file position */
  86. int osseek(fp,offset,whence)
  87.   FILE *fp; long offset; int whence;
  88. {
  89.     return (fseek(fp,offset,whence));
  90. }
  91.  
  92. /* osagetc - get a character from an ascii file */
  93. int osagetc(fp)
  94.   FILE *fp;
  95. {
  96.     return (getc(fp));
  97. }
  98.  
  99. /* osaputc - put a character to an ascii file */
  100. int osaputc(ch,fp)
  101.   int ch; FILE *fp;
  102. {
  103.     return (putc(ch,fp));
  104. }
  105.  
  106. /* osbgetc - get a character from a binary file */
  107. int osbgetc(fp)
  108.   FILE *fp;
  109. {
  110.     return (getc(fp));
  111. }
  112.  
  113. /* osbputc - put a character to a binary file */
  114. int osbputc(ch,fp)
  115.   int ch; FILE *fp;
  116. {
  117.     return (putc(ch,fp));
  118. }
  119.  
  120. /* ostgetc - get a character from the terminal */
  121. int ostgetc()
  122. {
  123.     /* check for a buffered character */
  124.     if (lcount--)
  125.     return (lbuf[lindex++]);
  126.  
  127.     /* get an input line */
  128.     do {
  129.     fgets(lbuf,LBSIZE,stdin);
  130.     } while ((lcount = strlen(lbuf)) == 0);
  131.  
  132.     /* write it to the transcript file */
  133.     if (tfp)
  134.     for (lindex = 0; lindex < lcount; ++lindex)
  135.         osaputc(lbuf[lindex],tfp);
  136.     lindex = 0; lcount--;
  137.  
  138.     /* return the first character */
  139.     return (lbuf[lindex++]);
  140. }
  141.  
  142. /* ostputc - put a character to the terminal */
  143. ostputc(ch)
  144.   int ch;
  145. {
  146.     /* check for control characters */
  147.     oscheck();
  148.  
  149.     /* output the character */
  150.     putchar(ch);
  151.  
  152.     /* output the character to the transcript file */
  153.     if (tfp)
  154.     osaputc(ch,tfp);
  155. }
  156.  
  157. /* osflush - flush the terminal input buffer */
  158. osflush()
  159. {
  160.     lindex = lcount = 0;
  161. }
  162.  
  163. /* oscheck - check for control characters during execution */
  164. oscheck()
  165. {
  166. }
  167.  
  168. /* xsystem - execute a system command */
  169. LVAL xsystem()
  170. {
  171.     char *cmd="sh";
  172.     if (moreargs())
  173.     cmd = (char *)getstring(xlgastring());
  174.     xllastarg();
  175.     return (system(cmd) == 0 ? true : cvfixnum((FIXTYPE)errno));
  176. }
  177.