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