2 * Pager: Routines to create a "more" running out of a particular file
5 * Copyright 1987, 1988 by MIT Student Information Processing Board
7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation for any purpose is hereby granted, provided that
9 * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. M.I.T. and the
12 * M.I.T. S.I.P.B. make no representations about the suitability of
13 * this software for any purpose. It is provided "as is" without
14 * express or implied warranty.
26 #include "ss_internal.h"
28 #include <sys/types.h>
31 #ifdef HAVE_SYS_PRCTL_H
32 #include <sys/prctl.h>
34 #define PR_GET_DUMPABLE 3
36 #if (!defined(HAVE_PRCTL) && defined(linux))
37 #include <sys/syscall.h>
40 static char MORE[] = "more";
41 extern char *_ss_pager_name;
42 extern char *getenv PROTOTYPE((const char *));
44 char *ss_safe_getenv(const char *arg)
46 if ((getuid() != geteuid()) || (getgid() != getegid()))
49 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
52 #if (defined(linux) && defined(SYS_prctl))
53 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
58 #ifdef HAVE___SECURE_GETENV
59 return __secure_getenv(arg);
66 * this needs a *lot* of work....
69 * handle SIGINT sensibly
70 * allow finer control -- put-page-break-here
74 int ss_pager_create(void)
78 if (pipe(filedes) != 0)
86 * Child; dup read half to 0, close all but 0, 1, and 2
88 if (dup2(filedes[0], 0) == -1)
93 * Parent: close "read" side of pipe, return
96 (void) close(filedes[0]);
100 #else /* don't fork */
101 int ss_pager_create()
104 fd = open("/dev/tty", O_WRONLY, 0);
114 for (i = 3; i < 32; i++)
116 (void) signal(SIGINT, SIG_DFL);
117 sigprocmask(SIG_BLOCK, 0, &mask);
118 sigdelset(&mask, SIGINT);
119 sigprocmask(SIG_SETMASK, &mask, 0);
120 if (_ss_pager_name == (char *)NULL) {
121 if ((_ss_pager_name = ss_safe_getenv("PAGER")) == (char *)NULL)
122 _ss_pager_name = MORE;
124 (void) execlp(_ss_pager_name, _ss_pager_name, (char *) NULL);
126 /* minimal recovery if pager program isn't found */
129 while ((n = read(0, buf, 80)) > 0)