1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
|
/* Copyright (c) 1993
* Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
* Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
* Copyright (c) 1987 Oliver Laumann
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING); if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
****************************************************************
*/
#include "rcs.h"
RCS_ID("$Id$ FAU")
#include <sys/types.h>
#include <sys/stat.h> /* mkdir() declaration */
#include <signal.h>
#include "config.h"
#include "screen.h"
#include "extern.h"
#ifdef SVR4
# include <sys/resource.h>
#endif
extern struct display *display;
extern int eff_uid, real_uid;
extern int eff_gid, real_gid;
extern struct mline mline_old;
extern char *null, *blank;
char *
SaveStr(str)
register const char *str;
{
register char *cp;
if ((cp = malloc(strlen(str) + 1)) == NULL)
Panic(0, strnomem);
else
strcpy(cp, str);
return cp;
}
/* cheap strstr replacement */
char *
InStr(str, pat)
char *str;
const char *pat;
{
int npat = strlen(pat);
for (;*str; str++)
if (!strncmp(str, pat, npat))
return str;
return 0;
}
#ifndef HAVE_STRERROR
char *
strerror(err)
int err;
{
extern int sys_nerr;
extern char *sys_errlist[];
static char er[20];
if (err > 0 && err < sys_nerr)
return sys_errlist[err];
sprintf(er, "Error %d", err);
return er;
}
#endif
void
centerline(str)
char *str;
{
int l, n;
ASSERT(display);
n = strlen(str);
if (n > D_width - 1)
n = D_width - 1;
l = (D_width - 1 - n) / 2;
if (l > 0)
AddStrn("", l);
AddStrn(str, n);
AddStr("\r\n");
}
char *
Filename(s)
char *s;
{
register char *p = s;
if (p)
while (*p)
if (*p++ == '/')
s = p;
return s;
}
char *
stripdev(nam)
char *nam;
{
#ifdef apollo
char *p;
if (nam == NULL)
return NULL;
if (p = strstr(nam,"/dev/"))
return p + 5;
#else /* apollo */
if (nam == NULL)
return NULL;
# ifdef SVR4
/* unixware has /dev/pts012 as synonym for /dev/pts/12 */
if (!strncmp(nam, "/dev/pts", 8) && nam[8] >= '0' && nam[8] <= '9')
{
static char b[13];
sprintf(b, "pts/%d", atoi(nam + 8));
return b;
}
# endif /* SVR4 */
if (strncmp(nam, "/dev/", 5) == 0)
return nam + 5;
#endif /* apollo */
return nam;
}
/*
* Signal handling
*/
#ifdef POSIX
sigret_t (*xsignal(sig, func)) __P(SIGPROTOARG)
int sig;
sigret_t (*func) __P(SIGPROTOARG);
{
struct sigaction osa, sa;
sa.sa_handler = func;
(void)sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(sig, &sa, &osa))
return (sigret_t (*)__P(SIGPROTOARG))-1;
return osa.sa_handler;
}
#else
# ifdef hpux
/*
* hpux has berkeley signal semantics if we use sigvector,
* but not, if we use signal, so we define our own signal() routine.
*/
void (*xsignal(sig, func)) __P(SIGPROTOARG)
int sig;
void (*func) __P(SIGPROTOARG);
{
struct sigvec osv, sv;
sv.sv_handler = func;
sv.sv_mask = sigmask(sig);
sv.sv_flags = SV_BSDSIG;
if (sigvector(sig, &sv, &osv) < 0)
return (void (*)__P(SIGPROTOARG))(BADSIG);
return osv.sv_handler;
}
# endif /* hpux */
#endif /* POSIX */
/*
* uid/gid handling
*/
#ifdef HAVE_SETEUID
void
xseteuid(euid)
int euid;
{
if (seteuid(euid) == 0)
return;
seteuid(0);
if (seteuid(euid))
Panic(errno, "seteuid");
}
void
xsetegid(egid)
int egid;
{
if (setegid(egid))
Panic(errno, "setegid");
}
#else /* HAVE_SETEUID */
# ifdef HAVE_SETREUID
void
xseteuid(euid)
int euid;
{
int oeuid;
oeuid = geteuid();
if (oeuid == euid)
return;
if (getuid() != euid)
oeuid = getuid();
if (setreuid(oeuid, euid))
Panic(errno, "setreuid");
}
void
xsetegid(egid)
int egid;
{
int oegid;
oegid = getegid();
if (oegid == egid)
return;
if (getgid() != egid)
oegid = getgid();
if (setregid(oegid, egid))
Panic(errno, "setregid");
}
# endif /* HAVE_SETREUID */
#endif /* HAVE_SETEUID */
#ifdef NEED_OWN_BCOPY
void
xbcopy(s1, s2, len)
register char *s1, *s2;
register int len;
{
if (s1 < s2 && s2 < s1 + len)
{
s1 += len;
s2 += len;
while (len-- > 0)
*--s2 = *--s1;
}
else
while (len-- > 0)
*s2++ = *s1++;
}
#endif /* NEED_OWN_BCOPY */
void
bclear(p, n)
char *p;
int n;
{
bcopy(blank, p, n);
}
void
Kill(pid, sig)
int pid, sig;
{
if (pid < 2)
return;
(void) kill(pid, sig);
}
void
closeallfiles(except)
int except;
{
int f;
#ifdef SVR4
struct rlimit rl;
if ((getrlimit(RLIMIT_NOFILE, &rl) == 0) && rl.rlim_max != RLIM_INFINITY)
f = rl.rlim_max;
else
#endif /* SVR4 */
#if defined(SYSV) && defined(NOFILE) && !defined(ISC)
f = NOFILE;
#else /* SYSV && !ISC */
f = getdtablesize();
#endif /* SYSV && !ISC */
while (--f > 2)
if (f != except)
close(f);
}
/*
* Security - switch to real uid
*/
#ifndef USE_SETEUID
static int UserPID;
static sigret_t (*Usersigcld)__P(SIGPROTOARG);
#endif
static int UserSTAT;
int
UserContext()
{
#ifndef USE_SETEUID
if (eff_uid == real_uid && eff_gid == real_gid)
return 1;
Usersigcld = signal(SIGCHLD, SIG_DFL);
debug("UserContext: forking.\n");
switch (UserPID = fork())
{
case -1:
Msg(errno, "fork");
return -1;
case 0:
signal(SIGHUP, SIG_DFL);
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
# ifdef BSDJOBS
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
# endif
setuid(real_uid);
setgid(real_gid);
return 1;
default:
return 0;
}
#else
xseteuid(real_uid);
xsetegid(real_gid);
return 1;
#endif
}
void
UserReturn(val)
int val;
{
#ifndef USE_SETEUID
if (eff_uid == real_uid && eff_gid == real_gid)
UserSTAT = val;
else
_exit(val);
#else
xseteuid(eff_uid);
xsetegid(eff_gid);
UserSTAT = val;
#endif
}
int
UserStatus()
{
#ifndef USE_SETEUID
int i;
# ifdef BSDWAIT
union wait wstat;
# else
int wstat;
# endif
if (eff_uid == real_uid && eff_gid == real_gid)
return UserSTAT;
if (UserPID < 0)
return -1;
while ((errno = 0, i = wait(&wstat)) != UserPID)
if (i < 0 && errno != EINTR)
break;
(void) signal(SIGCHLD, Usersigcld);
if (i == -1)
return -1;
return WEXITSTATUS(wstat);
#else
return UserSTAT;
#endif
}
#ifdef NEED_RENAME
int
rename (old, new)
char *old;
char *new;
{
if (link(old, new) < 0)
return -1;
return unlink(old);
}
#endif
int
AddXChar(buf, ch)
char *buf;
int ch;
{
char *p = buf;
if (ch < ' ' || ch == 0x7f)
{
*p++ = '^';
*p++ = ch ^ 0x40;
}
else if (ch >= 0x80)
{
*p++ = '\\';
*p++ = (ch >> 6 & 7) + '0';
*p++ = (ch >> 3 & 7) + '0';
*p++ = (ch >> 0 & 7) + '0';
}
else
*p++ = ch;
return p - buf;
}
int
AddXChars(buf, len, str)
char *buf, *str;
int len;
{
char *p;
if (str == 0)
{
*buf = 0;
return 0;
}
len -= 4; /* longest sequence produced by AddXChar() */
for (p = buf; p < buf + len && *str; str++)
{
if (*str == ' ')
*p++ = *str;
else
p += AddXChar(p, *str);
}
*p = 0;
return p - buf;
}
#ifdef DEBUG
void
opendebug(new, shout)
int new, shout;
{
char buf[256];
#ifdef _MODE_T
mode_t oumask = umask(0);
#else
int oumask = umask(0);
#endif
ASSERT(!dfp);
(void) mkdir(DEBUGDIR, 0777);
sprintf(buf, shout ? "%s/SCREEN.%d" : "%s/screen.%d", DEBUGDIR, getpid());
if (!(dfp = fopen(buf, new ? "w" : "a")))
dfp = stderr;
else
(void)chmod(buf, 0666);
(void)umask(oumask);
debug("opendebug: done.\n");
}
#endif /* DEBUG */
/*
* "$HOST blafoo" -> "localhost blafoo"
* "${HOST}blafoo" -> "localhostblafoo"
* "\$HOST blafoo" -> "$HOST blafoo"
* "\\$HOST blafoo" -> "\localhost blafoo"
* "'$HOST ${HOST}'" -> "'$HOST ${HOST}'"
* "'\$HOST'" -> "'\$HOST'"
* "\'$HOST' $HOST" -> "'localhost' $HOST"
*
* "$:termcapname:" -> "termcapvalue"
* "$:terminfoname:" -> "termcapvalue"
*
* "\101" -> "A"
* "^a" -> "\001"
*
* display == NULL is valid here!
*/
char *
expand_vars(ss, d)
char *ss;
struct display *d;
{
static char ebuf[2048];
int esize = sizeof(ebuf) - 1, vtype, quofl = 0;
register char *e = ebuf;
register char *s = ss;
register char *v;
char xbuf[11];
int i;
while (s && *s != '\0' && *s != '\n' && esize > 0)
{
if (*s == '\'')
quofl ^= 1;
if (*s == '$' && !quofl)
{
char *p, c;
p = ++s;
switch (*s)
{
case '{':
p = ++s;
while (*p != '}')
if (*p++ == '\0')
return ss;
vtype = 0; /* env var */
break;
case ':':
p = ++s;
while (*p != ':')
if (*p++ == '\0')
return ss;
vtype = 1; /* termcap string */
break;
default:
while (*p != ' ' && *p != '\0' && *p != '\n')
p++;
vtype = 0; /* env var */
}
c = *p;
debug1("exp: c='%c'\n", c);
*p = '\0';
if (vtype == 0)
{
v = xbuf;
if (strcmp(s, "TERM") == 0)
v = d ? d->d_termname : "unknown";
else if (strcmp(s, "COLUMNS") == 0)
sprintf(xbuf, "%d", d ? d->d_width : -1);
else if (strcmp(s, "LINES") == 0)
sprintf(xbuf, "%d", d ? d->d_height : -1);
else
v = getenv(s);
}
else
v = gettermcapstring(s);
if (v)
{
debug2("exp: $'%s'='%s'\n", s, v);
while (*v && esize-- > 0)
*e++ = *v++;
}
else
debug1("exp: '%s' not env\n", s); /* '{'-: */
if ((*p = c) == '}' || c == ':')
p++;
s = p;
}
else if (*s == '^' && !quofl)
{
s++;
i = *s++;
if (i == '?')
i = '\177';
else
i &= 0x1f;
*e++ = i;
esize--;
}
else
{
/*
* \$, \\$, \\, \\\, \012 are reduced here,
* other sequences starting whith \ are passed through.
*/
if (s[0] == '\\' && !quofl)
{
if (s[1] >= '0' && s[1] <= '7')
{
s++;
i = *s - '0';
s++;
if (*s >= '0' && *s <= '7')
{
i = i * 8 + *s - '0';
s++;
if (*s >= '0' && *s <= '7')
{
i = i * 8 + *s - '0';
s++;
}
}
debug2("expandvars: octal coded character %o (%d)\n", i, i);
*e++ = i;
esize--;
continue;
}
else
{
if (s[1] == '$' ||
(s[1] == '\\' && s[2] == '$') ||
s[1] == '\'' ||
(s[1] == '\\' && s[2] == '\'') ||
s[1] == '^' ||
(s[1] == '\\' && s[2] == '^'))
s++;
}
}
*e++ = *s++;
esize--;
}
}
if (esize <= 0)
Msg(0, "expand_vars: buffer overflow\n");
*e = '\0';
return ebuf;
}
#ifdef TERMINFO
/*
* This is a replacement for the buggy _delay function from the termcap
* emulation of libcurses, which ignores ospeed.
*/
int
_delay(delay, outc)
register int delay;
int (*outc) __P((int));
{
int pad;
extern short ospeed;
static short osp2pad[] = {
0,2000,1333,909,743,666,500,333,166,83,55,41,20,10,5,2,1,1
};
if (ospeed <= 0 || ospeed >= sizeof(osp2pad)/sizeof(*osp2pad))
return 0;
pad =osp2pad[ospeed];
delay = (delay + pad / 2) / pad;
while (delay-- > 0)
(*outc)(0);
return 0;
}
# ifdef linux
/* stupid stupid linux ncurses! It won't to padding with
* zeros but sleeps instead. This breaks CalcCost, of course.
* Also, the ncurses wait functions use a global variable
* to store the current outc function. Oh well...
*/
int (*save_outc) __P((int));
# undef tputs
void
xtputs(str, affcnt, outc)
char *str;
int affcnt;
int (*outc) __P((int));
{
extern int tputs __P((const char *, int, int (*)(int)));
save_outc = outc;
tputs(str, affcnt, outc);
}
int
_nc_timed_wait(mode, ms, tlp)
int mode, ms, *tlp;
{
_delay(ms * 10, save_outc);
return 0;
}
# endif /* linux */
#endif
#ifndef USEVARARGS
# define xva_arg(s, t, tn) (*(t *)(s += xsnoff(tn, 0, 0), s - xsnoff(tn, 0, 0)))
# define xva_list char *
static int
xsnoff(a, b, c)
int a;
char *b;
int c;
{
return a ? (char *)&c - (char *)&b : (char *)&b - (char *)&a;
}
int
xsnprintf(s, n, fmt, p1, p2, p3, p4, p5, p6)
char *s;
int n;
char *fmt;
unsigned long p1, p2, p3, p4, p5, p6;
{
int xvsnprintf __P((char *, int, char *, xva_list));
return xvsnprintf(s, n, fmt, (char *)&fmt + xsnoff(1, 0, 0));
}
#else
# define xva_arg(s, t, tn) va_arg(s, t)
# define xva_list va_list
#endif
#if !defined(USEVARARGS) || !defined(HAVE_VSNPRINTF)
int
xvsnprintf(s, n, fmt, stack)
char *s;
int n;
char *fmt;
xva_list stack;
{
char *f, *sf = 0;
int i, on, argl = 0;
char myf[10], buf[20];
char *arg, *myfp;
on = n;
f = fmt;
arg = 0;
while(arg || (sf = index(f, '%')) || (sf = f + strlen(f)))
{
if (arg == 0)
{
arg = f;
argl = sf - f;
}
if (argl)
{
i = argl > n - 1 ? n - 1 : argl;
strncpy(s, arg, i);
s += i;
n -= i;
if (i < argl)
{
*s = 0;
return on;
}
}
arg = 0;
if (sf == 0)
continue;
f = sf;
sf = 0;
if (!*f)
break;
myfp = myf;
*myfp++ = *f++;
while (((*f >= '0' && *f <='9') || *f == '#') && myfp - myf < 8)
*myfp++ = *f++;
*myfp++ = *f;
*myfp = 0;
if (!*f++)
break;
switch(f[-1])
{
case '%':
arg = "%";
break;
case 'c':
case 'o':
case 'd':
case 'x':
i = xva_arg(stack, int, 0);
sprintf(buf, myf, i);
arg = buf;
break;
case 's':
arg = xva_arg(stack, char *, 1);
if (arg == 0)
arg = "NULL";
break;
default:
arg = "";
break;
}
argl = strlen(arg);
}
*s = 0;
return on - n;
}
#endif
|