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