Declare Fcompare_strings.
[bpt/emacs.git] / src / sysdep.c
CommitLineData
86a5659e 1/* Interfaces to system-dependent kernel and library entries.
2ac53dfa 2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
86a5659e
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
7088d1ca 8the Free Software Foundation; either version 2, or (at your option)
86a5659e
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
86a5659e
JB
20
21
22#include <signal.h>
23#include <setjmp.h>
24
18160b98 25#include <config.h>
86a5659e 26#include "lisp.h"
9ac0d9e0 27#include "blockinput.h"
86a5659e
JB
28#undef NULL
29
30#define min(x,y) ((x) > (y) ? (y) : (x))
31
32/* In this file, open, read and write refer to the system calls,
33 not our sugared interfaces sys_open, sys_read and sys_write.
34 Contrariwise, for systems where we use the system calls directly,
35 define sys_read, etc. here as aliases for them. */
36#ifndef read
37#define sys_read read
38#define sys_write write
39#endif /* `read' is not a macro */
40
41#undef read
42#undef write
43
fe03522b
RS
44#ifdef WINDOWSNT
45#define read _read
46#define write _write
47#include <windows.h>
48extern int errno;
49#endif /* not WINDOWSNT */
50
86a5659e
JB
51#ifndef close
52#define sys_close close
53#else
54#undef close
55#endif
56
57#ifndef open
58#define sys_open open
59#else /* `open' is a macro */
60#undef open
61#endif /* `open' is a macro */
62
986ffb24
JB
63/* Does anyone other than VMS need this? */
64#ifndef fwrite
65#define sys_fwrite fwrite
66#else
67#undef fwrite
68#endif
69
e24f1d55
RS
70#ifndef HAVE_H_ERRNO
71extern int h_errno;
72#endif
73
86a5659e
JB
74#include <stdio.h>
75#include <sys/types.h>
76#include <sys/stat.h>
77#include <errno.h>
78
dca8521c
RS
79/* Get _POSIX_VDISABLE, if it is available. */
80#ifdef HAVE_UNISTD_H
81#include <unistd.h>
82#endif
83
b05af5d3
PE
84/* Get SI_SRPC_DOMAIN, if it is available. */
85#ifdef HAVE_SYS_SYSTEMINFO_H
86#include <sys/systeminfo.h>
87#endif
88
207bdbdb
RS
89#ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
90#include <dos.h>
91#include "dosfns.h"
92#include "msdos.h"
93#include <sys/param.h>
15614e61
RS
94
95#if __DJGPP__ > 1
96extern int etext;
97extern unsigned start __asm__ ("start");
98#endif
207bdbdb
RS
99#endif
100
86a5659e 101extern int errno;
86a5659e 102
bb4bc8e2
RM
103#ifdef VMS
104#include <rms.h>
105#include <ttdef.h>
106#include <tt2def.h>
107#include <iodef.h>
108#include <ssdef.h>
109#include <descrip.h>
110#include <fibdef.h>
111#include <atrdef.h>
112#include <ctype.h>
113#include <string.h>
114#ifdef __GNUC__
115#include <sys/file.h>
116#else
117#include <file.h>
118#endif
119#undef F_SETFL
120#ifndef RAB$C_BID
121#include <rab.h>
122#endif
fe03522b 123#define MAXIOSIZE (32 * PAGESIZE) /* Don't I/O more than 32 blocks at a time */
bb4bc8e2
RM
124#endif /* VMS */
125
126#ifndef BSD4_1
2a633456 127#ifdef BSD_SYSTEM /* avoid writing defined (BSD_SYSTEM) || defined (USG)
bb4bc8e2
RM
128 because the vms compiler doesn't grok `defined' */
129#include <fcntl.h>
130#endif
131#ifdef USG
132#ifndef USG5
133#include <fcntl.h>
134#endif
135#endif
136#endif /* not 4.1 bsd */
86a5659e 137
207bdbdb 138#ifndef MSDOS
86a5659e 139#include <sys/ioctl.h>
207bdbdb 140#endif
2a633456 141
e04a4e0d 142#include "systty.h"
94c8642a 143#include "syswait.h"
86a5659e 144
86a5659e
JB
145#ifdef BROKEN_TIOCGWINSZ
146#undef TIOCGWINSZ
8dca3179 147#undef TIOCSWINSZ
86a5659e
JB
148#endif
149
b917b2e2 150#if defined(USG) || defined(DGUX)
86a5659e
JB
151#include <sys/utsname.h>
152#include <string.h>
153#ifndef MEMORY_IN_STRING_H
154#include <memory.h>
155#endif
81444907 156#if defined (TIOCGWINSZ) || defined (ISC4_0)
86a5659e
JB
157#ifdef NEED_SIOCTL
158#include <sys/sioctl.h>
159#endif
160#ifdef NEED_PTEM_H
161#include <sys/stream.h>
162#include <sys/ptem.h>
163#endif
81444907 164#endif /* TIOCGWINSZ or ISC4_0 */
b917b2e2 165#endif /* USG or DGUX */
86a5659e 166
86a5659e
JB
167extern int quit_char;
168
0137dbf7 169#include "frame.h"
86a5659e
JB
170#include "window.h"
171#include "termhooks.h"
172#include "termchar.h"
173#include "termopts.h"
174#include "dispextern.h"
175#include "process.h"
176
fe03522b
RS
177#ifdef WINDOWSNT
178#include <direct.h>
179/* In process.h which conflicts with the local copy. */
180#define _P_WAIT 0
181int _CRTAPI1 _spawnlp (int, const char *, const char *, ...);
182int _CRTAPI1 _getpid (void);
183#endif
184
86a5659e
JB
185#ifdef NONSYSTEM_DIR_LIBRARY
186#include "ndir.h"
187#endif /* NONSYSTEM_DIR_LIBRARY */
188
91bac16a
JB
189#include "syssignal.h"
190#include "systime.h"
d79998bc
KH
191#ifdef HAVE_UTIME_H
192#include <utime.h>
193#endif
194
195#ifndef HAVE_UTIMES
196#ifndef HAVE_STRUCT_UTIMBUF
197/* We want to use utime rather than utimes, but we couldn't find the
198 structure declaration. We'll use the traditional one. */
199struct utimbuf {
200 long actime;
201 long modtime;
202};
203#endif
204#endif
86a5659e 205
e336874b
KH
206#ifndef VFORK_RETURN_TYPE
207#define VFORK_RETURN_TYPE int
208#endif
209
a00d5589
RS
210/* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
211#ifndef LPASS8
212#define LPASS8 0
213#endif
214
215#ifdef BSD4_1
216#define LNOFLSH 0100000
217#endif
218
86a5659e
JB
219static int baud_convert[] =
220#ifdef BAUD_CONVERT
221 BAUD_CONVERT;
222#else
223 {
224 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
225 1800, 2400, 4800, 9600, 19200, 38400
226 };
227#endif
228
ba5971b3
RS
229#if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
230extern short ospeed;
231#else
515b04d0
RS
232#if defined (HAVE_TERMIOS_H) && defined (LINUX)
233#include <termios.h>
825e7e55
RS
234/* HJL's version of libc is said to need this on the Alpha.
235 On the other hand, DEC OSF1 on the Alpha needs ospeed to be a short. */
f2361c60
RS
236extern speed_t ospeed;
237#else
9f80a1c8 238extern short ospeed;
f2361c60 239#endif
ba5971b3 240#endif
86a5659e 241
91bac16a 242/* The file descriptor for Emacs's input terminal.
0217ed57
RS
243 Under Unix, this is normally zero except when using X;
244 under VMS, we place the input channel number here. */
245int input_fd;
b9c4113e
RS
246
247void croak P_ ((char *));
248
249
64e971c3
RS
250\f
251/* Specify a different file descriptor for further input operations. */
252
253void
254change_input_fd (fd)
255 int fd;
256{
257 input_fd = fd;
258}
259
260/* Discard pending input on descriptor input_fd. */
91bac16a 261
08633194 262void
86a5659e
JB
263discard_tty_input ()
264{
fe03522b 265#ifndef WINDOWSNT
91bac16a 266 struct emacs_tty buf;
86a5659e
JB
267
268 if (noninteractive)
269 return;
270
271 /* Discarding input is not safe when the input could contain
272 replies from the X server. So don't do it. */
273 if (read_socket_hook)
274 return;
275
276#ifdef VMS
277 end_kbd_input ();
91bac16a
JB
278 SYS$QIOW (0, input_fd, IO$_READVBLK|IO$M_PURGE, input_iosb, 0, 0,
279 &buf.main, 0, 0, terminator_mask, 0, 0);
86a5659e
JB
280 queue_kbd_input ();
281#else /* not VMS */
282#ifdef APOLLO
283 {
284 int zero = 0;
64e971c3 285 ioctl (input_fd, TIOCFLUSH, &zero);
86a5659e
JB
286 }
287#else /* not Apollo */
fe03522b 288#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
207bdbdb 289 while (dos_keyread () != -1)
fe03522b 290 ;
207bdbdb 291#else /* not MSDOS */
91bac16a
JB
292 EMACS_GET_TTY (input_fd, &buf);
293 EMACS_SET_TTY (input_fd, &buf, 0);
207bdbdb 294#endif /* not MSDOS */
86a5659e
JB
295#endif /* not Apollo */
296#endif /* not VMS */
fe03522b 297#endif /* not WINDOWSNT */
86a5659e
JB
298}
299
300#ifdef SIGTSTP
301
64e971c3
RS
302/* Arrange for character C to be read as the next input from
303 the terminal. */
304
dfcf069d 305void
86a5659e
JB
306stuff_char (c)
307 char c;
308{
23dab951
RS
309 if (read_socket_hook)
310 return;
311
86a5659e
JB
312/* Should perhaps error if in batch mode */
313#ifdef TIOCSTI
64e971c3 314 ioctl (input_fd, TIOCSTI, &c);
86a5659e 315#else /* no TIOCSTI */
71f06467 316 error ("Cannot stuff terminal input characters in this version of Unix");
86a5659e
JB
317#endif /* no TIOCSTI */
318}
319
320#endif /* SIGTSTP */
64e971c3 321\f
08633194 322void
86a5659e
JB
323init_baud_rate ()
324{
86a5659e
JB
325 if (noninteractive)
326 ospeed = 0;
327 else
328 {
dfc366c9
RS
329#ifdef INIT_BAUD_RATE
330 INIT_BAUD_RATE ();
331#else
fe03522b 332#ifdef DOS_NT
207bdbdb 333 ospeed = 15;
fe03522b 334#else /* not DOS_NT */
86a5659e 335#ifdef VMS
91bac16a
JB
336 struct sensemode sg;
337
338 SYS$QIOW (0, input_fd, IO$_SENSEMODE, &sg, 0, 0,
86a5659e 339 &sg.class, 12, 0, 0, 0, 0 );
91bac16a
JB
340 ospeed = sg.xmit_baud;
341#else /* not VMS */
e04a4e0d
JB
342#ifdef HAVE_TERMIOS
343 struct termios sg;
91bac16a 344
71f06467 345 sg.c_cflag = B9600;
64e971c3 346 tcgetattr (input_fd, &sg);
d7272cff 347 ospeed = cfgetospeed (&sg);
5baff9ce 348#if defined (USE_GETOBAUD) && defined (getobaud)
2f43149b
RS
349 /* m88k-motorola-sysv3 needs this (ghazi@noc.rutgers.edu) 9/1/94. */
350 if (ospeed == 0)
351 ospeed = getobaud (sg.c_cflag);
352#endif
e04a4e0d
JB
353#else /* neither VMS nor TERMIOS */
354#ifdef HAVE_TERMIO
355 struct termio sg;
91bac16a 356
71f06467 357 sg.c_cflag = B9600;
e04a4e0d 358#ifdef HAVE_TCATTR
64e971c3 359 tcgetattr (input_fd, &sg);
e04a4e0d 360#else
6c65530f 361 ioctl (input_fd, TCGETA, &sg);
e04a4e0d 362#endif
91bac16a 363 ospeed = sg.c_cflag & CBAUD;
e04a4e0d 364#else /* neither VMS nor TERMIOS nor TERMIO */
91bac16a
JB
365 struct sgttyb sg;
366
367 sg.sg_ospeed = B9600;
64e971c3 368 if (ioctl (input_fd, TIOCGETP, &sg) < 0)
d7272cff 369 abort ();
91bac16a 370 ospeed = sg.sg_ospeed;
91bac16a 371#endif /* not HAVE_TERMIO */
e04a4e0d 372#endif /* not HAVE_TERMIOS */
86a5659e 373#endif /* not VMS */
fe03522b 374#endif /* not DOS_NT */
dfc366c9 375#endif /* not INIT_BAUD_RATE */
86a5659e
JB
376 }
377
378 baud_rate = (ospeed < sizeof baud_convert / sizeof baud_convert[0]
fe03522b 379 ? baud_convert[ospeed] : 9600);
86a5659e
JB
380 if (baud_rate == 0)
381 baud_rate = 1200;
382}
383
384/*ARGSUSED*/
dfcf069d 385void
86a5659e
JB
386set_exclusive_use (fd)
387 int fd;
388{
389#ifdef FIOCLEX
390 ioctl (fd, FIOCLEX, 0);
391#endif
392 /* Ok to do nothing if this feature does not exist */
393}
64e971c3 394\f
86a5659e
JB
395#ifndef subprocesses
396
397wait_without_blocking ()
398{
2a633456 399#ifdef BSD_SYSTEM
86a5659e
JB
400 wait3 (0, WNOHANG | WUNTRACED, 0);
401#else
402 croak ("wait_without_blocking");
403#endif
404 synch_process_alive = 0;
405}
406
407#endif /* not subprocesses */
408
409int wait_debugging; /* Set nonzero to make following function work under dbx
fe03522b 410 (at least for bsd). */
86a5659e
JB
411
412SIGTYPE
413wait_for_termination_signal ()
414{}
415
416/* Wait for subprocess with process id `pid' to terminate and
417 make sure it will get eliminated (not remain forever as a zombie) */
418
08633194 419void
86a5659e
JB
420wait_for_termination (pid)
421 int pid;
422{
423 while (1)
424 {
425#ifdef subprocesses
426#ifdef VMS
427 int status;
428
986ffb24 429 status = SYS$FORCEX (&pid, 0, 0);
86a5659e
JB
430 break;
431#else /* not VMS */
2a633456 432#if defined (BSD_SYSTEM) || (defined (HPUX) && !defined (HPUX_5))
4c8975ad
RS
433 /* Note that kill returns -1 even if the process is just a zombie now.
434 But inevitably a SIGCHLD interrupt should be generated
435 and child_sig will do wait3 and make the process go away. */
436 /* There is some indication that there is a bug involved with
437 termination of subprocesses, perhaps involving a kernel bug too,
438 but no idea what it is. Just as a hunch we signal SIGCHLD to see
439 if that causes the problem to go away or get worse. */
440 sigsetmask (sigmask (SIGCHLD));
441 if (0 > kill (pid, 0))
fe03522b 442 {
4c8975ad
RS
443 sigsetmask (SIGEMPTYMASK);
444 kill (getpid (), SIGCHLD);
445 break;
446 }
447 if (wait_debugging)
448 sleep (1);
449 else
450 sigpause (SIGEMPTYMASK);
2a633456 451#else /* not BSD_SYSTEM, and not HPUX version >= 6 */
05695621 452#if defined (UNIPLUS)
4c8975ad
RS
453 if (0 > kill (pid, 0))
454 break;
455 wait (0);
2a633456 456#else /* neither BSD_SYSTEM nor UNIPLUS: random sysV */
fe03522b 457#ifdef POSIX_SIGNALS /* would this work for LINUX as well? */
9ab714c7
RS
458 sigblock (sigmask (SIGCHLD));
459 if (0 > kill (pid, 0))
460 {
461 sigunblock (sigmask (SIGCHLD));
462 break;
463 }
92c995de 464 sigpause (SIGEMPTYMASK);
9ab714c7 465#else /* not POSIX_SIGNALS */
4c8975ad
RS
466#ifdef HAVE_SYSV_SIGPAUSE
467 sighold (SIGCHLD);
468 if (0 > kill (pid, 0))
469 {
470 sigrelse (SIGCHLD);
471 break;
472 }
473 sigpause (SIGCHLD);
474#else /* not HAVE_SYSV_SIGPAUSE */
fe03522b
RS
475#ifdef WINDOWSNT
476 wait (0);
477 break;
478#else /* not WINDOWSNT */
4c8975ad 479 if (0 > kill (pid, 0))
86a5659e 480 break;
4c8975ad
RS
481 /* Using sleep instead of pause avoids timing error.
482 If the inferior dies just before the sleep,
483 we lose just one second. */
484 sleep (1);
fe03522b 485#endif /* not WINDOWSNT */
4c8975ad 486#endif /* not HAVE_SYSV_SIGPAUSE */
9ab714c7 487#endif /* not POSIX_SIGNALS */
4c8975ad 488#endif /* not UNIPLUS */
2a633456 489#endif /* not BSD_SYSTEM, and not HPUX version >= 6 */
86a5659e
JB
490#endif /* not VMS */
491#else /* not subprocesses */
15614e61
RS
492#if __DJGPP__ > 1
493 break;
494#else /* not __DJGPP__ > 1 */
86a5659e
JB
495#ifndef BSD4_1
496 if (kill (pid, 0) < 0)
497 break;
498 wait (0);
499#else /* BSD4_1 */
500 int status;
501 status = wait (0);
502 if (status == pid || status == -1)
503 break;
504#endif /* BSD4_1 */
15614e61 505#endif /* not __DJGPP__ > 1*/
86a5659e
JB
506#endif /* not subprocesses */
507 }
508}
509
510#ifdef subprocesses
511
512/*
513 * flush any pending output
514 * (may flush input as well; it does not matter the way we use it)
515 */
516
08633194 517void
86a5659e
JB
518flush_pending_output (channel)
519 int channel;
520{
521#ifdef HAVE_TERMIOS
522 /* If we try this, we get hit with SIGTTIN, because
523 the child's tty belongs to the child's pgrp. */
524#else
525#ifdef TCFLSH
526 ioctl (channel, TCFLSH, 1);
527#else
528#ifdef TIOCFLUSH
529 int zero = 0;
530 /* 3rd arg should be ignored
531 but some 4.2 kernels actually want the address of an int
532 and nonzero means something different. */
533 ioctl (channel, TIOCFLUSH, &zero);
534#endif
535#endif
536#endif
537}
64e971c3 538\f
86a5659e
JB
539#ifndef VMS
540/* Set up the terminal at the other end of a pseudo-terminal that
541 we will be controlling an inferior through.
542 It should not echo or do line-editing, since that is done
543 in Emacs. No padding needed for insertion into an Emacs buffer. */
544
08633194 545void
86a5659e
JB
546child_setup_tty (out)
547 int out;
548{
fe03522b 549#ifndef DOS_NT
91bac16a
JB
550 struct emacs_tty s;
551
552 EMACS_GET_TTY (out, &s);
86a5659e 553
31be8d24 554#if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
91bac16a
JB
555 s.main.c_oflag |= OPOST; /* Enable output postprocessing */
556 s.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL on output */
9d4e5eea 557#ifdef NLDLY
91bac16a
JB
558 s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
559 /* No output delays */
9d4e5eea 560#endif
91bac16a
JB
561 s.main.c_lflag &= ~ECHO; /* Disable echo */
562 s.main.c_lflag |= ISIG; /* Enable signals */
9d4e5eea
RS
563#ifdef IUCLC
564 s.main.c_iflag &= ~IUCLC; /* Disable downcasing on input. */
565#endif
f7097b2a
RS
566#ifdef ISTRIP
567 s.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
568#endif
23e4c8be 569#ifdef OLCUC
9d4e5eea
RS
570 s.main.c_oflag &= ~OLCUC; /* Disable upcasing on output. */
571#endif
f7097b2a 572 s.main.c_oflag &= ~TAB3; /* Disable tab expansion */
1bf96fb5 573 s.main.c_cflag = (s.main.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */
91bac16a 574#if 0
eb8c3be9 575 /* Said to be unnecessary: */
91bac16a
JB
576 s.main.c_cc[VMIN] = 1; /* minimum number of characters to accept */
577 s.main.c_cc[VTIME] = 0; /* wait forever for at least 1 character */
578#endif
579
580 s.main.c_lflag |= ICANON; /* Enable erase/kill and eof processing */
581 s.main.c_cc[VEOF] = 04; /* insure that EOF is Control-D */
441f6399
RS
582 s.main.c_cc[VERASE] = CDISABLE; /* disable erase processing */
583 s.main.c_cc[VKILL] = CDISABLE; /* disable kill processing */
91bac16a 584
86a5659e 585#ifdef HPUX
91bac16a 586 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
86a5659e 587#endif /* HPUX */
91bac16a 588
86a5659e
JB
589#ifdef AIX
590/* AIX enhanced edit loses NULs, so disable it */
591#ifndef IBMR2AIX
91bac16a
JB
592 s.main.c_line = 0;
593 s.main.c_iflag &= ~ASCEDIT;
86a5659e
JB
594#endif
595 /* Also, PTY overloads NUL and BREAK.
596 don't ignore break, but don't signal either, so it looks like NUL. */
91bac16a
JB
597 s.main.c_iflag &= ~IGNBRK;
598 s.main.c_iflag &= ~BRKINT;
599 /* QUIT and INTR work better as signals, so disable character forms */
91bac16a 600 s.main.c_cc[VINTR] = 0377;
e6cc3307
RS
601#ifdef SIGNALS_VIA_CHARACTERS
602 /* the QUIT and INTR character are used in process_send_signal
603 so set them here to something useful. */
604 if (s.main.c_cc[VQUIT] == 0377)
605 s.main.c_cc[VQUIT] = '\\'&037; /* Control-\ */
606 if (s.main.c_cc[VINTR] == 0377)
607 s.main.c_cc[VINTR] = 'C'&037; /* Control-C */
608#else /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
609 /* QUIT and INTR work better as signals, so disable character forms */
610 s.main.c_cc[VQUIT] = 0377;
611 s.main.c_cc[VINTR] = 0377;
91bac16a 612 s.main.c_lflag &= ~ISIG;
e6cc3307
RS
613#endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
614 s.main.c_cc[VEOL] = 0377;
91bac16a 615 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
86a5659e
JB
616#endif /* AIX */
617
618#else /* not HAVE_TERMIO */
91bac16a
JB
619
620 s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
621 | CBREAK | TANDEM);
a00d5589 622 s.main.sg_flags |= LPASS8;
91bac16a
JB
623 s.main.sg_erase = 0377;
624 s.main.sg_kill = 0377;
1bf96fb5 625 s.lmode = LLITOUT | s.lmode; /* Don't strip 8th bit */
91bac16a 626
86a5659e
JB
627#endif /* not HAVE_TERMIO */
628
91bac16a 629 EMACS_SET_TTY (out, &s, 0);
86a5659e
JB
630
631#ifdef BSD4_1
632 if (interrupt_input)
633 reset_sigio ();
634#endif /* BSD4_1 */
635#ifdef RTU
636 {
637 int zero = 0;
638 ioctl (out, FIOASYNC, &zero);
639 }
640#endif /* RTU */
fe03522b 641#endif /* not DOS_NT */
86a5659e
JB
642}
643#endif /* not VMS */
644
645#endif /* subprocesses */
64e971c3 646\f
86a5659e
JB
647/* Record a signal code and the handler for it. */
648struct save_signal
649{
650 int code;
35a05cca 651 SIGTYPE (*handler) P_ ((int));
86a5659e
JB
652};
653
35a05cca
AS
654static void save_signal_handlers P_ ((struct save_signal *));
655static void restore_signal_handlers P_ ((struct save_signal *));
656
86a5659e
JB
657/* Suspend the Emacs process; give terminal to its superior. */
658
08633194 659void
86a5659e
JB
660sys_suspend ()
661{
662#ifdef VMS
88191e36
RS
663 /* "Foster" parentage allows emacs to return to a subprocess that attached
664 to the current emacs as a cheaper than starting a whole new process. This
665 is set up by KEPTEDITOR.COM. */
666 unsigned long parent_id, foster_parent_id;
667 char *fpid_string;
668
669 fpid_string = getenv ("EMACS_PARENT_PID");
670 if (fpid_string != NULL)
671 {
672 sscanf (fpid_string, "%x", &foster_parent_id);
673 if (foster_parent_id != 0)
674 parent_id = foster_parent_id;
675 else
676 parent_id = getppid ();
677 }
678 else
679 parent_id = getppid ();
680
9ac0d9e0 681 xfree (fpid_string); /* On VMS, this was malloc'd */
86a5659e 682
86a5659e
JB
683 if (parent_id && parent_id != 0xffffffff)
684 {
685 SIGTYPE (*oldsig)() = (int) signal (SIGINT, SIG_IGN);
686 int status = LIB$ATTACH (&parent_id) & 1;
687 signal (SIGINT, oldsig);
688 return status;
689 }
690 else
691 {
692 struct {
693 int l;
694 char *a;
695 } d_prompt;
696 d_prompt.l = sizeof ("Emacs: "); /* Our special prompt */
697 d_prompt.a = "Emacs: "; /* Just a reminder */
986ffb24 698 LIB$SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt, 0);
86a5659e
JB
699 return 1;
700 }
701 return -1;
702#else
207bdbdb 703#if defined(SIGTSTP) && !defined(MSDOS)
86a5659e 704
5a570e37 705 {
e89a2cd5 706 int pgrp = EMACS_GETPGRP (0);
5a570e37
JB
707 EMACS_KILLPG (pgrp, SIGTSTP);
708 }
86a5659e
JB
709
710#else /* No SIGTSTP */
711#ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
712 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
713 kill (getpid (), SIGQUIT);
714
715#else /* No SIGTSTP or USG_JOBCTRL */
716
717/* On a system where suspending is not implemented,
718 instead fork a subshell and let it talk directly to the terminal
719 while we wait. */
a0932daa
KH
720 sys_subshell ();
721
722#endif /* no USG_JOBCTRL */
723#endif /* no SIGTSTP */
724#endif /* not VMS */
725}
726
727/* Fork a subshell. */
728
08633194 729void
a0932daa
KH
730sys_subshell ()
731{
732#ifndef VMS
ad00c243 733#ifdef DOS_NT /* Demacs 1.1.2 91/10/20 Manabu Higashida */
a0932daa
KH
734 int st;
735 char oldwd[MAXPATHLEN+1]; /* Fixed length is safe on MSDOS. */
736#endif
efa04277 737 int pid;
86a5659e 738 struct save_signal saved_handlers[5];
efa04277
RS
739 Lisp_Object dir;
740 unsigned char *str = 0;
741 int len;
86a5659e
JB
742
743 saved_handlers[0].code = SIGINT;
744 saved_handlers[1].code = SIGQUIT;
745 saved_handlers[2].code = SIGTERM;
746#ifdef SIGIO
747 saved_handlers[3].code = SIGIO;
748 saved_handlers[4].code = 0;
749#else
750 saved_handlers[3].code = 0;
751#endif
752
efa04277
RS
753 /* Mentioning current_buffer->buffer would mean including buffer.h,
754 which somehow wedges the hp compiler. So instead... */
755
756 dir = intern ("default-directory");
0e7e7a58 757 if (NILP (Fboundp (dir)))
efa04277
RS
758 goto xyzzy;
759 dir = Fsymbol_value (dir);
914e81a2 760 if (!STRINGP (dir))
efa04277
RS
761 goto xyzzy;
762
763 dir = expand_and_dir_to_file (Funhandled_file_name_directory (dir), Qnil);
764 str = (unsigned char *) alloca (XSTRING (dir)->size + 2);
765 len = XSTRING (dir)->size;
766 bcopy (XSTRING (dir)->data, str, len);
767 if (str[len - 1] != '/') str[len++] = '/';
768 str[len] = 0;
769 xyzzy:
770
ad00c243 771#ifdef DOS_NT
7964ba9e 772 pid = 0;
718ca3cf
RS
773#if __DJGPP__ > 1
774 save_signal_handlers (saved_handlers);
775 synch_process_alive = 1;
776#endif /* __DJGPP__ > 1 */
7964ba9e 777#else
efa04277 778 pid = vfork ();
86a5659e
JB
779 if (pid == -1)
780 error ("Can't spawn subshell");
7964ba9e
RS
781#endif
782
86a5659e
JB
783 if (pid == 0)
784 {
7964ba9e 785 char *sh = 0;
86a5659e 786
ad00c243 787#ifdef DOS_NT /* MW, Aug 1993 */
207bdbdb 788 getwd (oldwd);
7964ba9e
RS
789 if (sh == 0)
790 sh = (char *) egetenv ("SUSPEND"); /* KFS, 1994-12-14 */
207bdbdb 791#endif
7964ba9e
RS
792 if (sh == 0)
793 sh = (char *) egetenv ("SHELL");
86a5659e
JB
794 if (sh == 0)
795 sh = "sh";
207bdbdb 796
86a5659e 797 /* Use our buffer's default directory for the subshell. */
efa04277 798 if (str)
86a5659e 799 chdir (str);
efa04277 800
86a5659e
JB
801#ifdef subprocesses
802 close_process_descs (); /* Close Emacs's pipes/ptys */
803#endif
1593c2fe 804
6f8b4d01 805#ifdef SET_EMACS_PRIORITY
1593c2fe
JB
806 {
807 extern int emacs_priority;
808
6f8b4d01 809 if (emacs_priority < 0)
1593c2fe
JB
810 nice (-emacs_priority);
811 }
812#endif
813
fe03522b 814#ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
207bdbdb
RS
815 st = system (sh);
816 chdir (oldwd);
7964ba9e 817#if 0 /* This is also reported if last command executed in subshell failed, KFS */
207bdbdb 818 if (st)
fe03522b 819 report_file_error ("Can't execute subshell", Fcons (build_string (sh), Qnil));
7964ba9e 820#endif
207bdbdb 821#else /* not MSDOS */
fe03522b 822#ifdef WINDOWSNT
fe03522b
RS
823 /* Waits for process completion */
824 pid = _spawnlp (_P_WAIT, sh, sh, NULL);
ad00c243 825 chdir (oldwd);
fe03522b
RS
826 if (pid == -1)
827 write (1, "Can't execute subshell", 22);
fe03522b 828#else /* not WINDOWSNT */
86a5659e
JB
829 execlp (sh, sh, 0);
830 write (1, "Can't execute subshell", 22);
831 _exit (1);
fe03522b 832#endif /* not WINDOWSNT */
207bdbdb 833#endif /* not MSDOS */
86a5659e
JB
834 }
835
718ca3cf
RS
836 /* Do this now if we did not do it before. */
837#if !defined (MSDOS) || __DJGPP__ == 1
86a5659e 838 save_signal_handlers (saved_handlers);
ffafc793 839 synch_process_alive = 1;
718ca3cf
RS
840#endif
841
ad00c243 842#ifndef DOS_NT
86a5659e 843 wait_for_termination (pid);
7964ba9e 844#endif
86a5659e 845 restore_signal_handlers (saved_handlers);
718ca3cf 846 synch_process_alive = 0;
a0932daa 847#endif /* !VMS */
86a5659e
JB
848}
849
35a05cca 850static void
86a5659e
JB
851save_signal_handlers (saved_handlers)
852 struct save_signal *saved_handlers;
853{
854 while (saved_handlers->code)
855 {
508b171c 856 saved_handlers->handler
35a05cca 857 = (SIGTYPE (*) P_ ((int))) signal (saved_handlers->code, SIG_IGN);
86a5659e
JB
858 saved_handlers++;
859 }
860}
861
35a05cca 862static void
86a5659e
JB
863restore_signal_handlers (saved_handlers)
864 struct save_signal *saved_handlers;
865{
866 while (saved_handlers->code)
867 {
868 signal (saved_handlers->code, saved_handlers->handler);
869 saved_handlers++;
870 }
871}
872\f
873#ifdef F_SETFL
874
875int old_fcntl_flags;
876
08633194 877void
23dab951
RS
878init_sigio (fd)
879 int fd;
86a5659e
JB
880{
881#ifdef FASYNC
23dab951
RS
882 old_fcntl_flags = fcntl (fd, F_GETFL, 0) & ~FASYNC;
883 fcntl (fd, F_SETFL, old_fcntl_flags | FASYNC);
86a5659e 884#endif
23dab951 885 interrupts_deferred = 0;
86a5659e
JB
886}
887
590034f9 888void
86a5659e
JB
889reset_sigio ()
890{
891 unrequest_sigio ();
892}
893
eb8c3be9 894#ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
86a5659e 895
08633194 896void
86a5659e
JB
897request_sigio ()
898{
23dab951
RS
899 if (read_socket_hook)
900 return;
901
86a5659e 902#ifdef SIGWINCH
e065a56e 903 sigunblock (sigmask (SIGWINCH));
86a5659e 904#endif
64e971c3 905 fcntl (input_fd, F_SETFL, old_fcntl_flags | FASYNC);
86a5659e
JB
906
907 interrupts_deferred = 0;
908}
909
08633194 910void
86a5659e
JB
911unrequest_sigio ()
912{
23dab951
RS
913 if (read_socket_hook)
914 return;
915
86a5659e 916#ifdef SIGWINCH
e065a56e 917 sigblock (sigmask (SIGWINCH));
86a5659e 918#endif
64e971c3 919 fcntl (input_fd, F_SETFL, old_fcntl_flags);
86a5659e
JB
920 interrupts_deferred = 1;
921}
922
923#else /* no FASYNC */
924#ifdef STRIDE /* Stride doesn't have FASYNC - use FIOASYNC */
925
08633194 926void
86a5659e
JB
927request_sigio ()
928{
929 int on = 1;
23dab951
RS
930
931 if (read_socket_hook)
932 return;
933
64e971c3 934 ioctl (input_fd, FIOASYNC, &on);
86a5659e
JB
935 interrupts_deferred = 0;
936}
937
08633194 938void
86a5659e
JB
939unrequest_sigio ()
940{
941 int off = 0;
942
23dab951
RS
943 if (read_socket_hook)
944 return;
945
64e971c3 946 ioctl (input_fd, FIOASYNC, &off);
86a5659e
JB
947 interrupts_deferred = 1;
948}
949
950#else /* not FASYNC, not STRIDE */
951
25ab68af
RS
952#ifdef _CX_UX
953
954#include <termios.h>
955
08633194 956void
25ab68af
RS
957request_sigio ()
958{
959 int on = 1;
960 sigset_t st;
961
23dab951
RS
962 if (read_socket_hook)
963 return;
964
25ab68af
RS
965 sigemptyset(&st);
966 sigaddset(&st, SIGIO);
967 ioctl (input_fd, FIOASYNC, &on);
968 interrupts_deferred = 0;
969 sigprocmask(SIG_UNBLOCK, &st, (sigset_t *)0);
970}
971
08633194 972void
25ab68af
RS
973unrequest_sigio ()
974{
975 int off = 0;
976
23dab951
RS
977 if (read_socket_hook)
978 return;
979
25ab68af
RS
980 ioctl (input_fd, FIOASYNC, &off);
981 interrupts_deferred = 1;
982}
983
984#else /* ! _CX_UX */
985
08633194 986void
86a5659e
JB
987request_sigio ()
988{
23dab951
RS
989 if (read_socket_hook)
990 return;
991
86a5659e
JB
992 croak ("request_sigio");
993}
994
08633194 995void
86a5659e
JB
996unrequest_sigio ()
997{
23dab951
RS
998 if (read_socket_hook)
999 return;
1000
86a5659e
JB
1001 croak ("unrequest_sigio");
1002}
1003
25ab68af 1004#endif /* _CX_UX */
86a5659e
JB
1005#endif /* STRIDE */
1006#endif /* FASYNC */
1007#endif /* F_SETFL */
1008\f
9ae8f997
JB
1009/* Saving and restoring the process group of Emacs's terminal. */
1010
0ba73609 1011#ifdef BSD_PGRPS
9ae8f997
JB
1012
1013/* The process group of which Emacs was a member when it initially
1014 started.
1015
1016 If Emacs was in its own process group (i.e. inherited_pgroup ==
1017 getpid ()), then we know we're running under a shell with job
1018 control (Emacs would never be run as part of a pipeline).
1019 Everything is fine.
1020
1021 If Emacs was not in its own process group, then we know we're
1022 running under a shell (or a caller) that doesn't know how to
1023 separate itself from Emacs (like sh). Emacs must be in its own
1024 process group in order to receive SIGIO correctly. In this
1025 situation, we put ourselves in our own pgroup, forcibly set the
1026 tty's pgroup to our pgroup, and make sure to restore and reinstate
1027 the tty's pgroup just like any other terminal setting. If
1028 inherited_group was not the tty's pgroup, then we'll get a
1029 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
1030 it goes foreground in the future, which is what should happen. */
1031int inherited_pgroup;
1032
1033/* Split off the foreground process group to Emacs alone.
1034 When we are in the foreground, but not started in our own process
1035 group, redirect the TTY to point to our own process group. We need
1036 to be in our own process group to receive SIGIO properly. */
dfcf069d 1037void
9ae8f997
JB
1038narrow_foreground_group ()
1039{
1040 int me = getpid ();
1041
1042 setpgrp (0, inherited_pgroup);
1043 if (inherited_pgroup != me)
64e971c3 1044 EMACS_SET_TTY_PGRP (input_fd, &me);
9ae8f997
JB
1045 setpgrp (0, me);
1046}
1047
1048/* Set the tty to our original foreground group. */
dfcf069d 1049void
9ae8f997
JB
1050widen_foreground_group ()
1051{
1052 if (inherited_pgroup != getpid ())
64e971c3 1053 EMACS_SET_TTY_PGRP (input_fd, &inherited_pgroup);
9ae8f997
JB
1054 setpgrp (0, inherited_pgroup);
1055}
1056
0ba73609 1057#endif /* BSD_PGRPS */
9ae8f997 1058\f
68936329
JB
1059/* Getting and setting emacs_tty structures. */
1060
1061/* Set *TC to the parameters associated with the terminal FD.
1062 Return zero if all's well, or -1 if we ran into an error we
1063 couldn't deal with. */
1064int
1065emacs_get_tty (fd, settings)
1066 int fd;
1067 struct emacs_tty *settings;
1068{
1069 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
1070#ifdef HAVE_TCATTR
1071 /* We have those nifty POSIX tcmumbleattr functions. */
1072 if (tcgetattr (fd, &settings->main) < 0)
1073 return -1;
1074
1075#else
1076#ifdef HAVE_TERMIO
1077 /* The SYSV-style interface? */
1078 if (ioctl (fd, TCGETA, &settings->main) < 0)
1079 return -1;
1080
1081#else
1082#ifdef VMS
1083 /* Vehemently Monstrous System? :-) */
1084 if (! (SYS$QIOW (0, fd, IO$_SENSEMODE, settings, 0, 0,
1085 &settings->main.class, 12, 0, 0, 0, 0)
1086 & 1))
1087 return -1;
1088
1089#else
fe03522b 1090#ifndef DOS_NT
68936329
JB
1091 /* I give up - I hope you have the BSD ioctls. */
1092 if (ioctl (fd, TIOCGETP, &settings->main) < 0)
1093 return -1;
fe03522b 1094#endif /* not DOS_NT */
68936329
JB
1095#endif
1096#endif
1097#endif
1098
1099 /* Suivant - Do we have to get struct ltchars data? */
50b8cf60 1100#ifdef HAVE_LTCHARS
68936329
JB
1101 if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
1102 return -1;
1103#endif
1104
1105 /* How about a struct tchars and a wordful of lmode bits? */
50b8cf60 1106#ifdef HAVE_TCHARS
68936329
JB
1107 if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
1108 || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
1109 return -1;
1110#endif
1111
1112 /* We have survived the tempest. */
1113 return 0;
1114}
1115
1116
1117/* Set the parameters of the tty on FD according to the contents of
394049ec 1118 *SETTINGS. If FLUSHP is non-zero, we discard input.
68936329 1119 Return 0 if all went well, and -1 if anything failed. */
394049ec 1120
68936329 1121int
394049ec 1122emacs_set_tty (fd, settings, flushp)
68936329
JB
1123 int fd;
1124 struct emacs_tty *settings;
394049ec 1125 int flushp;
68936329
JB
1126{
1127 /* Set the primary parameters - baud rate, character size, etcetera. */
1128#ifdef HAVE_TCATTR
e6cc3307 1129 int i;
68936329
JB
1130 /* We have those nifty POSIX tcmumbleattr functions.
1131 William J. Smith <wjs@wiis.wang.com> writes:
1132 "POSIX 1003.1 defines tcsetattr() to return success if it was
1133 able to perform any of the requested actions, even if some
1134 of the requested actions could not be performed.
1135 We must read settings back to ensure tty setup properly.
1136 AIX requires this to keep tty from hanging occasionally." */
eb8c3be9 1137 /* This make sure that we don't loop indefinitely in here. */
e6cc3307 1138 for (i = 0 ; i < 10 ; i++)
394049ec 1139 if (tcsetattr (fd, flushp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
68936329
JB
1140 {
1141 if (errno == EINTR)
1142 continue;
1143 else
1144 return -1;
1145 }
1146 else
1147 {
1148 struct termios new;
1149
1150 /* Get the current settings, and see if they're what we asked for. */
1151 tcgetattr (fd, &new);
e6cc3307
RS
1152 /* We cannot use memcmp on the whole structure here because under
1153 * aix386 the termios structure has some reserved field that may
1154 * not be filled in.
1155 */
1156 if ( new.c_iflag == settings->main.c_iflag
1157 && new.c_oflag == settings->main.c_oflag
1158 && new.c_cflag == settings->main.c_cflag
1159 && new.c_lflag == settings->main.c_lflag
1160 && memcmp(new.c_cc, settings->main.c_cc, NCCS) == 0)
68936329 1161 break;
e6cc3307
RS
1162 else
1163 continue;
68936329
JB
1164 }
1165
1166#else
1167#ifdef HAVE_TERMIO
1168 /* The SYSV-style interface? */
394049ec 1169 if (ioctl (fd, flushp ? TCSETAF : TCSETAW, &settings->main) < 0)
68936329
JB
1170 return -1;
1171
1172#else
1173#ifdef VMS
1174 /* Vehemently Monstrous System? :-) */
1175 if (! (SYS$QIOW (0, fd, IO$_SETMODE, &input_iosb, 0, 0,
1176 &settings->main.class, 12, 0, 0, 0, 0)
1177 & 1))
1178 return -1;
1179
1180#else
fe03522b 1181#ifndef DOS_NT
68936329 1182 /* I give up - I hope you have the BSD ioctls. */
394049ec 1183 if (ioctl (fd, (flushp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
68936329 1184 return -1;
fe03522b 1185#endif /* not DOS_NT */
68936329
JB
1186
1187#endif
1188#endif
1189#endif
1190
1191 /* Suivant - Do we have to get struct ltchars data? */
50b8cf60 1192#ifdef HAVE_LTCHARS
68936329
JB
1193 if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0)
1194 return -1;
1195#endif
1196
1197 /* How about a struct tchars and a wordful of lmode bits? */
50b8cf60 1198#ifdef HAVE_TCHARS
68936329
JB
1199 if (ioctl (fd, TIOCSETC, &settings->tchars) < 0
1200 || ioctl (fd, TIOCLSET, &settings->lmode) < 0)
1201 return -1;
1202#endif
1203
1204 /* We have survived the tempest. */
1205 return 0;
1206}
1207
1208\f
91bac16a
JB
1209/* The initial tty mode bits */
1210struct emacs_tty old_tty;
86a5659e 1211
7e32a4fb
KH
1212/* 1 if we have been through init_sys_modes. */
1213int term_initted;
1214
1215/* 1 if outer tty status has been recorded. */
1216int old_tty_valid;
86a5659e 1217
91bac16a
JB
1218#ifdef BSD4_1
1219/* BSD 4.1 needs to keep track of the lmode bits in order to start
1220 sigio. */
1221int lmode;
1222#endif
1223
46f2fdac 1224#ifndef F_SETOWN_BUG
86a5659e
JB
1225#ifdef F_SETOWN
1226int old_fcntl_owner;
1227#endif /* F_SETOWN */
46f2fdac 1228#endif /* F_SETOWN_BUG */
86a5659e 1229
86a5659e
JB
1230/* This may also be defined in stdio,
1231 but if so, this does no harm,
1232 and using the same name avoids wasting the other one's space. */
1233
1234#if defined (USG) || defined (DGUX)
1235unsigned char _sobuf[BUFSIZ+8];
1236#else
1237char _sobuf[BUFSIZ];
1238#endif
1239
50b8cf60 1240#ifdef HAVE_LTCHARS
86a5659e
JB
1241static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
1242#endif
50b8cf60 1243#ifdef HAVE_TCHARS
4ec5cb58 1244static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
86a5659e
JB
1245#endif
1246
08633194 1247void
86a5659e
JB
1248init_sys_modes ()
1249{
91bac16a
JB
1250 struct emacs_tty tty;
1251
86a5659e
JB
1252#ifdef VMS
1253#if 0
1254 static int oob_chars[2] = {0, 1 << 7}; /* catch C-g's */
1255 extern int (*interrupt_signal) ();
1256#endif
1257#endif
1258
4ec5cb58
RS
1259 Vtty_erase_char = Qnil;
1260
86a5659e
JB
1261 if (noninteractive)
1262 return;
1263
1264#ifdef VMS
1265 if (!input_ef)
1266 input_ef = get_kbd_event_flag ();
1267 /* LIB$GET_EF (&input_ef); */
1268 SYS$CLREF (input_ef);
1269 waiting_for_ast = 0;
1270 if (!timer_ef)
1271 timer_ef = get_timer_event_flag ();
1272 /* LIB$GET_EF (&timer_ef); */
1273 SYS$CLREF (timer_ef);
210b2b4f 1274#if 0
86a5659e
JB
1275 if (!process_ef)
1276 {
1277 LIB$GET_EF (&process_ef);
1278 SYS$CLREF (process_ef);
1279 }
1280 if (input_ef / 32 != process_ef / 32)
1281 croak ("Input and process event flags in different clusters.");
210b2b4f 1282#endif
86a5659e 1283 if (input_ef / 32 != timer_ef / 32)
210b2b4f
JB
1284 croak ("Input and timer event flags in different clusters.");
1285#if 0
86a5659e
JB
1286 input_eflist = ((unsigned) 1 << (input_ef % 32)) |
1287 ((unsigned) 1 << (process_ef % 32));
210b2b4f 1288#endif
86a5659e
JB
1289 timer_eflist = ((unsigned) 1 << (input_ef % 32)) |
1290 ((unsigned) 1 << (timer_ef % 32));
86a5659e
JB
1291#ifndef VMS4_4
1292 sys_access_reinit ();
1293#endif
86a5659e 1294#endif /* not VMS */
91bac16a 1295
0ba73609 1296#ifdef BSD_PGRPS
9ae8f997
JB
1297 if (! read_socket_hook && EQ (Vwindow_system, Qnil))
1298 narrow_foreground_group ();
1299#endif
1300
23cafe43 1301#ifdef HAVE_WINDOW_SYSTEM
87485d6f
MW
1302 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1303 needs the initialization code below. */
86a5659e 1304 if (!read_socket_hook && EQ (Vwindow_system, Qnil))
87485d6f 1305#endif
86a5659e 1306 {
23dab951
RS
1307 EMACS_GET_TTY (input_fd, &old_tty);
1308
7e32a4fb
KH
1309 old_tty_valid = 1;
1310
91bac16a 1311 tty = old_tty;
86a5659e 1312
31be8d24 1313#if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
2e34157c 1314 XSETINT (Vtty_erase_char, old_tty.main.c_cc[VERASE]);
4ec5cb58 1315
421dd92f
RS
1316#ifdef DGUX
1317 /* This allows meta to be sent on 8th bit. */
1318 tty.main.c_iflag &= ~INPCK; /* don't check input for parity */
1319#endif
91bac16a
JB
1320 tty.main.c_iflag |= (IGNBRK); /* Ignore break condition */
1321 tty.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
7f371164
RS
1322#ifdef INLCR /* I'm just being cautious,
1323 since I can't check how widespread INLCR is--rms. */
1324 tty.main.c_iflag &= ~INLCR; /* Disable map of NL to CR on input */
1325#endif
86a5659e 1326#ifdef ISTRIP
91bac16a 1327 tty.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
86a5659e 1328#endif
91bac16a
JB
1329 tty.main.c_lflag &= ~ECHO; /* Disable echo */
1330 tty.main.c_lflag &= ~ICANON; /* Disable erase/kill processing */
e2b40c23 1331#ifdef IEXTEN
b26bc18b 1332 tty.main.c_lflag &= ~IEXTEN; /* Disable other editing characters. */
e2b40c23 1333#endif
91bac16a 1334 tty.main.c_lflag |= ISIG; /* Enable signals */
86a5659e
JB
1335 if (flow_control)
1336 {
91bac16a 1337 tty.main.c_iflag |= IXON; /* Enable start/stop output control */
86a5659e 1338#ifdef IXANY
91bac16a 1339 tty.main.c_iflag &= ~IXANY;
86a5659e
JB
1340#endif /* IXANY */
1341 }
1342 else
91bac16a
JB
1343 tty.main.c_iflag &= ~IXON; /* Disable start/stop output control */
1344 tty.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL
1345 on output */
1346 tty.main.c_oflag &= ~TAB3; /* Disable tab expansion */
86a5659e
JB
1347#ifdef CS8
1348 if (meta_key)
1349 {
91bac16a
JB
1350 tty.main.c_cflag |= CS8; /* allow 8th bit on input */
1351 tty.main.c_cflag &= ~PARENB;/* Don't check parity */
86a5659e
JB
1352 }
1353#endif
91bac16a 1354 tty.main.c_cc[VINTR] = quit_char; /* C-g (usually) gives SIGINT */
86a5659e
JB
1355 /* Set up C-g for both SIGQUIT and SIGINT.
1356 We don't know which we will get, but we handle both alike
1357 so which one it really gives us does not matter. */
91bac16a
JB
1358 tty.main.c_cc[VQUIT] = quit_char;
1359 tty.main.c_cc[VMIN] = 1; /* Input should wait for at least 1 char */
1360 tty.main.c_cc[VTIME] = 0; /* no matter how long that takes. */
86a5659e 1361#ifdef VSWTCH
e2b40c23 1362 tty.main.c_cc[VSWTCH] = CDISABLE; /* Turn off shell layering use
91bac16a 1363 of C-z */
86a5659e 1364#endif /* VSWTCH */
c179a6d1 1365
86a5659e 1366#if defined (mips) || defined (HAVE_TCATTR)
86a5659e 1367#ifdef VSUSP
e2b40c23 1368 tty.main.c_cc[VSUSP] = CDISABLE; /* Turn off mips handling of C-z. */
86a5659e
JB
1369#endif /* VSUSP */
1370#ifdef V_DSUSP
e2b40c23 1371 tty.main.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y. */
86a5659e 1372#endif /* V_DSUSP */
e2b40c23
RS
1373#ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
1374 tty.main.c_cc[VDSUSP] = CDISABLE;
1375#endif /* VDSUSP */
92c995de
RS
1376#ifdef VLNEXT
1377 tty.main.c_cc[VLNEXT] = CDISABLE;
1378#endif /* VLNEXT */
1379#ifdef VREPRINT
1380 tty.main.c_cc[VREPRINT] = CDISABLE;
1381#endif /* VREPRINT */
1382#ifdef VWERASE
1383 tty.main.c_cc[VWERASE] = CDISABLE;
1384#endif /* VWERASE */
1385#ifdef VDISCARD
1386 tty.main.c_cc[VDISCARD] = CDISABLE;
1387#endif /* VDISCARD */
c179a6d1
RS
1388
1389 if (flow_control)
1390 {
421dd92f 1391#ifdef VSTART
c179a6d1 1392 tty.main.c_cc[VSTART] = '\021';
421dd92f
RS
1393#endif /* VSTART */
1394#ifdef VSTOP
c179a6d1 1395 tty.main.c_cc[VSTOP] = '\023';
421dd92f 1396#endif /* VSTOP */
c179a6d1
RS
1397 }
1398 else
1399 {
1400#ifdef VSTART
1401 tty.main.c_cc[VSTART] = CDISABLE;
1402#endif /* VSTART */
1403#ifdef VSTOP
1404 tty.main.c_cc[VSTOP] = CDISABLE;
1405#endif /* VSTOP */
1406 }
86a5659e 1407#endif /* mips or HAVE_TCATTR */
c179a6d1 1408
441f6399 1409#ifdef SET_LINE_DISCIPLINE
ea5a0917 1410 /* Need to explicitly request TERMIODISC line discipline or
441f6399
RS
1411 Ultrix's termios does not work correctly. */
1412 tty.main.c_line = SET_LINE_DISCIPLINE;
1413#endif
86a5659e
JB
1414#ifdef AIX
1415#ifndef IBMR2AIX
441f6399 1416 /* AIX enhanced edit loses NULs, so disable it. */
91bac16a
JB
1417 tty.main.c_line = 0;
1418 tty.main.c_iflag &= ~ASCEDIT;
86a5659e 1419#else
91bac16a
JB
1420 tty.main.c_cc[VSTRT] = 255;
1421 tty.main.c_cc[VSTOP] = 255;
1422 tty.main.c_cc[VSUSP] = 255;
1423 tty.main.c_cc[VDSUSP] = 255;
86a5659e 1424#endif /* IBMR2AIX */
ac567c95
RS
1425 if (flow_control)
1426 {
1427#ifdef VSTART
1428 tty.main.c_cc[VSTART] = '\021';
1429#endif /* VSTART */
1430#ifdef VSTOP
1431 tty.main.c_cc[VSTOP] = '\023';
1432#endif /* VSTOP */
1433 }
86a5659e
JB
1434 /* Also, PTY overloads NUL and BREAK.
1435 don't ignore break, but don't signal either, so it looks like NUL.
1436 This really serves a purpose only if running in an XTERM window
1437 or via TELNET or the like, but does no harm elsewhere. */
91bac16a
JB
1438 tty.main.c_iflag &= ~IGNBRK;
1439 tty.main.c_iflag &= ~BRKINT;
86a5659e
JB
1440#endif
1441#else /* if not HAVE_TERMIO */
1442#ifdef VMS
91bac16a 1443 tty.main.tt_char |= TT$M_NOECHO;
86a5659e 1444 if (meta_key)
986ffb24 1445 tty.main.tt_char |= TT$M_EIGHTBIT;
86a5659e 1446 if (flow_control)
91bac16a 1447 tty.main.tt_char |= TT$M_TTSYNC;
86a5659e 1448 else
91bac16a
JB
1449 tty.main.tt_char &= ~TT$M_TTSYNC;
1450 tty.main.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
86a5659e 1451#else /* not VMS (BSD, that is) */
fe03522b 1452#ifndef DOS_NT
a61e51f0 1453 XSETINT (Vtty_erase_char, tty.main.sg_erase);
91bac16a 1454 tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
86a5659e 1455 if (meta_key)
91bac16a
JB
1456 tty.main.sg_flags |= ANYP;
1457 tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
fe03522b 1458#endif /* not DOS_NT */
86a5659e
JB
1459#endif /* not VMS (BSD, that is) */
1460#endif /* not HAVE_TERMIO */
1461
91bac16a
JB
1462 /* If going to use CBREAK mode, we must request C-g to interrupt
1463 and turn off start and stop chars, etc. If not going to use
1464 CBREAK mode, do this anyway so as to turn off local flow
1465 control for user coming over network on 4.2; in this case,
1466 only t_stopc and t_startc really matter. */
1467#ifndef HAVE_TERMIO
50b8cf60 1468#ifdef HAVE_TCHARS
91bac16a
JB
1469 /* Note: if not using CBREAK mode, it makes no difference how we
1470 set this */
1471 tty.tchars = new_tchars;
1472 tty.tchars.t_intrc = quit_char;
1473 if (flow_control)
1474 {
1475 tty.tchars.t_startc = '\021';
1476 tty.tchars.t_stopc = '\023';
1477 }
1478
91bac16a 1479 tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | old_tty.lmode;
37fd7901
JB
1480#ifdef ultrix
1481 /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
1482 anything, and leaving it in breaks the meta key. Go figure. */
1483 tty.lmode &= ~LLITOUT;
1484#endif
91bac16a
JB
1485
1486#ifdef BSD4_1
1487 lmode = tty.lmode;
1488#endif
1489
50b8cf60 1490#endif /* HAVE_TCHARS */
91bac16a
JB
1491#endif /* not HAVE_TERMIO */
1492
50b8cf60 1493#ifdef HAVE_LTCHARS
91bac16a 1494 tty.ltchars = new_ltchars;
50b8cf60 1495#endif /* HAVE_LTCHARS */
207bdbdb 1496#ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
87485d6f
MW
1497 if (!term_initted)
1498 internal_terminal_init ();
207bdbdb
RS
1499 dos_ttraw ();
1500#endif
91bac16a
JB
1501
1502 EMACS_SET_TTY (input_fd, &tty, 0);
86a5659e
JB
1503
1504 /* This code added to insure that, if flow-control is not to be used,
0137dbf7 1505 we have an unlocked terminal at the start. */
91bac16a 1506
86a5659e 1507#ifdef TCXONC
64e971c3 1508 if (!flow_control) ioctl (input_fd, TCXONC, 1);
86a5659e
JB
1509#endif
1510#ifndef APOLLO
1511#ifdef TIOCSTART
64e971c3 1512 if (!flow_control) ioctl (input_fd, TIOCSTART, 0);
86a5659e
JB
1513#endif
1514#endif
1515
d228f207 1516#if defined (HAVE_TERMIOS) || defined (HPUX9)
51417996 1517#ifdef TCOON
d228f207
RS
1518 if (!flow_control) tcflow (input_fd, TCOON);
1519#endif
51417996 1520#endif
d228f207 1521
b97ab886 1522#ifdef AIXHFT
86a5659e
JB
1523 hft_init ();
1524#ifdef IBMR2AIX
1525 {
1526 /* IBM's HFT device usually thinks a ^J should be LF/CR. We need it
1527 to be only LF. This is the way that is done. */
1528 struct termio tty;
1529
1530 if (ioctl (1, HFTGETID, &tty) != -1)
1531 write (1, "\033[20l", 5);
1532 }
1533#endif
b97ab886 1534#endif /* AIXHFT */
86a5659e 1535
86a5659e
JB
1536#ifdef VMS
1537/* Appears to do nothing when in PASTHRU mode.
91bac16a 1538 SYS$QIOW (0, input_fd, IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
86a5659e
JB
1539 interrupt_signal, oob_chars, 0, 0, 0, 0);
1540*/
1541 queue_kbd_input (0);
1542#endif /* VMS */
1543 }
1544
1545#ifdef F_SETFL
46f2fdac 1546#ifndef F_SETOWN_BUG
eb8c3be9 1547#ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
d6a9be45
RS
1548 if (interrupt_input
1549 && ! read_socket_hook && EQ (Vwindow_system, Qnil))
86a5659e 1550 {
64e971c3
RS
1551 old_fcntl_owner = fcntl (input_fd, F_GETOWN, 0);
1552 fcntl (input_fd, F_SETOWN, getpid ());
23dab951 1553 init_sigio (input_fd);
86a5659e
JB
1554 }
1555#endif /* F_GETOWN */
46f2fdac 1556#endif /* F_SETOWN_BUG */
86a5659e
JB
1557#endif /* F_SETFL */
1558
1559#ifdef BSD4_1
1560 if (interrupt_input)
23dab951 1561 init_sigio (input_fd);
86a5659e
JB
1562#endif
1563
1564#ifdef VMS /* VMS sometimes has this symbol but lacks setvbuf. */
1565#undef _IOFBF
1566#endif
1567#ifdef _IOFBF
1568 /* This symbol is defined on recent USG systems.
1569 Someone says without this call USG won't really buffer the file
1570 even with a call to setbuf. */
1571 setvbuf (stdout, _sobuf, _IOFBF, sizeof _sobuf);
1572#else
1573 setbuf (stdout, _sobuf);
1574#endif
23cafe43 1575#ifdef HAVE_WINDOW_SYSTEM
c4295188
KS
1576 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1577 needs the initialization code below. */
ad00c243
GV
1578 if (EQ (Vwindow_system, Qnil)
1579#ifndef WINDOWSNT
1580 /* When running in tty mode on NT/Win95, we have a read_socket
1581 hook, but still need the rest of the initialization code below. */
1582 && (! read_socket_hook)
1583#endif
1584 )
c4295188 1585#endif
4b311aaf
RS
1586 set_terminal_modes ();
1587
86a5659e
JB
1588 if (term_initted && no_redraw_on_reenter)
1589 {
1590 if (display_completed)
1591 direct_output_forward_char (0);
1592 }
1593 else
1594 {
0137dbf7 1595 frame_garbaged = 1;
0137dbf7
JB
1596 if (FRAMEP (Vterminal_frame))
1597 FRAME_GARBAGED_P (XFRAME (Vterminal_frame)) = 1;
86a5659e 1598 }
91bac16a 1599
86a5659e
JB
1600 term_initted = 1;
1601}
1602
1603/* Return nonzero if safe to use tabs in output.
1604 At the time this is called, init_sys_modes has not been done yet. */
1605
dfcf069d 1606int
86a5659e
JB
1607tabs_safe_p ()
1608{
91bac16a
JB
1609 struct emacs_tty tty;
1610
1611 EMACS_GET_TTY (input_fd, &tty);
1612 return EMACS_TTY_TABS_OK (&tty);
86a5659e 1613}
73d5358f 1614\f
86a5659e 1615/* Get terminal size from system.
73d5358f
RS
1616 Store number of lines into *HEIGHTP and width into *WIDTHP.
1617 We store 0 if there's no valid information. */
86a5659e 1618
08633194 1619void
0137dbf7 1620get_frame_size (widthp, heightp)
86a5659e
JB
1621 int *widthp, *heightp;
1622{
86a5659e 1623
86a5659e 1624#ifdef TIOCGWINSZ
91bac16a
JB
1625
1626 /* BSD-style. */
86a5659e 1627 struct winsize size;
91bac16a
JB
1628
1629 if (ioctl (input_fd, TIOCGWINSZ, &size) == -1)
1630 *widthp = *heightp = 0;
1631 else
1632 {
1633 *widthp = size.ws_col;
1634 *heightp = size.ws_row;
1635 }
1636
1637#else
1638#ifdef TIOCGSIZE
1639
1640 /* SunOS - style. */
1641 struct ttysize size;
1642
1643 if (ioctl (input_fd, TIOCGSIZE, &size) == -1)
1644 *widthp = *heightp = 0;
1645 else
1646 {
1647 *widthp = size.ts_cols;
1648 *heightp = size.ts_lines;
1649 }
1650
1651#else
86a5659e 1652#ifdef VMS
91bac16a
JB
1653
1654 struct sensemode tty;
1655
1656 SYS$QIOW (0, input_fd, IO$_SENSEMODE, &tty, 0, 0,
86a5659e
JB
1657 &tty.class, 12, 0, 0, 0, 0);
1658 *widthp = tty.scr_wid;
1659 *heightp = tty.scr_len;
91bac16a 1660
207bdbdb
RS
1661#else
1662#ifdef MSDOS
1663 *widthp = ScreenCols ();
1664 *heightp = ScreenRows ();
86a5659e
JB
1665#else /* system doesn't know size */
1666 *widthp = 0;
1667 *heightp = 0;
207bdbdb 1668#endif
91bac16a
JB
1669
1670#endif /* not VMS */
1671#endif /* not SunOS-style */
1672#endif /* not BSD-style */
86a5659e 1673}
91bac16a 1674
73d5358f
RS
1675/* Set the logical window size associated with descriptor FD
1676 to HEIGHT and WIDTH. This is used mainly with ptys. */
1677
1678int
1679set_window_size (fd, height, width)
1680 int fd, height, width;
1681{
1682#ifdef TIOCSWINSZ
1683
1684 /* BSD-style. */
1685 struct winsize size;
1686 size.ws_row = height;
1687 size.ws_col = width;
1688
1689 if (ioctl (fd, TIOCSWINSZ, &size) == -1)
1690 return 0; /* error */
1691 else
1692 return 1;
1693
1694#else
1695#ifdef TIOCSSIZE
1696
1697 /* SunOS - style. */
1698 struct ttysize size;
1699 size.ts_lines = height;
1700 size.ts_cols = width;
1701
1702 if (ioctl (fd, TIOCGSIZE, &size) == -1)
1703 return 0;
1704 else
1705 return 1;
1706#else
1707 return -1;
1708#endif /* not SunOS-style */
1709#endif /* not BSD-style */
1710}
1711
86a5659e 1712\f
91bac16a 1713/* Prepare the terminal for exiting Emacs; move the cursor to the
0137dbf7 1714 bottom of the frame, turn off interrupt-driven I/O, etc. */
08633194 1715void
86a5659e
JB
1716reset_sys_modes ()
1717{
1718 if (noninteractive)
1719 {
1720 fflush (stdout);
1721 return;
1722 }
1723 if (!term_initted)
1724 return;
23cafe43 1725#ifdef HAVE_WINDOW_SYSTEM
87485d6f
MW
1726 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1727 needs the clean-up code below. */
ad00c243
GV
1728 if (!EQ (Vwindow_system, Qnil)
1729#ifndef WINDOWSNT
1730 /* When running in tty mode on NT/Win95, we have a read_socket
1731 hook, but still need the rest of the clean-up code below. */
1732 || read_socket_hook
1733#endif
1734 )
86a5659e 1735 return;
87485d6f 1736#endif
0137dbf7
JB
1737 cursor_to (FRAME_HEIGHT (selected_frame) - 1, 0);
1738 clear_end_of_line (FRAME_WIDTH (selected_frame));
86a5659e 1739 /* clear_end_of_line may move the cursor */
0137dbf7 1740 cursor_to (FRAME_HEIGHT (selected_frame) - 1, 0);
b97ab886 1741#if defined (IBMR2AIX) && defined (AIXHFT)
86a5659e
JB
1742 {
1743 /* HFT devices normally use ^J as a LF/CR. We forced it to
1744 do the LF only. Now, we need to reset it. */
1745 struct termio tty;
1746
1747 if (ioctl (1, HFTGETID, &tty) != -1)
1748 write (1, "\033[20h", 5);
1749 }
1750#endif
1751
1752 reset_terminal_modes ();
1753 fflush (stdout);
2a633456 1754#ifdef BSD_SYSTEM
86a5659e
JB
1755#ifndef BSD4_1
1756 /* Avoid possible loss of output when changing terminal modes. */
1757 fsync (fileno (stdout));
1758#endif
1759#endif
91bac16a 1760
86a5659e 1761#ifdef F_SETFL
46f2fdac 1762#ifndef F_SETOWN_BUG
eb8c3be9 1763#ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
86a5659e
JB
1764 if (interrupt_input)
1765 {
1766 reset_sigio ();
64e971c3 1767 fcntl (input_fd, F_SETOWN, old_fcntl_owner);
86a5659e
JB
1768 }
1769#endif /* F_SETOWN */
46f2fdac 1770#endif /* F_SETOWN_BUG */
a6b00318
KH
1771#ifdef O_NDELAY
1772 fcntl (input_fd, F_SETFL, fcntl (input_fd, F_GETFL, 0) & ~O_NDELAY);
1773#endif
86a5659e
JB
1774#endif /* F_SETFL */
1775#ifdef BSD4_1
1776 if (interrupt_input)
1777 reset_sigio ();
1778#endif /* BSD4_1 */
91bac16a 1779
7e32a4fb
KH
1780 if (old_tty_valid)
1781 while (EMACS_SET_TTY (input_fd, &old_tty, 0) < 0 && errno == EINTR)
1782 ;
86a5659e 1783
207bdbdb
RS
1784#ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
1785 dos_ttcooked ();
1786#endif
1787
441f6399
RS
1788#ifdef SET_LINE_DISCIPLINE
1789 /* Ultrix's termios *ignores* any line discipline except TERMIODISC.
1790 A different old line discipline is therefore not restored, yet.
1791 Restore the old line discipline by hand. */
1792 ioctl (0, TIOCSETD, &old_tty.main.c_line);
1793#endif
1794
b97ab886 1795#ifdef AIXHFT
86a5659e
JB
1796 hft_reset ();
1797#endif
9ae8f997 1798
0ba73609 1799#ifdef BSD_PGRPS
9ae8f997
JB
1800 widen_foreground_group ();
1801#endif
86a5659e
JB
1802}
1803\f
1804#ifdef HAVE_PTYS
1805
1806/* Set up the proper status flags for use of a pty. */
1807
08633194 1808void
86a5659e
JB
1809setup_pty (fd)
1810 int fd;
1811{
1812 /* I'm told that TOICREMOTE does not mean control chars
1813 "can't be sent" but rather that they don't have
1814 input-editing or signaling effects.
1815 That should be good, because we have other ways
1816 to do those things in Emacs.
1817 However, telnet mode seems not to work on 4.2.
1818 So TIOCREMOTE is turned off now. */
1819
1820 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1821 will hang. In particular, the "timeout" feature (which
1822 causes a read to return if there is no data available)
1823 does this. Also it is known that telnet mode will hang
1824 in such a way that Emacs must be stopped (perhaps this
1825 is the same problem).
1826
1827 If TIOCREMOTE is turned off, then there is a bug in
1828 hp-ux which sometimes loses data. Apparently the
1829 code which blocks the master process when the internal
1830 buffer fills up does not work. Other than this,
1831 though, everything else seems to work fine.
1832
1833 Since the latter lossage is more benign, we may as well
1834 lose that way. -- cph */
1835#ifdef FIONBIO
1836#ifdef SYSV_PTYS
1837 {
1838 int on = 1;
1839 ioctl (fd, FIONBIO, &on);
1840 }
1841#endif
1842#endif
1843#ifdef IBMRTAIX
1844 /* On AIX, the parent gets SIGHUP when a pty attached child dies. So, we */
1845 /* ignore SIGHUP once we've started a child on a pty. Note that this may */
1846 /* cause EMACS not to die when it should, i.e., when its own controlling */
1847 /* tty goes away. I've complained to the AIX developers, and they may */
1848 /* change this behavior, but I'm not going to hold my breath. */
1849 signal (SIGHUP, SIG_IGN);
1850#endif
1851}
1852#endif /* HAVE_PTYS */
1853\f
1854#ifdef VMS
1855
1856/* Assigning an input channel is done at the start of Emacs execution.
1857 This is called each time Emacs is resumed, also, but does nothing
1858 because input_chain is no longer zero. */
1859
dfcf069d 1860void
86a5659e
JB
1861init_vms_input ()
1862{
1863 int status;
1864
91bac16a 1865 if (input_fd == 0)
86a5659e 1866 {
91bac16a 1867 status = SYS$ASSIGN (&input_dsc, &input_fd, 0, 0);
86a5659e
JB
1868 if (! (status & 1))
1869 LIB$STOP (status);
1870 }
1871}
1872
1873/* Deassigning the input channel is done before exiting. */
1874
dfcf069d 1875void
86a5659e
JB
1876stop_vms_input ()
1877{
91bac16a 1878 return SYS$DASSGN (input_fd);
86a5659e
JB
1879}
1880
1881short input_buffer;
1882
1883/* Request reading one character into the keyboard buffer.
1884 This is done as soon as the buffer becomes empty. */
1885
dfcf069d 1886void
86a5659e
JB
1887queue_kbd_input ()
1888{
1889 int status;
210b2b4f
JB
1890 extern kbd_input_ast ();
1891
86a5659e
JB
1892 waiting_for_ast = 0;
1893 stop_input = 0;
91bac16a 1894 status = SYS$QIO (0, input_fd, IO$_READVBLK,
86a5659e
JB
1895 &input_iosb, kbd_input_ast, 1,
1896 &input_buffer, 1, 0, terminator_mask, 0, 0);
1897}
1898
1899int input_count;
1900
1901/* Ast routine that is called when keyboard input comes in
1902 in accord with the SYS$QIO above. */
1903
dfcf069d 1904void
86a5659e
JB
1905kbd_input_ast ()
1906{
1907 register int c = -1;
1908 int old_errno = errno;
ffd56f97 1909 extern EMACS_TIME *input_available_clear_time;
86a5659e
JB
1910
1911 if (waiting_for_ast)
1912 SYS$SETEF (input_ef);
1913 waiting_for_ast = 0;
1914 input_count++;
1915#ifdef ASTDEBUG
1916 if (input_count == 25)
1917 exit (1);
1918 printf ("Ast # %d,", input_count);
1919 printf (" iosb = %x, %x, %x, %x",
1920 input_iosb.offset, input_iosb.status, input_iosb.termlen,
1921 input_iosb.term);
1922#endif
1923 if (input_iosb.offset)
1924 {
1925 c = input_buffer;
1926#ifdef ASTDEBUG
1927 printf (", char = 0%o", c);
1928#endif
1929 }
1930#ifdef ASTDEBUG
1931 printf ("\n");
1932 fflush (stdout);
1933 sleep (1);
1934#endif
1935 if (! stop_input)
1936 queue_kbd_input ();
1937 if (c >= 0)
1938 {
1939 struct input_event e;
1940 e.kind = ascii_keystroke;
c81d47b4 1941 XSETINT (e.code, c);
c81d47b4 1942 XSETFRAME (e.frame_or_window, selected_frame);
86a5659e
JB
1943 kbd_buffer_store_event (&e);
1944 }
ffd56f97
JB
1945 if (input_available_clear_time)
1946 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
86a5659e
JB
1947 errno = old_errno;
1948}
1949
1950/* Wait until there is something in kbd_buffer. */
1951
dfcf069d 1952void
86a5659e
JB
1953wait_for_kbd_input ()
1954{
1955 extern int have_process_input, process_exited;
1956
1957 /* If already something, avoid doing system calls. */
1958 if (detect_input_pending ())
1959 {
1960 return;
1961 }
1962 /* Clear a flag, and tell ast routine above to set it. */
1963 SYS$CLREF (input_ef);
1964 waiting_for_ast = 1;
1965 /* Check for timing error: ast happened while we were doing that. */
1966 if (!detect_input_pending ())
1967 {
1968 /* No timing error: wait for flag to be set. */
1969 set_waiting_for_input (0);
1970 SYS$WFLOR (input_ef, input_eflist);
1971 clear_waiting_for_input (0);
1972 if (!detect_input_pending ())
1973 /* Check for subprocess input availability */
1974 {
1975 int dsp = have_process_input || process_exited;
1976
1977 SYS$CLREF (process_ef);
1978 if (have_process_input)
1979 process_command_input ();
1980 if (process_exited)
1981 process_exit ();
1982 if (dsp)
1983 {
1984 update_mode_lines++;
56b18525 1985 prepare_menu_bars ();
86a5659e
JB
1986 redisplay_preserve_echo_area ();
1987 }
1988 }
1989 }
1990 waiting_for_ast = 0;
1991}
1992
1993/* Get rid of any pending QIO, when we are about to suspend
1994 or when we want to throw away pending input.
1995 We wait for a positive sign that the AST routine has run
1996 and therefore there is no I/O request queued when we return.
1997 SYS$SETAST is used to avoid a timing error. */
1998
dfcf069d 1999void
86a5659e
JB
2000end_kbd_input ()
2001{
2002#ifdef ASTDEBUG
2003 printf ("At end_kbd_input.\n");
2004 fflush (stdout);
2005 sleep (1);
2006#endif
2007 if (LIB$AST_IN_PROG ()) /* Don't wait if suspending from kbd_buffer_store_event! */
2008 {
91bac16a 2009 SYS$CANCEL (input_fd);
86a5659e
JB
2010 return;
2011 }
2012
2013 SYS$SETAST (0);
2014 /* Clear a flag, and tell ast routine above to set it. */
2015 SYS$CLREF (input_ef);
2016 waiting_for_ast = 1;
2017 stop_input = 1;
91bac16a 2018 SYS$CANCEL (input_fd);
86a5659e
JB
2019 SYS$SETAST (1);
2020 SYS$WAITFR (input_ef);
2021 waiting_for_ast = 0;
2022}
2023
2024/* Wait for either input available or time interval expiry. */
2025
dfcf069d 2026void
86a5659e
JB
2027input_wait_timeout (timeval)
2028 int timeval; /* Time to wait, in seconds */
2029{
2030 int time [2];
2031 static int zero = 0;
2032 static int large = -10000000;
2033
2034 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
2035
2036 /* If already something, avoid doing system calls. */
2037 if (detect_input_pending ())
2038 {
2039 return;
2040 }
2041 /* Clear a flag, and tell ast routine above to set it. */
2042 SYS$CLREF (input_ef);
2043 waiting_for_ast = 1;
2044 /* Check for timing error: ast happened while we were doing that. */
2045 if (!detect_input_pending ())
2046 {
2047 /* No timing error: wait for flag to be set. */
2048 SYS$CANTIM (1, 0);
2049 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
2050 SYS$WFLOR (timer_ef, timer_eflist); /* Wait for timer expiry or input */
2051 }
2052 waiting_for_ast = 0;
2053}
2054
2055/* The standard `sleep' routine works some other way
2056 and it stops working if you have ever quit out of it.
2057 This one continues to work. */
2058
2059sys_sleep (timeval)
2060 int timeval;
2061{
2062 int time [2];
2063 static int zero = 0;
2064 static int large = -10000000;
2065
2066 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
2067
2068 SYS$CANTIM (1, 0);
2069 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
2070 SYS$WAITFR (timer_ef); /* Wait for timer expiry only */
2071}
2072
08633194 2073void
23dab951
RS
2074init_sigio (fd)
2075 int fd;
86a5659e
JB
2076{
2077 request_sigio ();
2078}
2079
2080reset_sigio ()
2081{
2082 unrequest_sigio ();
2083}
2084
08633194 2085void
86a5659e
JB
2086request_sigio ()
2087{
2088 croak ("request sigio");
2089}
2090
08633194 2091void
86a5659e
JB
2092unrequest_sigio ()
2093{
2094 croak ("unrequest sigio");
2095}
2096
2097#endif /* VMS */
2098\f
2099/* Note that VMS compiler won't accept defined (CANNOT_DUMP). */
2100#ifndef CANNOT_DUMP
2101#define NEED_STARTS
2102#endif
2103
2104#ifndef SYSTEM_MALLOC
2105#ifndef NEED_STARTS
2106#define NEED_STARTS
2107#endif
2108#endif
2109
2110#ifdef NEED_STARTS
2111/* Some systems that cannot dump also cannot implement these. */
2112
2113/*
2114 * Return the address of the start of the text segment prior to
2115 * doing an unexec. After unexec the return value is undefined.
2116 * See crt0.c for further explanation and _start.
2117 *
2118 */
2119
260fe597 2120#ifndef HAVE_TEXT_START
86a5659e
JB
2121char *
2122start_of_text ()
2123{
2124#ifdef TEXT_START
2125 return ((char *) TEXT_START);
2126#else
2127#ifdef GOULD
2128 extern csrt ();
2129 return ((char *) csrt);
2130#else /* not GOULD */
2131 extern int _start ();
2132 return ((char *) _start);
2133#endif /* GOULD */
2134#endif /* TEXT_START */
2135}
260fe597 2136#endif /* not HAVE_TEXT_START */
86a5659e
JB
2137
2138/*
2139 * Return the address of the start of the data segment prior to
2140 * doing an unexec. After unexec the return value is undefined.
2141 * See crt0.c for further information and definition of data_start.
2142 *
2143 * Apparently, on BSD systems this is etext at startup. On
2144 * USG systems (swapping) this is highly mmu dependent and
2145 * is also dependent on whether or not the program is running
2146 * with shared text. Generally there is a (possibly large)
2147 * gap between end of text and start of data with shared text.
2148 *
2149 * On Uniplus+ systems with shared text, data starts at a
2150 * fixed address. Each port (from a given oem) is generally
2151 * different, and the specific value of the start of data can
2152 * be obtained via the UniPlus+ specific "uvar" system call,
2153 * however the method outlined in crt0.c seems to be more portable.
2154 *
2155 * Probably what will have to happen when a USG unexec is available,
2156 * at least on UniPlus, is temacs will have to be made unshared so
2157 * that text and data are contiguous. Then once loadup is complete,
2158 * unexec will produce a shared executable where the data can be
ea5a0917 2159 * at the normal shared text boundary and the startofdata variable
86a5659e
JB
2160 * will be patched by unexec to the correct value.
2161 *
2162 */
2163
2164char *
2165start_of_data ()
2166{
2167#ifdef DATA_START
2168 return ((char *) DATA_START);
6c65530f
JB
2169#else
2170#ifdef ORDINARY_LINK
2171 /*
2172 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
2173 * data_start isn't defined. We take the address of environ, which
2174 * is known to live at or near the start of the system crt0.c, and
2175 * we don't sweat the handful of bytes that might lose.
2176 */
2177 extern char **environ;
2178
2179 return((char *) &environ);
86a5659e
JB
2180#else
2181 extern int data_start;
2182 return ((char *) &data_start);
6c65530f
JB
2183#endif /* ORDINARY_LINK */
2184#endif /* DATA_START */
86a5659e
JB
2185}
2186#endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
2187
2188#ifndef CANNOT_DUMP
2189/* Some systems that cannot dump also cannot implement these. */
2190
2191/*
2192 * Return the address of the end of the text segment prior to
2193 * doing an unexec. After unexec the return value is undefined.
2194 */
2195
2196char *
2197end_of_text ()
2198{
2199#ifdef TEXT_END
2200 return ((char *) TEXT_END);
2201#else
2202 extern int etext;
2203 return ((char *) &etext);
2204#endif
2205}
2206
2207/*
2208 * Return the address of the end of the data segment prior to
2209 * doing an unexec. After unexec the return value is undefined.
2210 */
2211
2212char *
2213end_of_data ()
2214{
2215#ifdef DATA_END
2216 return ((char *) DATA_END);
2217#else
2218 extern int edata;
2219 return ((char *) &edata);
2220#endif
2221}
2222
2223#endif /* not CANNOT_DUMP */
2224\f
c0c86835
KH
2225/* init_system_name sets up the string for the Lisp function
2226 system-name to return. */
86a5659e
JB
2227
2228#ifdef BSD4_1
2229#include <whoami.h>
2230#endif
2231
c0c86835 2232extern Lisp_Object Vsystem_name;
86a5659e 2233
f8a80313 2234#ifndef BSD4_1
f8a80313
RS
2235#ifndef VMS
2236#ifdef HAVE_SOCKETS
2237#include <sys/socket.h>
2238#include <netdb.h>
2239#endif /* HAVE_SOCKETS */
2240#endif /* not VMS */
f8a80313
RS
2241#endif /* not BSD4_1 */
2242
c0c86835
KH
2243void
2244init_system_name ()
86a5659e 2245{
86a5659e 2246#ifdef BSD4_1
c0c86835 2247 Vsystem_name = build_string (sysname);
67004ffb 2248#else
67004ffb 2249#ifdef VMS
c0c86835
KH
2250 char *sp, *end;
2251 if ((sp = egetenv ("SYS$NODE")) == 0)
2252 Vsystem_name = build_string ("vax-vms");
2253 else if ((end = index (sp, ':')) == 0)
2254 Vsystem_name = build_string (sp);
2255 else
2256 Vsystem_name = make_string (sp, end - sp);
67004ffb 2257#else
210b2b4f 2258#ifndef HAVE_GETHOSTNAME
c0c86835
KH
2259 struct utsname uts;
2260 uname (&uts);
2261 Vsystem_name = build_string (uts.nodename);
67004ffb 2262#else /* HAVE_GETHOSTNAME */
cc6e7269 2263 unsigned int hostname_size = 256;
c0c86835
KH
2264 char *hostname = (char *) alloca (hostname_size);
2265
2266 /* Try to get the host name; if the buffer is too short, try
2267 again. Apparently, the only indication gethostname gives of
2268 whether the buffer was large enough is the presence or absence
2269 of a '\0' in the string. Eech. */
2270 for (;;)
2271 {
2272 gethostname (hostname, hostname_size - 1);
2273 hostname[hostname_size - 1] = '\0';
2274
2275 /* Was the buffer large enough for the '\0'? */
2276 if (strlen (hostname) < hostname_size - 1)
2277 break;
2278
2279 hostname_size <<= 1;
2280 hostname = (char *) alloca (hostname_size);
2281 }
67004ffb 2282#ifdef HAVE_SOCKETS
c0c86835
KH
2283 /* Turn the hostname into the official, fully-qualified hostname.
2284 Don't do this if we're going to dump; this can confuse system
2285 libraries on some machines and make the dumped emacs core dump. */
67004ffb 2286#ifndef CANNOT_DUMP
c0c86835 2287 if (initialized)
67004ffb 2288#endif /* not CANNOT_DUMP */
960d894c
KH
2289 if (! index (hostname, '.'))
2290 {
2291 struct hostent *hp;
2292 int count;
2293 for (count = 0;; count++)
2294 {
e24f1d55 2295#ifdef TRY_AGAIN
960d894c 2296 h_errno = 0;
e24f1d55 2297#endif
960d894c 2298 hp = gethostbyname (hostname);
efa04277 2299#ifdef TRY_AGAIN
960d894c
KH
2300 if (! (hp == 0 && h_errno == TRY_AGAIN))
2301#endif
2302 break;
2303 if (count >= 5)
2304 break;
2305 Fsleep_for (make_number (1), Qnil);
2306 }
2307 if (hp)
2308 {
2309 char *fqdn = (char *) hp->h_name;
2310 char *p;
2311
2312 if (!index (fqdn, '.'))
2313 {
2314 /* We still don't have a fully qualified domain name.
2315 Try to find one in the list of alternate names */
2316 char **alias = hp->h_aliases;
2317 while (*alias && !index (*alias, '.'))
2318 alias++;
2319 if (*alias)
2320 fqdn = *alias;
2321 }
2322 hostname = fqdn;
ab1649fe 2323#if 0
960d894c
KH
2324 /* Convert the host name to lower case. */
2325 /* Using ctype.h here would introduce a possible locale
2326 dependence that is probably wrong for hostnames. */
2327 p = hostname;
2328 while (*p)
2329 {
2330 if (*p >= 'A' && *p <= 'Z')
2331 *p += 'a' - 'A';
2332 p++;
2333 }
2334#endif
2335 }
2336 }
67004ffb 2337#endif /* HAVE_SOCKETS */
6250a9db
KH
2338 /* We used to try using getdomainname here,
2339 but NIIBE Yutaka <gniibe@etl.go.jp> says that
0b93aa53
RS
2340 getdomainname gets the NIS/YP domain which often is not the same
2341 as in Internet domain name. */
6250a9db
KH
2342#if 0 /* Turned off because sysinfo is not really likely to return the
2343 correct Internet domain. */
0b93aa53 2344#if (HAVE_SYSINFO && defined (SI_SRPC_DOMAIN))
b05af5d3
PE
2345 if (! index (hostname, '.'))
2346 {
2347 /* The hostname is not fully qualified. Append the domain name. */
2348
2349 int hostlen = strlen (hostname);
2350 int domain_size = 256;
2351
2352 for (;;)
2353 {
825e7e55
RS
2354 char *domain = (char *) alloca (domain_size + 1);
2355 char *fqdn = (char *) alloca (hostlen + 1 + domain_size + 1);
b05af5d3
PE
2356 int sys_domain_size = sysinfo (SI_SRPC_DOMAIN, domain, domain_size);
2357 if (sys_domain_size <= 0)
2358 break;
2359 if (domain_size < sys_domain_size)
2360 {
2361 domain_size = sys_domain_size;
2362 continue;
2363 }
b05af5d3 2364 strcpy (fqdn, hostname);
825e7e55
RS
2365 if (domain[0] == '.')
2366 strcpy (fqdn + hostlen, domain);
9b80a5aa 2367 else if (domain[0] != 0)
825e7e55
RS
2368 {
2369 fqdn[hostlen] = '.';
2370 strcpy (fqdn + hostlen + 1, domain);
2371 }
b05af5d3
PE
2372 hostname = fqdn;
2373 break;
2374 }
2375 }
0b93aa53 2376#endif /* HAVE_SYSINFO && defined (SI_SRPC_DOMAIN) */
6250a9db 2377#endif /* 0 */
c0c86835 2378 Vsystem_name = build_string (hostname);
67004ffb 2379#endif /* HAVE_GETHOSTNAME */
210b2b4f 2380#endif /* VMS */
67004ffb 2381#endif /* BSD4_1 */
c0c86835
KH
2382 {
2383 unsigned char *p;
2384 for (p = XSTRING (Vsystem_name)->data; *p; p++)
2385 if (*p == ' ' || *p == '\t')
2386 *p = '-';
2387 }
67004ffb 2388}
86a5659e 2389\f
7964ba9e 2390#ifndef MSDOS
86a5659e 2391#ifndef VMS
86d1f23a 2392#if !defined (HAVE_SELECT) || defined (BROKEN_SELECT_NON_X)
86a5659e 2393
86d1f23a
KH
2394#include "sysselect.h"
2395#undef select
2396
2397#if defined (HAVE_X_WINDOWS) && !defined (HAVE_SELECT)
86a5659e
JB
2398/* Cause explanatory error message at compile time,
2399 since the select emulation is not good enough for X. */
2400int *x = &x_windows_lose_if_no_select_system_call;
2401#endif
2402
2403/* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
2404 * Only checks read descriptors.
2405 */
2406/* How long to wait between checking fds in select */
2407#define SELECT_PAUSE 1
2408int select_alarmed;
2409
2410/* For longjmp'ing back to read_input_waiting. */
2411
2412jmp_buf read_alarm_throw;
2413
2414/* Nonzero if the alarm signal should throw back to read_input_waiting.
2415 The read_socket_hook function sets this to 1 while it is waiting. */
2416
2417int read_alarm_should_throw;
2418
2419SIGTYPE
2420select_alarm ()
2421{
2422 select_alarmed = 1;
2423#ifdef BSD4_1
2424 sigrelse (SIGALRM);
2425#else /* not BSD4_1 */
2426 signal (SIGALRM, SIG_IGN);
2427#endif /* not BSD4_1 */
2428 if (read_alarm_should_throw)
2429 longjmp (read_alarm_throw, 1);
2430}
2431
fe03522b 2432#ifndef WINDOWSNT
86a5659e
JB
2433/* Only rfds are checked. */
2434int
86d1f23a 2435sys_select (nfds, rfds, wfds, efds, timeout)
86a5659e 2436 int nfds;
86d1f23a
KH
2437 SELECT_TYPE *rfds, *wfds, *efds;
2438 EMACS_TIME *timeout;
86a5659e 2439{
86d1f23a
KH
2440 int ravail = 0, old_alarm;
2441 SELECT_TYPE orfds;
2442 int timeoutval;
2443 int *local_timeout;
86a5659e
JB
2444 extern int proc_buffered_char[];
2445#ifndef subprocesses
2446 int process_tick = 0, update_tick = 0;
2447#else
2448 extern int process_tick, update_tick;
2449#endif
2450 SIGTYPE (*old_trap) ();
2451 unsigned char buf;
2452
86d1f23a
KH
2453#if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
2454 /* If we're using X, then the native select will work; we only need the
2455 emulation for non-X usage. */
2456 if (!NILP (Vwindow_system))
2457 return select (nfds, rfds, wfds, efds, timeout);
2458#endif
2459 timeoutval = timeout ? EMACS_SECS (*timeout) : 100000;
2460 local_timeout = &timeoutval;
2461 FD_ZERO (&orfds);
86a5659e
JB
2462 if (rfds)
2463 {
2464 orfds = *rfds;
86d1f23a 2465 FD_ZERO (rfds);
86a5659e
JB
2466 }
2467 if (wfds)
86d1f23a 2468 FD_ZERO (wfds);
86a5659e 2469 if (efds)
86d1f23a 2470 FD_ZERO (efds);
86a5659e
JB
2471
2472 /* If we are looking only for the terminal, with no timeout,
2473 just read it and wait -- that's more efficient. */
86d1f23a
KH
2474 if (*local_timeout == 100000 && process_tick == update_tick
2475 && FD_ISSET (0, &orfds))
86a5659e 2476 {
86d1f23a
KH
2477 int fd;
2478 for (fd = 1; fd < nfds; ++fd)
2479 if (FD_ISSET (fd, &orfds))
2480 goto hardway;
86a5659e
JB
2481 if (! detect_input_pending ())
2482 read_input_waiting ();
86d1f23a 2483 FD_SET (0, rfds);
86a5659e
JB
2484 return 1;
2485 }
2486
86d1f23a 2487 hardway:
86a5659e
JB
2488 /* Once a second, till the timer expires, check all the flagged read
2489 * descriptors to see if any input is available. If there is some then
2490 * set the corresponding bit in the return copy of rfds.
2491 */
2492 while (1)
2493 {
86d1f23a 2494 register int to_check, fd;
86a5659e
JB
2495
2496 if (rfds)
2497 {
86d1f23a 2498 for (to_check = nfds, fd = 0; --to_check >= 0; fd++)
86a5659e 2499 {
86d1f23a 2500 if (FD_ISSET (fd, &orfds))
86a5659e
JB
2501 {
2502 int avail = 0, status = 0;
2503
86d1f23a 2504 if (fd == 0)
86a5659e
JB
2505 avail = detect_input_pending (); /* Special keyboard handler */
2506 else
2507 {
2508#ifdef FIONREAD
2509 status = ioctl (fd, FIONREAD, &avail);
2510#else /* no FIONREAD */
2511 /* Hoping it will return -1 if nothing available
2512 or 0 if all 0 chars requested are read. */
2513 if (proc_buffered_char[fd] >= 0)
2514 avail = 1;
2515 else
2516 {
2517 avail = read (fd, &buf, 1);
2518 if (avail > 0)
2519 proc_buffered_char[fd] = buf;
2520 }
2521#endif /* no FIONREAD */
2522 }
2523 if (status >= 0 && avail > 0)
2524 {
86d1f23a 2525 FD_SET (fd, rfds);
86a5659e
JB
2526 ravail++;
2527 }
2528 }
2529 }
2530 }
2531 if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
2532 break;
2533 old_alarm = alarm (0);
34567704 2534 old_trap = signal (SIGALRM, select_alarm);
86a5659e
JB
2535 select_alarmed = 0;
2536 alarm (SELECT_PAUSE);
2537 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2538 while (select_alarmed == 0 && *local_timeout != 0
2539 && process_tick == update_tick)
2540 {
2541 /* If we are interested in terminal input,
2542 wait by reading the terminal.
2543 That makes instant wakeup for terminal input at least. */
86d1f23a 2544 if (FD_ISSET (0, &orfds))
86a5659e
JB
2545 {
2546 read_input_waiting ();
2547 if (detect_input_pending ())
2548 select_alarmed = 1;
2549 }
2550 else
2551 pause ();
2552 }
2553 (*local_timeout) -= SELECT_PAUSE;
2554 /* Reset the old alarm if there was one */
2555 alarm (0);
2556 signal (SIGALRM, old_trap);
2557 if (old_alarm != 0)
2558 {
2559 /* Reset or forge an interrupt for the original handler. */
2560 old_alarm -= SELECT_PAUSE;
2561 if (old_alarm <= 0)
2562 kill (getpid (), SIGALRM); /* Fake an alarm with the orig' handler */
2563 else
2564 alarm (old_alarm);
2565 }
2566 if (*local_timeout == 0) /* Stop on timer being cleared */
2567 break;
2568 }
2569 return ravail;
2570}
fe03522b 2571#endif /* not WINDOWSNT */
86a5659e
JB
2572
2573/* Read keyboard input into the standard buffer,
2574 waiting for at least one character. */
2575
23cafe43
GV
2576/* Make all keyboard buffers much bigger when using a window system. */
2577#ifdef HAVE_WINDOW_SYSTEM
86a5659e
JB
2578#define BUFFER_SIZE_FACTOR 16
2579#else
2580#define BUFFER_SIZE_FACTOR 1
2581#endif
2582
dfcf069d 2583void
86a5659e
JB
2584read_input_waiting ()
2585{
86a5659e 2586 struct input_event e;
34567704
JB
2587 int nread, i;
2588 extern int quit_char;
86a5659e
JB
2589
2590 if (read_socket_hook)
2591 {
f4a7e5bd
RS
2592 struct input_event buf[256];
2593
86a5659e
JB
2594 read_alarm_should_throw = 0;
2595 if (! setjmp (read_alarm_throw))
f4a7e5bd 2596 nread = (*read_socket_hook) (0, buf, 256, 1, 0);
86a5659e
JB
2597 else
2598 nread = -1;
f4a7e5bd
RS
2599
2600 /* Scan the chars for C-g and store them in kbd_buffer. */
2601 for (i = 0; i < nread; i++)
2602 {
2603 kbd_buffer_store_event (&buf[i]);
2604 /* Don't look at input that follows a C-g too closely.
2605 This reduces lossage due to autorepeat on C-g. */
2606 if (buf[i].kind == ascii_keystroke
30bef8ec 2607 && buf[i].code == quit_char)
f4a7e5bd
RS
2608 break;
2609 }
86a5659e
JB
2610 }
2611 else
86a5659e 2612 {
f4a7e5bd
RS
2613 char buf[3];
2614 nread = read (fileno (stdin), buf, 1);
2615
2616 /* Scan the chars for C-g and store them in kbd_buffer. */
2617 e.kind = ascii_keystroke;
bd513d9d 2618 XSETFRAME (e.frame_or_window, selected_frame);
f4a7e5bd
RS
2619 e.modifiers = 0;
2620 for (i = 0; i < nread; i++)
a00d5589 2621 {
f4a7e5bd
RS
2622 /* Convert chars > 0177 to meta events if desired.
2623 We do this under the same conditions that read_avail_input does. */
2624 if (read_socket_hook == 0)
2625 {
2626 /* If the user says she has a meta key, then believe her. */
2627 if (meta_key == 1 && (buf[i] & 0x80))
2628 e.modifiers = meta_modifier;
2629 if (meta_key != 2)
2630 buf[i] &= ~0x80;
2631 }
b95520f5 2632
c81d47b4 2633 XSETINT (e.code, buf[i]);
f4a7e5bd
RS
2634 kbd_buffer_store_event (&e);
2635 /* Don't look at input that follows a C-g too closely.
2636 This reduces lossage due to autorepeat on C-g. */
2637 if (buf[i] == quit_char)
2638 break;
2639 }
86a5659e
JB
2640 }
2641}
2642
2643#endif /* not HAVE_SELECT */
2644#endif /* not VMS */
7964ba9e 2645#endif /* not MSDOS */
86a5659e
JB
2646\f
2647#ifdef BSD4_1
86a5659e
JB
2648/*
2649 * Partially emulate 4.2 open call.
2650 * open is defined as this in 4.1.
2651 *
2652 * - added by Michael Bloom @ Citicorp/TTI
2653 *
2654 */
2655
2656int
2657sys_open (path, oflag, mode)
2658 char *path;
2659 int oflag, mode;
2660{
2661 if (oflag & O_CREAT)
2662 return creat (path, mode);
2663 else
2664 return open (path, oflag);
2665}
2666
08633194 2667void
23dab951
RS
2668init_sigio (fd)
2669 int fd;
86a5659e
JB
2670{
2671 if (noninteractive)
2672 return;
2673 lmode = LINTRUP | lmode;
23dab951 2674 ioctl (fd, TIOCLSET, &lmode);
86a5659e
JB
2675}
2676
dfcf069d 2677void
86a5659e
JB
2678reset_sigio ()
2679{
2680 if (noninteractive)
2681 return;
2682 lmode = ~LINTRUP & lmode;
2683 ioctl (0, TIOCLSET, &lmode);
2684}
2685
08633194 2686void
86a5659e
JB
2687request_sigio ()
2688{
2689 sigrelse (SIGTINT);
2690
2691 interrupts_deferred = 0;
2692}
2693
08633194 2694void
86a5659e
JB
2695unrequest_sigio ()
2696{
2697 sighold (SIGTINT);
2698
2699 interrupts_deferred = 1;
2700}
2701
2702/* still inside #ifdef BSD4_1 */
2703#ifdef subprocesses
2704
2705int sigheld; /* Mask of held signals */
2706
dfcf069d 2707void
86a5659e
JB
2708sigholdx (signum)
2709 int signum;
2710{
2711 sigheld |= sigbit (signum);
2712 sighold (signum);
2713}
2714
dfcf069d 2715void
86a5659e
JB
2716sigisheld (signum)
2717 int signum;
2718{
2719 sigheld |= sigbit (signum);
2720}
2721
dfcf069d 2722void
86a5659e
JB
2723sigunhold (signum)
2724 int signum;
2725{
2726 sigheld &= ~sigbit (signum);
2727 sigrelse (signum);
2728}
2729
dfcf069d 2730void
86a5659e
JB
2731sigfree () /* Free all held signals */
2732{
2733 int i;
2734 for (i = 0; i < NSIG; i++)
2735 if (sigheld & sigbit (i))
2736 sigrelse (i);
2737 sigheld = 0;
2738}
2739
dfcf069d 2740int
86a5659e
JB
2741sigbit (i)
2742{
2743 return 1 << (i - 1);
2744}
2745#endif /* subprocesses */
2746#endif /* BSD4_1 */
2747\f
2748/* POSIX signals support - DJB */
2749/* Anyone with POSIX signals should have ANSI C declarations */
2750
2751#ifdef POSIX_SIGNALS
2752
c639b0e3 2753sigset_t empty_mask, full_mask;
86a5659e 2754
dfcf069d 2755void
86a5659e
JB
2756init_signals ()
2757{
00eaaa32
JB
2758 sigemptyset (&empty_mask);
2759 sigfillset (&full_mask);
86a5659e
JB
2760}
2761
86a5659e
JB
2762signal_handler_t
2763sys_signal (int signal_number, signal_handler_t action)
2764{
c639b0e3 2765 struct sigaction new_action, old_action;
86a5659e 2766 sigemptyset (&new_action.sa_mask);
e5c99298 2767 new_action.sa_handler = action;
25ab68af
RS
2768#ifdef SA_RESTART
2769 /* Emacs mostly works better with restartable system services. If this
2770 * flag exists, we probably want to turn it on here.
2771 */
2772 new_action.sa_flags = SA_RESTART;
2773#else
4a785b6e 2774 new_action.sa_flags = 0;
25ab68af 2775#endif
d32b2f3c 2776 sigaction (signal_number, &new_action, &old_action);
e5c99298 2777 return (old_action.sa_handler);
86a5659e
JB
2778}
2779
e065a56e
JB
2780#ifndef __GNUC__
2781/* If we're compiling with GCC, we don't need this function, since it
2782 can be written as a macro. */
2783sigset_t
2784sys_sigmask (int sig)
2785{
2786 sigset_t mask;
2787 sigemptyset (&mask);
2788 sigaddset (&mask, sig);
2789 return mask;
2790}
2791#endif
2792
86a5659e
JB
2793/* I'd like to have these guys return pointers to the mask storage in here,
2794 but there'd be trouble if the code was saving multiple masks. I'll be
2795 safe and pass the structure. It normally won't be more than 2 bytes
2796 anyhow. - DJB */
2797
2798sigset_t
2799sys_sigblock (sigset_t new_mask)
2800{
2801 sigset_t old_mask;
2802 sigprocmask (SIG_BLOCK, &new_mask, &old_mask);
2803 return (old_mask);
2804}
2805
2806sigset_t
2807sys_sigunblock (sigset_t new_mask)
2808{
2809 sigset_t old_mask;
2810 sigprocmask (SIG_UNBLOCK, &new_mask, &old_mask);
2811 return (old_mask);
2812}
2813
2814sigset_t
2815sys_sigsetmask (sigset_t new_mask)
2816{
2817 sigset_t old_mask;
2818 sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
2819 return (old_mask);
2820}
2821
2822#endif /* POSIX_SIGNALS */
2823\f
9927a7b1 2824#ifndef HAVE_RANDOM
4bb8c8b7
KH
2825#ifdef random
2826#define HAVE_RANDOM
2827#endif
2828#endif
2829
2830/* Figure out how many bits the system's random number generator uses.
2831 `random' and `lrand48' are assumed to return 31 usable bits.
2832 BSD `rand' returns a 31 bit value but the low order bits are unusable;
2833 so we'll shift it and treat it like the 15-bit USG `rand'. */
2834
2835#ifndef RAND_BITS
2836# ifdef HAVE_RANDOM
2837# define RAND_BITS 31
2838# else /* !HAVE_RANDOM */
2839# ifdef HAVE_LRAND48
2840# define RAND_BITS 31
2841# define random lrand48
2842# else /* !HAVE_LRAND48 */
2843# define RAND_BITS 15
2844# if RAND_MAX == 32767
2845# define random rand
2846# else /* RAND_MAX != 32767 */
2847# if RAND_MAX == 2147483647
2848# define random() (rand () >> 16)
2849# else /* RAND_MAX != 2147483647 */
2850# ifdef USG
2851# define random rand
2852# else
2853# define random() (rand () >> 16)
2a633456 2854# endif /* !USG */
4bb8c8b7
KH
2855# endif /* RAND_MAX != 2147483647 */
2856# endif /* RAND_MAX != 32767 */
2857# endif /* !HAVE_LRAND48 */
2858# endif /* !HAVE_RANDOM */
2859#endif /* !RAND_BITS */
2e46c7c6 2860
4bb8c8b7
KH
2861void
2862seed_random (arg)
2863 long arg;
86a5659e 2864{
4bb8c8b7
KH
2865#ifdef HAVE_RANDOM
2866 srandom ((unsigned int)arg);
f8b53a82 2867#else
4bb8c8b7 2868# ifdef HAVE_LRAND48
76425a49 2869 srand48 (arg);
4bb8c8b7
KH
2870# else
2871 srand ((unsigned int)arg);
2872# endif
2e46c7c6 2873#endif
86a5659e
JB
2874}
2875
4bb8c8b7
KH
2876/*
2877 * Build a full Emacs-sized word out of whatever we've got.
2878 * This suffices even for a 64-bit architecture with a 15-bit rand.
2879 */
2880long
2881get_random ()
2882{
2883 long val = random ();
2884#if VALBITS > RAND_BITS
2885 val = (val << RAND_BITS) ^ random ();
2886#if VALBITS > 2*RAND_BITS
2887 val = (val << RAND_BITS) ^ random ();
2888#if VALBITS > 3*RAND_BITS
2889 val = (val << RAND_BITS) ^ random ();
2890#if VALBITS > 4*RAND_BITS
2891 val = (val << RAND_BITS) ^ random ();
2892#endif /* need at least 5 */
2893#endif /* need at least 4 */
2894#endif /* need at least 3 */
2895#endif /* need at least 2 */
2896 return val & ((1L << VALBITS) - 1);
2897}
86a5659e
JB
2898\f
2899#ifdef WRONG_NAME_INSQUE
2900
2901insque (q,p)
2902 caddr_t q,p;
2903{
2904 _insque (q,p);
2905}
2906
2907#endif
2908\f
2909#ifdef VMS
2910
2911#ifdef getenv
2912/* If any place else asks for the TERM variable,
2913 allow it to be overridden with the EMACS_TERM variable
2914 before attempting to translate the logical name TERM. As a last
2915 resort, ask for VAX C's special idea of the TERM variable. */
2916#undef getenv
2917char *
2918sys_getenv (name)
2919 char *name;
2920{
2921 register char *val;
2922 static char buf[256];
2923 static struct dsc$descriptor_s equiv
2924 = {sizeof (buf), DSC$K_DTYPE_T, DSC$K_CLASS_S, buf};
2925 static struct dsc$descriptor_s d_name
2926 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2927 short eqlen;
2928
2929 if (!strcmp (name, "TERM"))
2930 {
2931 val = (char *) getenv ("EMACS_TERM");
2932 if (val)
2933 return val;
2934 }
2935
2936 d_name.dsc$w_length = strlen (name);
2937 d_name.dsc$a_pointer = name;
986ffb24 2938 if (LIB$SYS_TRNLOG (&d_name, &eqlen, &equiv) == 1)
86a5659e
JB
2939 {
2940 char *str = (char *) xmalloc (eqlen + 1);
2941 bcopy (buf, str, eqlen);
2942 str[eqlen] = '\0';
2943 /* This is a storage leak, but a pain to fix. With luck,
2944 no one will ever notice. */
2945 return str;
2946 }
2947 return (char *) getenv (name);
2948}
2949#endif /* getenv */
2950
2951#ifdef abort
2952/* Since VMS doesn't believe in core dumps, the only way to debug this beast is
2953 to force a call on the debugger from within the image. */
2954#undef abort
2955sys_abort ()
2956{
2957 reset_sys_modes ();
2958 LIB$SIGNAL (SS$_DEBUG);
2959}
2960#endif /* abort */
2961#endif /* VMS */
2962\f
2963#ifdef VMS
2964#ifdef LINK_CRTL_SHARE
ea5a0917 2965#ifdef SHARABLE_LIB_BUG
eb8c3be9 2966/* Variables declared noshare and initialized in sharable libraries
86a5659e
JB
2967 cannot be shared. The VMS linker incorrectly forces you to use a private
2968 version which is uninitialized... If not for this "feature", we
2969 could use the C library definition of sys_nerr and sys_errlist. */
2970int sys_nerr = 35;
2971char *sys_errlist[] =
2972 {
2973 "error 0",
2974 "not owner",
2975 "no such file or directory",
2976 "no such process",
2977 "interrupted system call",
2978 "i/o error",
2979 "no such device or address",
2980 "argument list too long",
2981 "exec format error",
2982 "bad file number",
2983 "no child process",
2984 "no more processes",
2985 "not enough memory",
2986 "permission denied",
2987 "bad address",
2988 "block device required",
2989 "mount devices busy",
2990 "file exists",
2991 "cross-device link",
2992 "no such device",
2993 "not a directory",
2994 "is a directory",
2995 "invalid argument",
2996 "file table overflow",
2997 "too many open files",
2998 "not a typewriter",
2999 "text file busy",
3000 "file too big",
3001 "no space left on device",
3002 "illegal seek",
3003 "read-only file system",
3004 "too many links",
3005 "broken pipe",
3006 "math argument",
3007 "result too large",
3008 "I/O stream empty",
3009 "vax/vms specific error code nontranslatable error"
3010 };
ea5a0917 3011#endif /* SHARABLE_LIB_BUG */
86a5659e
JB
3012#endif /* LINK_CRTL_SHARE */
3013#endif /* VMS */
7088d1ca
RM
3014
3015#ifndef HAVE_STRERROR
fe03522b 3016#ifndef WINDOWSNT
7088d1ca
RM
3017char *
3018strerror (errnum)
3019 int errnum;
3020{
3021 extern char *sys_errlist[];
3022 extern int sys_nerr;
3023
3024 if (errnum >= 0 && errnum < sys_nerr)
3025 return sys_errlist[errnum];
3026 return (char *) "Unknown error";
3027}
fe03522b 3028#endif /* not WINDOWSNT */
7088d1ca 3029#endif /* ! HAVE_STRERROR */
86a5659e
JB
3030\f
3031#ifdef INTERRUPTIBLE_OPEN
3032
3033int
3034/* VARARGS 2 */
3035sys_open (path, oflag, mode)
3036 char *path;
3037 int oflag, mode;
3038{
3039 register int rtnval;
3040
3041 while ((rtnval = open (path, oflag, mode)) == -1
3042 && (errno == EINTR));
3043 return (rtnval);
3044}
3045
3046#endif /* INTERRUPTIBLE_OPEN */
3047
3048#ifdef INTERRUPTIBLE_CLOSE
3049
dfcf069d 3050int
86a5659e
JB
3051sys_close (fd)
3052 int fd;
3053{
fe111daf 3054 int did_retry = 0;
86a5659e
JB
3055 register int rtnval;
3056
3057 while ((rtnval = close (fd)) == -1
fe111daf
KH
3058 && (errno == EINTR))
3059 did_retry = 1;
3060
3061 /* If close is interrupted SunOS 4.1 may or may not have closed the
3062 file descriptor. If it did the second close will fail with
3063 errno = EBADF. That means we have succeeded. */
3064 if (rtnval == -1 && did_retry && errno == EBADF)
3065 return 0;
3066
86a5659e
JB
3067 return rtnval;
3068}
3069
3070#endif /* INTERRUPTIBLE_CLOSE */
3071
3072#ifdef INTERRUPTIBLE_IO
3073
3074int
3075sys_read (fildes, buf, nbyte)
3076 int fildes;
3077 char *buf;
3078 unsigned int nbyte;
3079{
3080 register int rtnval;
3081
3082 while ((rtnval = read (fildes, buf, nbyte)) == -1
3083 && (errno == EINTR));
3084 return (rtnval);
3085}
3086
3087int
3088sys_write (fildes, buf, nbyte)
3089 int fildes;
3090 char *buf;
3091 unsigned int nbyte;
3092{
b95520f5 3093 register int rtnval, bytes_written;
86a5659e 3094
b95520f5
BF
3095 bytes_written = 0;
3096
3097 while (nbyte > 0)
3098 {
3099 rtnval = write (fildes, buf, nbyte);
3100
3101 if (rtnval == -1)
3102 {
3103 if (errno == EINTR)
3104 continue;
3105 else
aa670904 3106 return (bytes_written ? bytes_written : -1);
b95520f5
BF
3107 }
3108
3109 buf += rtnval;
3110 nbyte -= rtnval;
3111 bytes_written += rtnval;
3112 }
3113 return (bytes_written);
86a5659e
JB
3114}
3115
3116#endif /* INTERRUPTIBLE_IO */
3117\f
0e18d8ef 3118#ifndef HAVE_VFORK
fe03522b 3119#ifndef WINDOWSNT
0e18d8ef 3120/*
fe03522b 3121 * Substitute fork for vfork on USG flavors.
0e18d8ef
RS
3122 */
3123
e336874b 3124VFORK_RETURN_TYPE
0e18d8ef
RS
3125vfork ()
3126{
3127 return (fork ());
3128}
fe03522b 3129#endif /* not WINDOWSNT */
0e18d8ef
RS
3130#endif /* not HAVE_VFORK */
3131\f
86a5659e
JB
3132#ifdef USG
3133/*
3134 * All of the following are for USG.
3135 *
3136 * On USG systems the system calls are INTERRUPTIBLE by signals
3137 * that the user program has elected to catch. Thus the system call
3138 * must be retried in these cases. To handle this without massive
3139 * changes in the source code, we remap the standard system call names
3140 * to names for our own functions in sysdep.c that do the system call
3141 * with retries. Actually, for portability reasons, it is good
3142 * programming practice, as this example shows, to limit all actual
eb8c3be9 3143 * system calls to a single occurrence in the source. Sure, this
86a5659e
JB
3144 * adds an extra level of function call overhead but it is almost
3145 * always negligible. Fred Fish, Unisoft Systems Inc.
3146 */
3147
00eaaa32 3148#ifndef HAVE_SYS_SIGLIST
86a5659e
JB
3149char *sys_siglist[NSIG + 1] =
3150{
3151#ifdef AIX
3152/* AIX has changed the signals a bit */
3153 "bogus signal", /* 0 */
3154 "hangup", /* 1 SIGHUP */
3155 "interrupt", /* 2 SIGINT */
3156 "quit", /* 3 SIGQUIT */
3157 "illegal instruction", /* 4 SIGILL */
3158 "trace trap", /* 5 SIGTRAP */
3159 "IOT instruction", /* 6 SIGIOT */
3160 "crash likely", /* 7 SIGDANGER */
3161 "floating point exception", /* 8 SIGFPE */
3162 "kill", /* 9 SIGKILL */
3163 "bus error", /* 10 SIGBUS */
3164 "segmentation violation", /* 11 SIGSEGV */
3165 "bad argument to system call", /* 12 SIGSYS */
3166 "write on a pipe with no one to read it", /* 13 SIGPIPE */
3167 "alarm clock", /* 14 SIGALRM */
3168 "software termination signum", /* 15 SIGTERM */
3169 "user defined signal 1", /* 16 SIGUSR1 */
3170 "user defined signal 2", /* 17 SIGUSR2 */
3171 "death of a child", /* 18 SIGCLD */
3172 "power-fail restart", /* 19 SIGPWR */
3173 "bogus signal", /* 20 */
3174 "bogus signal", /* 21 */
3175 "bogus signal", /* 22 */
3176 "bogus signal", /* 23 */
3177 "bogus signal", /* 24 */
3178 "LAN I/O interrupt", /* 25 SIGAIO */
3179 "PTY I/O interrupt", /* 26 SIGPTY */
3180 "I/O intervention required", /* 27 SIGIOINT */
6056c55b 3181#ifdef AIXHFT
86a5659e
JB
3182 "HFT grant", /* 28 SIGGRANT */
3183 "HFT retract", /* 29 SIGRETRACT */
3184 "HFT sound done", /* 30 SIGSOUND */
3185 "HFT input ready", /* 31 SIGMSG */
6056c55b 3186#endif
86a5659e
JB
3187#else /* not AIX */
3188 "bogus signal", /* 0 */
3189 "hangup", /* 1 SIGHUP */
3190 "interrupt", /* 2 SIGINT */
3191 "quit", /* 3 SIGQUIT */
3192 "illegal instruction", /* 4 SIGILL */
3193 "trace trap", /* 5 SIGTRAP */
3194 "IOT instruction", /* 6 SIGIOT */
3195 "EMT instruction", /* 7 SIGEMT */
3196 "floating point exception", /* 8 SIGFPE */
3197 "kill", /* 9 SIGKILL */
3198 "bus error", /* 10 SIGBUS */
3199 "segmentation violation", /* 11 SIGSEGV */
3200 "bad argument to system call", /* 12 SIGSYS */
3201 "write on a pipe with no one to read it", /* 13 SIGPIPE */
3202 "alarm clock", /* 14 SIGALRM */
3203 "software termination signum", /* 15 SIGTERM */
3204 "user defined signal 1", /* 16 SIGUSR1 */
3205 "user defined signal 2", /* 17 SIGUSR2 */
3206 "death of a child", /* 18 SIGCLD */
3207 "power-fail restart", /* 19 SIGPWR */
0f0ea229
RS
3208#ifdef sun
3209 "window size change", /* 20 SIGWINCH */
3210 "urgent socket condition", /* 21 SIGURG */
ea5a0917 3211 "pollable event occurred", /* 22 SIGPOLL */
0f0ea229
RS
3212 "stop (cannot be caught or ignored)", /* 23 SIGSTOP */
3213 "user stop requested from tty", /* 24 SIGTSTP */
3214 "stopped process has been continued", /* 25 SIGCONT */
3215 "background tty read attempted", /* 26 SIGTTIN */
3216 "background tty write attempted", /* 27 SIGTTOU */
3217 "virtual timer expired", /* 28 SIGVTALRM */
3218 "profiling timer expired", /* 29 SIGPROF */
3219 "exceeded cpu limit", /* 30 SIGXCPU */
3220 "exceeded file size limit", /* 31 SIGXFSZ */
3221 "process's lwps are blocked", /* 32 SIGWAITING */
3222 "special signal used by thread library", /* 33 SIGLWP */
2da0df4d 3223#ifdef SIGFREEZE
0f0ea229 3224 "Special Signal Used By CPR", /* 34 SIGFREEZE */
2da0df4d
RS
3225#endif
3226#ifdef SIGTHAW
0f0ea229 3227 "Special Signal Used By CPR", /* 35 SIGTHAW */
2da0df4d 3228#endif
0f0ea229 3229#endif /* sun */
86a5659e
JB
3230#endif /* not AIX */
3231 0
3232 };
70080de1 3233#endif /* HAVE_SYS_SIGLIST */
86a5659e
JB
3234
3235/*
3236 * Warning, this function may not duplicate 4.2 action properly
3237 * under error conditions.
3238 */
3239
3240#ifndef MAXPATHLEN
3241/* In 4.1, param.h fails to define this. */
3242#define MAXPATHLEN 1024
3243#endif
3244
3245#ifndef HAVE_GETWD
3246
3247char *
3248getwd (pathname)
3249 char *pathname;
3250{
3251 char *npath, *spath;
3252 extern char *getcwd ();
3253
9ac0d9e0 3254 BLOCK_INPUT; /* getcwd uses malloc */
86a5659e 3255 spath = npath = getcwd ((char *) 0, MAXPATHLEN);
f4a7e5bd
RS
3256 if (spath == 0)
3257 return spath;
86a5659e
JB
3258 /* On Altos 3068, getcwd can return @hostname/dir, so discard
3259 up to first slash. Should be harmless on other systems. */
3260 while (*npath && *npath != '/')
3261 npath++;
3262 strcpy (pathname, npath);
3263 free (spath); /* getcwd uses malloc */
9ac0d9e0 3264 UNBLOCK_INPUT;
86a5659e
JB
3265 return pathname;
3266}
3267
3268#endif /* HAVE_GETWD */
3269
3270/*
3271 * Emulate rename using unlink/link. Note that this is
3272 * only partially correct. Also, doesn't enforce restriction
3273 * that files be of same type (regular->regular, dir->dir, etc).
3274 */
3275
4746118a
JB
3276#ifndef HAVE_RENAME
3277
86a5659e 3278rename (from, to)
19c7afdf
JB
3279 const char *from;
3280 const char *to;
86a5659e
JB
3281{
3282 if (access (from, 0) == 0)
3283 {
3284 unlink (to);
3285 if (link (from, to) == 0)
3286 if (unlink (from) == 0)
3287 return (0);
3288 }
3289 return (-1);
3290}
3291
4746118a
JB
3292#endif
3293
86a5659e
JB
3294
3295#ifdef HPUX
3296#ifndef HAVE_PERROR
3297
3298/* HPUX curses library references perror, but as far as we know
3299 it won't be called. Anyway this definition will do for now. */
3300
3301perror ()
3302{
3303}
3304
3305#endif /* not HAVE_PERROR */
3306#endif /* HPUX */
3307
3308#ifndef HAVE_DUP2
3309
3310/*
3311 * Emulate BSD dup2. First close newd if it already exists.
3312 * Then, attempt to dup oldd. If not successful, call dup2 recursively
3313 * until we are, then close the unsuccessful ones.
3314 */
3315
3316dup2 (oldd, newd)
3317 int oldd;
3318 int newd;
3319{
3320 register int fd, ret;
3321
3322 sys_close (newd);
3323
3324#ifdef F_DUPFD
3325 fd = fcntl (oldd, F_DUPFD, newd);
3326 if (fd != newd)
7088d1ca 3327 error ("can't dup2 (%i,%i) : %s", oldd, newd, strerror (errno));
86a5659e
JB
3328#else
3329 fd = dup (old);
3330 if (fd == -1)
3331 return -1;
3332 if (fd == new)
3333 return new;
3334 ret = dup2 (old,new);
3335 sys_close (fd);
3336 return ret;
3337#endif
3338}
3339
3340#endif /* not HAVE_DUP2 */
3341
3342/*
3343 * Gettimeofday. Simulate as much as possible. Only accurate
3344 * to nearest second. Emacs doesn't use tzp so ignore it for now.
3345 * Only needed when subprocesses are defined.
3346 */
3347
3348#ifdef subprocesses
3349#ifndef VMS
3350#ifndef HAVE_GETTIMEOFDAY
3351#ifdef HAVE_TIMEVAL
3352
3353/* ARGSUSED */
dfcf069d 3354int
86a5659e
JB
3355gettimeofday (tp, tzp)
3356 struct timeval *tp;
3357 struct timezone *tzp;
3358{
3359 extern long time ();
3360
3361 tp->tv_sec = time ((long *)0);
3362 tp->tv_usec = 0;
4ca7594f
RS
3363 if (tzp != 0)
3364 tzp->tz_minuteswest = -1;
dfcf069d 3365 return 0;
86a5659e
JB
3366}
3367
3368#endif
3369#endif
3370#endif
3371#endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
3372
3373/*
3374 * This function will go away as soon as all the stubs fixed. (fnf)
3375 */
3376
dfcf069d 3377void
86a5659e
JB
3378croak (badfunc)
3379 char *badfunc;
3380{
3381 printf ("%s not yet implemented\r\n", badfunc);
3382 reset_sys_modes ();
3383 exit (1);
3384}
3385
3386#endif /* USG */
3387\f
3388#ifdef DGUX
3389
3390char *sys_siglist[NSIG + 1] =
3391{
3392 "null signal", /* 0 SIGNULL */
3393 "hangup", /* 1 SIGHUP */
3394 "interrupt", /* 2 SIGINT */
3395 "quit", /* 3 SIGQUIT */
3396 "illegal instruction", /* 4 SIGILL */
3397 "trace trap", /* 5 SIGTRAP */
3398 "abort termination", /* 6 SIGABRT */
3399 "SIGEMT", /* 7 SIGEMT */
3400 "floating point exception", /* 8 SIGFPE */
3401 "kill", /* 9 SIGKILL */
3402 "bus error", /* 10 SIGBUS */
3403 "segmentation violation", /* 11 SIGSEGV */
3404 "bad argument to system call", /* 12 SIGSYS */
3405 "write on a pipe with no reader", /* 13 SIGPIPE */
3406 "alarm clock", /* 14 SIGALRM */
3407 "software termination signal", /* 15 SIGTERM */
3408 "user defined signal 1", /* 16 SIGUSR1 */
3409 "user defined signal 2", /* 17 SIGUSR2 */
3410 "child stopped or terminated", /* 18 SIGCLD */
3411 "power-fail restart", /* 19 SIGPWR */
3412 "window size changed", /* 20 SIGWINCH */
3413 "undefined", /* 21 */
eb8c3be9 3414 "pollable event occurred", /* 22 SIGPOLL */
86a5659e
JB
3415 "sendable stop signal not from tty", /* 23 SIGSTOP */
3416 "stop signal from tty", /* 24 SIGSTP */
3417 "continue a stopped process", /* 25 SIGCONT */
3418 "attempted background tty read", /* 26 SIGTTIN */
3419 "attempted background tty write", /* 27 SIGTTOU */
3420 "undefined", /* 28 */
3421 "undefined", /* 29 */
3422 "undefined", /* 30 */
3423 "undefined", /* 31 */
3424 "undefined", /* 32 */
3425 "socket (TCP/IP) urgent data arrival", /* 33 SIGURG */
3426 "I/O is possible", /* 34 SIGIO */
3427 "exceeded cpu time limit", /* 35 SIGXCPU */
3428 "exceeded file size limit", /* 36 SIGXFSZ */
3429 "virtual time alarm", /* 37 SIGVTALRM */
3430 "profiling time alarm", /* 38 SIGPROF */
3431 "undefined", /* 39 */
3432 "file record locks revoked", /* 40 SIGLOST */
3433 "undefined", /* 41 */
3434 "undefined", /* 42 */
3435 "undefined", /* 43 */
3436 "undefined", /* 44 */
3437 "undefined", /* 45 */
3438 "undefined", /* 46 */
3439 "undefined", /* 47 */
3440 "undefined", /* 48 */
3441 "undefined", /* 49 */
3442 "undefined", /* 50 */
3443 "undefined", /* 51 */
3444 "undefined", /* 52 */
3445 "undefined", /* 53 */
3446 "undefined", /* 54 */
3447 "undefined", /* 55 */
3448 "undefined", /* 56 */
3449 "undefined", /* 57 */
3450 "undefined", /* 58 */
3451 "undefined", /* 59 */
3452 "undefined", /* 60 */
3453 "undefined", /* 61 */
3454 "undefined", /* 62 */
3455 "undefined", /* 63 */
3456 "notification message in mess. queue", /* 64 SIGDGNOTIFY */
3457 0
3458};
3459
3460#endif /* DGUX */
3461\f
3462/* Directory routines for systems that don't have them. */
3463
3464#ifdef SYSV_SYSTEM_DIR
3465
3466#include <dirent.h>
3467
1db6401c 3468#if defined(BROKEN_CLOSEDIR) || !defined(HAVE_CLOSEDIR)
cfdc57af 3469
86a5659e
JB
3470int
3471closedir (dirp)
3472 register DIR *dirp; /* stream from opendir */
3473{
cfdc57af
RS
3474 int rtnval;
3475
3476 rtnval = sys_close (dirp->dd_fd);
1b929d25 3477
65aa44ac
JB
3478 /* Some systems (like Solaris) allocate the buffer and the DIR all
3479 in one block. Why in the world are we freeing this ourselves
3480 anyway? */
3481#if ! (defined (sun) && defined (USG5_4))
3482 xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
3483#endif
9ac0d9e0 3484 xfree ((char *) dirp);
cfdc57af
RS
3485
3486 return rtnval;
86a5659e 3487}
1db6401c 3488#endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
86a5659e
JB
3489#endif /* SYSV_SYSTEM_DIR */
3490
3491#ifdef NONSYSTEM_DIR_LIBRARY
3492
3493DIR *
3494opendir (filename)
3495 char *filename; /* name of directory */
3496{
3497 register DIR *dirp; /* -> malloc'ed storage */
3498 register int fd; /* file descriptor for read */
3499 struct stat sbuf; /* result of fstat */
3500
3501 fd = sys_open (filename, 0);
3502 if (fd < 0)
3503 return 0;
3504
9ac0d9e0 3505 BLOCK_INPUT;
86a5659e
JB
3506 if (fstat (fd, &sbuf) < 0
3507 || (sbuf.st_mode & S_IFMT) != S_IFDIR
3508 || (dirp = (DIR *) malloc (sizeof (DIR))) == 0)
3509 {
3510 sys_close (fd);
9ac0d9e0 3511 UNBLOCK_INPUT;
86a5659e
JB
3512 return 0; /* bad luck today */
3513 }
9ac0d9e0 3514 UNBLOCK_INPUT;
86a5659e
JB
3515
3516 dirp->dd_fd = fd;
3517 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
3518
3519 return dirp;
3520}
3521
3522void
3523closedir (dirp)
3524 register DIR *dirp; /* stream from opendir */
3525{
3526 sys_close (dirp->dd_fd);
9ac0d9e0 3527 xfree ((char *) dirp);
86a5659e
JB
3528}
3529
3530
3531#ifndef VMS
3532#define DIRSIZ 14
3533struct olddir
3534 {
3535 ino_t od_ino; /* inode */
3536 char od_name[DIRSIZ]; /* filename */
3537 };
3538#endif /* not VMS */
3539
3540struct direct dir_static; /* simulated directory contents */
3541
3542/* ARGUSED */
3543struct direct *
3544readdir (dirp)
3545 register DIR *dirp; /* stream from opendir */
3546{
3547#ifndef VMS
3548 register struct olddir *dp; /* -> directory data */
3549#else /* VMS */
3550 register struct dir$_name *dp; /* -> directory data */
3551 register struct dir$_version *dv; /* -> version data */
3552#endif /* VMS */
3553
3554 for (; ;)
3555 {
3556 if (dirp->dd_loc >= dirp->dd_size)
3557 dirp->dd_loc = dirp->dd_size = 0;
3558
3559 if (dirp->dd_size == 0 /* refill buffer */
3560 && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3561 return 0;
3562
3563#ifndef VMS
3564 dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc];
3565 dirp->dd_loc += sizeof (struct olddir);
3566
3567 if (dp->od_ino != 0) /* not deleted entry */
3568 {
3569 dir_static.d_ino = dp->od_ino;
3570 strncpy (dir_static.d_name, dp->od_name, DIRSIZ);
3571 dir_static.d_name[DIRSIZ] = '\0';
3572 dir_static.d_namlen = strlen (dir_static.d_name);
3573 dir_static.d_reclen = sizeof (struct direct)
3574 - MAXNAMLEN + 3
3575 + dir_static.d_namlen - dir_static.d_namlen % 4;
3576 return &dir_static; /* -> simulated structure */
3577 }
3578#else /* VMS */
3579 dp = (struct dir$_name *) dirp->dd_buf;
3580 if (dirp->dd_loc == 0)
3581 dirp->dd_loc = (dp->dir$b_namecount&1) ? dp->dir$b_namecount + 1
3582 : dp->dir$b_namecount;
3583 dv = (struct dir$_version *)&dp->dir$t_name[dirp->dd_loc];
3584 dir_static.d_ino = dv->dir$w_fid_num;
3585 dir_static.d_namlen = dp->dir$b_namecount;
3586 dir_static.d_reclen = sizeof (struct direct)
3587 - MAXNAMLEN + 3
3588 + dir_static.d_namlen - dir_static.d_namlen % 4;
3589 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3590 dir_static.d_name[dir_static.d_namlen] = '\0';
3591 dirp->dd_loc = dirp->dd_size; /* only one record at a time */
3592 return &dir_static;
3593#endif /* VMS */
3594 }
3595}
3596
3597#ifdef VMS
3598/* readdirver is just like readdir except it returns all versions of a file
3599 as separate entries. */
3600
3601/* ARGUSED */
3602struct direct *
3603readdirver (dirp)
3604 register DIR *dirp; /* stream from opendir */
3605{
3606 register struct dir$_name *dp; /* -> directory data */
3607 register struct dir$_version *dv; /* -> version data */
3608
3609 if (dirp->dd_loc >= dirp->dd_size - sizeof (struct dir$_name))
3610 dirp->dd_loc = dirp->dd_size = 0;
3611
3612 if (dirp->dd_size == 0 /* refill buffer */
3613 && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3614 return 0;
3615
3616 dp = (struct dir$_name *) dirp->dd_buf;
3617 if (dirp->dd_loc == 0)
3618 dirp->dd_loc = (dp->dir$b_namecount & 1) ? dp->dir$b_namecount + 1
3619 : dp->dir$b_namecount;
3620 dv = (struct dir$_version *) &dp->dir$t_name[dirp->dd_loc];
3621 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3622 sprintf (&dir_static.d_name[dp->dir$b_namecount], ";%d", dv->dir$w_version);
3623 dir_static.d_namlen = strlen (dir_static.d_name);
3624 dir_static.d_ino = dv->dir$w_fid_num;
3625 dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3
3626 + dir_static.d_namlen - dir_static.d_namlen % 4;
3627 dirp->dd_loc = ((char *) (++dv) - dp->dir$t_name);
3628 return &dir_static;
3629}
3630
3631#endif /* VMS */
3632
3633#endif /* NONSYSTEM_DIR_LIBRARY */
23524fb9
JB
3634
3635\f
53ea491a 3636int
061ea326 3637set_file_times (filename, atime, mtime)
8334eb21 3638 char *filename;
53ea491a
KH
3639 EMACS_TIME atime, mtime;
3640{
3641#ifdef HAVE_UTIMES
3642 struct timeval tv[2];
3643 tv[0] = atime;
3644 tv[1] = mtime;
8334eb21
RS
3645 return utimes (filename, tv);
3646#else /* not HAVE_UTIMES */
53ea491a
KH
3647 struct utimbuf utb;
3648 utb.actime = EMACS_SECS (atime);
3649 utb.modtime = EMACS_SECS (mtime);
8334eb21
RS
3650 return utime (filename, &utb);
3651#endif /* not HAVE_UTIMES */
53ea491a
KH
3652}
3653\f
23524fb9
JB
3654/* mkdir and rmdir functions, for systems which don't have them. */
3655
3656#ifndef HAVE_MKDIR
3657/*
3658 * Written by Robert Rother, Mariah Corporation, August 1985.
3659 *
3660 * If you want it, it's yours. All I ask in return is that if you
3661 * figure out how to do this in a Bourne Shell script you send me
3662 * a copy.
3663 * sdcsvax!rmr or rmr@uscd
3664 *
3665 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
3666 * subroutine. 11Mar86; hoptoad!gnu
3667 *
3668 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
3669 * subroutine didn't return EEXIST. It does now.
3670 */
3671
3672/*
3673 * Make a directory.
3674 */
f3892946
RS
3675#ifdef MKDIR_PROTOTYPE
3676MKDIR_PROTOTYPE
3677#else
23524fb9
JB
3678int
3679mkdir (dpath, dmode)
3680 char *dpath;
3681 int dmode;
f3892946 3682#endif
23524fb9 3683{
039f26a4 3684 int cpid, status, fd;
23524fb9
JB
3685 struct stat statbuf;
3686
3687 if (stat (dpath, &statbuf) == 0)
3688 {
3689 errno = EEXIST; /* Stat worked, so it already exists */
3690 return -1;
3691 }
3692
3693 /* If stat fails for a reason other than non-existence, return error */
3694 if (errno != ENOENT)
3695 return -1;
3696
039f26a4 3697 synch_process_alive = 1;
23524fb9
JB
3698 switch (cpid = fork ())
3699 {
3700
039f26a4 3701 case -1: /* Error in fork */
23524fb9
JB
3702 return (-1); /* Errno is set already */
3703
3704 case 0: /* Child process */
3705 /*
3706 * Cheap hack to set mode of new directory. Since this
3707 * child process is going away anyway, we zap its umask.
3708 * FIXME, this won't suffice to set SUID, SGID, etc. on this
3709 * directory. Does anybody care?
3710 */
3711 status = umask (0); /* Get current umask */
3712 status = umask (status | (0777 & ~dmode)); /* Set for mkdir */
039f26a4
RS
3713 fd = sys_open("/dev/null", 2);
3714 if (fd >= 0)
3715 {
3716 dup2 (fd, 0);
3717 dup2 (fd, 1);
3718 dup2 (fd, 2);
3719 }
23524fb9
JB
3720 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
3721 _exit (-1); /* Can't exec /bin/mkdir */
3722
3723 default: /* Parent process */
039f26a4 3724 wait_for_termination (cpid);
23524fb9
JB
3725 }
3726
039f26a4 3727 if (synch_process_death != 0 || synch_process_retcode != 0)
23524fb9
JB
3728 {
3729 errno = EIO; /* We don't know why, but */
3730 return -1; /* /bin/mkdir failed */
3731 }
3732
3733 return 0;
3734}
3735#endif /* not HAVE_MKDIR */
3736
3737#ifndef HAVE_RMDIR
3738int
3739rmdir (dpath)
3740 char *dpath;
3741{
039f26a4 3742 int cpid, status, fd;
23524fb9
JB
3743 struct stat statbuf;
3744
3745 if (stat (dpath, &statbuf) != 0)
3746 {
3747 /* Stat just set errno. We don't have to */
3748 return -1;
3749 }
3750
039f26a4 3751 synch_process_alive = 1;
23524fb9
JB
3752 switch (cpid = fork ())
3753 {
3754
039f26a4 3755 case -1: /* Error in fork */
23524fb9
JB
3756 return (-1); /* Errno is set already */
3757
3758 case 0: /* Child process */
039f26a4
RS
3759 fd = sys_open("/dev/null", 2);
3760 if (fd >= 0)
3761 {
3762 dup2 (fd, 0);
3763 dup2 (fd, 1);
3764 dup2 (fd, 2);
3765 }
f560db78
RS
3766 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
3767 _exit (-1); /* Can't exec /bin/rmdir */
3768
207bdbdb 3769 default: /* Parent process */
f560db78 3770 wait_for_termination (cpid);
23524fb9
JB
3771 }
3772
f560db78 3773 if (synch_process_death != 0 || synch_process_retcode != 0)
23524fb9
JB
3774 {
3775 errno = EIO; /* We don't know why, but */
f560db78 3776 return -1; /* /bin/rmdir failed */
23524fb9
JB
3777 }
3778
3779 return 0;
3780}
3781#endif /* !HAVE_RMDIR */
3782
3783
86a5659e
JB
3784\f
3785/* Functions for VMS */
3786#ifdef VMS
91bac16a 3787#include "vms-pwd.h"
86a5659e
JB
3788#include <acldef.h>
3789#include <chpdef.h>
3790#include <jpidef.h>
3791
3792/* Return as a string the VMS error string pertaining to STATUS.
3793 Reuses the same static buffer each time it is called. */
3794
3795char *
3796vmserrstr (status)
3797 int status; /* VMS status code */
3798{
3799 int bufadr[2];
3800 short len;
3801 static char buf[257];
3802
3803 bufadr[0] = sizeof buf - 1;
3804 bufadr[1] = (int) buf;
3805 if (! (SYS$GETMSG (status, &len, bufadr, 0x1, 0) & 1))
3806 return "untranslatable VMS error status";
3807 buf[len] = '\0';
3808 return buf;
3809}
3810
3811#ifdef access
3812#undef access
3813
3814/* The following is necessary because 'access' emulation by VMS C (2.0) does
3815 * not work correctly. (It also doesn't work well in version 2.3.)
3816 */
3817
3818#ifdef VMS4_4
3819
3820#define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3821 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3822
3823typedef union {
3824 struct {
3825 unsigned short s_buflen;
3826 unsigned short s_code;
3827 char *s_bufadr;
3828 unsigned short *s_retlenadr;
3829 } s;
3830 int end;
3831} item;
3832#define buflen s.s_buflen
3833#define code s.s_code
3834#define bufadr s.s_bufadr
3835#define retlenadr s.s_retlenadr
3836
3837#define R_OK 4 /* test for read permission */
3838#define W_OK 2 /* test for write permission */
3839#define X_OK 1 /* test for execute (search) permission */
3840#define F_OK 0 /* test for presence of file */
3841
3842int
3843sys_access (path, mode)
3844 char *path;
3845 int mode;
3846{
3847 static char *user = NULL;
3848 char dir_fn[512];
3849
3850 /* translate possible directory spec into .DIR file name, so brain-dead
3851 * access can treat the directory like a file. */
3852 if (directory_file_name (path, dir_fn))
3853 path = dir_fn;
3854
3855 if (mode == F_OK)
3856 return access (path, mode);
3857 if (user == NULL && (user = (char *) getenv ("USER")) == NULL)
3858 return -1;
3859 {
3860 int stat;
3861 int flags;
3862 int acces;
3863 unsigned short int dummy;
3864 item itemlst[3];
3865 static int constant = ACL$C_FILE;
3866 DESCRIPTOR (path_desc, path);
3867 DESCRIPTOR (user_desc, user);
3868
3869 flags = 0;
3870 acces = 0;
3871 if ((mode & X_OK) && ((stat = access (path, mode)) < 0 || mode == X_OK))
3872 return stat;
3873 if (mode & R_OK)
3874 acces |= CHP$M_READ;
3875 if (mode & W_OK)
3876 acces |= CHP$M_WRITE;
3877 itemlst[0].buflen = sizeof (int);
3878 itemlst[0].code = CHP$_FLAGS;
3879 itemlst[0].bufadr = (char *) &flags;
3880 itemlst[0].retlenadr = &dummy;
3881 itemlst[1].buflen = sizeof (int);
3882 itemlst[1].code = CHP$_ACCESS;
3883 itemlst[1].bufadr = (char *) &acces;
3884 itemlst[1].retlenadr = &dummy;
3885 itemlst[2].end = CHP$_END;
3886 stat = SYS$CHECK_ACCESS (&constant, &path_desc, &user_desc, itemlst);
3887 return stat == SS$_NORMAL ? 0 : -1;
3888 }
3889}
3890
3891#else /* not VMS4_4 */
3892
3893#include <prvdef.h>
fe03522b
RS
3894#define ACE$M_WRITE 2
3895#define ACE$C_KEYID 1
86a5659e
JB
3896
3897static unsigned short memid, grpid;
3898static unsigned int uic;
3899
3900/* Called from init_sys_modes, so it happens not very often
3901 but at least each time Emacs is loaded. */
dfcf069d 3902void
86a5659e
JB
3903sys_access_reinit ()
3904{
3905 uic = 0;
3906}
3907
3908int
3909sys_access (filename, type)
3910 char * filename;
3911 int type;
3912{
3913 struct FAB fab;
3914 struct XABPRO xab;
3915 int status, size, i, typecode, acl_controlled;
3916 unsigned int *aclptr, *aclend, aclbuf[60];
3917 union prvdef prvmask;
3918
3919 /* Get UIC and GRP values for protection checking. */
3920 if (uic == 0)
3921 {
3922 status = LIB$GETJPI (&JPI$_UIC, 0, 0, &uic, 0, 0);
3923 if (! (status & 1))
3924 return -1;
3925 memid = uic & 0xFFFF;
3926 grpid = uic >> 16;
3927 }
3928
fe03522b 3929 if (type != 2) /* not checking write access */
86a5659e
JB
3930 return access (filename, type);
3931
3932 /* Check write protection. */
3933
fe03522b 3934#define CHECKPRIV(bit) (prvmask.bit)
ea5a0917 3935#define WRITABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
86a5659e
JB
3936
3937 /* Find privilege bits */
986ffb24 3938 status = SYS$SETPRV (0, 0, 0, prvmask);
86a5659e
JB
3939 if (! (status & 1))
3940 error ("Unable to find privileges: %s", vmserrstr (status));
3941 if (CHECKPRIV (PRV$V_BYPASS))
3942 return 0; /* BYPASS enabled */
3943 fab = cc$rms_fab;
3944 fab.fab$b_fac = FAB$M_GET;
3945 fab.fab$l_fna = filename;
3946 fab.fab$b_fns = strlen (filename);
3947 fab.fab$l_xab = &xab;
3948 xab = cc$rms_xabpro;
3949 xab.xab$l_aclbuf = aclbuf;
3950 xab.xab$w_aclsiz = sizeof (aclbuf);
986ffb24 3951 status = SYS$OPEN (&fab, 0, 0);
86a5659e
JB
3952 if (! (status & 1))
3953 return -1;
986ffb24 3954 SYS$CLOSE (&fab, 0, 0);
86a5659e 3955 /* Check system access */
ea5a0917 3956 if (CHECKPRIV (PRV$V_SYSPRV) && WRITABLE (XAB$V_SYS))
86a5659e
JB
3957 return 0;
3958 /* Check ACL entries, if any */
3959 acl_controlled = 0;
3960 if (xab.xab$w_acllen > 0)
3961 {
3962 aclptr = aclbuf;
3963 aclend = &aclbuf[xab.xab$w_acllen / 4];
3964 while (*aclptr && aclptr < aclend)
3965 {
3966 size = (*aclptr & 0xff) / 4;
3967 typecode = (*aclptr >> 8) & 0xff;
3968 if (typecode == ACE$C_KEYID)
3969 for (i = size - 1; i > 1; i--)
3970 if (aclptr[i] == uic)
3971 {
3972 acl_controlled = 1;
3973 if (aclptr[1] & ACE$M_WRITE)
3974 return 0; /* Write access through ACL */
3975 }
3976 aclptr = &aclptr[size];
3977 }
3978 if (acl_controlled) /* ACL specified, prohibits write access */
3979 return -1;
3980 }
3981 /* No ACL entries specified, check normal protection */
ea5a0917 3982 if (WRITABLE (XAB$V_WLD)) /* World writable */
86a5659e 3983 return 0;
ea5a0917 3984 if (WRITABLE (XAB$V_GRP) &&
86a5659e 3985 (unsigned short) (xab.xab$l_uic >> 16) == grpid)
ea5a0917
KH
3986 return 0; /* Group writable */
3987 if (WRITABLE (XAB$V_OWN) &&
86a5659e 3988 (xab.xab$l_uic & 0xFFFF) == memid)
ea5a0917 3989 return 0; /* Owner writable */
86a5659e 3990
ea5a0917 3991 return -1; /* Not writable */
86a5659e
JB
3992}
3993#endif /* not VMS4_4 */
3994#endif /* access */
3995
3996static char vtbuf[NAM$C_MAXRSS+1];
3997
3998/* translate a vms file spec to a unix path */
3999char *
4000sys_translate_vms (vfile)
4001 char * vfile;
4002{
4003 char * p;
4004 char * targ;
4005
4006 if (!vfile)
4007 return 0;
4008
4009 targ = vtbuf;
4010
4011 /* leading device or logical name is a root directory */
4012 if (p = strchr (vfile, ':'))
4013 {
4014 *targ++ = '/';
4015 while (vfile < p)
4016 *targ++ = *vfile++;
4017 vfile++;
4018 *targ++ = '/';
4019 }
4020 p = vfile;
4021 if (*p == '[' || *p == '<')
4022 {
4023 while (*++vfile != *p + 2)
4024 switch (*vfile)
4025 {
4026 case '.':
4027 if (vfile[-1] == *p)
4028 *targ++ = '.';
4029 *targ++ = '/';
4030 break;
4031
4032 case '-':
4033 *targ++ = '.';
4034 *targ++ = '.';
4035 break;
4036
4037 default:
4038 *targ++ = *vfile;
4039 break;
4040 }
4041 vfile++;
4042 *targ++ = '/';
4043 }
4044 while (*vfile)
4045 *targ++ = *vfile++;
4046
4047 return vtbuf;
4048}
4049
4050static char utbuf[NAM$C_MAXRSS+1];
4051
4052/* translate a unix path to a VMS file spec */
4053char *
4054sys_translate_unix (ufile)
4055 char * ufile;
4056{
4057 int slash_seen = 0;
4058 char *p;
4059 char * targ;
4060
4061 if (!ufile)
4062 return 0;
4063
4064 targ = utbuf;
4065
4066 if (*ufile == '/')
4067 {
4068 ufile++;
4069 }
4070
4071 while (*ufile)
4072 {
4073 switch (*ufile)
4074 {
4075 case '/':
4076 if (slash_seen)
4077 if (index (&ufile[1], '/'))
4078 *targ++ = '.';
4079 else
4080 *targ++ = ']';
4081 else
4082 {
4083 *targ++ = ':';
4084 if (index (&ufile[1], '/'))
4085 *targ++ = '[';
4086 slash_seen = 1;
4087 }
4088 break;
4089
4090 case '.':
4091 if (strncmp (ufile, "./", 2) == 0)
4092 {
4093 if (!slash_seen)
4094 {
4095 *targ++ = '[';
4096 slash_seen = 1;
4097 }
4098 ufile++; /* skip the dot */
4099 if (index (&ufile[1], '/'))
4100 *targ++ = '.';
4101 else
4102 *targ++ = ']';
4103 }
4104 else if (strncmp (ufile, "../", 3) == 0)
4105 {
4106 if (!slash_seen)
4107 {
4108 *targ++ = '[';
4109 slash_seen = 1;
4110 }
4111 *targ++ = '-';
4112 ufile += 2; /* skip the dots */
4113 if (index (&ufile[1], '/'))
4114 *targ++ = '.';
4115 else
4116 *targ++ = ']';
4117 }
4118 else
4119 *targ++ = *ufile;
4120 break;
4121
4122 default:
4123 *targ++ = *ufile;
4124 break;
4125 }
4126 ufile++;
4127 }
4128 *targ = '\0';
4129
4130 return utbuf;
4131}
4132
4133char *
4134getwd (pathname)
4135 char *pathname;
4136{
f4a7e5bd 4137 char *ptr, *val;
210b2b4f 4138 extern char *getcwd ();
86a5659e 4139
210b2b4f
JB
4140#define MAXPATHLEN 1024
4141
9ac0d9e0 4142 ptr = xmalloc (MAXPATHLEN);
f4a7e5bd
RS
4143 val = getcwd (ptr, MAXPATHLEN);
4144 if (val == 0)
4145 {
4146 xfree (ptr);
4147 return val;
4148 }
210b2b4f 4149 strcpy (pathname, ptr);
9ac0d9e0 4150 xfree (ptr);
210b2b4f
JB
4151
4152 return pathname;
86a5659e
JB
4153}
4154
dfcf069d 4155int
86a5659e
JB
4156getppid ()
4157{
4158 long item_code = JPI$_OWNER;
4159 unsigned long parent_id;
4160 int status;
4161
4162 if (((status = LIB$GETJPI (&item_code, 0, 0, &parent_id)) & 1) == 0)
4163 {
4164 errno = EVMSERR;
4165 vaxc$errno = status;
4166 return -1;
4167 }
4168 return parent_id;
4169}
4170
4171#undef getuid
4172unsigned
4173sys_getuid ()
4174{
4175 return (getgid () << 16) | getuid ();
4176}
4177
4178int
4179sys_read (fildes, buf, nbyte)
4180 int fildes;
4181 char *buf;
4182 unsigned int nbyte;
4183{
4184 return read (fildes, buf, (nbyte < MAXIOSIZE ? nbyte : MAXIOSIZE));
4185}
4186
4187#if 0
4188int
4189sys_write (fildes, buf, nbyte)
4190 int fildes;
4191 char *buf;
4192 unsigned int nbyte;
4193{
4194 register int nwrote, rtnval = 0;
4195
4196 while (nbyte > MAXIOSIZE && (nwrote = write (fildes, buf, MAXIOSIZE)) > 0) {
4197 nbyte -= nwrote;
4198 buf += nwrote;
4199 rtnval += nwrote;
4200 }
4201 if (nwrote < 0)
4202 return rtnval ? rtnval : -1;
4203 if ((nwrote = write (fildes, buf, nbyte)) < 0)
4204 return rtnval ? rtnval : -1;
4205 return (rtnval + nwrote);
4206}
4207#endif /* 0 */
4208
4209/*
4210 * VAX/VMS VAX C RTL really loses. It insists that records
4211 * end with a newline (carriage return) character, and if they
4212 * don't it adds one (nice of it isn't it!)
4213 *
4214 * Thus we do this stupidity below.
4215 */
4216
4217int
4218sys_write (fildes, buf, nbytes)
4219 int fildes;
4220 char *buf;
4221 unsigned int nbytes;
4222{
4223 register char *p;
4224 register char *e;
23b0668c
JB
4225 int sum = 0;
4226 struct stat st;
4227
4228 fstat (fildes, &st);
86a5659e 4229 p = buf;
86a5659e
JB
4230 while (nbytes > 0)
4231 {
23b0668c
JB
4232 int len, retval;
4233
4234 /* Handle fixed-length files with carriage control. */
4235 if (st.st_fab_rfm == FAB$C_FIX
4236 && ((st.st_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
4237 {
4238 len = st.st_fab_mrs;
4239 retval = write (fildes, p, min (len, nbytes));
4240 if (retval != len)
4241 return -1;
4242 retval++; /* This skips the implied carriage control */
4243 }
4244 else
4245 {
4246 e = p + min (MAXIOSIZE, nbytes) - 1;
4247 while (*e != '\n' && e > p) e--;
4248 if (p == e) /* Ok.. so here we add a newline... sigh. */
4249 e = p + min (MAXIOSIZE, nbytes) - 1;
4250 len = e + 1 - p;
4251 retval = write (fildes, p, len);
4252 if (retval != len)
4253 return -1;
4254 }
4255 p += retval;
4256 sum += retval;
86a5659e
JB
4257 nbytes -= retval;
4258 }
4259 return sum;
4260}
4261
4262/* Create file NEW copying its attributes from file OLD. If
4263 OLD is 0 or does not exist, create based on the value of
4264 vms_stmlf_recfm. */
4265
4266/* Protection value the file should ultimately have.
4267 Set by create_copy_attrs, and use by rename_sansversions. */
4268static unsigned short int fab_final_pro;
4269
4270int
4271creat_copy_attrs (old, new)
4272 char *old, *new;
4273{
4274 struct FAB fab = cc$rms_fab;
4275 struct XABPRO xabpro;
4276 char aclbuf[256]; /* Choice of size is arbitrary. See below. */
4277 extern int vms_stmlf_recfm;
4278
4279 if (old)
4280 {
4281 fab.fab$b_fac = FAB$M_GET;
4282 fab.fab$l_fna = old;
4283 fab.fab$b_fns = strlen (old);
4284 fab.fab$l_xab = (char *) &xabpro;
4285 xabpro = cc$rms_xabpro;
4286 xabpro.xab$l_aclbuf = aclbuf;
4287 xabpro.xab$w_aclsiz = sizeof aclbuf;
4288 /* Call $OPEN to fill in the fab & xabpro fields. */
986ffb24 4289 if (SYS$OPEN (&fab, 0, 0) & 1)
86a5659e 4290 {
986ffb24 4291 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
4292 fab.fab$l_alq = 0; /* zero the allocation quantity */
4293 if (xabpro.xab$w_acllen > 0)
4294 {
4295 if (xabpro.xab$w_acllen > sizeof aclbuf)
4296 /* If the acl buffer was too short, redo open with longer one.
4297 Wouldn't need to do this if there were some system imposed
4298 limit on the size of an ACL, but I can't find any such. */
4299 {
4300 xabpro.xab$l_aclbuf = (char *) alloca (xabpro.xab$w_acllen);
4301 xabpro.xab$w_aclsiz = xabpro.xab$w_acllen;
986ffb24
JB
4302 if (SYS$OPEN (&fab, 0, 0) & 1)
4303 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
4304 else
4305 old = 0;
4306 }
4307 }
4308 else
4309 xabpro.xab$l_aclbuf = 0;
4310 }
4311 else
4312 old = 0;
4313 }
4314 fab.fab$l_fna = new;
4315 fab.fab$b_fns = strlen (new);
4316 if (!old)
4317 {
4318 fab.fab$l_xab = 0;
4319 fab.fab$b_rfm = vms_stmlf_recfm ? FAB$C_STMLF : FAB$C_VAR;
4320 fab.fab$b_rat = FAB$M_CR;
4321 }
4322
4323 /* Set the file protections such that we will be able to manipulate
4324 this file. Once we are done writing and renaming it, we will set
4325 the protections back. */
4326 if (old)
4327 fab_final_pro = xabpro.xab$w_pro;
4328 else
986ffb24 4329 SYS$SETDFPROT (0, &fab_final_pro);
86a5659e
JB
4330 xabpro.xab$w_pro &= 0xff0f; /* set O:rewd for now. This is set back later. */
4331
4332 /* Create the new file with either default attrs or attrs copied
4333 from old file. */
4334 if (!(SYS$CREATE (&fab, 0, 0) & 1))
4335 return -1;
986ffb24 4336 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
4337 /* As this is a "replacement" for creat, return a file descriptor
4338 opened for writing. */
4339 return open (new, O_WRONLY);
4340}
4341
4342#ifdef creat
4343#undef creat
4344#include <varargs.h>
4345#ifdef __GNUC__
4346#ifndef va_count
4347#define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
4348#endif
4349#endif
4350
dfcf069d 4351int
86a5659e
JB
4352sys_creat (va_alist)
4353 va_dcl
4354{
eb8c3be9 4355 va_list list_incrementer;
86a5659e
JB
4356 char *name;
4357 int mode;
4358 int rfd; /* related file descriptor */
4359 int fd; /* Our new file descriptor */
4360 int count;
4361 struct stat st_buf;
4362 char rfm[12];
4363 char rat[15];
4364 char mrs[13];
4365 char fsz[13];
4366 extern int vms_stmlf_recfm;
4367
4368 va_count (count);
eb8c3be9
JB
4369 va_start (list_incrementer);
4370 name = va_arg (list_incrementer, char *);
4371 mode = va_arg (list_incrementer, int);
86a5659e 4372 if (count > 2)
eb8c3be9
JB
4373 rfd = va_arg (list_incrementer, int);
4374 va_end (list_incrementer);
86a5659e
JB
4375 if (count > 2)
4376 {
4377 /* Use information from the related file descriptor to set record
4378 format of the newly created file. */
4379 fstat (rfd, &st_buf);
4380 switch (st_buf.st_fab_rfm)
4381 {
4382 case FAB$C_FIX:
4383 strcpy (rfm, "rfm = fix");
4384 sprintf (mrs, "mrs = %d", st_buf.st_fab_mrs);
4385 strcpy (rat, "rat = ");
4386 if (st_buf.st_fab_rat & FAB$M_CR)
4387 strcat (rat, "cr");
4388 else if (st_buf.st_fab_rat & FAB$M_FTN)
4389 strcat (rat, "ftn");
4390 else if (st_buf.st_fab_rat & FAB$M_PRN)
4391 strcat (rat, "prn");
4392 if (st_buf.st_fab_rat & FAB$M_BLK)
4393 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4394 strcat (rat, ", blk");
4395 else
4396 strcat (rat, "blk");
4397 return creat (name, 0, rfm, rat, mrs);
4398
4399 case FAB$C_VFC:
4400 strcpy (rfm, "rfm = vfc");
4401 sprintf (fsz, "fsz = %d", st_buf.st_fab_fsz);
4402 strcpy (rat, "rat = ");
4403 if (st_buf.st_fab_rat & FAB$M_CR)
4404 strcat (rat, "cr");
4405 else if (st_buf.st_fab_rat & FAB$M_FTN)
4406 strcat (rat, "ftn");
4407 else if (st_buf.st_fab_rat & FAB$M_PRN)
4408 strcat (rat, "prn");
4409 if (st_buf.st_fab_rat & FAB$M_BLK)
4410 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4411 strcat (rat, ", blk");
4412 else
4413 strcat (rat, "blk");
4414 return creat (name, 0, rfm, rat, fsz);
4415
4416 case FAB$C_STM:
4417 strcpy (rfm, "rfm = stm");
4418 break;
4419
4420 case FAB$C_STMCR:
4421 strcpy (rfm, "rfm = stmcr");
4422 break;
4423
4424 case FAB$C_STMLF:
4425 strcpy (rfm, "rfm = stmlf");
4426 break;
4427
4428 case FAB$C_UDF:
4429 strcpy (rfm, "rfm = udf");
4430 break;
4431
4432 case FAB$C_VAR:
4433 strcpy (rfm, "rfm = var");
4434 break;
4435 }
4436 strcpy (rat, "rat = ");
4437 if (st_buf.st_fab_rat & FAB$M_CR)
4438 strcat (rat, "cr");
4439 else if (st_buf.st_fab_rat & FAB$M_FTN)
4440 strcat (rat, "ftn");
4441 else if (st_buf.st_fab_rat & FAB$M_PRN)
4442 strcat (rat, "prn");
4443 if (st_buf.st_fab_rat & FAB$M_BLK)
4444 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4445 strcat (rat, ", blk");
4446 else
4447 strcat (rat, "blk");
4448 }
4449 else
4450 {
4451 strcpy (rfm, vms_stmlf_recfm ? "rfm = stmlf" : "rfm=var");
4452 strcpy (rat, "rat=cr");
4453 }
4454 /* Until the VAX C RTL fixes the many bugs with modes, always use
4455 mode 0 to get the user's default protection. */
4456 fd = creat (name, 0, rfm, rat);
4457 if (fd < 0 && errno == EEXIST)
4458 {
4459 if (unlink (name) < 0)
4460 report_file_error ("delete", build_string (name));
4461 fd = creat (name, 0, rfm, rat);
4462 }
4463 return fd;
4464}
4465#endif /* creat */
4466
4467/* fwrite to stdout is S L O W. Speed it up by using fputc...*/
dfcf069d 4468int
86a5659e
JB
4469sys_fwrite (ptr, size, num, fp)
4470 register char * ptr;
4471 FILE * fp;
4472{
4473 register int tot = num * size;
4474
4475 while (tot--)
4476 fputc (*ptr++, fp);
dfcf069d 4477 return num;
86a5659e
JB
4478}
4479
4480/*
4481 * The VMS C library routine creat actually creates a new version of an
4482 * existing file rather than truncating the old version. There are times
4483 * when this is not the desired behavior, for instance, when writing an
4484 * auto save file (you only want one version), or when you don't have
4485 * write permission in the directory containing the file (but the file
4486 * itself is writable). Hence this routine, which is equivalent to
4487 * "close (creat (fn, 0));" on Unix if fn already exists.
4488 */
4489int
4490vms_truncate (fn)
4491 char *fn;
4492{
4493 struct FAB xfab = cc$rms_fab;
4494 struct RAB xrab = cc$rms_rab;
4495 int status;
4496
4497 xfab.fab$l_fop = FAB$M_TEF; /* free allocated but unused blocks on close */
4498 xfab.fab$b_fac = FAB$M_TRN | FAB$M_GET; /* allow truncate and get access */
4499 xfab.fab$b_shr = FAB$M_NIL; /* allow no sharing - file must be locked */
4500 xfab.fab$l_fna = fn;
4501 xfab.fab$b_fns = strlen (fn);
4502 xfab.fab$l_dna = ";0"; /* default to latest version of the file */
4503 xfab.fab$b_dns = 2;
4504 xrab.rab$l_fab = &xfab;
4505
4506 /* This gibberish opens the file, positions to the first record, and
4507 deletes all records from there until the end of file. */
986ffb24 4508 if ((SYS$OPEN (&xfab) & 01) == 01)
86a5659e 4509 {
986ffb24
JB
4510 if ((SYS$CONNECT (&xrab) & 01) == 01 &&
4511 (SYS$FIND (&xrab) & 01) == 01 &&
4512 (SYS$TRUNCATE (&xrab) & 01) == 01)
86a5659e
JB
4513 status = 0;
4514 else
4515 status = -1;
4516 }
4517 else
4518 status = -1;
986ffb24 4519 SYS$CLOSE (&xfab);
86a5659e
JB
4520 return status;
4521}
4522
4523/* Define this symbol to actually read SYSUAF.DAT. This requires either
4524 SYSPRV or a readable SYSUAF.DAT. */
4525
4526#ifdef READ_SYSUAF
4527/*
4528 * getuaf.c
4529 *
4530 * Routine to read the VMS User Authorization File and return
4531 * a specific user's record.
4532 */
4533
4534static struct UAF retuaf;
4535
4536struct UAF *
4537get_uaf_name (uname)
4538 char * uname;
4539{
4540 register status;
4541 struct FAB uaf_fab;
4542 struct RAB uaf_rab;
4543
4544 uaf_fab = cc$rms_fab;
4545 uaf_rab = cc$rms_rab;
4546 /* initialize fab fields */
4547 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4548 uaf_fab.fab$b_fns = 21;
4549 uaf_fab.fab$b_fac = FAB$M_GET;
4550 uaf_fab.fab$b_org = FAB$C_IDX;
4551 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4552 /* initialize rab fields */
4553 uaf_rab.rab$l_fab = &uaf_fab;
4554 /* open the User Authorization File */
986ffb24 4555 status = SYS$OPEN (&uaf_fab);
86a5659e
JB
4556 if (!(status&1))
4557 {
4558 errno = EVMSERR;
4559 vaxc$errno = status;
4560 return 0;
4561 }
986ffb24 4562 status = SYS$CONNECT (&uaf_rab);
86a5659e
JB
4563 if (!(status&1))
4564 {
4565 errno = EVMSERR;
4566 vaxc$errno = status;
4567 return 0;
4568 }
4569 /* read the requested record - index is in uname */
4570 uaf_rab.rab$l_kbf = uname;
4571 uaf_rab.rab$b_ksz = strlen (uname);
4572 uaf_rab.rab$b_rac = RAB$C_KEY;
4573 uaf_rab.rab$l_ubf = (char *)&retuaf;
4574 uaf_rab.rab$w_usz = sizeof retuaf;
986ffb24 4575 status = SYS$GET (&uaf_rab);
86a5659e
JB
4576 if (!(status&1))
4577 {
4578 errno = EVMSERR;
4579 vaxc$errno = status;
4580 return 0;
4581 }
4582 /* close the User Authorization File */
986ffb24 4583 status = SYS$DISCONNECT (&uaf_rab);
86a5659e
JB
4584 if (!(status&1))
4585 {
4586 errno = EVMSERR;
4587 vaxc$errno = status;
4588 return 0;
4589 }
986ffb24 4590 status = SYS$CLOSE (&uaf_fab);
86a5659e
JB
4591 if (!(status&1))
4592 {
4593 errno = EVMSERR;
4594 vaxc$errno = status;
4595 return 0;
4596 }
4597 return &retuaf;
4598}
4599
4600struct UAF *
4601get_uaf_uic (uic)
4602 unsigned long uic;
4603{
4604 register status;
4605 struct FAB uaf_fab;
4606 struct RAB uaf_rab;
4607
4608 uaf_fab = cc$rms_fab;
4609 uaf_rab = cc$rms_rab;
4610 /* initialize fab fields */
4611 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4612 uaf_fab.fab$b_fns = 21;
4613 uaf_fab.fab$b_fac = FAB$M_GET;
4614 uaf_fab.fab$b_org = FAB$C_IDX;
4615 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4616 /* initialize rab fields */
4617 uaf_rab.rab$l_fab = &uaf_fab;
4618 /* open the User Authorization File */
986ffb24 4619 status = SYS$OPEN (&uaf_fab);
86a5659e
JB
4620 if (!(status&1))
4621 {
4622 errno = EVMSERR;
4623 vaxc$errno = status;
4624 return 0;
4625 }
986ffb24 4626 status = SYS$CONNECT (&uaf_rab);
86a5659e
JB
4627 if (!(status&1))
4628 {
4629 errno = EVMSERR;
4630 vaxc$errno = status;
4631 return 0;
4632 }
4633 /* read the requested record - index is in uic */
4634 uaf_rab.rab$b_krf = 1; /* 1st alternate key */
4635 uaf_rab.rab$l_kbf = (char *) &uic;
4636 uaf_rab.rab$b_ksz = sizeof uic;
4637 uaf_rab.rab$b_rac = RAB$C_KEY;
4638 uaf_rab.rab$l_ubf = (char *)&retuaf;
4639 uaf_rab.rab$w_usz = sizeof retuaf;
986ffb24 4640 status = SYS$GET (&uaf_rab);
86a5659e
JB
4641 if (!(status&1))
4642 {
4643 errno = EVMSERR;
4644 vaxc$errno = status;
4645 return 0;
4646 }
4647 /* close the User Authorization File */
986ffb24 4648 status = SYS$DISCONNECT (&uaf_rab);
86a5659e
JB
4649 if (!(status&1))
4650 {
4651 errno = EVMSERR;
4652 vaxc$errno = status;
4653 return 0;
4654 }
986ffb24 4655 status = SYS$CLOSE (&uaf_fab);
86a5659e
JB
4656 if (!(status&1))
4657 {
4658 errno = EVMSERR;
4659 vaxc$errno = status;
4660 return 0;
4661 }
4662 return &retuaf;
4663}
4664
4665static struct passwd retpw;
4666
4667struct passwd *
4668cnv_uaf_pw (up)
4669 struct UAF * up;
4670{
4671 char * ptr;
4672
4673 /* copy these out first because if the username is 32 chars, the next
4674 section will overwrite the first byte of the UIC */
4675 retpw.pw_uid = up->uaf$w_mem;
4676 retpw.pw_gid = up->uaf$w_grp;
4677
ea5a0917 4678 /* I suppose this is not the best style, to possibly overwrite one
86a5659e
JB
4679 byte beyond the end of the field, but what the heck... */
4680 ptr = &up->uaf$t_username[UAF$S_USERNAME];
4681 while (ptr[-1] == ' ')
4682 ptr--;
4683 *ptr = '\0';
4684 strcpy (retpw.pw_name, up->uaf$t_username);
4685
4686 /* the rest of these are counted ascii strings */
4687 strncpy (retpw.pw_gecos, &up->uaf$t_owner[1], up->uaf$t_owner[0]);
4688 retpw.pw_gecos[up->uaf$t_owner[0]] = '\0';
4689 strncpy (retpw.pw_dir, &up->uaf$t_defdev[1], up->uaf$t_defdev[0]);
4690 retpw.pw_dir[up->uaf$t_defdev[0]] = '\0';
4691 strncat (retpw.pw_dir, &up->uaf$t_defdir[1], up->uaf$t_defdir[0]);
4692 retpw.pw_dir[up->uaf$t_defdev[0] + up->uaf$t_defdir[0]] = '\0';
4693 strncpy (retpw.pw_shell, &up->uaf$t_defcli[1], up->uaf$t_defcli[0]);
4694 retpw.pw_shell[up->uaf$t_defcli[0]] = '\0';
4695
4696 return &retpw;
4697}
4698#else /* not READ_SYSUAF */
4699static struct passwd retpw;
4700#endif /* not READ_SYSUAF */
4701
4702struct passwd *
4703getpwnam (name)
4704 char * name;
4705{
4706#ifdef READ_SYSUAF
4707 struct UAF *up;
4708#else
4709 char * user;
4710 char * dir;
4711 unsigned char * full;
4712#endif /* READ_SYSUAF */
4713 char *ptr = name;
4714
4715 while (*ptr)
4716 {
4717 if ('a' <= *ptr && *ptr <= 'z')
4718 *ptr -= 040;
4719 ptr++;
4720 }
4721#ifdef READ_SYSUAF
4722 if (!(up = get_uaf_name (name)))
4723 return 0;
4724 return cnv_uaf_pw (up);
4725#else
4726 if (strcmp (name, getenv ("USER")) == 0)
4727 {
4728 retpw.pw_uid = getuid ();
4729 retpw.pw_gid = getgid ();
4730 strcpy (retpw.pw_name, name);
4731 if (full = egetenv ("FULLNAME"))
4732 strcpy (retpw.pw_gecos, full);
4733 else
4734 *retpw.pw_gecos = '\0';
4735 strcpy (retpw.pw_dir, egetenv ("HOME"));
4736 *retpw.pw_shell = '\0';
4737 return &retpw;
4738 }
4739 else
4740 return 0;
4741#endif /* not READ_SYSUAF */
4742}
4743
4744struct passwd *
4745getpwuid (uid)
4746 unsigned long uid;
4747{
4748#ifdef READ_SYSUAF
4749 struct UAF * up;
4750
4751 if (!(up = get_uaf_uic (uid)))
4752 return 0;
4753 return cnv_uaf_pw (up);
4754#else
4755 if (uid == sys_getuid ())
4756 return getpwnam (egetenv ("USER"));
4757 else
4758 return 0;
4759#endif /* not READ_SYSUAF */
4760}
4761
4762/* return total address space available to the current process. This is
4763 the sum of the current p0 size, p1 size and free page table entries
4764 available. */
dfcf069d 4765int
86a5659e
JB
4766vlimit ()
4767{
4768 int item_code;
4769 unsigned long free_pages;
4770 unsigned long frep0va;
4771 unsigned long frep1va;
4772 register status;
4773
4774 item_code = JPI$_FREPTECNT;
4775 if (((status = LIB$GETJPI (&item_code, 0, 0, &free_pages)) & 1) == 0)
4776 {
4777 errno = EVMSERR;
4778 vaxc$errno = status;
4779 return -1;
4780 }
4781 free_pages *= 512;
4782
4783 item_code = JPI$_FREP0VA;
4784 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep0va)) & 1) == 0)
4785 {
4786 errno = EVMSERR;
4787 vaxc$errno = status;
4788 return -1;
4789 }
4790 item_code = JPI$_FREP1VA;
4791 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep1va)) & 1) == 0)
4792 {
4793 errno = EVMSERR;
4794 vaxc$errno = status;
4795 return -1;
4796 }
4797
4798 return free_pages + frep0va + (0x7fffffff - frep1va);
4799}
4800
dfcf069d 4801int
86a5659e
JB
4802define_logical_name (varname, string)
4803 char *varname;
4804 char *string;
4805{
4806 struct dsc$descriptor_s strdsc =
4807 {strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string};
4808 struct dsc$descriptor_s envdsc =
4809 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4810 struct dsc$descriptor_s lnmdsc =
4811 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4812
4813 return LIB$SET_LOGICAL (&envdsc, &strdsc, &lnmdsc, 0, 0);
4814}
4815
dfcf069d 4816int
86a5659e
JB
4817delete_logical_name (varname)
4818 char *varname;
4819{
4820 struct dsc$descriptor_s envdsc =
4821 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4822 struct dsc$descriptor_s lnmdsc =
4823 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4824
4825 return LIB$DELETE_LOGICAL (&envdsc, &lnmdsc);
4826}
4827
dfcf069d 4828int
86a5659e 4829ulimit ()
dfcf069d
AS
4830{
4831 return 0;
4832}
86a5659e 4833
dfcf069d 4834int
86a5659e 4835setpgrp ()
dfcf069d
AS
4836{
4837 return 0;
4838}
86a5659e 4839
dfcf069d 4840int
86a5659e
JB
4841execvp ()
4842{
4843 error ("execvp system call not implemented");
dfcf069d 4844 return -1;
86a5659e
JB
4845}
4846
4847int
4848rename (from, to)
4849 char *from, *to;
4850{
4851 int status;
4852 struct FAB from_fab = cc$rms_fab, to_fab = cc$rms_fab;
4853 struct NAM from_nam = cc$rms_nam, to_nam = cc$rms_nam;
4854 char from_esn[NAM$C_MAXRSS];
4855 char to_esn[NAM$C_MAXRSS];
4856
4857 from_fab.fab$l_fna = from;
4858 from_fab.fab$b_fns = strlen (from);
4859 from_fab.fab$l_nam = &from_nam;
4860 from_fab.fab$l_fop = FAB$M_NAM;
4861
4862 from_nam.nam$l_esa = from_esn;
4863 from_nam.nam$b_ess = sizeof from_esn;
4864
4865 to_fab.fab$l_fna = to;
4866 to_fab.fab$b_fns = strlen (to);
4867 to_fab.fab$l_nam = &to_nam;
4868 to_fab.fab$l_fop = FAB$M_NAM;
4869
4870 to_nam.nam$l_esa = to_esn;
4871 to_nam.nam$b_ess = sizeof to_esn;
4872
4873 status = SYS$RENAME (&from_fab, 0, 0, &to_fab);
4874
4875 if (status & 1)
4876 return 0;
4877 else
4878 {
4879 if (status == RMS$_DEV)
4880 errno = EXDEV;
4881 else
4882 errno = EVMSERR;
4883 vaxc$errno = status;
4884 return -1;
4885 }
4886}
4887
4888/* This function renames a file like `rename', but it strips
4889 the version number from the "to" filename, such that the "to" file is
4890 will always be a new version. It also sets the file protection once it is
4891 finished. The protection that we will use is stored in fab_final_pro,
4892 and was set when we did a creat_copy_attrs to create the file that we
4893 are renaming.
4894
4895 We could use the chmod function, but Eunichs uses 3 bits per user category
eb8c3be9 4896 to describe the protection, and VMS uses 4 (write and delete are separate
86a5659e
JB
4897 bits). To maintain portability, the VMS implementation of `chmod' wires
4898 the W and D bits together. */
4899
4900
4901static struct fibdef fib; /* We need this initialized to zero */
4902char vms_file_written[NAM$C_MAXRSS];
4903
4904int
4905rename_sans_version (from,to)
4906 char *from, *to;
4907{
4908 short int chan;
4909 int stat;
4910 short int iosb[4];
4911 int status;
4912 struct FAB to_fab = cc$rms_fab;
4913 struct NAM to_nam = cc$rms_nam;
4914 struct dsc$descriptor fib_d ={sizeof (fib),0,0,(char*) &fib};
4915 struct dsc$descriptor fib_attr[2]
4916 = {{sizeof (fab_final_pro),ATR$C_FPRO,0,(char*) &fab_final_pro},{0,0,0,0}};
4917 char to_esn[NAM$C_MAXRSS];
4918
4919 $DESCRIPTOR (disk,to_esn);
4920
4921 to_fab.fab$l_fna = to;
4922 to_fab.fab$b_fns = strlen (to);
4923 to_fab.fab$l_nam = &to_nam;
4924 to_fab.fab$l_fop = FAB$M_NAM;
4925
4926 to_nam.nam$l_esa = to_esn;
4927 to_nam.nam$b_ess = sizeof to_esn;
4928
4929 status = SYS$PARSE (&to_fab, 0, 0); /* figure out the full file name */
4930
4931 if (to_nam.nam$l_fnb && NAM$M_EXP_VER)
4932 *(to_nam.nam$l_ver) = '\0';
4933
4934 stat = rename (from, to_esn);
4935 if (stat < 0)
4936 return stat;
4937
4938 strcpy (vms_file_written, to_esn);
4939
4940 to_fab.fab$l_fna = vms_file_written; /* this points to the versionless name */
4941 to_fab.fab$b_fns = strlen (vms_file_written);
4942
4943 /* Now set the file protection to the correct value */
986ffb24 4944 SYS$OPEN (&to_fab, 0, 0); /* This fills in the nam$w_fid fields */
86a5659e
JB
4945
4946 /* Copy these fields into the fib */
4947 fib.fib$r_fid_overlay.fib$w_fid[0] = to_nam.nam$w_fid[0];
4948 fib.fib$r_fid_overlay.fib$w_fid[1] = to_nam.nam$w_fid[1];
4949 fib.fib$r_fid_overlay.fib$w_fid[2] = to_nam.nam$w_fid[2];
4950
986ffb24 4951 SYS$CLOSE (&to_fab, 0, 0);
86a5659e 4952
986ffb24 4953 stat = SYS$ASSIGN (&disk, &chan, 0, 0); /* open a channel to the disk */
86a5659e 4954 if (!stat)
986ffb24
JB
4955 LIB$SIGNAL (stat);
4956 stat = SYS$QIOW (0, chan, IO$_MODIFY, iosb, 0, 0, &fib_d,
86a5659e
JB
4957 0, 0, 0, &fib_attr, 0);
4958 if (!stat)
986ffb24
JB
4959 LIB$SIGNAL (stat);
4960 stat = SYS$DASSGN (chan);
86a5659e 4961 if (!stat)
986ffb24 4962 LIB$SIGNAL (stat);
0137dbf7 4963 strcpy (vms_file_written, to_esn); /* We will write this to the terminal*/
86a5659e
JB
4964 return 0;
4965}
4966
dfcf069d 4967int
86a5659e
JB
4968link (file, new)
4969 char * file, * new;
4970{
4971 register status;
4972 struct FAB fab;
4973 struct NAM nam;
4974 unsigned short fid[3];
4975 char esa[NAM$C_MAXRSS];
4976
4977 fab = cc$rms_fab;
4978 fab.fab$l_fop = FAB$M_OFP;
4979 fab.fab$l_fna = file;
4980 fab.fab$b_fns = strlen (file);
4981 fab.fab$l_nam = &nam;
4982
4983 nam = cc$rms_nam;
4984 nam.nam$l_esa = esa;
4985 nam.nam$b_ess = NAM$C_MAXRSS;
4986
4987 status = SYS$PARSE (&fab);
4988 if ((status & 1) == 0)
4989 {
4990 errno = EVMSERR;
4991 vaxc$errno = status;
4992 return -1;
4993 }
4994 status = SYS$SEARCH (&fab);
4995 if ((status & 1) == 0)
4996 {
4997 errno = EVMSERR;
4998 vaxc$errno = status;
4999 return -1;
5000 }
5001
5002 fid[0] = nam.nam$w_fid[0];
5003 fid[1] = nam.nam$w_fid[1];
5004 fid[2] = nam.nam$w_fid[2];
5005
5006 fab.fab$l_fna = new;
5007 fab.fab$b_fns = strlen (new);
5008
5009 status = SYS$PARSE (&fab);
5010 if ((status & 1) == 0)
5011 {
5012 errno = EVMSERR;
5013 vaxc$errno = status;
5014 return -1;
5015 }
5016
5017 nam.nam$w_fid[0] = fid[0];
5018 nam.nam$w_fid[1] = fid[1];
5019 nam.nam$w_fid[2] = fid[2];
5020
5021 nam.nam$l_esa = nam.nam$l_name;
5022 nam.nam$b_esl = nam.nam$b_name + nam.nam$b_type + nam.nam$b_ver;
5023
5024 status = SYS$ENTER (&fab);
5025 if ((status & 1) == 0)
5026 {
5027 errno = EVMSERR;
5028 vaxc$errno = status;
5029 return -1;
5030 }
5031
5032 return 0;
5033}
5034
dfcf069d 5035void
86a5659e
JB
5036croak (badfunc)
5037 char *badfunc;
5038{
5039 printf ("%s not yet implemented\r\n", badfunc);
5040 reset_sys_modes ();
5041 exit (1);
5042}
5043
5044long
5045random ()
5046{
5047 /* Arrange to return a range centered on zero. */
5048 return rand () - (1 << 30);
5049}
5050
dfcf069d 5051void
86a5659e
JB
5052srandom (seed)
5053{
5054 srand (seed);
5055}
5056#endif /* VMS */
5057\f
b97ab886 5058#ifdef AIXHFT
86a5659e
JB
5059
5060/* Called from init_sys_modes. */
dfcf069d 5061void
86a5659e
JB
5062hft_init ()
5063{
5064 int junk;
5065
5066 /* If we're not on an HFT we shouldn't do any of this. We determine
5067 if we are on an HFT by trying to get an HFT error code. If this
5068 call fails, we're not on an HFT. */
5069#ifdef IBMR2AIX
5070 if (ioctl (0, HFQERROR, &junk) < 0)
5071 return;
5072#else /* not IBMR2AIX */
5073 if (ioctl (0, HFQEIO, 0) < 0)
5074 return;
5075#endif /* not IBMR2AIX */
5076
5077 /* On AIX the default hft keyboard mapping uses backspace rather than delete
5078 as the rubout key's ASCII code. Here this is changed. The bug is that
5079 there's no way to determine the old mapping, so in reset_sys_modes
5080 we need to assume that the normal map had been present. Of course, this
5081 code also doesn't help if on a terminal emulator which doesn't understand
5082 HFT VTD's. */
5083 {
5084 struct hfbuf buf;
5085 struct hfkeymap keymap;
5086
5087 buf.hf_bufp = (char *)&keymap;
5088 buf.hf_buflen = sizeof (keymap);
5089 keymap.hf_nkeys = 2;
5090 keymap.hfkey[0].hf_kpos = 15;
5091 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
5092#ifdef IBMR2AIX
5093 keymap.hfkey[0].hf_keyidh = '<';
5094#else /* not IBMR2AIX */
5095 keymap.hfkey[0].hf_page = '<';
5096#endif /* not IBMR2AIX */
5097 keymap.hfkey[0].hf_char = 127;
5098 keymap.hfkey[1].hf_kpos = 15;
5099 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
5100#ifdef IBMR2AIX
5101 keymap.hfkey[1].hf_keyidh = '<';
5102#else /* not IBMR2AIX */
5103 keymap.hfkey[1].hf_page = '<';
5104#endif /* not IBMR2AIX */
5105 keymap.hfkey[1].hf_char = 127;
5106 hftctl (0, HFSKBD, &buf);
5107 }
5108 /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
5109 at times. */
5110 line_ins_del_ok = char_ins_del_ok = 0;
5111}
5112
5113/* Reset the rubout key to backspace. */
5114
dfcf069d 5115void
86a5659e
JB
5116hft_reset ()
5117{
5118 struct hfbuf buf;
5119 struct hfkeymap keymap;
5120 int junk;
5121
5122#ifdef IBMR2AIX
5123 if (ioctl (0, HFQERROR, &junk) < 0)
5124 return;
5125#else /* not IBMR2AIX */
5126 if (ioctl (0, HFQEIO, 0) < 0)
5127 return;
5128#endif /* not IBMR2AIX */
5129
5130 buf.hf_bufp = (char *)&keymap;
5131 buf.hf_buflen = sizeof (keymap);
5132 keymap.hf_nkeys = 2;
5133 keymap.hfkey[0].hf_kpos = 15;
5134 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
5135#ifdef IBMR2AIX
5136 keymap.hfkey[0].hf_keyidh = '<';
5137#else /* not IBMR2AIX */
5138 keymap.hfkey[0].hf_page = '<';
5139#endif /* not IBMR2AIX */
5140 keymap.hfkey[0].hf_char = 8;
5141 keymap.hfkey[1].hf_kpos = 15;
5142 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
5143#ifdef IBMR2AIX
5144 keymap.hfkey[1].hf_keyidh = '<';
5145#else /* not IBMR2AIX */
5146 keymap.hfkey[1].hf_page = '<';
5147#endif /* not IBMR2AIX */
5148 keymap.hfkey[1].hf_char = 8;
5149 hftctl (0, HFSKBD, &buf);
5150}
5151
b97ab886 5152#endif /* AIXHFT */
c238be24
RS
5153
5154#ifdef USE_DL_STUBS
5155
5156/* These are included on Sunos 4.1 when we do not use shared libraries.
5157 X11 libraries may refer to these functions but (we hope) do not
5158 actually call them. */
5159
5160void *
5161dlopen ()
5162{
5163 return 0;
5164}
5165
5166void *
5167dlsym ()
5168{
5169 return 0;
5170}
5171
5172int
5173dlclose ()
5174{
5175 return -1;
5176}
5177
5178#endif /* USE_DL_STUBS */
51417996
RS
5179\f
5180#ifndef BSTRING
5181
5182#ifndef bzero
5183
5184void
5185bzero (b, length)
5186 register char *b;
5187 register int length;
5188{
5189#ifdef VMS
5190 short zero = 0;
5191 long max_str = 65535;
5192
5193 while (length > max_str) {
5194 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
5195 length -= max_str;
5196 b += max_str;
5197 }
5198 max_str = length;
5199 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
5200#else
5201 while (length-- > 0)
5202 *b++ = 0;
5203#endif /* not VMS */
5204}
5205
5206#endif /* no bzero */
5207#endif /* BSTRING */
5208
c7f93f28 5209#if (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY)
51417996
RS
5210#undef bcopy
5211
5212/* Saying `void' requires a declaration, above, where bcopy is used
5213 and that declaration causes pain for systems where bcopy is a macro. */
5214bcopy (b1, b2, length)
5215 register char *b1;
5216 register char *b2;
5217 register int length;
5218{
5219#ifdef VMS
5220 long max_str = 65535;
5221
5222 while (length > max_str) {
5223 (void) LIB$MOVC3 (&max_str, b1, b2);
5224 length -= max_str;
5225 b1 += max_str;
5226 b2 += max_str;
5227 }
5228 max_str = length;
5229 (void) LIB$MOVC3 (&length, b1, b2);
5230#else
5231 while (length-- > 0)
5232 *b2++ = *b1++;
5233#endif /* not VMS */
5234}
dfcf069d 5235#endif /* (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY) */
51417996 5236
c7f93f28 5237#ifndef BSTRING
51417996
RS
5238#ifndef bcmp
5239int
5240bcmp (b1, b2, length) /* This could be a macro! */
5241 register char *b1;
5242 register char *b2;
5243 register int length;
5244{
5245#ifdef VMS
5246 struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
5247 struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
5248
5249 return STR$COMPARE (&src1, &src2);
5250#else
5251 while (length-- > 0)
5252 if (*b1++ != *b2++)
5253 return 1;
5254
5255 return 0;
5256#endif /* not VMS */
5257}
5258#endif /* no bcmp */
5259#endif /* not BSTRING */