Don't prototype srandom; it takes an unsigned argument
[bpt/emacs.git] / src / sysdep.c
CommitLineData
86a5659e 1/* Interfaces to system-dependent kernel and library entries.
5bdd7bdd
GM
2 Copyright (C) 1985, 86,87,88,93,94,95, 1999, 2000
3 Free Software Foundation, Inc.
86a5659e
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
7088d1ca 9the Free Software Foundation; either version 2, or (at your option)
86a5659e
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
86a5659e
JB
21
22
789cf808 23#include "config.h"
86a5659e
JB
24#include <signal.h>
25#include <setjmp.h>
d3eb3bfa
DL
26#ifdef HAVE_UNISTD_H
27#include <unistd.h>
28#endif
86a5659e 29#include "lisp.h"
8296bbf8
DL
30/* Including stdlib.h isn't necessarily enough to get srandom
31 declared, e.g. without __USE_XOPEN_EXTENDED with glibc 2. */
32#ifdef HAVE_RANDOM
33extern long int random P_ ((void));
ff4c29f4
GM
34#if 0 /* Don't prototype srandom; it takes an unsigned argument on
35 some systems, and an unsigned long on others, like FreeBSD
36 4.1. */
8296bbf8
DL
37extern void srandom P_ ((unsigned int));
38#endif
ff4c29f4 39#endif
8296bbf8 40
9ac0d9e0 41#include "blockinput.h"
86a5659e
JB
42#undef NULL
43
c4ea52a6 44#ifdef macintosh
1a578e9b
AC
45/* It is essential to include stdlib.h so that this file picks up
46 the correct definitions of rand, srand, and RAND_MAX.
47 Otherwise random numbers will not work correctly. */
48#include <stdlib.h>
49
c4ea52a6
RS
50#ifndef subprocesses
51/* Nonzero means delete a process right away if it exits (process.c). */
52static int delete_exited_processes;
53#endif
c4ea52a6
RS
54#endif /* macintosh */
55
86a5659e
JB
56#define min(x,y) ((x) > (y) ? (y) : (x))
57
fe03522b 58#ifdef WINDOWSNT
e15b6288
JR
59#define read sys_read
60#define write sys_write
fe03522b 61#include <windows.h>
e36ec798
AI
62#ifndef NULL
63#define NULL 0
64#endif
fe03522b
RS
65#endif /* not WINDOWSNT */
66
986ffb24
JB
67/* Does anyone other than VMS need this? */
68#ifndef fwrite
69#define sys_fwrite fwrite
70#else
71#undef fwrite
72#endif
73
86a5659e
JB
74#include <stdio.h>
75#include <sys/types.h>
76#include <sys/stat.h>
77#include <errno.h>
78
dca8521c
RS
79/* Get _POSIX_VDISABLE, if it is available. */
80#ifdef HAVE_UNISTD_H
81#include <unistd.h>
82#endif
83
d3eb3bfa
DL
84#ifdef HAVE_STDLIB_H
85#include <stdlib.h>
86#endif
87
f95c3f91 88#ifdef HAVE_SETPGID
2b7e8799 89#if !defined (USG) || defined (BSD_PGRPS)
c8875a65 90#undef setpgrp
f95c3f91
GM
91#define setpgrp setpgid
92#endif
2b7e8799 93#endif
f95c3f91 94
b05af5d3
PE
95/* Get SI_SRPC_DOMAIN, if it is available. */
96#ifdef HAVE_SYS_SYSTEMINFO_H
97#include <sys/systeminfo.h>
98#endif
99
207bdbdb
RS
100#ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
101#include <dos.h>
102#include "dosfns.h"
103#include "msdos.h"
104#include <sys/param.h>
15614e61
RS
105
106#if __DJGPP__ > 1
107extern int etext;
108extern unsigned start __asm__ ("start");
109#endif
207bdbdb
RS
110#endif
111
e36ec798 112#ifndef USE_CRT_DLL
9ee9af7a 113#ifndef errno
86a5659e 114extern int errno;
9ee9af7a 115#endif
e36ec798 116#endif
86a5659e 117
bb4bc8e2
RM
118#ifdef VMS
119#include <rms.h>
120#include <ttdef.h>
121#include <tt2def.h>
122#include <iodef.h>
123#include <ssdef.h>
124#include <descrip.h>
125#include <fibdef.h>
126#include <atrdef.h>
127#include <ctype.h>
128#include <string.h>
129#ifdef __GNUC__
130#include <sys/file.h>
131#else
132#include <file.h>
133#endif
134#undef F_SETFL
135#ifndef RAB$C_BID
136#include <rab.h>
137#endif
fe03522b 138#define MAXIOSIZE (32 * PAGESIZE) /* Don't I/O more than 32 blocks at a time */
bb4bc8e2
RM
139#endif /* VMS */
140
141#ifndef BSD4_1
2a633456 142#ifdef BSD_SYSTEM /* avoid writing defined (BSD_SYSTEM) || defined (USG)
bb4bc8e2
RM
143 because the vms compiler doesn't grok `defined' */
144#include <fcntl.h>
145#endif
146#ifdef USG
147#ifndef USG5
148#include <fcntl.h>
149#endif
150#endif
151#endif /* not 4.1 bsd */
86a5659e 152
207bdbdb 153#ifndef MSDOS
86a5659e 154#include <sys/ioctl.h>
207bdbdb 155#endif
2a633456 156
e04a4e0d 157#include "systty.h"
94c8642a 158#include "syswait.h"
86a5659e 159
86a5659e
JB
160#ifdef BROKEN_TIOCGWINSZ
161#undef TIOCGWINSZ
8dca3179 162#undef TIOCSWINSZ
86a5659e
JB
163#endif
164
c4ea52a6 165#if defined (USG) || defined (DGUX)
86a5659e 166#include <sys/utsname.h>
86a5659e
JB
167#ifndef MEMORY_IN_STRING_H
168#include <memory.h>
169#endif
81444907 170#if defined (TIOCGWINSZ) || defined (ISC4_0)
86a5659e
JB
171#ifdef NEED_SIOCTL
172#include <sys/sioctl.h>
173#endif
174#ifdef NEED_PTEM_H
175#include <sys/stream.h>
176#include <sys/ptem.h>
177#endif
81444907 178#endif /* TIOCGWINSZ or ISC4_0 */
b917b2e2 179#endif /* USG or DGUX */
86a5659e 180
86a5659e
JB
181extern int quit_char;
182
e36ec798 183#include "keyboard.h"
0137dbf7 184#include "frame.h"
86a5659e
JB
185#include "window.h"
186#include "termhooks.h"
187#include "termchar.h"
188#include "termopts.h"
189#include "dispextern.h"
190#include "process.h"
191
fe03522b
RS
192#ifdef WINDOWSNT
193#include <direct.h>
194/* In process.h which conflicts with the local copy. */
195#define _P_WAIT 0
196int _CRTAPI1 _spawnlp (int, const char *, const char *, ...);
197int _CRTAPI1 _getpid (void);
198#endif
199
86a5659e
JB
200#ifdef NONSYSTEM_DIR_LIBRARY
201#include "ndir.h"
202#endif /* NONSYSTEM_DIR_LIBRARY */
203
91bac16a
JB
204#include "syssignal.h"
205#include "systime.h"
d79998bc
KH
206#ifdef HAVE_UTIME_H
207#include <utime.h>
208#endif
209
210#ifndef HAVE_UTIMES
211#ifndef HAVE_STRUCT_UTIMBUF
212/* We want to use utime rather than utimes, but we couldn't find the
213 structure declaration. We'll use the traditional one. */
214struct utimbuf {
215 long actime;
216 long modtime;
217};
218#endif
219#endif
86a5659e 220
a00d5589
RS
221/* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
222#ifndef LPASS8
223#define LPASS8 0
224#endif
225
226#ifdef BSD4_1
227#define LNOFLSH 0100000
228#endif
229
86a5659e
JB
230static int baud_convert[] =
231#ifdef BAUD_CONVERT
232 BAUD_CONVERT;
233#else
234 {
235 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
236 1800, 2400, 4800, 9600, 19200, 38400
237 };
238#endif
239
f0d21750
GM
240#ifdef HAVE_SPEED_T
241#include <termios.h>
f0d21750 242#else
ba5971b3 243#if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
ba5971b3 244#else
515b04d0
RS
245#if defined (HAVE_TERMIOS_H) && defined (LINUX)
246#include <termios.h>
f2361c60 247#endif
ba5971b3 248#endif
f0d21750 249#endif
86a5659e 250
5bdd7bdd
GM
251int emacs_ospeed;
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 344 if (noninteractive)
5bdd7bdd 345 emacs_ospeed = 0;
86a5659e
JB
346 else
347 {
dfc366c9
RS
348#ifdef INIT_BAUD_RATE
349 INIT_BAUD_RATE ();
350#else
fe03522b 351#ifdef DOS_NT
5bdd7bdd 352 emacs_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 );
5bdd7bdd 359 emacs_ospeed = sg.xmit_baud;
91bac16a 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);
5bdd7bdd 366 emacs_ospeed = cfgetospeed (&sg);
5baff9ce 367#if defined (USE_GETOBAUD) && defined (getobaud)
2f43149b 368 /* m88k-motorola-sysv3 needs this (ghazi@noc.rutgers.edu) 9/1/94. */
5bdd7bdd
GM
369 if (emacs_ospeed == 0)
370 emacs_ospeed = getobaud (sg.c_cflag);
2f43149b 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
5bdd7bdd 382 emacs_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 ();
5bdd7bdd 389 emacs_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
5bdd7bdd
GM
397 baud_rate = (emacs_ospeed < sizeof baud_convert / sizeof baud_convert[0]
398 ? baud_convert[emacs_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
8581cd64
KH
2290#ifdef TRY_AGAIN
2291#ifndef HAVE_H_ERRNO
2292extern int h_errno;
2293#endif
2294#endif /* TRY_AGAIN */
2295
c0c86835
KH
2296void
2297init_system_name ()
86a5659e 2298{
86a5659e 2299#ifdef BSD4_1
c0c86835 2300 Vsystem_name = build_string (sysname);
67004ffb 2301#else
67004ffb 2302#ifdef VMS
c0c86835
KH
2303 char *sp, *end;
2304 if ((sp = egetenv ("SYS$NODE")) == 0)
2305 Vsystem_name = build_string ("vax-vms");
2306 else if ((end = index (sp, ':')) == 0)
2307 Vsystem_name = build_string (sp);
2308 else
2309 Vsystem_name = make_string (sp, end - sp);
67004ffb 2310#else
210b2b4f 2311#ifndef HAVE_GETHOSTNAME
c0c86835
KH
2312 struct utsname uts;
2313 uname (&uts);
2314 Vsystem_name = build_string (uts.nodename);
67004ffb 2315#else /* HAVE_GETHOSTNAME */
cc6e7269 2316 unsigned int hostname_size = 256;
c0c86835
KH
2317 char *hostname = (char *) alloca (hostname_size);
2318
2319 /* Try to get the host name; if the buffer is too short, try
2320 again. Apparently, the only indication gethostname gives of
2321 whether the buffer was large enough is the presence or absence
2322 of a '\0' in the string. Eech. */
2323 for (;;)
2324 {
2325 gethostname (hostname, hostname_size - 1);
2326 hostname[hostname_size - 1] = '\0';
2327
2328 /* Was the buffer large enough for the '\0'? */
2329 if (strlen (hostname) < hostname_size - 1)
2330 break;
2331
2332 hostname_size <<= 1;
2333 hostname = (char *) alloca (hostname_size);
2334 }
67004ffb 2335#ifdef HAVE_SOCKETS
c0c86835
KH
2336 /* Turn the hostname into the official, fully-qualified hostname.
2337 Don't do this if we're going to dump; this can confuse system
2338 libraries on some machines and make the dumped emacs core dump. */
67004ffb 2339#ifndef CANNOT_DUMP
c0c86835 2340 if (initialized)
67004ffb 2341#endif /* not CANNOT_DUMP */
960d894c
KH
2342 if (! index (hostname, '.'))
2343 {
2344 struct hostent *hp;
2345 int count;
2346 for (count = 0;; count++)
2347 {
e24f1d55 2348#ifdef TRY_AGAIN
960d894c 2349 h_errno = 0;
e24f1d55 2350#endif
960d894c 2351 hp = gethostbyname (hostname);
efa04277 2352#ifdef TRY_AGAIN
960d894c
KH
2353 if (! (hp == 0 && h_errno == TRY_AGAIN))
2354#endif
2355 break;
2356 if (count >= 5)
2357 break;
2358 Fsleep_for (make_number (1), Qnil);
2359 }
2360 if (hp)
2361 {
2362 char *fqdn = (char *) hp->h_name;
2363 char *p;
2364
2365 if (!index (fqdn, '.'))
2366 {
2367 /* We still don't have a fully qualified domain name.
2368 Try to find one in the list of alternate names */
2369 char **alias = hp->h_aliases;
2370 while (*alias && !index (*alias, '.'))
2371 alias++;
2372 if (*alias)
2373 fqdn = *alias;
2374 }
2375 hostname = fqdn;
ab1649fe 2376#if 0
960d894c
KH
2377 /* Convert the host name to lower case. */
2378 /* Using ctype.h here would introduce a possible locale
2379 dependence that is probably wrong for hostnames. */
2380 p = hostname;
2381 while (*p)
2382 {
2383 if (*p >= 'A' && *p <= 'Z')
2384 *p += 'a' - 'A';
2385 p++;
2386 }
2387#endif
2388 }
2389 }
67004ffb 2390#endif /* HAVE_SOCKETS */
6250a9db
KH
2391 /* We used to try using getdomainname here,
2392 but NIIBE Yutaka <gniibe@etl.go.jp> says that
0b93aa53
RS
2393 getdomainname gets the NIS/YP domain which often is not the same
2394 as in Internet domain name. */
6250a9db
KH
2395#if 0 /* Turned off because sysinfo is not really likely to return the
2396 correct Internet domain. */
0b93aa53 2397#if (HAVE_SYSINFO && defined (SI_SRPC_DOMAIN))
b05af5d3
PE
2398 if (! index (hostname, '.'))
2399 {
2400 /* The hostname is not fully qualified. Append the domain name. */
2401
2402 int hostlen = strlen (hostname);
2403 int domain_size = 256;
2404
2405 for (;;)
2406 {
825e7e55
RS
2407 char *domain = (char *) alloca (domain_size + 1);
2408 char *fqdn = (char *) alloca (hostlen + 1 + domain_size + 1);
b05af5d3
PE
2409 int sys_domain_size = sysinfo (SI_SRPC_DOMAIN, domain, domain_size);
2410 if (sys_domain_size <= 0)
2411 break;
2412 if (domain_size < sys_domain_size)
2413 {
2414 domain_size = sys_domain_size;
2415 continue;
2416 }
b05af5d3 2417 strcpy (fqdn, hostname);
825e7e55
RS
2418 if (domain[0] == '.')
2419 strcpy (fqdn + hostlen, domain);
9b80a5aa 2420 else if (domain[0] != 0)
825e7e55
RS
2421 {
2422 fqdn[hostlen] = '.';
2423 strcpy (fqdn + hostlen + 1, domain);
2424 }
b05af5d3
PE
2425 hostname = fqdn;
2426 break;
2427 }
2428 }
0b93aa53 2429#endif /* HAVE_SYSINFO && defined (SI_SRPC_DOMAIN) */
6250a9db 2430#endif /* 0 */
c0c86835 2431 Vsystem_name = build_string (hostname);
67004ffb 2432#endif /* HAVE_GETHOSTNAME */
210b2b4f 2433#endif /* VMS */
67004ffb 2434#endif /* BSD4_1 */
c0c86835
KH
2435 {
2436 unsigned char *p;
2437 for (p = XSTRING (Vsystem_name)->data; *p; p++)
2438 if (*p == ' ' || *p == '\t')
2439 *p = '-';
2440 }
67004ffb 2441}
86a5659e 2442\f
7964ba9e 2443#ifndef MSDOS
86a5659e 2444#ifndef VMS
86d1f23a 2445#if !defined (HAVE_SELECT) || defined (BROKEN_SELECT_NON_X)
86a5659e 2446
86d1f23a
KH
2447#include "sysselect.h"
2448#undef select
2449
2450#if defined (HAVE_X_WINDOWS) && !defined (HAVE_SELECT)
86a5659e
JB
2451/* Cause explanatory error message at compile time,
2452 since the select emulation is not good enough for X. */
2453int *x = &x_windows_lose_if_no_select_system_call;
2454#endif
2455
2456/* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
2457 * Only checks read descriptors.
2458 */
2459/* How long to wait between checking fds in select */
2460#define SELECT_PAUSE 1
2461int select_alarmed;
2462
2463/* For longjmp'ing back to read_input_waiting. */
2464
2465jmp_buf read_alarm_throw;
2466
2467/* Nonzero if the alarm signal should throw back to read_input_waiting.
2468 The read_socket_hook function sets this to 1 while it is waiting. */
2469
2470int read_alarm_should_throw;
2471
2472SIGTYPE
2473select_alarm ()
2474{
2475 select_alarmed = 1;
2476#ifdef BSD4_1
2477 sigrelse (SIGALRM);
2478#else /* not BSD4_1 */
2479 signal (SIGALRM, SIG_IGN);
2480#endif /* not BSD4_1 */
2481 if (read_alarm_should_throw)
2482 longjmp (read_alarm_throw, 1);
2483}
2484
fe03522b 2485#ifndef WINDOWSNT
86a5659e
JB
2486/* Only rfds are checked. */
2487int
86d1f23a 2488sys_select (nfds, rfds, wfds, efds, timeout)
86a5659e 2489 int nfds;
86d1f23a
KH
2490 SELECT_TYPE *rfds, *wfds, *efds;
2491 EMACS_TIME *timeout;
86a5659e 2492{
ffdcc91d 2493 int ravail = 0;
86d1f23a
KH
2494 SELECT_TYPE orfds;
2495 int timeoutval;
2496 int *local_timeout;
86a5659e
JB
2497 extern int proc_buffered_char[];
2498#ifndef subprocesses
2499 int process_tick = 0, update_tick = 0;
2500#else
2501 extern int process_tick, update_tick;
2502#endif
86a5659e
JB
2503 unsigned char buf;
2504
86d1f23a
KH
2505#if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
2506 /* If we're using X, then the native select will work; we only need the
2507 emulation for non-X usage. */
2508 if (!NILP (Vwindow_system))
2509 return select (nfds, rfds, wfds, efds, timeout);
2510#endif
2511 timeoutval = timeout ? EMACS_SECS (*timeout) : 100000;
2512 local_timeout = &timeoutval;
2513 FD_ZERO (&orfds);
86a5659e
JB
2514 if (rfds)
2515 {
2516 orfds = *rfds;
86d1f23a 2517 FD_ZERO (rfds);
86a5659e
JB
2518 }
2519 if (wfds)
86d1f23a 2520 FD_ZERO (wfds);
86a5659e 2521 if (efds)
86d1f23a 2522 FD_ZERO (efds);
86a5659e
JB
2523
2524 /* If we are looking only for the terminal, with no timeout,
2525 just read it and wait -- that's more efficient. */
86d1f23a
KH
2526 if (*local_timeout == 100000 && process_tick == update_tick
2527 && FD_ISSET (0, &orfds))
86a5659e 2528 {
86d1f23a
KH
2529 int fd;
2530 for (fd = 1; fd < nfds; ++fd)
2531 if (FD_ISSET (fd, &orfds))
2532 goto hardway;
86a5659e
JB
2533 if (! detect_input_pending ())
2534 read_input_waiting ();
86d1f23a 2535 FD_SET (0, rfds);
86a5659e
JB
2536 return 1;
2537 }
2538
86d1f23a 2539 hardway:
86a5659e
JB
2540 /* Once a second, till the timer expires, check all the flagged read
2541 * descriptors to see if any input is available. If there is some then
2542 * set the corresponding bit in the return copy of rfds.
2543 */
2544 while (1)
2545 {
86d1f23a 2546 register int to_check, fd;
86a5659e
JB
2547
2548 if (rfds)
2549 {
86d1f23a 2550 for (to_check = nfds, fd = 0; --to_check >= 0; fd++)
86a5659e 2551 {
86d1f23a 2552 if (FD_ISSET (fd, &orfds))
86a5659e
JB
2553 {
2554 int avail = 0, status = 0;
2555
86d1f23a 2556 if (fd == 0)
86a5659e
JB
2557 avail = detect_input_pending (); /* Special keyboard handler */
2558 else
2559 {
2560#ifdef FIONREAD
2561 status = ioctl (fd, FIONREAD, &avail);
2562#else /* no FIONREAD */
2563 /* Hoping it will return -1 if nothing available
2564 or 0 if all 0 chars requested are read. */
2565 if (proc_buffered_char[fd] >= 0)
2566 avail = 1;
2567 else
2568 {
2569 avail = read (fd, &buf, 1);
2570 if (avail > 0)
2571 proc_buffered_char[fd] = buf;
2572 }
2573#endif /* no FIONREAD */
2574 }
2575 if (status >= 0 && avail > 0)
2576 {
86d1f23a 2577 FD_SET (fd, rfds);
86a5659e
JB
2578 ravail++;
2579 }
2580 }
2581 }
2582 }
2583 if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
2584 break;
ffdcc91d
GM
2585
2586 turn_on_atimers (0);
2587 signal (SIGALRM, select_alarm);
86a5659e
JB
2588 select_alarmed = 0;
2589 alarm (SELECT_PAUSE);
ffdcc91d 2590
86a5659e
JB
2591 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2592 while (select_alarmed == 0 && *local_timeout != 0
2593 && process_tick == update_tick)
2594 {
2595 /* If we are interested in terminal input,
2596 wait by reading the terminal.
2597 That makes instant wakeup for terminal input at least. */
86d1f23a 2598 if (FD_ISSET (0, &orfds))
86a5659e
JB
2599 {
2600 read_input_waiting ();
2601 if (detect_input_pending ())
2602 select_alarmed = 1;
2603 }
2604 else
2605 pause ();
2606 }
2607 (*local_timeout) -= SELECT_PAUSE;
ffdcc91d
GM
2608
2609 /* Reset the old alarm if there was one. */
2610 turn_on_atimers (1);
2611
86a5659e
JB
2612 if (*local_timeout == 0) /* Stop on timer being cleared */
2613 break;
2614 }
2615 return ravail;
2616}
fe03522b 2617#endif /* not WINDOWSNT */
86a5659e
JB
2618
2619/* Read keyboard input into the standard buffer,
2620 waiting for at least one character. */
2621
23cafe43
GV
2622/* Make all keyboard buffers much bigger when using a window system. */
2623#ifdef HAVE_WINDOW_SYSTEM
86a5659e
JB
2624#define BUFFER_SIZE_FACTOR 16
2625#else
2626#define BUFFER_SIZE_FACTOR 1
2627#endif
2628
dfcf069d 2629void
86a5659e
JB
2630read_input_waiting ()
2631{
86a5659e 2632 struct input_event e;
34567704
JB
2633 int nread, i;
2634 extern int quit_char;
86a5659e
JB
2635
2636 if (read_socket_hook)
2637 {
f4a7e5bd
RS
2638 struct input_event buf[256];
2639
86a5659e
JB
2640 read_alarm_should_throw = 0;
2641 if (! setjmp (read_alarm_throw))
ce424b26 2642 nread = (*read_socket_hook) (0, buf, 256, 1);
86a5659e
JB
2643 else
2644 nread = -1;
f4a7e5bd
RS
2645
2646 /* Scan the chars for C-g and store them in kbd_buffer. */
2647 for (i = 0; i < nread; i++)
2648 {
2649 kbd_buffer_store_event (&buf[i]);
2650 /* Don't look at input that follows a C-g too closely.
2651 This reduces lossage due to autorepeat on C-g. */
2652 if (buf[i].kind == ascii_keystroke
30bef8ec 2653 && buf[i].code == quit_char)
f4a7e5bd
RS
2654 break;
2655 }
86a5659e
JB
2656 }
2657 else
86a5659e 2658 {
f4a7e5bd
RS
2659 char buf[3];
2660 nread = read (fileno (stdin), buf, 1);
2661
2662 /* Scan the chars for C-g and store them in kbd_buffer. */
2663 e.kind = ascii_keystroke;
2d064114 2664 e.frame_or_window = selected_frame;
f4a7e5bd
RS
2665 e.modifiers = 0;
2666 for (i = 0; i < nread; i++)
a00d5589 2667 {
f4a7e5bd
RS
2668 /* Convert chars > 0177 to meta events if desired.
2669 We do this under the same conditions that read_avail_input does. */
2670 if (read_socket_hook == 0)
2671 {
2672 /* If the user says she has a meta key, then believe her. */
2673 if (meta_key == 1 && (buf[i] & 0x80))
2674 e.modifiers = meta_modifier;
2675 if (meta_key != 2)
2676 buf[i] &= ~0x80;
2677 }
b95520f5 2678
c81d47b4 2679 XSETINT (e.code, buf[i]);
f4a7e5bd
RS
2680 kbd_buffer_store_event (&e);
2681 /* Don't look at input that follows a C-g too closely.
2682 This reduces lossage due to autorepeat on C-g. */
2683 if (buf[i] == quit_char)
2684 break;
2685 }
86a5659e
JB
2686 }
2687}
2688
2689#endif /* not HAVE_SELECT */
2690#endif /* not VMS */
7964ba9e 2691#endif /* not MSDOS */
86a5659e
JB
2692\f
2693#ifdef BSD4_1
08633194 2694void
23dab951
RS
2695init_sigio (fd)
2696 int fd;
86a5659e
JB
2697{
2698 if (noninteractive)
2699 return;
2700 lmode = LINTRUP | lmode;
23dab951 2701 ioctl (fd, TIOCLSET, &lmode);
86a5659e
JB
2702}
2703
dfcf069d 2704void
86a5659e
JB
2705reset_sigio ()
2706{
2707 if (noninteractive)
2708 return;
2709 lmode = ~LINTRUP & lmode;
2710 ioctl (0, TIOCLSET, &lmode);
2711}
2712
08633194 2713void
86a5659e
JB
2714request_sigio ()
2715{
2716 sigrelse (SIGTINT);
2717
2718 interrupts_deferred = 0;
2719}
2720
08633194 2721void
86a5659e
JB
2722unrequest_sigio ()
2723{
2724 sighold (SIGTINT);
2725
2726 interrupts_deferred = 1;
2727}
2728
2729/* still inside #ifdef BSD4_1 */
2730#ifdef subprocesses
2731
2732int sigheld; /* Mask of held signals */
2733
dfcf069d 2734void
86a5659e
JB
2735sigholdx (signum)
2736 int signum;
2737{
2738 sigheld |= sigbit (signum);
2739 sighold (signum);
2740}
2741
dfcf069d 2742void
86a5659e
JB
2743sigisheld (signum)
2744 int signum;
2745{
2746 sigheld |= sigbit (signum);
2747}
2748
dfcf069d 2749void
86a5659e
JB
2750sigunhold (signum)
2751 int signum;
2752{
2753 sigheld &= ~sigbit (signum);
2754 sigrelse (signum);
2755}
2756
dfcf069d 2757void
86a5659e
JB
2758sigfree () /* Free all held signals */
2759{
2760 int i;
2761 for (i = 0; i < NSIG; i++)
2762 if (sigheld & sigbit (i))
2763 sigrelse (i);
2764 sigheld = 0;
2765}
2766
dfcf069d 2767int
86a5659e
JB
2768sigbit (i)
2769{
2770 return 1 << (i - 1);
2771}
2772#endif /* subprocesses */
2773#endif /* BSD4_1 */
2774\f
2775/* POSIX signals support - DJB */
2776/* Anyone with POSIX signals should have ANSI C declarations */
2777
2778#ifdef POSIX_SIGNALS
2779
c639b0e3 2780sigset_t empty_mask, full_mask;
86a5659e 2781
86a5659e
JB
2782signal_handler_t
2783sys_signal (int signal_number, signal_handler_t action)
2784{
c639b0e3 2785 struct sigaction new_action, old_action;
86a5659e 2786 sigemptyset (&new_action.sa_mask);
e5c99298 2787 new_action.sa_handler = action;
25ab68af
RS
2788#ifdef SA_RESTART
2789 /* Emacs mostly works better with restartable system services. If this
2790 * flag exists, we probably want to turn it on here.
2791 */
2792 new_action.sa_flags = SA_RESTART;
2793#else
4a785b6e 2794 new_action.sa_flags = 0;
25ab68af 2795#endif
d32b2f3c 2796 sigaction (signal_number, &new_action, &old_action);
e5c99298 2797 return (old_action.sa_handler);
86a5659e
JB
2798}
2799
e065a56e
JB
2800#ifndef __GNUC__
2801/* If we're compiling with GCC, we don't need this function, since it
2802 can be written as a macro. */
2803sigset_t
2804sys_sigmask (int sig)
2805{
2806 sigset_t mask;
2807 sigemptyset (&mask);
2808 sigaddset (&mask, sig);
2809 return mask;
2810}
2811#endif
2812
86a5659e
JB
2813/* I'd like to have these guys return pointers to the mask storage in here,
2814 but there'd be trouble if the code was saving multiple masks. I'll be
2815 safe and pass the structure. It normally won't be more than 2 bytes
2816 anyhow. - DJB */
2817
2818sigset_t
2819sys_sigblock (sigset_t new_mask)
2820{
2821 sigset_t old_mask;
2822 sigprocmask (SIG_BLOCK, &new_mask, &old_mask);
2823 return (old_mask);
2824}
2825
2826sigset_t
2827sys_sigunblock (sigset_t new_mask)
2828{
2829 sigset_t old_mask;
2830 sigprocmask (SIG_UNBLOCK, &new_mask, &old_mask);
2831 return (old_mask);
2832}
2833
2834sigset_t
2835sys_sigsetmask (sigset_t new_mask)
2836{
2837 sigset_t old_mask;
2838 sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
2839 return (old_mask);
2840}
2841
2842#endif /* POSIX_SIGNALS */
2843\f
ca9c0567
PE
2844#if !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED
2845static char *my_sys_siglist[NSIG];
2846# ifdef sys_siglist
2847# undef sys_siglist
2848# endif
2849# define sys_siglist my_sys_siglist
2850#endif
2851
2852void
2853init_signals ()
2854{
2855#ifdef POSIX_SIGNALS
2856 sigemptyset (&empty_mask);
2857 sigfillset (&full_mask);
2858#endif
2859
2860#if !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED
2861 if (! initialized)
2862 {
2863# ifdef SIGABRT
2864 sys_siglist[SIGABRT] = "Aborted";
2865# endif
2866# ifdef SIGAIO
2867 sys_siglist[SIGAIO] = "LAN I/O interrupt";
2868# endif
2869# ifdef SIGALRM
2870 sys_siglist[SIGALRM] = "Alarm clock";
2871# endif
2872# ifdef SIGBUS
2873 sys_siglist[SIGBUS] = "Bus error";
2874# endif
2875# ifdef SIGCLD
2876 sys_siglist[SIGCLD] = "Child status changed";
2877# endif
2878# ifdef SIGCHLD
2879 sys_siglist[SIGCHLD] = "Child status changed";
2880# endif
2881# ifdef SIGCONT
2882 sys_siglist[SIGCONT] = "Continued";
2883# endif
2884# ifdef SIGDANGER
2885 sys_siglist[SIGDANGER] = "Swap space dangerously low";
2886# endif
2887# ifdef SIGDGNOTIFY
2888 sys_siglist[SIGDGNOTIFY] = "Notification message in queue";
2889# endif
2890# ifdef SIGEMT
2891 sys_siglist[SIGEMT] = "Emulation trap";
2892# endif
2893# ifdef SIGFPE
2894 sys_siglist[SIGFPE] = "Arithmetic exception";
2895# endif
2896# ifdef SIGFREEZE
2897 sys_siglist[SIGFREEZE] = "SIGFREEZE";
2898# endif
2899# ifdef SIGGRANT
2900 sys_siglist[SIGGRANT] = "Monitor mode granted";
2901# endif
2902# ifdef SIGHUP
2903 sys_siglist[SIGHUP] = "Hangup";
2904# endif
2905# ifdef SIGILL
2906 sys_siglist[SIGILL] = "Illegal instruction";
2907# endif
2908# ifdef SIGINT
2909 sys_siglist[SIGINT] = "Interrupt";
2910# endif
2911# ifdef SIGIO
2912 sys_siglist[SIGIO] = "I/O possible";
2913# endif
2914# ifdef SIGIOINT
2915 sys_siglist[SIGIOINT] = "I/O intervention required";
2916# endif
2917# ifdef SIGIOT
2918 sys_siglist[SIGIOT] = "IOT trap";
2919# endif
2920# ifdef SIGKILL
2921 sys_siglist[SIGKILL] = "Killed";
2922# endif
2923# ifdef SIGLOST
2924 sys_siglist[SIGLOST] = "Resource lost";
2925# endif
2926# ifdef SIGLWP
2927 sys_siglist[SIGLWP] = "SIGLWP";
2928# endif
2929# ifdef SIGMSG
2930 sys_siglist[SIGMSG] = "Monitor mode data available";
2931# endif
2932# ifdef SIGPHONE
2933 sys_siglist[SIGWIND] = "SIGPHONE";
2934# endif
2935# ifdef SIGPIPE
2936 sys_siglist[SIGPIPE] = "Broken pipe";
2937# endif
2938# ifdef SIGPOLL
2939 sys_siglist[SIGPOLL] = "Pollable event occurred";
2940# endif
2941# ifdef SIGPROF
2942 sys_siglist[SIGPROF] = "Profiling timer expired";
2943# endif
2944# ifdef SIGPTY
2945 sys_siglist[SIGPTY] = "PTY I/O interrupt";
2946# endif
2947# ifdef SIGPWR
2948 sys_siglist[SIGPWR] = "Power-fail restart";
2949# endif
2950# ifdef SIGQUIT
2951 sys_siglist[SIGQUIT] = "Quit";
2952# endif
2953# ifdef SIGRETRACT
2954 sys_siglist[SIGRETRACT] = "Need to relinguish monitor mode";
2955# endif
2956# ifdef SIGSAK
2957 sys_siglist[SIGSAK] = "Secure attention";
2958# endif
2959# ifdef SIGSEGV
2960 sys_siglist[SIGSEGV] = "Segmentation violation";
2961# endif
2962# ifdef SIGSOUND
2963 sys_siglist[SIGSOUND] = "Sound completed";
2964# endif
2965# ifdef SIGSTOP
2966 sys_siglist[SIGSTOP] = "Stopped (signal)";
2967# endif
2968# ifdef SIGSTP
2969 sys_siglist[SIGSTP] = "Stopped (user)";
2970# endif
2971# ifdef SIGSYS
2972 sys_siglist[SIGSYS] = "Bad argument to system call";
2973# endif
2974# ifdef SIGTERM
2975 sys_siglist[SIGTERM] = "Terminated";
2976# endif
2977# ifdef SIGTHAW
2978 sys_siglist[SIGTHAW] = "SIGTHAW";
2979# endif
2980# ifdef SIGTRAP
2981 sys_siglist[SIGTRAP] = "Trace/breakpoint trap";
2982# endif
2983# ifdef SIGTSTP
2984 sys_siglist[SIGTSTP] = "Stopped (user)";
2985# endif
2986# ifdef SIGTTIN
2987 sys_siglist[SIGTTIN] = "Stopped (tty input)";
2988# endif
2989# ifdef SIGTTOU
2990 sys_siglist[SIGTTOU] = "Stopped (tty output)";
2991# endif
2992# ifdef SIGURG
2993 sys_siglist[SIGURG] = "Urgent I/O condition";
2994# endif
2995# ifdef SIGUSR1
2996 sys_siglist[SIGUSR1] = "User defined signal 1";
2997# endif
2998# ifdef SIGUSR2
2999 sys_siglist[SIGUSR2] = "User defined signal 2";
3000# endif
3001# ifdef SIGVTALRM
3002 sys_siglist[SIGVTALRM] = "Virtual timer expired";
3003# endif
3004# ifdef SIGWAITING
3005 sys_siglist[SIGWAITING] = "Process's LWPs are blocked";
3006# endif
3007# ifdef SIGWINCH
3008 sys_siglist[SIGWINCH] = "Window size changed";
3009# endif
3010# ifdef SIGWIND
3011 sys_siglist[SIGWIND] = "SIGWIND";
3012# endif
3013# ifdef SIGXCPU
3014 sys_siglist[SIGXCPU] = "CPU time limit exceeded";
3015# endif
3016# ifdef SIGXFSZ
3017 sys_siglist[SIGXFSZ] = "File size limit exceeded";
3018# endif
3019 }
3020#endif /* !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED */
3021}
3022\f
9927a7b1 3023#ifndef HAVE_RANDOM
4bb8c8b7
KH
3024#ifdef random
3025#define HAVE_RANDOM
3026#endif
3027#endif
3028
3029/* Figure out how many bits the system's random number generator uses.
3030 `random' and `lrand48' are assumed to return 31 usable bits.
3031 BSD `rand' returns a 31 bit value but the low order bits are unusable;
3032 so we'll shift it and treat it like the 15-bit USG `rand'. */
3033
3034#ifndef RAND_BITS
3035# ifdef HAVE_RANDOM
3036# define RAND_BITS 31
3037# else /* !HAVE_RANDOM */
3038# ifdef HAVE_LRAND48
3039# define RAND_BITS 31
3040# define random lrand48
3041# else /* !HAVE_LRAND48 */
3042# define RAND_BITS 15
3043# if RAND_MAX == 32767
3044# define random rand
3045# else /* RAND_MAX != 32767 */
3046# if RAND_MAX == 2147483647
3047# define random() (rand () >> 16)
3048# else /* RAND_MAX != 2147483647 */
3049# ifdef USG
3050# define random rand
3051# else
3052# define random() (rand () >> 16)
2a633456 3053# endif /* !USG */
4bb8c8b7
KH
3054# endif /* RAND_MAX != 2147483647 */
3055# endif /* RAND_MAX != 32767 */
3056# endif /* !HAVE_LRAND48 */
3057# endif /* !HAVE_RANDOM */
3058#endif /* !RAND_BITS */
2e46c7c6 3059
4bb8c8b7
KH
3060void
3061seed_random (arg)
3062 long arg;
86a5659e 3063{
4bb8c8b7
KH
3064#ifdef HAVE_RANDOM
3065 srandom ((unsigned int)arg);
f8b53a82 3066#else
4bb8c8b7 3067# ifdef HAVE_LRAND48
76425a49 3068 srand48 (arg);
4bb8c8b7
KH
3069# else
3070 srand ((unsigned int)arg);
3071# endif
2e46c7c6 3072#endif
86a5659e
JB
3073}
3074
4bb8c8b7
KH
3075/*
3076 * Build a full Emacs-sized word out of whatever we've got.
3077 * This suffices even for a 64-bit architecture with a 15-bit rand.
3078 */
3079long
3080get_random ()
3081{
3082 long val = random ();
3083#if VALBITS > RAND_BITS
3084 val = (val << RAND_BITS) ^ random ();
3085#if VALBITS > 2*RAND_BITS
3086 val = (val << RAND_BITS) ^ random ();
3087#if VALBITS > 3*RAND_BITS
3088 val = (val << RAND_BITS) ^ random ();
3089#if VALBITS > 4*RAND_BITS
3090 val = (val << RAND_BITS) ^ random ();
3091#endif /* need at least 5 */
3092#endif /* need at least 4 */
3093#endif /* need at least 3 */
3094#endif /* need at least 2 */
3095 return val & ((1L << VALBITS) - 1);
3096}
86a5659e
JB
3097\f
3098#ifdef WRONG_NAME_INSQUE
3099
3100insque (q,p)
3101 caddr_t q,p;
3102{
3103 _insque (q,p);
3104}
3105
3106#endif
3107\f
3108#ifdef VMS
3109
3110#ifdef getenv
3111/* If any place else asks for the TERM variable,
3112 allow it to be overridden with the EMACS_TERM variable
3113 before attempting to translate the logical name TERM. As a last
3114 resort, ask for VAX C's special idea of the TERM variable. */
3115#undef getenv
3116char *
3117sys_getenv (name)
3118 char *name;
3119{
3120 register char *val;
3121 static char buf[256];
3122 static struct dsc$descriptor_s equiv
3123 = {sizeof (buf), DSC$K_DTYPE_T, DSC$K_CLASS_S, buf};
3124 static struct dsc$descriptor_s d_name
3125 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
3126 short eqlen;
3127
3128 if (!strcmp (name, "TERM"))
3129 {
3130 val = (char *) getenv ("EMACS_TERM");
3131 if (val)
3132 return val;
3133 }
3134
3135 d_name.dsc$w_length = strlen (name);
3136 d_name.dsc$a_pointer = name;
986ffb24 3137 if (LIB$SYS_TRNLOG (&d_name, &eqlen, &equiv) == 1)
86a5659e
JB
3138 {
3139 char *str = (char *) xmalloc (eqlen + 1);
3140 bcopy (buf, str, eqlen);
3141 str[eqlen] = '\0';
3142 /* This is a storage leak, but a pain to fix. With luck,
3143 no one will ever notice. */
3144 return str;
3145 }
3146 return (char *) getenv (name);
3147}
3148#endif /* getenv */
3149
3150#ifdef abort
3151/* Since VMS doesn't believe in core dumps, the only way to debug this beast is
3152 to force a call on the debugger from within the image. */
3153#undef abort
3154sys_abort ()
3155{
3156 reset_sys_modes ();
3157 LIB$SIGNAL (SS$_DEBUG);
3158}
3159#endif /* abort */
3160#endif /* VMS */
3161\f
3162#ifdef VMS
3163#ifdef LINK_CRTL_SHARE
ea5a0917 3164#ifdef SHARABLE_LIB_BUG
eb8c3be9 3165/* Variables declared noshare and initialized in sharable libraries
86a5659e
JB
3166 cannot be shared. The VMS linker incorrectly forces you to use a private
3167 version which is uninitialized... If not for this "feature", we
3168 could use the C library definition of sys_nerr and sys_errlist. */
3169int sys_nerr = 35;
3170char *sys_errlist[] =
3171 {
3172 "error 0",
3173 "not owner",
3174 "no such file or directory",
3175 "no such process",
3176 "interrupted system call",
3177 "i/o error",
3178 "no such device or address",
3179 "argument list too long",
3180 "exec format error",
3181 "bad file number",
3182 "no child process",
3183 "no more processes",
3184 "not enough memory",
3185 "permission denied",
3186 "bad address",
3187 "block device required",
3188 "mount devices busy",
3189 "file exists",
3190 "cross-device link",
3191 "no such device",
3192 "not a directory",
3193 "is a directory",
3194 "invalid argument",
3195 "file table overflow",
3196 "too many open files",
3197 "not a typewriter",
3198 "text file busy",
3199 "file too big",
3200 "no space left on device",
3201 "illegal seek",
3202 "read-only file system",
3203 "too many links",
3204 "broken pipe",
3205 "math argument",
3206 "result too large",
3207 "I/O stream empty",
3208 "vax/vms specific error code nontranslatable error"
3209 };
ea5a0917 3210#endif /* SHARABLE_LIB_BUG */
86a5659e
JB
3211#endif /* LINK_CRTL_SHARE */
3212#endif /* VMS */
7088d1ca
RM
3213
3214#ifndef HAVE_STRERROR
fe03522b 3215#ifndef WINDOWSNT
7088d1ca
RM
3216char *
3217strerror (errnum)
3218 int errnum;
3219{
3220 extern char *sys_errlist[];
3221 extern int sys_nerr;
3222
3223 if (errnum >= 0 && errnum < sys_nerr)
3224 return sys_errlist[errnum];
3225 return (char *) "Unknown error";
3226}
fe03522b 3227#endif /* not WINDOWSNT */
7088d1ca 3228#endif /* ! HAVE_STRERROR */
86a5659e 3229\f
86a5659e 3230int
68c45bf0 3231emacs_open (path, oflag, mode)
86a5659e
JB
3232 char *path;
3233 int oflag, mode;
3234{
3235 register int rtnval;
68c45bf0
PE
3236
3237#ifdef BSD4_1
3238 if (oflag & O_CREAT)
3239 return creat (path, mode);
3240#endif
86a5659e
JB
3241
3242 while ((rtnval = open (path, oflag, mode)) == -1
3243 && (errno == EINTR));
3244 return (rtnval);
3245}
3246
dfcf069d 3247int
68c45bf0 3248emacs_close (fd)
86a5659e
JB
3249 int fd;
3250{
fe111daf 3251 int did_retry = 0;
86a5659e
JB
3252 register int rtnval;
3253
3254 while ((rtnval = close (fd)) == -1
fe111daf
KH
3255 && (errno == EINTR))
3256 did_retry = 1;
3257
3258 /* If close is interrupted SunOS 4.1 may or may not have closed the
3259 file descriptor. If it did the second close will fail with
3260 errno = EBADF. That means we have succeeded. */
3261 if (rtnval == -1 && did_retry && errno == EBADF)
3262 return 0;
3263
86a5659e
JB
3264 return rtnval;
3265}
3266
86a5659e 3267int
68c45bf0 3268emacs_read (fildes, buf, nbyte)
86a5659e
JB
3269 int fildes;
3270 char *buf;
3271 unsigned int nbyte;
3272{
3273 register int rtnval;
3274
3275 while ((rtnval = read (fildes, buf, nbyte)) == -1
3276 && (errno == EINTR));
3277 return (rtnval);
3278}
3279
3280int
68c45bf0 3281emacs_write (fildes, buf, nbyte)
86a5659e
JB
3282 int fildes;
3283 char *buf;
3284 unsigned int nbyte;
3285{
b95520f5 3286 register int rtnval, bytes_written;
86a5659e 3287
b95520f5
BF
3288 bytes_written = 0;
3289
3290 while (nbyte > 0)
3291 {
3292 rtnval = write (fildes, buf, nbyte);
3293
3294 if (rtnval == -1)
3295 {
3296 if (errno == EINTR)
3297 continue;
3298 else
aa670904 3299 return (bytes_written ? bytes_written : -1);
b95520f5
BF
3300 }
3301
3302 buf += rtnval;
3303 nbyte -= rtnval;
3304 bytes_written += rtnval;
3305 }
3306 return (bytes_written);
86a5659e 3307}
86a5659e
JB
3308\f
3309#ifdef USG
3310/*
3311 * All of the following are for USG.
3312 *
3313 * On USG systems the system calls are INTERRUPTIBLE by signals
3314 * that the user program has elected to catch. Thus the system call
3315 * must be retried in these cases. To handle this without massive
3316 * changes in the source code, we remap the standard system call names
3317 * to names for our own functions in sysdep.c that do the system call
3318 * with retries. Actually, for portability reasons, it is good
3319 * programming practice, as this example shows, to limit all actual
eb8c3be9 3320 * system calls to a single occurrence in the source. Sure, this
86a5659e
JB
3321 * adds an extra level of function call overhead but it is almost
3322 * always negligible. Fred Fish, Unisoft Systems Inc.
3323 */
3324
86a5659e
JB
3325/*
3326 * Warning, this function may not duplicate 4.2 action properly
3327 * under error conditions.
3328 */
3329
3330#ifndef MAXPATHLEN
3331/* In 4.1, param.h fails to define this. */
3332#define MAXPATHLEN 1024
3333#endif
3334
3335#ifndef HAVE_GETWD
3336
3337char *
3338getwd (pathname)
3339 char *pathname;
3340{
3341 char *npath, *spath;
3342 extern char *getcwd ();
3343
9ac0d9e0 3344 BLOCK_INPUT; /* getcwd uses malloc */
86a5659e 3345 spath = npath = getcwd ((char *) 0, MAXPATHLEN);
f4a7e5bd
RS
3346 if (spath == 0)
3347 return spath;
86a5659e
JB
3348 /* On Altos 3068, getcwd can return @hostname/dir, so discard
3349 up to first slash. Should be harmless on other systems. */
3350 while (*npath && *npath != '/')
3351 npath++;
3352 strcpy (pathname, npath);
3353 free (spath); /* getcwd uses malloc */
9ac0d9e0 3354 UNBLOCK_INPUT;
86a5659e
JB
3355 return pathname;
3356}
3357
3358#endif /* HAVE_GETWD */
3359
3360/*
3361 * Emulate rename using unlink/link. Note that this is
3362 * only partially correct. Also, doesn't enforce restriction
3363 * that files be of same type (regular->regular, dir->dir, etc).
3364 */
3365
4746118a
JB
3366#ifndef HAVE_RENAME
3367
86a5659e 3368rename (from, to)
19c7afdf
JB
3369 const char *from;
3370 const char *to;
86a5659e
JB
3371{
3372 if (access (from, 0) == 0)
3373 {
3374 unlink (to);
3375 if (link (from, to) == 0)
3376 if (unlink (from) == 0)
3377 return (0);
3378 }
3379 return (-1);
3380}
3381
4746118a
JB
3382#endif
3383
86a5659e
JB
3384
3385#ifdef HPUX
3386#ifndef HAVE_PERROR
3387
3388/* HPUX curses library references perror, but as far as we know
3389 it won't be called. Anyway this definition will do for now. */
3390
3391perror ()
3392{
3393}
3394
3395#endif /* not HAVE_PERROR */
3396#endif /* HPUX */
3397
3398#ifndef HAVE_DUP2
3399
3400/*
3401 * Emulate BSD dup2. First close newd if it already exists.
3402 * Then, attempt to dup oldd. If not successful, call dup2 recursively
3403 * until we are, then close the unsuccessful ones.
3404 */
3405
3406dup2 (oldd, newd)
3407 int oldd;
3408 int newd;
3409{
3410 register int fd, ret;
3411
68c45bf0 3412 emacs_close (newd);
86a5659e
JB
3413
3414#ifdef F_DUPFD
68c45bf0 3415 return fcntl (oldd, F_DUPFD, newd);
86a5659e
JB
3416#else
3417 fd = dup (old);
3418 if (fd == -1)
3419 return -1;
3420 if (fd == new)
3421 return new;
3422 ret = dup2 (old,new);
68c45bf0 3423 emacs_close (fd);
86a5659e
JB
3424 return ret;
3425#endif
3426}
3427
3428#endif /* not HAVE_DUP2 */
3429
3430/*
3431 * Gettimeofday. Simulate as much as possible. Only accurate
3432 * to nearest second. Emacs doesn't use tzp so ignore it for now.
3433 * Only needed when subprocesses are defined.
3434 */
3435
3436#ifdef subprocesses
3437#ifndef VMS
3438#ifndef HAVE_GETTIMEOFDAY
3439#ifdef HAVE_TIMEVAL
3440
3441/* ARGSUSED */
dfcf069d 3442int
86a5659e
JB
3443gettimeofday (tp, tzp)
3444 struct timeval *tp;
3445 struct timezone *tzp;
3446{
3447 extern long time ();
3448
3449 tp->tv_sec = time ((long *)0);
3450 tp->tv_usec = 0;
4ca7594f
RS
3451 if (tzp != 0)
3452 tzp->tz_minuteswest = -1;
dfcf069d 3453 return 0;
86a5659e
JB
3454}
3455
3456#endif
3457#endif
3458#endif
3459#endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
3460
3461/*
3462 * This function will go away as soon as all the stubs fixed. (fnf)
3463 */
3464
dfcf069d 3465void
86a5659e
JB
3466croak (badfunc)
3467 char *badfunc;
3468{
3469 printf ("%s not yet implemented\r\n", badfunc);
3470 reset_sys_modes ();
3471 exit (1);
3472}
3473
3474#endif /* USG */
3475\f
86a5659e
JB
3476/* Directory routines for systems that don't have them. */
3477
3478#ifdef SYSV_SYSTEM_DIR
3479
3480#include <dirent.h>
3481
c4ea52a6 3482#if defined (BROKEN_CLOSEDIR) || !defined (HAVE_CLOSEDIR)
cfdc57af 3483
86a5659e
JB
3484int
3485closedir (dirp)
3486 register DIR *dirp; /* stream from opendir */
3487{
cfdc57af
RS
3488 int rtnval;
3489
68c45bf0 3490 rtnval = emacs_close (dirp->dd_fd);
1b929d25 3491
65aa44ac
JB
3492 /* Some systems (like Solaris) allocate the buffer and the DIR all
3493 in one block. Why in the world are we freeing this ourselves
3494 anyway? */
3495#if ! (defined (sun) && defined (USG5_4))
3496 xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
3497#endif
9ac0d9e0 3498 xfree ((char *) dirp);
cfdc57af
RS
3499
3500 return rtnval;
86a5659e 3501}
1db6401c 3502#endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
86a5659e
JB
3503#endif /* SYSV_SYSTEM_DIR */
3504
3505#ifdef NONSYSTEM_DIR_LIBRARY
3506
3507DIR *
3508opendir (filename)
3509 char *filename; /* name of directory */
3510{
3511 register DIR *dirp; /* -> malloc'ed storage */
3512 register int fd; /* file descriptor for read */
3513 struct stat sbuf; /* result of fstat */
3514
68c45bf0 3515 fd = emacs_open (filename, O_RDONLY, 0);
86a5659e
JB
3516 if (fd < 0)
3517 return 0;
3518
9ac0d9e0 3519 BLOCK_INPUT;
86a5659e
JB
3520 if (fstat (fd, &sbuf) < 0
3521 || (sbuf.st_mode & S_IFMT) != S_IFDIR
7f77dbe3 3522 || (dirp = (DIR *) xmalloc (sizeof (DIR))) == 0)
86a5659e 3523 {
68c45bf0 3524 emacs_close (fd);
9ac0d9e0 3525 UNBLOCK_INPUT;
86a5659e
JB
3526 return 0; /* bad luck today */
3527 }
9ac0d9e0 3528 UNBLOCK_INPUT;
86a5659e
JB
3529
3530 dirp->dd_fd = fd;
3531 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
3532
3533 return dirp;
3534}
3535
3536void
3537closedir (dirp)
3538 register DIR *dirp; /* stream from opendir */
3539{
68c45bf0 3540 emacs_close (dirp->dd_fd);
9ac0d9e0 3541 xfree ((char *) dirp);
86a5659e
JB
3542}
3543
3544
3545#ifndef VMS
3546#define DIRSIZ 14
3547struct olddir
3548 {
3549 ino_t od_ino; /* inode */
3550 char od_name[DIRSIZ]; /* filename */
3551 };
3552#endif /* not VMS */
3553
3554struct direct dir_static; /* simulated directory contents */
3555
3556/* ARGUSED */
3557struct direct *
3558readdir (dirp)
3559 register DIR *dirp; /* stream from opendir */
3560{
3561#ifndef VMS
3562 register struct olddir *dp; /* -> directory data */
3563#else /* VMS */
3564 register struct dir$_name *dp; /* -> directory data */
3565 register struct dir$_version *dv; /* -> version data */
3566#endif /* VMS */
3567
3568 for (; ;)
3569 {
3570 if (dirp->dd_loc >= dirp->dd_size)
3571 dirp->dd_loc = dirp->dd_size = 0;
3572
3573 if (dirp->dd_size == 0 /* refill buffer */
68c45bf0 3574 && (dirp->dd_size = emacs_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
86a5659e
JB
3575 return 0;
3576
3577#ifndef VMS
3578 dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc];
3579 dirp->dd_loc += sizeof (struct olddir);
3580
3581 if (dp->od_ino != 0) /* not deleted entry */
3582 {
3583 dir_static.d_ino = dp->od_ino;
3584 strncpy (dir_static.d_name, dp->od_name, DIRSIZ);
3585 dir_static.d_name[DIRSIZ] = '\0';
3586 dir_static.d_namlen = strlen (dir_static.d_name);
3587 dir_static.d_reclen = sizeof (struct direct)
3588 - MAXNAMLEN + 3
3589 + dir_static.d_namlen - dir_static.d_namlen % 4;
3590 return &dir_static; /* -> simulated structure */
3591 }
3592#else /* VMS */
3593 dp = (struct dir$_name *) dirp->dd_buf;
3594 if (dirp->dd_loc == 0)
3595 dirp->dd_loc = (dp->dir$b_namecount&1) ? dp->dir$b_namecount + 1
3596 : dp->dir$b_namecount;
3597 dv = (struct dir$_version *)&dp->dir$t_name[dirp->dd_loc];
3598 dir_static.d_ino = dv->dir$w_fid_num;
3599 dir_static.d_namlen = dp->dir$b_namecount;
3600 dir_static.d_reclen = sizeof (struct direct)
3601 - MAXNAMLEN + 3
3602 + dir_static.d_namlen - dir_static.d_namlen % 4;
3603 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3604 dir_static.d_name[dir_static.d_namlen] = '\0';
3605 dirp->dd_loc = dirp->dd_size; /* only one record at a time */
3606 return &dir_static;
3607#endif /* VMS */
3608 }
3609}
3610
3611#ifdef VMS
3612/* readdirver is just like readdir except it returns all versions of a file
3613 as separate entries. */
3614
3615/* ARGUSED */
3616struct direct *
3617readdirver (dirp)
3618 register DIR *dirp; /* stream from opendir */
3619{
3620 register struct dir$_name *dp; /* -> directory data */
3621 register struct dir$_version *dv; /* -> version data */
3622
3623 if (dirp->dd_loc >= dirp->dd_size - sizeof (struct dir$_name))
3624 dirp->dd_loc = dirp->dd_size = 0;
3625
3626 if (dirp->dd_size == 0 /* refill buffer */
3627 && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3628 return 0;
3629
3630 dp = (struct dir$_name *) dirp->dd_buf;
3631 if (dirp->dd_loc == 0)
3632 dirp->dd_loc = (dp->dir$b_namecount & 1) ? dp->dir$b_namecount + 1
3633 : dp->dir$b_namecount;
3634 dv = (struct dir$_version *) &dp->dir$t_name[dirp->dd_loc];
3635 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3636 sprintf (&dir_static.d_name[dp->dir$b_namecount], ";%d", dv->dir$w_version);
3637 dir_static.d_namlen = strlen (dir_static.d_name);
3638 dir_static.d_ino = dv->dir$w_fid_num;
3639 dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3
3640 + dir_static.d_namlen - dir_static.d_namlen % 4;
3641 dirp->dd_loc = ((char *) (++dv) - dp->dir$t_name);
3642 return &dir_static;
3643}
3644
3645#endif /* VMS */
3646
3647#endif /* NONSYSTEM_DIR_LIBRARY */
23524fb9
JB
3648
3649\f
53ea491a 3650int
061ea326 3651set_file_times (filename, atime, mtime)
8334eb21 3652 char *filename;
53ea491a
KH
3653 EMACS_TIME atime, mtime;
3654{
3655#ifdef HAVE_UTIMES
3656 struct timeval tv[2];
3657 tv[0] = atime;
3658 tv[1] = mtime;
8334eb21
RS
3659 return utimes (filename, tv);
3660#else /* not HAVE_UTIMES */
53ea491a
KH
3661 struct utimbuf utb;
3662 utb.actime = EMACS_SECS (atime);
3663 utb.modtime = EMACS_SECS (mtime);
8334eb21
RS
3664 return utime (filename, &utb);
3665#endif /* not HAVE_UTIMES */
53ea491a
KH
3666}
3667\f
23524fb9
JB
3668/* mkdir and rmdir functions, for systems which don't have them. */
3669
3670#ifndef HAVE_MKDIR
3671/*
3672 * Written by Robert Rother, Mariah Corporation, August 1985.
3673 *
3674 * If you want it, it's yours. All I ask in return is that if you
3675 * figure out how to do this in a Bourne Shell script you send me
3676 * a copy.
3677 * sdcsvax!rmr or rmr@uscd
3678 *
3679 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
3680 * subroutine. 11Mar86; hoptoad!gnu
3681 *
3682 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
3683 * subroutine didn't return EEXIST. It does now.
3684 */
3685
3686/*
3687 * Make a directory.
3688 */
f3892946
RS
3689#ifdef MKDIR_PROTOTYPE
3690MKDIR_PROTOTYPE
3691#else
23524fb9
JB
3692int
3693mkdir (dpath, dmode)
3694 char *dpath;
3695 int dmode;
f3892946 3696#endif
23524fb9 3697{
039f26a4 3698 int cpid, status, fd;
23524fb9
JB
3699 struct stat statbuf;
3700
3701 if (stat (dpath, &statbuf) == 0)
3702 {
3703 errno = EEXIST; /* Stat worked, so it already exists */
3704 return -1;
3705 }
3706
3707 /* If stat fails for a reason other than non-existence, return error */
3708 if (errno != ENOENT)
3709 return -1;
3710
039f26a4 3711 synch_process_alive = 1;
23524fb9
JB
3712 switch (cpid = fork ())
3713 {
3714
039f26a4 3715 case -1: /* Error in fork */
23524fb9
JB
3716 return (-1); /* Errno is set already */
3717
3718 case 0: /* Child process */
3719 /*
3720 * Cheap hack to set mode of new directory. Since this
3721 * child process is going away anyway, we zap its umask.
3722 * FIXME, this won't suffice to set SUID, SGID, etc. on this
3723 * directory. Does anybody care?
3724 */
3725 status = umask (0); /* Get current umask */
3726 status = umask (status | (0777 & ~dmode)); /* Set for mkdir */
68c45bf0 3727 fd = emacs_open ("/dev/null", O_RDWR, 0);
039f26a4
RS
3728 if (fd >= 0)
3729 {
3730 dup2 (fd, 0);
3731 dup2 (fd, 1);
3732 dup2 (fd, 2);
3733 }
23524fb9
JB
3734 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
3735 _exit (-1); /* Can't exec /bin/mkdir */
3736
3737 default: /* Parent process */
039f26a4 3738 wait_for_termination (cpid);
23524fb9
JB
3739 }
3740
039f26a4 3741 if (synch_process_death != 0 || synch_process_retcode != 0)
23524fb9
JB
3742 {
3743 errno = EIO; /* We don't know why, but */
3744 return -1; /* /bin/mkdir failed */
3745 }
3746
3747 return 0;
3748}
3749#endif /* not HAVE_MKDIR */
3750
3751#ifndef HAVE_RMDIR
3752int
3753rmdir (dpath)
3754 char *dpath;
3755{
039f26a4 3756 int cpid, status, fd;
23524fb9
JB
3757 struct stat statbuf;
3758
3759 if (stat (dpath, &statbuf) != 0)
3760 {
3761 /* Stat just set errno. We don't have to */
3762 return -1;
3763 }
3764
039f26a4 3765 synch_process_alive = 1;
23524fb9
JB
3766 switch (cpid = fork ())
3767 {
3768
039f26a4 3769 case -1: /* Error in fork */
23524fb9
JB
3770 return (-1); /* Errno is set already */
3771
3772 case 0: /* Child process */
68c45bf0 3773 fd = emacs_open ("/dev/null", O_RDWR, 0);
039f26a4
RS
3774 if (fd >= 0)
3775 {
3776 dup2 (fd, 0);
3777 dup2 (fd, 1);
3778 dup2 (fd, 2);
3779 }
f560db78
RS
3780 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
3781 _exit (-1); /* Can't exec /bin/rmdir */
3782
207bdbdb 3783 default: /* Parent process */
f560db78 3784 wait_for_termination (cpid);
23524fb9
JB
3785 }
3786
f560db78 3787 if (synch_process_death != 0 || synch_process_retcode != 0)
23524fb9
JB
3788 {
3789 errno = EIO; /* We don't know why, but */
f560db78 3790 return -1; /* /bin/rmdir failed */
23524fb9
JB
3791 }
3792
3793 return 0;
3794}
3795#endif /* !HAVE_RMDIR */
3796
3797
86a5659e
JB
3798\f
3799/* Functions for VMS */
3800#ifdef VMS
91bac16a 3801#include "vms-pwd.h"
86a5659e
JB
3802#include <acldef.h>
3803#include <chpdef.h>
3804#include <jpidef.h>
3805
3806/* Return as a string the VMS error string pertaining to STATUS.
3807 Reuses the same static buffer each time it is called. */
3808
3809char *
3810vmserrstr (status)
3811 int status; /* VMS status code */
3812{
3813 int bufadr[2];
3814 short len;
3815 static char buf[257];
3816
3817 bufadr[0] = sizeof buf - 1;
3818 bufadr[1] = (int) buf;
3819 if (! (SYS$GETMSG (status, &len, bufadr, 0x1, 0) & 1))
3820 return "untranslatable VMS error status";
3821 buf[len] = '\0';
3822 return buf;
3823}
3824
3825#ifdef access
3826#undef access
3827
3828/* The following is necessary because 'access' emulation by VMS C (2.0) does
3829 * not work correctly. (It also doesn't work well in version 2.3.)
3830 */
3831
3832#ifdef VMS4_4
3833
3834#define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3835 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3836
3837typedef union {
3838 struct {
3839 unsigned short s_buflen;
3840 unsigned short s_code;
3841 char *s_bufadr;
3842 unsigned short *s_retlenadr;
3843 } s;
3844 int end;
3845} item;
3846#define buflen s.s_buflen
3847#define code s.s_code
3848#define bufadr s.s_bufadr
3849#define retlenadr s.s_retlenadr
3850
3851#define R_OK 4 /* test for read permission */
3852#define W_OK 2 /* test for write permission */
3853#define X_OK 1 /* test for execute (search) permission */
3854#define F_OK 0 /* test for presence of file */
3855
3856int
3857sys_access (path, mode)
3858 char *path;
3859 int mode;
3860{
3861 static char *user = NULL;
3862 char dir_fn[512];
3863
3864 /* translate possible directory spec into .DIR file name, so brain-dead
3865 * access can treat the directory like a file. */
3866 if (directory_file_name (path, dir_fn))
3867 path = dir_fn;
3868
3869 if (mode == F_OK)
3870 return access (path, mode);
3871 if (user == NULL && (user = (char *) getenv ("USER")) == NULL)
3872 return -1;
3873 {
3874 int stat;
3875 int flags;
3876 int acces;
3877 unsigned short int dummy;
3878 item itemlst[3];
3879 static int constant = ACL$C_FILE;
3880 DESCRIPTOR (path_desc, path);
3881 DESCRIPTOR (user_desc, user);
3882
3883 flags = 0;
3884 acces = 0;
3885 if ((mode & X_OK) && ((stat = access (path, mode)) < 0 || mode == X_OK))
3886 return stat;
3887 if (mode & R_OK)
3888 acces |= CHP$M_READ;
3889 if (mode & W_OK)
3890 acces |= CHP$M_WRITE;
3891 itemlst[0].buflen = sizeof (int);
3892 itemlst[0].code = CHP$_FLAGS;
3893 itemlst[0].bufadr = (char *) &flags;
3894 itemlst[0].retlenadr = &dummy;
3895 itemlst[1].buflen = sizeof (int);
3896 itemlst[1].code = CHP$_ACCESS;
3897 itemlst[1].bufadr = (char *) &acces;
3898 itemlst[1].retlenadr = &dummy;
3899 itemlst[2].end = CHP$_END;
3900 stat = SYS$CHECK_ACCESS (&constant, &path_desc, &user_desc, itemlst);
3901 return stat == SS$_NORMAL ? 0 : -1;
3902 }
3903}
3904
3905#else /* not VMS4_4 */
3906
3907#include <prvdef.h>
fe03522b
RS
3908#define ACE$M_WRITE 2
3909#define ACE$C_KEYID 1
86a5659e
JB
3910
3911static unsigned short memid, grpid;
3912static unsigned int uic;
3913
3914/* Called from init_sys_modes, so it happens not very often
3915 but at least each time Emacs is loaded. */
dfcf069d 3916void
86a5659e
JB
3917sys_access_reinit ()
3918{
3919 uic = 0;
3920}
3921
3922int
3923sys_access (filename, type)
3924 char * filename;
3925 int type;
3926{
3927 struct FAB fab;
3928 struct XABPRO xab;
3929 int status, size, i, typecode, acl_controlled;
3930 unsigned int *aclptr, *aclend, aclbuf[60];
3931 union prvdef prvmask;
3932
3933 /* Get UIC and GRP values for protection checking. */
3934 if (uic == 0)
3935 {
3936 status = LIB$GETJPI (&JPI$_UIC, 0, 0, &uic, 0, 0);
3937 if (! (status & 1))
3938 return -1;
3939 memid = uic & 0xFFFF;
3940 grpid = uic >> 16;
3941 }
3942
fe03522b 3943 if (type != 2) /* not checking write access */
86a5659e
JB
3944 return access (filename, type);
3945
3946 /* Check write protection. */
3947
fe03522b 3948#define CHECKPRIV(bit) (prvmask.bit)
ea5a0917 3949#define WRITABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
86a5659e
JB
3950
3951 /* Find privilege bits */
986ffb24 3952 status = SYS$SETPRV (0, 0, 0, prvmask);
86a5659e
JB
3953 if (! (status & 1))
3954 error ("Unable to find privileges: %s", vmserrstr (status));
3955 if (CHECKPRIV (PRV$V_BYPASS))
3956 return 0; /* BYPASS enabled */
3957 fab = cc$rms_fab;
3958 fab.fab$b_fac = FAB$M_GET;
3959 fab.fab$l_fna = filename;
3960 fab.fab$b_fns = strlen (filename);
3961 fab.fab$l_xab = &xab;
3962 xab = cc$rms_xabpro;
3963 xab.xab$l_aclbuf = aclbuf;
3964 xab.xab$w_aclsiz = sizeof (aclbuf);
986ffb24 3965 status = SYS$OPEN (&fab, 0, 0);
86a5659e
JB
3966 if (! (status & 1))
3967 return -1;
986ffb24 3968 SYS$CLOSE (&fab, 0, 0);
86a5659e 3969 /* Check system access */
ea5a0917 3970 if (CHECKPRIV (PRV$V_SYSPRV) && WRITABLE (XAB$V_SYS))
86a5659e
JB
3971 return 0;
3972 /* Check ACL entries, if any */
3973 acl_controlled = 0;
3974 if (xab.xab$w_acllen > 0)
3975 {
3976 aclptr = aclbuf;
3977 aclend = &aclbuf[xab.xab$w_acllen / 4];
3978 while (*aclptr && aclptr < aclend)
3979 {
3980 size = (*aclptr & 0xff) / 4;
3981 typecode = (*aclptr >> 8) & 0xff;
3982 if (typecode == ACE$C_KEYID)
3983 for (i = size - 1; i > 1; i--)
3984 if (aclptr[i] == uic)
3985 {
3986 acl_controlled = 1;
3987 if (aclptr[1] & ACE$M_WRITE)
3988 return 0; /* Write access through ACL */
3989 }
3990 aclptr = &aclptr[size];
3991 }
3992 if (acl_controlled) /* ACL specified, prohibits write access */
3993 return -1;
3994 }
3995 /* No ACL entries specified, check normal protection */
ea5a0917 3996 if (WRITABLE (XAB$V_WLD)) /* World writable */
86a5659e 3997 return 0;
ea5a0917 3998 if (WRITABLE (XAB$V_GRP) &&
86a5659e 3999 (unsigned short) (xab.xab$l_uic >> 16) == grpid)
ea5a0917
KH
4000 return 0; /* Group writable */
4001 if (WRITABLE (XAB$V_OWN) &&
86a5659e 4002 (xab.xab$l_uic & 0xFFFF) == memid)
ea5a0917 4003 return 0; /* Owner writable */
86a5659e 4004
ea5a0917 4005 return -1; /* Not writable */
86a5659e
JB
4006}
4007#endif /* not VMS4_4 */
4008#endif /* access */
4009
4010static char vtbuf[NAM$C_MAXRSS+1];
4011
4012/* translate a vms file spec to a unix path */
4013char *
4014sys_translate_vms (vfile)
4015 char * vfile;
4016{
4017 char * p;
4018 char * targ;
4019
4020 if (!vfile)
4021 return 0;
4022
4023 targ = vtbuf;
4024
4025 /* leading device or logical name is a root directory */
4026 if (p = strchr (vfile, ':'))
4027 {
4028 *targ++ = '/';
4029 while (vfile < p)
4030 *targ++ = *vfile++;
4031 vfile++;
4032 *targ++ = '/';
4033 }
4034 p = vfile;
4035 if (*p == '[' || *p == '<')
4036 {
4037 while (*++vfile != *p + 2)
4038 switch (*vfile)
4039 {
4040 case '.':
4041 if (vfile[-1] == *p)
4042 *targ++ = '.';
4043 *targ++ = '/';
4044 break;
4045
4046 case '-':
4047 *targ++ = '.';
4048 *targ++ = '.';
4049 break;
4050
4051 default:
4052 *targ++ = *vfile;
4053 break;
4054 }
4055 vfile++;
4056 *targ++ = '/';
4057 }
4058 while (*vfile)
4059 *targ++ = *vfile++;
4060
4061 return vtbuf;
4062}
4063
4064static char utbuf[NAM$C_MAXRSS+1];
4065
4066/* translate a unix path to a VMS file spec */
4067char *
4068sys_translate_unix (ufile)
4069 char * ufile;
4070{
4071 int slash_seen = 0;
4072 char *p;
4073 char * targ;
4074
4075 if (!ufile)
4076 return 0;
4077
4078 targ = utbuf;
4079
4080 if (*ufile == '/')
4081 {
4082 ufile++;
4083 }
4084
4085 while (*ufile)
4086 {
4087 switch (*ufile)
4088 {
4089 case '/':
4090 if (slash_seen)
4091 if (index (&ufile[1], '/'))
4092 *targ++ = '.';
4093 else
4094 *targ++ = ']';
4095 else
4096 {
4097 *targ++ = ':';
4098 if (index (&ufile[1], '/'))
4099 *targ++ = '[';
4100 slash_seen = 1;
4101 }
4102 break;
4103
4104 case '.':
4105 if (strncmp (ufile, "./", 2) == 0)
4106 {
4107 if (!slash_seen)
4108 {
4109 *targ++ = '[';
4110 slash_seen = 1;
4111 }
4112 ufile++; /* skip the dot */
4113 if (index (&ufile[1], '/'))
4114 *targ++ = '.';
4115 else
4116 *targ++ = ']';
4117 }
4118 else if (strncmp (ufile, "../", 3) == 0)
4119 {
4120 if (!slash_seen)
4121 {
4122 *targ++ = '[';
4123 slash_seen = 1;
4124 }
4125 *targ++ = '-';
4126 ufile += 2; /* skip the dots */
4127 if (index (&ufile[1], '/'))
4128 *targ++ = '.';
4129 else
4130 *targ++ = ']';
4131 }
4132 else
4133 *targ++ = *ufile;
4134 break;
4135
4136 default:
4137 *targ++ = *ufile;
4138 break;
4139 }
4140 ufile++;
4141 }
4142 *targ = '\0';
4143
4144 return utbuf;
4145}
4146
4147char *
4148getwd (pathname)
4149 char *pathname;
4150{
f4a7e5bd 4151 char *ptr, *val;
210b2b4f 4152 extern char *getcwd ();
86a5659e 4153
210b2b4f
JB
4154#define MAXPATHLEN 1024
4155
9ac0d9e0 4156 ptr = xmalloc (MAXPATHLEN);
f4a7e5bd
RS
4157 val = getcwd (ptr, MAXPATHLEN);
4158 if (val == 0)
4159 {
4160 xfree (ptr);
4161 return val;
4162 }
210b2b4f 4163 strcpy (pathname, ptr);
9ac0d9e0 4164 xfree (ptr);
210b2b4f
JB
4165
4166 return pathname;
86a5659e
JB
4167}
4168
dfcf069d 4169int
86a5659e
JB
4170getppid ()
4171{
4172 long item_code = JPI$_OWNER;
4173 unsigned long parent_id;
4174 int status;
4175
4176 if (((status = LIB$GETJPI (&item_code, 0, 0, &parent_id)) & 1) == 0)
4177 {
4178 errno = EVMSERR;
4179 vaxc$errno = status;
4180 return -1;
4181 }
4182 return parent_id;
4183}
4184
4185#undef getuid
4186unsigned
4187sys_getuid ()
4188{
4189 return (getgid () << 16) | getuid ();
4190}
4191
68c45bf0 4192#undef read
86a5659e
JB
4193int
4194sys_read (fildes, buf, nbyte)
4195 int fildes;
4196 char *buf;
4197 unsigned int nbyte;
4198{
4199 return read (fildes, buf, (nbyte < MAXIOSIZE ? nbyte : MAXIOSIZE));
4200}
4201
4202#if 0
4203int
4204sys_write (fildes, buf, nbyte)
4205 int fildes;
4206 char *buf;
4207 unsigned int nbyte;
4208{
4209 register int nwrote, rtnval = 0;
4210
4211 while (nbyte > MAXIOSIZE && (nwrote = write (fildes, buf, MAXIOSIZE)) > 0) {
4212 nbyte -= nwrote;
4213 buf += nwrote;
4214 rtnval += nwrote;
4215 }
4216 if (nwrote < 0)
4217 return rtnval ? rtnval : -1;
4218 if ((nwrote = write (fildes, buf, nbyte)) < 0)
4219 return rtnval ? rtnval : -1;
4220 return (rtnval + nwrote);
4221}
4222#endif /* 0 */
4223
4224/*
4225 * VAX/VMS VAX C RTL really loses. It insists that records
4226 * end with a newline (carriage return) character, and if they
4227 * don't it adds one (nice of it isn't it!)
4228 *
4229 * Thus we do this stupidity below.
4230 */
4231
68c45bf0 4232#undef write
86a5659e
JB
4233int
4234sys_write (fildes, buf, nbytes)
4235 int fildes;
4236 char *buf;
4237 unsigned int nbytes;
4238{
4239 register char *p;
4240 register char *e;
23b0668c
JB
4241 int sum = 0;
4242 struct stat st;
4243
4244 fstat (fildes, &st);
86a5659e 4245 p = buf;
86a5659e
JB
4246 while (nbytes > 0)
4247 {
23b0668c
JB
4248 int len, retval;
4249
4250 /* Handle fixed-length files with carriage control. */
4251 if (st.st_fab_rfm == FAB$C_FIX
4252 && ((st.st_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
4253 {
4254 len = st.st_fab_mrs;
4255 retval = write (fildes, p, min (len, nbytes));
4256 if (retval != len)
4257 return -1;
4258 retval++; /* This skips the implied carriage control */
4259 }
4260 else
4261 {
4262 e = p + min (MAXIOSIZE, nbytes) - 1;
4263 while (*e != '\n' && e > p) e--;
4264 if (p == e) /* Ok.. so here we add a newline... sigh. */
4265 e = p + min (MAXIOSIZE, nbytes) - 1;
4266 len = e + 1 - p;
4267 retval = write (fildes, p, len);
4268 if (retval != len)
4269 return -1;
4270 }
4271 p += retval;
4272 sum += retval;
86a5659e
JB
4273 nbytes -= retval;
4274 }
4275 return sum;
4276}
4277
4278/* Create file NEW copying its attributes from file OLD. If
4279 OLD is 0 or does not exist, create based on the value of
4280 vms_stmlf_recfm. */
4281
4282/* Protection value the file should ultimately have.
4283 Set by create_copy_attrs, and use by rename_sansversions. */
4284static unsigned short int fab_final_pro;
4285
4286int
4287creat_copy_attrs (old, new)
4288 char *old, *new;
4289{
4290 struct FAB fab = cc$rms_fab;
4291 struct XABPRO xabpro;
4292 char aclbuf[256]; /* Choice of size is arbitrary. See below. */
4293 extern int vms_stmlf_recfm;
4294
4295 if (old)
4296 {
4297 fab.fab$b_fac = FAB$M_GET;
4298 fab.fab$l_fna = old;
4299 fab.fab$b_fns = strlen (old);
4300 fab.fab$l_xab = (char *) &xabpro;
4301 xabpro = cc$rms_xabpro;
4302 xabpro.xab$l_aclbuf = aclbuf;
4303 xabpro.xab$w_aclsiz = sizeof aclbuf;
4304 /* Call $OPEN to fill in the fab & xabpro fields. */
986ffb24 4305 if (SYS$OPEN (&fab, 0, 0) & 1)
86a5659e 4306 {
986ffb24 4307 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
4308 fab.fab$l_alq = 0; /* zero the allocation quantity */
4309 if (xabpro.xab$w_acllen > 0)
4310 {
4311 if (xabpro.xab$w_acllen > sizeof aclbuf)
4312 /* If the acl buffer was too short, redo open with longer one.
4313 Wouldn't need to do this if there were some system imposed
4314 limit on the size of an ACL, but I can't find any such. */
4315 {
4316 xabpro.xab$l_aclbuf = (char *) alloca (xabpro.xab$w_acllen);
4317 xabpro.xab$w_aclsiz = xabpro.xab$w_acllen;
986ffb24
JB
4318 if (SYS$OPEN (&fab, 0, 0) & 1)
4319 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
4320 else
4321 old = 0;
4322 }
4323 }
4324 else
4325 xabpro.xab$l_aclbuf = 0;
4326 }
4327 else
4328 old = 0;
4329 }
4330 fab.fab$l_fna = new;
4331 fab.fab$b_fns = strlen (new);
4332 if (!old)
4333 {
4334 fab.fab$l_xab = 0;
4335 fab.fab$b_rfm = vms_stmlf_recfm ? FAB$C_STMLF : FAB$C_VAR;
4336 fab.fab$b_rat = FAB$M_CR;
4337 }
4338
4339 /* Set the file protections such that we will be able to manipulate
4340 this file. Once we are done writing and renaming it, we will set
4341 the protections back. */
4342 if (old)
4343 fab_final_pro = xabpro.xab$w_pro;
4344 else
986ffb24 4345 SYS$SETDFPROT (0, &fab_final_pro);
86a5659e
JB
4346 xabpro.xab$w_pro &= 0xff0f; /* set O:rewd for now. This is set back later. */
4347
4348 /* Create the new file with either default attrs or attrs copied
4349 from old file. */
4350 if (!(SYS$CREATE (&fab, 0, 0) & 1))
4351 return -1;
986ffb24 4352 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
4353 /* As this is a "replacement" for creat, return a file descriptor
4354 opened for writing. */
4355 return open (new, O_WRONLY);
4356}
4357
4358#ifdef creat
4359#undef creat
4360#include <varargs.h>
4361#ifdef __GNUC__
4362#ifndef va_count
4363#define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
4364#endif
4365#endif
4366
dfcf069d 4367int
86a5659e
JB
4368sys_creat (va_alist)
4369 va_dcl
4370{
eb8c3be9 4371 va_list list_incrementer;
86a5659e
JB
4372 char *name;
4373 int mode;
4374 int rfd; /* related file descriptor */
4375 int fd; /* Our new file descriptor */
4376 int count;
4377 struct stat st_buf;
4378 char rfm[12];
4379 char rat[15];
4380 char mrs[13];
4381 char fsz[13];
4382 extern int vms_stmlf_recfm;
4383
4384 va_count (count);
eb8c3be9
JB
4385 va_start (list_incrementer);
4386 name = va_arg (list_incrementer, char *);
4387 mode = va_arg (list_incrementer, int);
86a5659e 4388 if (count > 2)
eb8c3be9
JB
4389 rfd = va_arg (list_incrementer, int);
4390 va_end (list_incrementer);
86a5659e
JB
4391 if (count > 2)
4392 {
4393 /* Use information from the related file descriptor to set record
4394 format of the newly created file. */
4395 fstat (rfd, &st_buf);
4396 switch (st_buf.st_fab_rfm)
4397 {
4398 case FAB$C_FIX:
4399 strcpy (rfm, "rfm = fix");
4400 sprintf (mrs, "mrs = %d", st_buf.st_fab_mrs);
4401 strcpy (rat, "rat = ");
4402 if (st_buf.st_fab_rat & FAB$M_CR)
4403 strcat (rat, "cr");
4404 else if (st_buf.st_fab_rat & FAB$M_FTN)
4405 strcat (rat, "ftn");
4406 else if (st_buf.st_fab_rat & FAB$M_PRN)
4407 strcat (rat, "prn");
4408 if (st_buf.st_fab_rat & FAB$M_BLK)
4409 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4410 strcat (rat, ", blk");
4411 else
4412 strcat (rat, "blk");
4413 return creat (name, 0, rfm, rat, mrs);
4414
4415 case FAB$C_VFC:
4416 strcpy (rfm, "rfm = vfc");
4417 sprintf (fsz, "fsz = %d", st_buf.st_fab_fsz);
4418 strcpy (rat, "rat = ");
4419 if (st_buf.st_fab_rat & FAB$M_CR)
4420 strcat (rat, "cr");
4421 else if (st_buf.st_fab_rat & FAB$M_FTN)
4422 strcat (rat, "ftn");
4423 else if (st_buf.st_fab_rat & FAB$M_PRN)
4424 strcat (rat, "prn");
4425 if (st_buf.st_fab_rat & FAB$M_BLK)
4426 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4427 strcat (rat, ", blk");
4428 else
4429 strcat (rat, "blk");
4430 return creat (name, 0, rfm, rat, fsz);
4431
4432 case FAB$C_STM:
4433 strcpy (rfm, "rfm = stm");
4434 break;
4435
4436 case FAB$C_STMCR:
4437 strcpy (rfm, "rfm = stmcr");
4438 break;
4439
4440 case FAB$C_STMLF:
4441 strcpy (rfm, "rfm = stmlf");
4442 break;
4443
4444 case FAB$C_UDF:
4445 strcpy (rfm, "rfm = udf");
4446 break;
4447
4448 case FAB$C_VAR:
4449 strcpy (rfm, "rfm = var");
4450 break;
4451 }
4452 strcpy (rat, "rat = ");
4453 if (st_buf.st_fab_rat & FAB$M_CR)
4454 strcat (rat, "cr");
4455 else if (st_buf.st_fab_rat & FAB$M_FTN)
4456 strcat (rat, "ftn");
4457 else if (st_buf.st_fab_rat & FAB$M_PRN)
4458 strcat (rat, "prn");
4459 if (st_buf.st_fab_rat & FAB$M_BLK)
4460 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4461 strcat (rat, ", blk");
4462 else
4463 strcat (rat, "blk");
4464 }
4465 else
4466 {
4467 strcpy (rfm, vms_stmlf_recfm ? "rfm = stmlf" : "rfm=var");
4468 strcpy (rat, "rat=cr");
4469 }
4470 /* Until the VAX C RTL fixes the many bugs with modes, always use
4471 mode 0 to get the user's default protection. */
4472 fd = creat (name, 0, rfm, rat);
4473 if (fd < 0 && errno == EEXIST)
4474 {
4475 if (unlink (name) < 0)
4476 report_file_error ("delete", build_string (name));
4477 fd = creat (name, 0, rfm, rat);
4478 }
4479 return fd;
4480}
4481#endif /* creat */
4482
4483/* fwrite to stdout is S L O W. Speed it up by using fputc...*/
dfcf069d 4484int
86a5659e
JB
4485sys_fwrite (ptr, size, num, fp)
4486 register char * ptr;
4487 FILE * fp;
4488{
4489 register int tot = num * size;
4490
4491 while (tot--)
4492 fputc (*ptr++, fp);
dfcf069d 4493 return num;
86a5659e
JB
4494}
4495
4496/*
4497 * The VMS C library routine creat actually creates a new version of an
4498 * existing file rather than truncating the old version. There are times
4499 * when this is not the desired behavior, for instance, when writing an
4500 * auto save file (you only want one version), or when you don't have
4501 * write permission in the directory containing the file (but the file
4502 * itself is writable). Hence this routine, which is equivalent to
4503 * "close (creat (fn, 0));" on Unix if fn already exists.
4504 */
4505int
4506vms_truncate (fn)
4507 char *fn;
4508{
4509 struct FAB xfab = cc$rms_fab;
4510 struct RAB xrab = cc$rms_rab;
4511 int status;
4512
4513 xfab.fab$l_fop = FAB$M_TEF; /* free allocated but unused blocks on close */
4514 xfab.fab$b_fac = FAB$M_TRN | FAB$M_GET; /* allow truncate and get access */
4515 xfab.fab$b_shr = FAB$M_NIL; /* allow no sharing - file must be locked */
4516 xfab.fab$l_fna = fn;
4517 xfab.fab$b_fns = strlen (fn);
4518 xfab.fab$l_dna = ";0"; /* default to latest version of the file */
4519 xfab.fab$b_dns = 2;
4520 xrab.rab$l_fab = &xfab;
4521
4522 /* This gibberish opens the file, positions to the first record, and
4523 deletes all records from there until the end of file. */
986ffb24 4524 if ((SYS$OPEN (&xfab) & 01) == 01)
86a5659e 4525 {
986ffb24
JB
4526 if ((SYS$CONNECT (&xrab) & 01) == 01 &&
4527 (SYS$FIND (&xrab) & 01) == 01 &&
4528 (SYS$TRUNCATE (&xrab) & 01) == 01)
86a5659e
JB
4529 status = 0;
4530 else
4531 status = -1;
4532 }
4533 else
4534 status = -1;
986ffb24 4535 SYS$CLOSE (&xfab);
86a5659e
JB
4536 return status;
4537}
4538
4539/* Define this symbol to actually read SYSUAF.DAT. This requires either
4540 SYSPRV or a readable SYSUAF.DAT. */
4541
4542#ifdef READ_SYSUAF
4543/*
4544 * getuaf.c
4545 *
4546 * Routine to read the VMS User Authorization File and return
4547 * a specific user's record.
4548 */
4549
4550static struct UAF retuaf;
4551
4552struct UAF *
4553get_uaf_name (uname)
4554 char * uname;
4555{
4556 register status;
4557 struct FAB uaf_fab;
4558 struct RAB uaf_rab;
4559
4560 uaf_fab = cc$rms_fab;
4561 uaf_rab = cc$rms_rab;
4562 /* initialize fab fields */
4563 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4564 uaf_fab.fab$b_fns = 21;
4565 uaf_fab.fab$b_fac = FAB$M_GET;
4566 uaf_fab.fab$b_org = FAB$C_IDX;
4567 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4568 /* initialize rab fields */
4569 uaf_rab.rab$l_fab = &uaf_fab;
4570 /* open the User Authorization File */
986ffb24 4571 status = SYS$OPEN (&uaf_fab);
86a5659e
JB
4572 if (!(status&1))
4573 {
4574 errno = EVMSERR;
4575 vaxc$errno = status;
4576 return 0;
4577 }
986ffb24 4578 status = SYS$CONNECT (&uaf_rab);
86a5659e
JB
4579 if (!(status&1))
4580 {
4581 errno = EVMSERR;
4582 vaxc$errno = status;
4583 return 0;
4584 }
4585 /* read the requested record - index is in uname */
4586 uaf_rab.rab$l_kbf = uname;
4587 uaf_rab.rab$b_ksz = strlen (uname);
4588 uaf_rab.rab$b_rac = RAB$C_KEY;
4589 uaf_rab.rab$l_ubf = (char *)&retuaf;
4590 uaf_rab.rab$w_usz = sizeof retuaf;
986ffb24 4591 status = SYS$GET (&uaf_rab);
86a5659e
JB
4592 if (!(status&1))
4593 {
4594 errno = EVMSERR;
4595 vaxc$errno = status;
4596 return 0;
4597 }
4598 /* close the User Authorization File */
986ffb24 4599 status = SYS$DISCONNECT (&uaf_rab);
86a5659e
JB
4600 if (!(status&1))
4601 {
4602 errno = EVMSERR;
4603 vaxc$errno = status;
4604 return 0;
4605 }
986ffb24 4606 status = SYS$CLOSE (&uaf_fab);
86a5659e
JB
4607 if (!(status&1))
4608 {
4609 errno = EVMSERR;
4610 vaxc$errno = status;
4611 return 0;
4612 }
4613 return &retuaf;
4614}
4615
4616struct UAF *
4617get_uaf_uic (uic)
4618 unsigned long uic;
4619{
4620 register status;
4621 struct FAB uaf_fab;
4622 struct RAB uaf_rab;
4623
4624 uaf_fab = cc$rms_fab;
4625 uaf_rab = cc$rms_rab;
4626 /* initialize fab fields */
4627 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4628 uaf_fab.fab$b_fns = 21;
4629 uaf_fab.fab$b_fac = FAB$M_GET;
4630 uaf_fab.fab$b_org = FAB$C_IDX;
4631 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4632 /* initialize rab fields */
4633 uaf_rab.rab$l_fab = &uaf_fab;
4634 /* open the User Authorization File */
986ffb24 4635 status = SYS$OPEN (&uaf_fab);
86a5659e
JB
4636 if (!(status&1))
4637 {
4638 errno = EVMSERR;
4639 vaxc$errno = status;
4640 return 0;
4641 }
986ffb24 4642 status = SYS$CONNECT (&uaf_rab);
86a5659e
JB
4643 if (!(status&1))
4644 {
4645 errno = EVMSERR;
4646 vaxc$errno = status;
4647 return 0;
4648 }
4649 /* read the requested record - index is in uic */
4650 uaf_rab.rab$b_krf = 1; /* 1st alternate key */
4651 uaf_rab.rab$l_kbf = (char *) &uic;
4652 uaf_rab.rab$b_ksz = sizeof uic;
4653 uaf_rab.rab$b_rac = RAB$C_KEY;
4654 uaf_rab.rab$l_ubf = (char *)&retuaf;
4655 uaf_rab.rab$w_usz = sizeof retuaf;
986ffb24 4656 status = SYS$GET (&uaf_rab);
86a5659e
JB
4657 if (!(status&1))
4658 {
4659 errno = EVMSERR;
4660 vaxc$errno = status;
4661 return 0;
4662 }
4663 /* close the User Authorization File */
986ffb24 4664 status = SYS$DISCONNECT (&uaf_rab);
86a5659e
JB
4665 if (!(status&1))
4666 {
4667 errno = EVMSERR;
4668 vaxc$errno = status;
4669 return 0;
4670 }
986ffb24 4671 status = SYS$CLOSE (&uaf_fab);
86a5659e
JB
4672 if (!(status&1))
4673 {
4674 errno = EVMSERR;
4675 vaxc$errno = status;
4676 return 0;
4677 }
4678 return &retuaf;
4679}
4680
4681static struct passwd retpw;
4682
4683struct passwd *
4684cnv_uaf_pw (up)
4685 struct UAF * up;
4686{
4687 char * ptr;
4688
4689 /* copy these out first because if the username is 32 chars, the next
4690 section will overwrite the first byte of the UIC */
4691 retpw.pw_uid = up->uaf$w_mem;
4692 retpw.pw_gid = up->uaf$w_grp;
4693
ea5a0917 4694 /* I suppose this is not the best style, to possibly overwrite one
86a5659e
JB
4695 byte beyond the end of the field, but what the heck... */
4696 ptr = &up->uaf$t_username[UAF$S_USERNAME];
4697 while (ptr[-1] == ' ')
4698 ptr--;
4699 *ptr = '\0';
4700 strcpy (retpw.pw_name, up->uaf$t_username);
4701
4702 /* the rest of these are counted ascii strings */
4703 strncpy (retpw.pw_gecos, &up->uaf$t_owner[1], up->uaf$t_owner[0]);
4704 retpw.pw_gecos[up->uaf$t_owner[0]] = '\0';
4705 strncpy (retpw.pw_dir, &up->uaf$t_defdev[1], up->uaf$t_defdev[0]);
4706 retpw.pw_dir[up->uaf$t_defdev[0]] = '\0';
4707 strncat (retpw.pw_dir, &up->uaf$t_defdir[1], up->uaf$t_defdir[0]);
4708 retpw.pw_dir[up->uaf$t_defdev[0] + up->uaf$t_defdir[0]] = '\0';
4709 strncpy (retpw.pw_shell, &up->uaf$t_defcli[1], up->uaf$t_defcli[0]);
4710 retpw.pw_shell[up->uaf$t_defcli[0]] = '\0';
4711
4712 return &retpw;
4713}
4714#else /* not READ_SYSUAF */
4715static struct passwd retpw;
4716#endif /* not READ_SYSUAF */
4717
4718struct passwd *
4719getpwnam (name)
4720 char * name;
4721{
4722#ifdef READ_SYSUAF
4723 struct UAF *up;
4724#else
4725 char * user;
4726 char * dir;
4727 unsigned char * full;
4728#endif /* READ_SYSUAF */
4729 char *ptr = name;
4730
4731 while (*ptr)
4732 {
4733 if ('a' <= *ptr && *ptr <= 'z')
4734 *ptr -= 040;
4735 ptr++;
4736 }
4737#ifdef READ_SYSUAF
4738 if (!(up = get_uaf_name (name)))
4739 return 0;
4740 return cnv_uaf_pw (up);
4741#else
4742 if (strcmp (name, getenv ("USER")) == 0)
4743 {
4744 retpw.pw_uid = getuid ();
4745 retpw.pw_gid = getgid ();
4746 strcpy (retpw.pw_name, name);
4747 if (full = egetenv ("FULLNAME"))
4748 strcpy (retpw.pw_gecos, full);
4749 else
4750 *retpw.pw_gecos = '\0';
4751 strcpy (retpw.pw_dir, egetenv ("HOME"));
4752 *retpw.pw_shell = '\0';
4753 return &retpw;
4754 }
4755 else
4756 return 0;
4757#endif /* not READ_SYSUAF */
4758}
4759
4760struct passwd *
4761getpwuid (uid)
4762 unsigned long uid;
4763{
4764#ifdef READ_SYSUAF
4765 struct UAF * up;
4766
4767 if (!(up = get_uaf_uic (uid)))
4768 return 0;
4769 return cnv_uaf_pw (up);
4770#else
4771 if (uid == sys_getuid ())
4772 return getpwnam (egetenv ("USER"));
4773 else
4774 return 0;
4775#endif /* not READ_SYSUAF */
4776}
4777
4778/* return total address space available to the current process. This is
4779 the sum of the current p0 size, p1 size and free page table entries
4780 available. */
dfcf069d 4781int
86a5659e
JB
4782vlimit ()
4783{
4784 int item_code;
4785 unsigned long free_pages;
4786 unsigned long frep0va;
4787 unsigned long frep1va;
4788 register status;
4789
4790 item_code = JPI$_FREPTECNT;
4791 if (((status = LIB$GETJPI (&item_code, 0, 0, &free_pages)) & 1) == 0)
4792 {
4793 errno = EVMSERR;
4794 vaxc$errno = status;
4795 return -1;
4796 }
4797 free_pages *= 512;
4798
4799 item_code = JPI$_FREP0VA;
4800 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep0va)) & 1) == 0)
4801 {
4802 errno = EVMSERR;
4803 vaxc$errno = status;
4804 return -1;
4805 }
4806 item_code = JPI$_FREP1VA;
4807 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep1va)) & 1) == 0)
4808 {
4809 errno = EVMSERR;
4810 vaxc$errno = status;
4811 return -1;
4812 }
4813
4814 return free_pages + frep0va + (0x7fffffff - frep1va);
4815}
4816
dfcf069d 4817int
86a5659e
JB
4818define_logical_name (varname, string)
4819 char *varname;
4820 char *string;
4821{
4822 struct dsc$descriptor_s strdsc =
4823 {strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string};
4824 struct dsc$descriptor_s envdsc =
4825 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4826 struct dsc$descriptor_s lnmdsc =
4827 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4828
4829 return LIB$SET_LOGICAL (&envdsc, &strdsc, &lnmdsc, 0, 0);
4830}
4831
dfcf069d 4832int
86a5659e
JB
4833delete_logical_name (varname)
4834 char *varname;
4835{
4836 struct dsc$descriptor_s envdsc =
4837 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4838 struct dsc$descriptor_s lnmdsc =
4839 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4840
4841 return LIB$DELETE_LOGICAL (&envdsc, &lnmdsc);
4842}
4843
dfcf069d 4844int
86a5659e 4845ulimit ()
dfcf069d
AS
4846{
4847 return 0;
4848}
86a5659e 4849
dfcf069d 4850int
86a5659e 4851setpgrp ()
dfcf069d
AS
4852{
4853 return 0;
4854}
86a5659e 4855
dfcf069d 4856int
86a5659e
JB
4857execvp ()
4858{
4859 error ("execvp system call not implemented");
dfcf069d 4860 return -1;
86a5659e
JB
4861}
4862
4863int
4864rename (from, to)
4865 char *from, *to;
4866{
4867 int status;
4868 struct FAB from_fab = cc$rms_fab, to_fab = cc$rms_fab;
4869 struct NAM from_nam = cc$rms_nam, to_nam = cc$rms_nam;
4870 char from_esn[NAM$C_MAXRSS];
4871 char to_esn[NAM$C_MAXRSS];
4872
4873 from_fab.fab$l_fna = from;
4874 from_fab.fab$b_fns = strlen (from);
4875 from_fab.fab$l_nam = &from_nam;
4876 from_fab.fab$l_fop = FAB$M_NAM;
4877
4878 from_nam.nam$l_esa = from_esn;
4879 from_nam.nam$b_ess = sizeof from_esn;
4880
4881 to_fab.fab$l_fna = to;
4882 to_fab.fab$b_fns = strlen (to);
4883 to_fab.fab$l_nam = &to_nam;
4884 to_fab.fab$l_fop = FAB$M_NAM;
4885
4886 to_nam.nam$l_esa = to_esn;
4887 to_nam.nam$b_ess = sizeof to_esn;
4888
4889 status = SYS$RENAME (&from_fab, 0, 0, &to_fab);
4890
4891 if (status & 1)
4892 return 0;
4893 else
4894 {
4895 if (status == RMS$_DEV)
4896 errno = EXDEV;
4897 else
4898 errno = EVMSERR;
4899 vaxc$errno = status;
4900 return -1;
4901 }
4902}
4903
4904/* This function renames a file like `rename', but it strips
4905 the version number from the "to" filename, such that the "to" file is
4906 will always be a new version. It also sets the file protection once it is
4907 finished. The protection that we will use is stored in fab_final_pro,
4908 and was set when we did a creat_copy_attrs to create the file that we
4909 are renaming.
4910
4911 We could use the chmod function, but Eunichs uses 3 bits per user category
eb8c3be9 4912 to describe the protection, and VMS uses 4 (write and delete are separate
86a5659e
JB
4913 bits). To maintain portability, the VMS implementation of `chmod' wires
4914 the W and D bits together. */
4915
4916
4917static struct fibdef fib; /* We need this initialized to zero */
4918char vms_file_written[NAM$C_MAXRSS];
4919
4920int
4921rename_sans_version (from,to)
4922 char *from, *to;
4923{
4924 short int chan;
4925 int stat;
4926 short int iosb[4];
4927 int status;
4928 struct FAB to_fab = cc$rms_fab;
4929 struct NAM to_nam = cc$rms_nam;
4930 struct dsc$descriptor fib_d ={sizeof (fib),0,0,(char*) &fib};
4931 struct dsc$descriptor fib_attr[2]
4932 = {{sizeof (fab_final_pro),ATR$C_FPRO,0,(char*) &fab_final_pro},{0,0,0,0}};
4933 char to_esn[NAM$C_MAXRSS];
4934
4935 $DESCRIPTOR (disk,to_esn);
4936
4937 to_fab.fab$l_fna = to;
4938 to_fab.fab$b_fns = strlen (to);
4939 to_fab.fab$l_nam = &to_nam;
4940 to_fab.fab$l_fop = FAB$M_NAM;
4941
4942 to_nam.nam$l_esa = to_esn;
4943 to_nam.nam$b_ess = sizeof to_esn;
4944
4945 status = SYS$PARSE (&to_fab, 0, 0); /* figure out the full file name */
4946
4947 if (to_nam.nam$l_fnb && NAM$M_EXP_VER)
4948 *(to_nam.nam$l_ver) = '\0';
4949
4950 stat = rename (from, to_esn);
4951 if (stat < 0)
4952 return stat;
4953
4954 strcpy (vms_file_written, to_esn);
4955
4956 to_fab.fab$l_fna = vms_file_written; /* this points to the versionless name */
4957 to_fab.fab$b_fns = strlen (vms_file_written);
4958
4959 /* Now set the file protection to the correct value */
986ffb24 4960 SYS$OPEN (&to_fab, 0, 0); /* This fills in the nam$w_fid fields */
86a5659e
JB
4961
4962 /* Copy these fields into the fib */
4963 fib.fib$r_fid_overlay.fib$w_fid[0] = to_nam.nam$w_fid[0];
4964 fib.fib$r_fid_overlay.fib$w_fid[1] = to_nam.nam$w_fid[1];
4965 fib.fib$r_fid_overlay.fib$w_fid[2] = to_nam.nam$w_fid[2];
4966
986ffb24 4967 SYS$CLOSE (&to_fab, 0, 0);
86a5659e 4968
986ffb24 4969 stat = SYS$ASSIGN (&disk, &chan, 0, 0); /* open a channel to the disk */
86a5659e 4970 if (!stat)
986ffb24
JB
4971 LIB$SIGNAL (stat);
4972 stat = SYS$QIOW (0, chan, IO$_MODIFY, iosb, 0, 0, &fib_d,
86a5659e
JB
4973 0, 0, 0, &fib_attr, 0);
4974 if (!stat)
986ffb24
JB
4975 LIB$SIGNAL (stat);
4976 stat = SYS$DASSGN (chan);
86a5659e 4977 if (!stat)
986ffb24 4978 LIB$SIGNAL (stat);
0137dbf7 4979 strcpy (vms_file_written, to_esn); /* We will write this to the terminal*/
86a5659e
JB
4980 return 0;
4981}
4982
dfcf069d 4983int
86a5659e
JB
4984link (file, new)
4985 char * file, * new;
4986{
4987 register status;
4988 struct FAB fab;
4989 struct NAM nam;
4990 unsigned short fid[3];
4991 char esa[NAM$C_MAXRSS];
4992
4993 fab = cc$rms_fab;
4994 fab.fab$l_fop = FAB$M_OFP;
4995 fab.fab$l_fna = file;
4996 fab.fab$b_fns = strlen (file);
4997 fab.fab$l_nam = &nam;
4998
4999 nam = cc$rms_nam;
5000 nam.nam$l_esa = esa;
5001 nam.nam$b_ess = NAM$C_MAXRSS;
5002
5003 status = SYS$PARSE (&fab);
5004 if ((status & 1) == 0)
5005 {
5006 errno = EVMSERR;
5007 vaxc$errno = status;
5008 return -1;
5009 }
5010 status = SYS$SEARCH (&fab);
5011 if ((status & 1) == 0)
5012 {
5013 errno = EVMSERR;
5014 vaxc$errno = status;
5015 return -1;
5016 }
5017
5018 fid[0] = nam.nam$w_fid[0];
5019 fid[1] = nam.nam$w_fid[1];
5020 fid[2] = nam.nam$w_fid[2];
5021
5022 fab.fab$l_fna = new;
5023 fab.fab$b_fns = strlen (new);
5024
5025 status = SYS$PARSE (&fab);
5026 if ((status & 1) == 0)
5027 {
5028 errno = EVMSERR;
5029 vaxc$errno = status;
5030 return -1;
5031 }
5032
5033 nam.nam$w_fid[0] = fid[0];
5034 nam.nam$w_fid[1] = fid[1];
5035 nam.nam$w_fid[2] = fid[2];
5036
5037 nam.nam$l_esa = nam.nam$l_name;
5038 nam.nam$b_esl = nam.nam$b_name + nam.nam$b_type + nam.nam$b_ver;
5039
5040 status = SYS$ENTER (&fab);
5041 if ((status & 1) == 0)
5042 {
5043 errno = EVMSERR;
5044 vaxc$errno = status;
5045 return -1;
5046 }
5047
5048 return 0;
5049}
5050
dfcf069d 5051void
86a5659e
JB
5052croak (badfunc)
5053 char *badfunc;
5054{
5055 printf ("%s not yet implemented\r\n", badfunc);
5056 reset_sys_modes ();
5057 exit (1);
5058}
5059
5060long
5061random ()
5062{
5063 /* Arrange to return a range centered on zero. */
5064 return rand () - (1 << 30);
5065}
5066
dfcf069d 5067void
86a5659e
JB
5068srandom (seed)
5069{
5070 srand (seed);
5071}
5072#endif /* VMS */
5073\f
b97ab886 5074#ifdef AIXHFT
86a5659e
JB
5075
5076/* Called from init_sys_modes. */
dfcf069d 5077void
86a5659e
JB
5078hft_init ()
5079{
5080 int junk;
5081
5082 /* If we're not on an HFT we shouldn't do any of this. We determine
5083 if we are on an HFT by trying to get an HFT error code. If this
5084 call fails, we're not on an HFT. */
5085#ifdef IBMR2AIX
5086 if (ioctl (0, HFQERROR, &junk) < 0)
5087 return;
5088#else /* not IBMR2AIX */
5089 if (ioctl (0, HFQEIO, 0) < 0)
5090 return;
5091#endif /* not IBMR2AIX */
5092
5093 /* On AIX the default hft keyboard mapping uses backspace rather than delete
5094 as the rubout key's ASCII code. Here this is changed. The bug is that
5095 there's no way to determine the old mapping, so in reset_sys_modes
5096 we need to assume that the normal map had been present. Of course, this
5097 code also doesn't help if on a terminal emulator which doesn't understand
c4ea52a6 5098 HFT VTD's. */
86a5659e
JB
5099 {
5100 struct hfbuf buf;
5101 struct hfkeymap keymap;
5102
5103 buf.hf_bufp = (char *)&keymap;
5104 buf.hf_buflen = sizeof (keymap);
5105 keymap.hf_nkeys = 2;
5106 keymap.hfkey[0].hf_kpos = 15;
5107 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
5108#ifdef IBMR2AIX
5109 keymap.hfkey[0].hf_keyidh = '<';
5110#else /* not IBMR2AIX */
5111 keymap.hfkey[0].hf_page = '<';
5112#endif /* not IBMR2AIX */
5113 keymap.hfkey[0].hf_char = 127;
5114 keymap.hfkey[1].hf_kpos = 15;
5115 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
5116#ifdef IBMR2AIX
5117 keymap.hfkey[1].hf_keyidh = '<';
5118#else /* not IBMR2AIX */
5119 keymap.hfkey[1].hf_page = '<';
5120#endif /* not IBMR2AIX */
5121 keymap.hfkey[1].hf_char = 127;
5122 hftctl (0, HFSKBD, &buf);
5123 }
5124 /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
5125 at times. */
5126 line_ins_del_ok = char_ins_del_ok = 0;
5127}
5128
c4ea52a6 5129/* Reset the rubout key to backspace. */
86a5659e 5130
dfcf069d 5131void
86a5659e
JB
5132hft_reset ()
5133{
5134 struct hfbuf buf;
5135 struct hfkeymap keymap;
5136 int junk;
5137
5138#ifdef IBMR2AIX
5139 if (ioctl (0, HFQERROR, &junk) < 0)
5140 return;
5141#else /* not IBMR2AIX */
5142 if (ioctl (0, HFQEIO, 0) < 0)
5143 return;
5144#endif /* not IBMR2AIX */
5145
5146 buf.hf_bufp = (char *)&keymap;
5147 buf.hf_buflen = sizeof (keymap);
5148 keymap.hf_nkeys = 2;
5149 keymap.hfkey[0].hf_kpos = 15;
5150 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
5151#ifdef IBMR2AIX
5152 keymap.hfkey[0].hf_keyidh = '<';
5153#else /* not IBMR2AIX */
5154 keymap.hfkey[0].hf_page = '<';
5155#endif /* not IBMR2AIX */
5156 keymap.hfkey[0].hf_char = 8;
5157 keymap.hfkey[1].hf_kpos = 15;
5158 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
5159#ifdef IBMR2AIX
5160 keymap.hfkey[1].hf_keyidh = '<';
5161#else /* not IBMR2AIX */
5162 keymap.hfkey[1].hf_page = '<';
5163#endif /* not IBMR2AIX */
5164 keymap.hfkey[1].hf_char = 8;
5165 hftctl (0, HFSKBD, &buf);
5166}
5167
b97ab886 5168#endif /* AIXHFT */
c238be24
RS
5169
5170#ifdef USE_DL_STUBS
5171
5172/* These are included on Sunos 4.1 when we do not use shared libraries.
5173 X11 libraries may refer to these functions but (we hope) do not
5174 actually call them. */
5175
5176void *
5177dlopen ()
5178{
5179 return 0;
5180}
5181
5182void *
5183dlsym ()
5184{
5185 return 0;
5186}
5187
5188int
5189dlclose ()
5190{
5191 return -1;
5192}
5193
5194#endif /* USE_DL_STUBS */
51417996
RS
5195\f
5196#ifndef BSTRING
5197
5198#ifndef bzero
5199
5200void
5201bzero (b, length)
5202 register char *b;
5203 register int length;
5204{
5205#ifdef VMS
5206 short zero = 0;
5207 long max_str = 65535;
5208
5209 while (length > max_str) {
5210 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
5211 length -= max_str;
5212 b += max_str;
5213 }
5214 max_str = length;
5215 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
5216#else
5217 while (length-- > 0)
5218 *b++ = 0;
5219#endif /* not VMS */
5220}
5221
5222#endif /* no bzero */
5223#endif /* BSTRING */
5224
c7f93f28 5225#if (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY)
51417996
RS
5226#undef bcopy
5227
5228/* Saying `void' requires a declaration, above, where bcopy is used
5229 and that declaration causes pain for systems where bcopy is a macro. */
5230bcopy (b1, b2, length)
5231 register char *b1;
5232 register char *b2;
5233 register int length;
5234{
5235#ifdef VMS
5236 long max_str = 65535;
5237
5238 while (length > max_str) {
5239 (void) LIB$MOVC3 (&max_str, b1, b2);
5240 length -= max_str;
5241 b1 += max_str;
5242 b2 += max_str;
5243 }
5244 max_str = length;
5245 (void) LIB$MOVC3 (&length, b1, b2);
5246#else
5247 while (length-- > 0)
5248 *b2++ = *b1++;
5249#endif /* not VMS */
5250}
dfcf069d 5251#endif /* (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY) */
51417996 5252
c7f93f28 5253#ifndef BSTRING
51417996
RS
5254#ifndef bcmp
5255int
5256bcmp (b1, b2, length) /* This could be a macro! */
5257 register char *b1;
5258 register char *b2;
5259 register int length;
5260{
5261#ifdef VMS
5262 struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
5263 struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
5264
5265 return STR$COMPARE (&src1, &src2);
5266#else
5267 while (length-- > 0)
5268 if (*b1++ != *b2++)
5269 return 1;
5270
5271 return 0;
5272#endif /* not VMS */
5273}
5274#endif /* no bcmp */
5275#endif /* not BSTRING */
68c45bf0
PE
5276\f
5277#ifndef HAVE_STRSIGNAL
5278char *
5279strsignal (code)
5280 int code;
5281{
5282 char *signame = 0;
5283
5284 if (0 <= code && code < NSIG)
5285 {
5286#ifdef VMS
5287 signame = sys_errlist[code];
5288#else
5289 /* Cast to suppress warning if the table has const char *. */
5290 signame = (char *) sys_siglist[code];
5291#endif
5292 }
c4ea52a6 5293
68c45bf0
PE
5294 return signame;
5295}
5296#endif /* HAVE_STRSIGNAL */
c4ea52a6 5297