Release v.5.0.0
[screen.git] / src / misc.c
blob26909237b0ad52eeda704861338fcb58c889859b
1 /* Copyright (c) 2008, 2009
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Micah Cowan (micah@cowan.name)
5 * Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9 * Copyright (c) 1987 Oliver Laumann
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, see
23 * https://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
29 #include "config.h"
31 #include "misc.h"
33 #include <poll.h>
34 #include <sys/types.h>
35 #include <sys/stat.h> /* mkdir() declaration */
36 #include <signal.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <stdbool.h>
41 #include "screen.h"
43 char *SaveStr(const char *str)
45 char *cp = strdup(str);
47 if (cp == NULL)
48 Panic(0, "%s", strnomem);
50 return cp;
53 char *SaveStrn(const char *str, size_t n)
55 char *cp = strndup(str, n + 1);
57 if (cp == NULL)
58 Panic(0, "%s", strnomem);
60 return cp;
63 void centerline(char *str, int y)
65 int l, n;
67 n = strlen(str);
68 if (n > flayer->l_width - 1)
69 n = flayer->l_width - 1;
70 l = (flayer->l_width - 1 - n) / 2;
71 LPutStr(flayer, str, n, &mchar_blank, l, y);
74 void leftline(char *str, int y, struct mchar *rend)
76 int l, n;
77 struct mchar mchar_dol;
79 mchar_dol = mchar_blank;
80 mchar_dol.image = '$';
82 l = n = strlen(str);
83 if (n > flayer->l_width - 1)
84 n = flayer->l_width - 1;
85 LPutStr(flayer, str, n, rend ? rend : &mchar_blank, 0, y);
86 if (n != l)
87 LPutChar(flayer, &mchar_dol, n, y);
90 char *Filename(char *s)
92 char *p = s;
94 if (p)
95 while (*p)
96 if (*p++ == '/')
97 s = p;
98 return s;
101 char *stripdev(char *name)
103 if (name == NULL)
104 return NULL;
105 if (strncmp(name, "/dev/", 5) == 0)
106 return name + 5;
107 return name;
111 * Signal handling
114 void (*xsignal(int sig, void (*func) (int))) (int) {
115 struct sigaction osa, sa;
116 sa.sa_handler = func;
117 (void)sigemptyset(&sa.sa_mask);
118 #ifdef SA_RESTART
119 sa.sa_flags = (sig == SIGCHLD ? SA_RESTART : 0);
120 #else
121 sa.sa_flags = 0;
122 #endif
123 if (sigaction(sig, &sa, &osa))
124 return (void (*)(int))-1;
125 return osa.sa_handler;
129 * uid/gid handling
132 void xseteuid(int euid)
134 if (seteuid(euid) == 0)
135 return;
136 if (seteuid(0) || seteuid(euid))
137 Panic(errno, "seteuid");
140 void xsetegid(int egid)
142 if (setegid(egid))
143 Panic(errno, "setegid");
146 void Kill(pid_t pid, int sig)
148 if (pid < 2)
149 return;
150 (void)kill(pid, sig);
153 void closeallfiles(int except)
155 struct pollfd pfd[1024];
156 int maxfd, i, fd, ret, z;
158 i = 3; /* skip stdin, stdout and stderr */
159 maxfd = getdtablesize();
161 while (i < maxfd) {
162 memset(pfd, 0, sizeof(pfd));
164 z = 0;
165 for (fd = i; fd < maxfd && fd < i + 1024; fd++)
166 pfd[z++].fd = fd;
168 ret = poll(pfd, fd - i, 0);
169 if (ret < 0)
170 Panic(errno, "poll");
172 z = 0;
173 for (fd = i; fd < maxfd && fd < i + 1024; fd++)
174 if (!(pfd[z++].revents & POLLNVAL) && fd != except)
175 close(fd);
177 i = fd;
182 * Security - switch to real uid
185 static int UserSTAT;
187 int UserContext(void)
189 xseteuid(real_uid);
190 xsetegid(real_gid);
191 return 1;
194 void UserReturn(int val)
196 xseteuid(eff_uid);
197 xsetegid(eff_gid);
198 UserSTAT = val;
201 int UserStatus(void)
203 return UserSTAT;
206 int AddXChar(char *buf, int ch)
208 char *p = buf;
210 if (ch < ' ' || ch == 0x7f) {
211 *p++ = '^';
212 *p++ = ch ^ 0x40;
213 } else if (ch >= 0x80) {
214 *p++ = '\\';
215 *p++ = (ch >> 6 & 7) + '0';
216 *p++ = (ch >> 3 & 7) + '0';
217 *p++ = (ch >> 0 & 7) + '0';
218 } else
219 *p++ = ch;
220 return p - buf;
223 int AddXChars(char *buf, int len, char *str)
225 char *p;
227 if (str == NULL) {
228 *buf = 0;
229 return 0;
231 len -= 4; /* longest sequence produced by AddXChar() */
232 for (p = buf; p < buf + len && *str; str++) {
233 if (*str == ' ')
234 *p++ = *str;
235 else
236 p += AddXChar(p, *str);
238 *p = 0;
239 return p - buf;