Start sentences on a new line, fix typo and use .An.
[dragonfly.git] / usr.bin / limits / limits.c
blob6b5f45535d0d5a9883024ee4d05a85ba4ece9b95
1 /*-
2 * Copyright (c) 1997 by
3 * David L. Nugent <davidn@blaze.net.au>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, is permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. This work was done expressly for inclusion into FreeBSD. Other use
16 * is permitted provided this notation is included.
17 * 4. Absolutely no warranty of function or purpose is made by the authors.
18 * 5. Modifications may be freely made to this file providing the above
19 * conditions are met.
21 * Display/change(+runprogram)/eval resource limits.
23 * $FreeBSD: src/usr.bin/limits/limits.c,v 1.7.2.3 2003/05/22 09:26:57 sheldonh Exp $
24 * $DragonFly: src/usr.bin/limits/limits.c,v 1.6 2006/03/06 03:10:51 swildner Exp $
27 #include <err.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/param.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <stdarg.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <pwd.h>
39 #include <login_cap.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
43 enum
45 SH_NONE,
46 SH_SH, /* sh */
47 SH_CSH, /* csh */
48 SH_BASH, /* gnu bash */
49 SH_TCSH, /* tcsh */
50 SH_KSH, /* (pd)ksh */
51 SH_ZSH, /* zsh */
52 SH_RC, /* rc or es */
53 SH_NUMBER
57 /* eval emitter for popular shells.
58 * Why aren't there any standards here? Most shells support either
59 * the csh 'limit' or sh 'ulimit' command, but each varies just
60 * enough that they aren't very compatible from one to the other.
62 static struct {
63 const char * name; /* Name of shell */
64 const char * inf; /* Name used for 'unlimited' resource */
65 const char * cmd; /* Intro text */
66 const char * hard; /* Hard limit text */
67 const char * soft; /* Soft limit text */
68 const char * both; /* Hard+Soft limit text */
69 struct {
70 const char * pfx;
71 const char * sfx;
72 int divisor;
73 } lprm[RLIM_NLIMITS];
74 } shellparm[] =
76 { "", "infinity", "Resource limits%s%s:\n", "-max", "-cur", "",
78 { " cputime%-4s %8s", " secs\n", 1 },
79 { " filesize%-4s %8s", " kb\n", 1024 },
80 { " datasize%-4s %8s", " kb\n", 1024 },
81 { " stacksize%-4s %8s", " kb\n", 1024 },
82 { " coredumpsize%-4s %8s", " kb\n", 1024 },
83 { " memoryuse%-4s %8s", " kb\n", 1024 },
84 { " memorylocked%-4s %8s", " kb\n", 1024 },
85 { " maxprocesses%-4s %8s", "\n", 1 },
86 { " openfiles%-4s %8s", "\n", 1 },
87 { " sbsize%-4s %8s", " bytes\n", 1 },
88 { " vmemoryuse%-4s %8s", " kb\n", 1024 },
89 #ifdef RLIMIT_POSIXLOCKS
90 { " posixlocks%-4s %8s", "\n", 1 },
91 #endif
94 { "sh", "unlimited", "", " -H", " -S", "",
96 { "ulimit%s -t %s", ";\n", 1 },
97 { "ulimit%s -f %s", ";\n", 512 },
98 { "ulimit%s -d %s", ";\n", 1024 },
99 { "ulimit%s -s %s", ";\n", 1024 },
100 { "ulimit%s -c %s", ";\n", 512 },
101 { "ulimit%s -m %s", ";\n", 1024 },
102 { "ulimit%s -l %s", ";\n", 1024 },
103 { "ulimit%s -u %s", ";\n", 1 },
104 { "ulimit%s -n %s", ";\n", 1 },
105 { "ulimit%s -b %s", ";\n", 1 },
106 { "ulimit%s -v %s", ";\n", 1024 },
107 #ifdef RLIMIT_POSIXLOCKS
108 { "ulimit%s -k %s", ";\n", 1 },
109 #endif
112 { "csh", "unlimited", "", " -h", "", NULL,
114 { "limit%s cputime %s", ";\n", 1 },
115 { "limit%s filesize %s", ";\n", 1024 },
116 { "limit%s datasize %s", ";\n", 1024 },
117 { "limit%s stacksize %s", ";\n", 1024 },
118 { "limit%s coredumpsize %s", ";\n", 1024 },
119 { "limit%s memoryuse %s", ";\n", 1024 },
120 { "limit%s memorylocked %s", ";\n", 1024 },
121 { "limit%s maxproc %s", ";\n", 1 },
122 { "limit%s openfiles %s", ";\n", 1 },
123 { "limit%s sbsize %s", ";\n", 1 },
124 { "limit%s vmemoryuse %s", ";\n", 1024 },
125 #ifdef RLIMIT_POSIXLOCKS
126 { "limit%s advlocks %s", ";\n", 1 },
127 #endif
130 { "bash|bash2", "unlimited", "", " -H", " -S", "",
132 { "ulimit%s -t %s", ";\n", 1 },
133 { "ulimit%s -f %s", ";\n", 1024 },
134 { "ulimit%s -d %s", ";\n", 1024 },
135 { "ulimit%s -s %s", ";\n", 1024 },
136 { "ulimit%s -c %s", ";\n", 1024 },
137 { "ulimit%s -m %s", ";\n", 1024 },
138 { "ulimit%s -l %s", ";\n", 1024 },
139 { "ulimit%s -u %s", ";\n", 1 },
140 { "ulimit%s -n %s", ";\n", 1 },
141 { "ulimit%s -b %s", ";\n", 1 },
142 { "ulimit%s -v %s", ";\n", 1024 },
143 #ifdef RLIMIT_POSIXLOCKS
144 { "ulimit%s -k %s", ";\n", 1 },
145 #endif
148 { "tcsh", "unlimited", "", " -h", "", NULL,
150 { "limit%s cputime %s", ";\n", 1 },
151 { "limit%s filesize %s", ";\n", 1024 },
152 { "limit%s datasize %s", ";\n", 1024 },
153 { "limit%s stacksize %s", ";\n", 1024 },
154 { "limit%s coredumpsize %s", ";\n", 1024 },
155 { "limit%s memoryuse %s", ";\n", 1024 },
156 { "limit%s memorylocked %s", ";\n", 1024 },
157 { "limit%s maxproc %s", ";\n", 1 },
158 { "limit%s descriptors %s", ";\n", 1 },
159 { "limit%s sbsize %s", ";\n", 1 },
160 { "limit%s vmemoryuse %s", ";\n", 1024 },
161 #ifdef RLIMIT_POSIXLOCKS
162 { "limit%s advlocks %s", ";\n", 1 },
163 #endif
166 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
168 { "ulimit%s -t %s", ";\n", 1 },
169 { "ulimit%s -f %s", ";\n", 512 },
170 { "ulimit%s -d %s", ";\n", 1024 },
171 { "ulimit%s -s %s", ";\n", 1024 },
172 { "ulimit%s -c %s", ";\n", 512 },
173 { "ulimit%s -m %s", ";\n", 1024 },
174 { "ulimit%s -l %s", ";\n", 1024 },
175 { "ulimit%s -p %s", ";\n", 1 },
176 { "ulimit%s -n %s", ";\n", 1 },
177 { "ulimit%s -b %s", ";\n", 1 },
178 { "ulimit%s -v %s", ";\n", 1024 },
179 #ifdef RLIMIT_POSIXLOCKS
180 { "ulimit%s -k %s", ";\n", 1 },
181 #endif
184 { "zsh", "unlimited", "", " -H", " -S", "",
186 { "ulimit%s -t %s", ";\n", 1 },
187 { "ulimit%s -f %s", ";\n", 512 },
188 { "ulimit%s -d %s", ";\n", 1024 },
189 { "ulimit%s -s %s", ";\n", 1024 },
190 { "ulimit%s -c %s", ";\n", 512 },
191 { "ulimit%s -m %s", ";\n", 1024 },
192 { "ulimit%s -l %s", ";\n", 1024 },
193 { "ulimit%s -u %s", ";\n", 1 },
194 { "ulimit%s -n %s", ";\n", 1 },
195 { "ulimit%s -b %s", ";\n", 1 },
196 { "ulimit%s -v %s", ";\n", 1024 },
197 #ifdef RLIMIT_POSIXLOCKS
198 { "ulimit%s -k %s", ";\n", 1 },
199 #endif
202 { "rc|es", "unlimited", "", " -h", "", NULL,
204 { "limit%s cputime %s", ";\n", 1 },
205 { "limit%s filesize %s", ";\n", 1024 },
206 { "limit%s datasize %s", ";\n", 1024 },
207 { "limit%s stacksize %s", ";\n", 1024 },
208 { "limit%s coredumpsize %s", ";\n", 1024 },
209 { "limit%s memoryuse %s", ";\n", 1024 },
210 { "limit%s lockedmemory %s", ";\n", 1024 },
211 { "limit%s processes %s", ";\n", 1 },
212 { "limit%s descriptors %s", ";\n", 1 },
213 { "limit%s sbsize %s", ";\n", 1 },
214 { "limit%s vmemoryuse %s", ";\n", 1024 },
215 #ifdef RLIMIT_POSIXLOCKS
216 { "limit%s advlocks %s", ";\n", 1 },
217 #endif
220 { NULL, NULL, NULL, NULL, NULL, NULL, {} }
223 static struct {
224 const char * cap;
225 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
226 } resources[RLIM_NLIMITS] = {
227 { "cputime", login_getcaptime },
228 { "filesize", login_getcapsize },
229 { "datasize", login_getcapsize },
230 { "stacksize", login_getcapsize },
231 { "coredumpsize", login_getcapsize },
232 { "memoryuse", login_getcapsize },
233 { "memorylocked", login_getcapsize },
234 { "maxproc", login_getcapnum },
235 { "openfiles", login_getcapnum },
236 { "sbsize", login_getcapsize },
237 { "vmemoryuse", login_getcapsize },
238 { "posixlocks", login_getcapnum },
242 * One letter for each resource levels.
243 * NOTE: There is a dependancy on the corresponding
244 * letter index being equal to the resource number.
245 * If sys/resource.h defines are changed, this needs
246 * to be modified accordingly!
249 #define RCS_STRING "tfdscmlunbvk"
251 static rlim_t resource_num(int which, int ch, const char *str);
252 static void usage(void);
253 static int getshelltype(void);
254 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
255 const char *pfx, const char *sfx, const char *which);
256 extern char **environ;
258 static const char rcs_string[] = RCS_STRING;
261 main(int argc, char *argv[])
263 char *p, *cls = NULL;
264 char *cleanenv[1];
265 struct passwd * pwd = NULL;
266 int rcswhich, shelltype;
267 int i, num_limits = 0;
268 int ch, doeval = 0, doall = 0;
269 login_cap_t * lc = NULL;
270 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
271 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
272 int which_limits[RLIM_NLIMITS];
273 rlim_t set_limits[RLIM_NLIMITS];
274 struct rlimit limits[RLIM_NLIMITS];
276 /* init resource tables */
277 for (i = 0; i < RLIM_NLIMITS; i++) {
278 which_limits[i] = 0; /* Don't set/display any */
279 set_limits[i] = RLIM_INFINITY;
280 /* Get current resource values */
281 if (getrlimit(i, &limits[i]) < 0) {
282 limits[i].rlim_cur = -1;
283 limits[i].rlim_max = -1;
287 optarg = NULL;
288 while ((ch = getopt(argc, argv, ":EeC:U:BSHabc:d:f:k:l:m:n:s:t:u:v:")) != -1) {
289 switch(ch) {
290 case 'a':
291 doall = 1;
292 break;
293 case 'E':
294 environ = cleanenv;
295 cleanenv[0] = NULL;
296 break;
297 case 'e':
298 doeval = 1;
299 break;
300 case 'C':
301 cls = optarg;
302 break;
303 case 'U':
304 if ((pwd = getpwnam(optarg)) == NULL) {
305 if (!isdigit(*optarg) ||
306 (pwd = getpwuid(atoi(optarg))) == NULL) {
307 warnx("invalid user `%s'", optarg);
308 usage();
311 break;
312 case 'H':
313 type = HARD;
314 break;
315 case 'S':
316 type = SOFT;
317 break;
318 case 'B':
319 type = SOFT|HARD;
320 break;
321 default:
322 case ':': /* Without arg */
323 if ((p = strchr(rcs_string, optopt)) != NULL) {
324 rcswhich = p - rcs_string;
327 * Backwards compatibility with earlier kernel which might
328 * support fewer resources.
330 if (rcswhich >= RLIM_NLIMITS) {
331 usage();
332 break;
334 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
335 --optind; /* back one arg, and make arg NULL */
336 optarg = NULL;
338 todo = optarg == NULL ? RCSSEL : RCSSET;
339 if (type == ANY)
340 type = BOTH;
341 which_limits[rcswhich] = optarg ? type : DISPLAYONLY;
342 set_limits[rcswhich] = resource_num(rcswhich, optopt, optarg);
343 num_limits++;
344 break;
346 /* FALLTHRU */
347 case '?':
348 usage();
350 optarg = NULL;
353 /* If user was specified, get class from that */
354 if (pwd != NULL)
355 lc = login_getpwclass(pwd);
356 else if (cls != NULL && *cls != '\0') {
357 lc = login_getclassbyname(cls, NULL);
358 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
359 fprintf(stderr, "login class '%s' non-existent, using %s\n",
360 cls, lc?lc->lc_class:"current settings");
363 /* If we have a login class, update resource table from that */
364 if (lc != NULL) {
365 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
366 char str[40];
367 rlim_t val;
369 /* current value overridden by resourcename or resourcename-cur */
370 sprintf(str, "%s-cur", resources[rcswhich].cap);
371 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
372 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
373 /* maximum value overridden by resourcename or resourcename-max */
374 sprintf(str, "%s-max", resources[rcswhich].cap);
375 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
376 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
380 /* now, let's determine what we wish to do with all this */
382 argv += optind;
384 /* If we're setting limits or doing an eval (ie. we're not just
385 * displaying), then check that hard limits are not lower than
386 * soft limits, and force rasing the hard limit if we need to if
387 * we are raising the soft limit, or lower the soft limit if we
388 * are lowering the hard limit.
390 if ((*argv || doeval) && getuid() == 0) {
392 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
393 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
394 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
395 limits[rcswhich].rlim_max = RLIM_INFINITY;
396 which_limits[rcswhich] |= HARD;
397 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
398 if (which_limits[rcswhich] == SOFT) {
399 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
400 which_limits[rcswhich] |= HARD;
401 } else if (which_limits[rcswhich] == HARD) {
402 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
403 which_limits[rcswhich] |= SOFT;
404 } else {
405 /* else.. if we're specifically setting both to
406 * silly values, then let it error out.
414 /* See if we've overridden anything specific on the command line */
415 if (num_limits && todo == RCSSET) {
416 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
417 if (which_limits[rcswhich] & HARD)
418 limits[rcswhich].rlim_max = set_limits[rcswhich];
419 if (which_limits[rcswhich] & SOFT)
420 limits[rcswhich].rlim_cur = set_limits[rcswhich];
424 /* If *argv is not NULL, then we are being asked to
425 * (perhaps) set environment variables and run a program
427 if (*argv) {
428 if (doeval) {
429 warnx("-e cannot be used with `cmd' option");
430 usage();
433 login_close(lc);
435 /* set leading environment variables, like eval(1) */
436 while (*argv && (p = strchr(*argv, '='))) {
437 if (setenv(*argv++, ++p, 1) == -1)
438 err(1, "setenv: cannot set %s=%s", *argv, p);
441 /* Set limits */
442 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
443 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
444 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
445 err(1, "setrlimit %s", resources[rcswhich].cap);
448 if (*argv == NULL)
449 usage();
451 execvp(*argv, argv);
452 err(1, "%s", *argv);
455 shelltype = doeval ? getshelltype() : SH_NONE;
457 if (type == ANY) /* Default to soft limits */
458 type = SOFT;
460 /* Display limits */
461 printf(shellparm[shelltype].cmd,
462 lc ? " for class " : " (current)",
463 lc ? lc->lc_class : "");
465 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
466 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
467 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
468 which_limits[rcswhich] = type;
469 if (shellparm[shelltype].lprm[rcswhich].pfx) {
470 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
471 print_limit(limits[rcswhich].rlim_max,
472 shellparm[shelltype].lprm[rcswhich].divisor,
473 shellparm[shelltype].inf,
474 shellparm[shelltype].lprm[rcswhich].pfx,
475 shellparm[shelltype].lprm[rcswhich].sfx,
476 shellparm[shelltype].both);
477 } else {
478 if (which_limits[rcswhich] & HARD) {
479 print_limit(limits[rcswhich].rlim_max,
480 shellparm[shelltype].lprm[rcswhich].divisor,
481 shellparm[shelltype].inf,
482 shellparm[shelltype].lprm[rcswhich].pfx,
483 shellparm[shelltype].lprm[rcswhich].sfx,
484 shellparm[shelltype].hard);
486 if (which_limits[rcswhich] & SOFT) {
487 print_limit(limits[rcswhich].rlim_cur,
488 shellparm[shelltype].lprm[rcswhich].divisor,
489 shellparm[shelltype].inf,
490 shellparm[shelltype].lprm[rcswhich].pfx,
491 shellparm[shelltype].lprm[rcswhich].sfx,
492 shellparm[shelltype].soft);
499 login_close(lc);
500 exit(EXIT_SUCCESS);
504 static void
505 usage(void)
507 (void)fprintf(stderr,
508 "usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstuvk [val]] [[name=val ...] cmd]\n");
509 exit(EXIT_FAILURE);
512 static void
513 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
515 char numbr[64];
517 if (limit == RLIM_INFINITY)
518 strcpy(numbr, inf);
519 else
520 sprintf(numbr, "%qd", (quad_t)((limit + divisor/2) / divisor));
521 printf(pfx, which, numbr);
522 printf(sfx, which);
527 static rlim_t
528 resource_num(int which, int ch, const char *str)
530 rlim_t res = RLIM_INFINITY;
532 if (str != NULL &&
533 !(strcasecmp(str, "inf") == 0 ||
534 strcasecmp(str, "infinity") == 0 ||
535 strcasecmp(str, "unlimit") == 0 ||
536 strcasecmp(str, "unlimited") == 0)) {
537 const char * s = str;
538 char *e;
540 switch (which) {
541 case RLIMIT_CPU: /* time values */
542 errno = 0;
543 res = 0;
544 while (*s) {
545 rlim_t tim = strtoq(s, &e, 0);
546 if (e == NULL || e == s || errno)
547 break;
548 switch (*e++) {
549 case 0: /* end of string */
550 e--;
551 default:
552 case 's': case 'S': /* seconds */
553 break;
554 case 'm': case 'M': /* minutes */
555 tim *= 60L;
556 break;
557 case 'h': case 'H': /* hours */
558 tim *= (60L * 60L);
559 break;
560 case 'd': case 'D': /* days */
561 tim *= (60L * 60L * 24L);
562 break;
563 case 'w': case 'W': /* weeks */
564 tim *= (60L * 60L * 24L * 7L);
565 case 'y': case 'Y': /* Years */
566 tim *= (60L * 60L * 24L * 365L);
568 s = e;
569 res += tim;
571 break;
572 case RLIMIT_FSIZE: /* Size values */
573 case RLIMIT_DATA:
574 case RLIMIT_STACK:
575 case RLIMIT_CORE:
576 case RLIMIT_RSS:
577 case RLIMIT_MEMLOCK:
578 case RLIMIT_VMEM:
579 errno = 0;
580 res = 0;
581 while (*s) {
582 rlim_t mult, tim = strtoq(s, &e, 0);
583 if (e == NULL || e == s || errno)
584 break;
585 switch (*e++) {
586 case 0: /* end of string */
587 e--;
588 default:
589 mult = 1;
590 break;
591 case 'b': case 'B': /* 512-byte blocks */
592 mult = 512;
593 break;
594 case 'k': case 'K': /* 1024-byte Kilobytes */
595 mult = 1024;
596 break;
597 case 'm': case 'M': /* 1024-k kbytes */
598 mult = 1024 * 1024;
599 break;
600 case 'g': case 'G': /* 1Gbyte */
601 mult = 1024 * 1024 * 1024;
602 break;
603 case 't': case 'T': /* 1TBte */
604 mult = 1024LL * 1024LL * 1024LL * 1024LL;
605 break;
607 s = e;
608 res += (tim * mult);
610 break;
611 case RLIMIT_NPROC:
612 case RLIMIT_NOFILE:
613 res = strtoq(s, &e, 0);
614 s = e;
615 break;
617 if (*s) {
618 warnx("invalid value -%c `%s'", ch, str);
619 usage();
622 return res;
626 static int
627 getshellbyname(const char * shell)
629 int i;
630 const char * q;
631 const char * p = strrchr(shell, '/');
633 p = p ? p + 1 : shell;
634 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
635 while (*q) {
636 int j = strcspn(q, "|");
638 if (j == 0)
639 break;
640 if (strncmp(p, q, j) == 0)
641 return i;
642 if (*(q += j))
643 ++q;
646 return SH_SH;
651 * Determine the type of shell our parent process is
652 * This is quite tricky, not 100% reliable and probably
653 * not nearly as thorough as it should be. Basically, this
654 * is a "best guess" only, but hopefully will work in
655 * most cases.
658 static int
659 getshelltype(void)
661 pid_t ppid = getppid();
663 if (ppid != 1) {
664 FILE * fp;
665 struct stat st;
666 char procdir[MAXPATHLEN], buf[128];
667 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
668 char * shell = getenv("SHELL");
670 if (shell != NULL && stat(shell, &st) != -1) {
671 struct stat st1;
673 strcpy(procdir+l, "file");
674 /* $SHELL is actual shell? */
675 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
676 return getshellbyname(shell);
678 strcpy(procdir+l, "status");
679 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
680 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
681 fclose(fp);
682 if (p != NULL)
683 return getshellbyname(p);
686 return SH_SH;