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