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)
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 ****************************************************************
34 #include <sys/types.h>
35 #include <sys/stat.h> /* mkdir() declaration */
43 char *SaveStr(const char *str
)
45 char *cp
= strdup(str
);
48 Panic(0, "%s", strnomem
);
53 char *SaveStrn(const char *str
, size_t n
)
55 char *cp
= strndup(str
, n
+ 1);
58 Panic(0, "%s", strnomem
);
63 void centerline(char *str
, int y
)
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
)
77 struct mchar mchar_dol
;
79 mchar_dol
= mchar_blank
;
80 mchar_dol
.image
= '$';
83 if (n
> flayer
->l_width
- 1)
84 n
= flayer
->l_width
- 1;
85 LPutStr(flayer
, str
, n
, rend
? rend
: &mchar_blank
, 0, y
);
87 LPutChar(flayer
, &mchar_dol
, n
, y
);
90 char *Filename(char *s
)
101 char *stripdev(char *name
)
105 if (strncmp(name
, "/dev/", 5) == 0)
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
);
119 sa
.sa_flags
= (sig
== SIGCHLD
? SA_RESTART
: 0);
123 if (sigaction(sig
, &sa
, &osa
))
124 return (void (*)(int))-1;
125 return osa
.sa_handler
;
132 void xseteuid(int euid
)
134 if (seteuid(euid
) == 0)
136 if (seteuid(0) || seteuid(euid
))
137 Panic(errno
, "seteuid");
140 void xsetegid(int egid
)
143 Panic(errno
, "setegid");
146 void Kill(pid_t pid
, int sig
)
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();
162 memset(pfd
, 0, sizeof(pfd
));
165 for (fd
= i
; fd
< maxfd
&& fd
< i
+ 1024; fd
++)
168 ret
= poll(pfd
, fd
- i
, 0);
170 Panic(errno
, "poll");
173 for (fd
= i
; fd
< maxfd
&& fd
< i
+ 1024; fd
++)
174 if (!(pfd
[z
++].revents
& POLLNVAL
) && fd
!= except
)
182 * Security - switch to real uid
187 int UserContext(void)
194 void UserReturn(int val
)
206 int AddXChar(char *buf
, int ch
)
210 if (ch
< ' ' || ch
== 0x7f) {
213 } else if (ch
>= 0x80) {
215 *p
++ = (ch
>> 6 & 7) + '0';
216 *p
++ = (ch
>> 3 & 7) + '0';
217 *p
++ = (ch
>> 0 & 7) + '0';
223 int AddXChars(char *buf
, int len
, char *str
)
231 len
-= 4; /* longest sequence produced by AddXChar() */
232 for (p
= buf
; p
< buf
+ len
&& *str
; str
++) {
236 p
+= AddXChar(p
, *str
);