summaryrefslogtreecommitdiff
path: root/src/runtime/rsys.r
blob: f4bdfc183d6877749d14b578c5714e4cbbbefd93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * File: rsys.r
 *  Contents: getstrg, host, longread, putstr
 */

/*
 * getstrg - read a line into buf from file fbp.  At most maxi characters
 *  are read.  getstrg returns the length of the line, not counting the
 *  newline.  Returns -1 if EOF and -2 if length was limited by maxi.
 *  Discards \r before \n in translated mode.  [[ Needs ferror() check. ]]
 */

int getstrg(buf, maxi, fbp)
register char *buf;
int maxi;
struct b_file *fbp;
   {
   register int c, l;
   FILE *fd;

   fd = fbp->fd;

   #ifdef XWindows
      if (isatty(fileno(fd))) wflushall();
   #endif				/* XWindows */

   l = 0;
   while (1) {

      #ifdef Graphics
         /* insert non-blocking read/code to service windows here */
      #endif				/* Graphics */

      if ((c = fgetc(fd)) == '\n')	/* \n terminates line */
	 break;
      if (c == '\r' && (fbp->status & Fs_Untrans) == 0) {
	 /* \r terminates line in translated mode */
	 if ((c = fgetc(fd)) != '\n')	/* consume following \n */
	     ungetc(c, fd);		/* (put back if not \n) */
	 break;
	 }
      if (c == EOF) {
	 if (l > 0) return l;
	 else return -1;
         }
      if (++l > maxi) {
	 ungetc(c, fd);
	 return -2;
	 }
      *buf++ = c;
      }
   return l;
   }

/*
 * iconhost - return some sort of host name into the buffer pointed at
 *  by hostname.  This code accommodates several different host name
 *  fetching schemes.
 */
void iconhost(hostname)
char *hostname;
   {
   /*
    * Use the uname system call.  (POSIX)
    */
   struct utsname utsn;
   uname(&utsn);
   strcpy(hostname,utsn.nodename);
   }

/*
 * Read a long string in shorter parts. (Standard read may not handle long
 *  strings.)
 */
word longread(s,width,len,fd)
FILE *fd;
int width;
char *s;
long len;
{
   tended char *ts = s;
   long tally = 0;
   long n = 0;

#ifdef XWindows
   if (isatty(fileno(fd))) wflushall();
#endif					/* XWindows */

   while (len > 0) {
      n = fread(ts, width, (int)((len < MaxIn) ? len : MaxIn), fd);
      if (n <= 0) {
         return tally;
	 }
      tally += n;
      ts += n;
      len -= n;
      }
   return tally;
   }

/*
 * Print string referenced by descriptor d. Note, d must not move during
 *   a garbage collection.
 */

int putstr(f, d)
register FILE *f;
dptr d;
   {
   register char *s;
   register word l;

   l = StrLen(*d);
   if (l == 0)
      return  Succeeded;
   s = StrLoc(*d);
   if (longwrite(s,l,f) < 0)
      return Failed;
   else
      return Succeeded;
   }

/*
 * idelay(n) - delay for n milliseconds
 */
int idelay(n)
int n;
   {
   #if MSWIN
      Sleep(n);
      return Succeeded;
   #else				/* MSWIN */
      struct timeval t;
      t.tv_sec = n / 1000;
      t.tv_usec = (n % 1000) * 1000;
      select(1, NULL, NULL, NULL, &t);
      return Succeeded;
   #endif				/* MSWIN */
   }

#ifdef KeyboardFncs

/*
 * Documentation notwithstanding, the Unix versions of the keyboard functions
 * read from standard input and not necessarily from the keyboard (/dev/tty).
 */
#define STDIN 0

/*
 * int getch() -- read character without echoing
 * int getche() -- read character with echoing
 *
 * Read and return a character from standard input in non-canonical
 * ("cbreak") mode.  Return -1 for EOF.
 *
 * Reading is done even if stdin is not a tty;
 * the tty get/set functions are just rejected by the system.
 */

int rchar(int with_echo);

int getch(void)		{ return rchar(0); }
int getche(void)	{ return rchar(1); }

int rchar(int with_echo)
{
   struct termios otty, tty;
   char c;
   int n;

   tcgetattr(STDIN, &otty);		/* get current tty attributes */

   tty = otty;
   tty.c_lflag &= ~ICANON;
   if (with_echo)
      tty.c_lflag |= ECHO;
   else
      tty.c_lflag &= ~ECHO;
   tcsetattr(STDIN, TCSANOW, &tty);	/* set temporary attributes */

   n = read(STDIN, &c, 1);		/* read one char from stdin */

   tcsetattr(STDIN, TCSANOW, &otty);	/* reset tty to original state */

   if (n == 1)				/* if read succeeded */
      return c & 0xFF;
   else
      return -1;
}

/*
 * kbhit() -- return nonzero if characters are available for getch/getche.
 */
int kbhit(void)
{
   struct termios otty, tty;
   fd_set fds;
   struct timeval tv;
   int rv;

   tcgetattr(STDIN, &otty);		/* get current tty attributes */

   tty = otty;
   tty.c_lflag &= ~ICANON;		/* disable input batching */
   tcsetattr(STDIN, TCSANOW, &tty);	/* set attribute temporarily */

   FD_ZERO(&fds);			/* initialize fd struct */
   FD_SET(STDIN, &fds);			/* set STDIN bit */
   tv.tv_sec = tv.tv_usec = 0;		/* set immediate return */
   rv = select(STDIN + 1, &fds, NULL, NULL, &tv);

   tcsetattr(STDIN, TCSANOW, &otty);	/* reset tty to original state */

   return rv;				/* return result */
}

#endif					/* KeyboardFncs */

#ifdef FAttrib
/*
 * make_mode takes mode_t type (an integer) input and returns the
 * file permission in the format of a string.
*/
char *make_mode (mode_t st_mode)
{
   char *buf;

   if ( (buf = (char *) malloc(sizeof(char)*11)) == NULL ) {
      fprintf(stderr,"fatal malloc error\n");
      return NULL;
   }

   if ( st_mode & S_IFIFO )      buf[0] = 'f';
   else if ( st_mode & S_IFCHR ) buf[0] = 'c';
   else if ( st_mode & S_IFDIR ) buf[0] = 'd';
   else if ( st_mode & S_IFREG ) buf[0] = '-';
   else			         buf[0] = '\?';

   if (st_mode & S_IREAD)  buf[1] = 'r';  else buf[1] = '-';
   if (st_mode & S_IWRITE) buf[2] = 'w';  else buf[2] = '-';
   if (st_mode & S_IEXEC)  buf[3] = 'x';  else buf[3] = '-';
   if (st_mode & S_IREAD)  buf[4] = 'r';  else buf[4] = '-';
   if (st_mode & S_IWRITE) buf[5] = 'w';  else buf[5] = '-';
   if (st_mode & S_IEXEC)  buf[6] = 'x';  else buf[6] = '-';
   if (st_mode & S_IREAD)  buf[7] = 'r';  else buf[7] = '-';
   if (st_mode & S_IWRITE) buf[8] = 'w';  else buf[8] = '-';
   if (st_mode & S_IEXEC)  buf[9] = 'x';  else buf[9] = '-';

   buf[10] = '\0';
   return buf;
}
#endif					/* FAttrib */