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.
18 #if HAVE_SECURE_GETENV
30 #include "ss_internal.h"
32 #include <sys/types.h>
35 #ifdef HAVE_SYS_PRCTL_H
36 #include <sys/prctl.h>
38 #define PR_GET_DUMPABLE 3
40 #if (!defined(HAVE_PRCTL) && defined(linux))
41 #include <sys/syscall.h>
44 static char MORE[] = "more";
45 extern char *getenv PROTOTYPE((const char *));
47 char *ss_safe_getenv(const char *arg)
49 if ((getuid() != geteuid()) || (getgid() != getegid()))
52 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
55 #if (defined(linux) && defined(SYS_prctl))
56 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
61 #if defined(HAVE_SECURE_GETENV)
62 return secure_getenv(arg);
63 #elif defined(HAVE___SECURE_GETENV)
64 return __secure_getenv(arg);
71 * this needs a *lot* of work....
74 * handle SIGINT sensibly
75 * allow finer control -- put-page-break-here
79 int ss_pager_create(void)
83 if (pipe(filedes) != 0)
91 * Child; dup read half to 0, close all but 0, 1, and 2
93 if (dup2(filedes[0], 0) == -1)
98 * Parent: close "read" side of pipe, return
101 (void) close(filedes[0]);
105 #else /* don't fork */
106 int ss_pager_create()
109 fd = open("/dev/tty", O_WRONLY, 0);
114 static int write_all(int fd, char *buf, size_t count)
120 ret = write(fd, buf, count);
122 if ((errno == EAGAIN) || (errno == EINTR))
133 void ss_page_stdin(void)
138 for (i = 3; i < 32; i++)
140 (void) signal(SIGINT, SIG_DFL);
141 sigprocmask(SIG_BLOCK, 0, &mask);
142 sigdelset(&mask, SIGINT);
143 sigprocmask(SIG_SETMASK, &mask, 0);
144 if (_ss_pager_name == (char *)NULL) {
145 if ((_ss_pager_name = ss_safe_getenv("PAGER")) == (char *)NULL)
146 _ss_pager_name = MORE;
148 (void) execlp(_ss_pager_name, _ss_pager_name, (char *) NULL);
150 /* minimal recovery if pager program isn't found */
153 while ((n = read(0, buf, 80)) > 0)
154 write_all(1, buf, n);