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