Fix long standing bug with old gnu tar files, add a check so tar will
[oweals/busybox.git] / procps / ps.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini ps implementation(s) for busybox
4  *
5  * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen  
6  * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the Free
10  * Software Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  * Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <termios.h>
32 #include <sys/ioctl.h>
33 #include "busybox.h"
34
35 static const int TERMINAL_WIDTH = 79;      /* not 80 in case terminal has linefold bug */
36
37
38
39 extern int ps_main(int argc, char **argv)
40 {
41         procps_status_t * p;
42         int i, len;
43 #ifdef CONFIG_FEATURE_AUTOWIDTH
44         struct winsize win = { 0, 0, 0, 0 };
45         int terminal_width = TERMINAL_WIDTH;
46 #else
47 #define terminal_width  TERMINAL_WIDTH
48 #endif
49
50
51 #ifdef CONFIG_FEATURE_AUTOWIDTH
52                 ioctl(fileno(stdout), TIOCGWINSZ, &win);
53                 if (win.ws_col > 0)
54                         terminal_width = win.ws_col - 1;
55 #endif
56
57         printf("  PID  Uid     VmSize Stat Command\n");
58         while ((p = procps_scan(1)) != 0) {
59                 char *namecmd = p->cmd;
60
61                 if(p->rss == 0)
62                         len = printf("%5d %-8s        %s ", p->pid, p->user, p->state);
63                 else
64                         len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
65                 i = terminal_width-len;
66
67                 if(namecmd != 0 && namecmd[0] != 0) {
68                         if(i < 0)
69                 i = 0;
70                         if(strlen(namecmd) > i)
71                                 namecmd[i] = 0;
72                         printf("%s\n", namecmd);
73                 } else {
74                         namecmd = p->short_cmd;
75                         if(i < 2)
76                                 i = 2;
77                         if(strlen(namecmd) > (i-2))
78                                 namecmd[i-2] = 0;
79                         printf("[%s]\n", namecmd);
80                 }
81                 free(p->cmd);
82         }
83         return EXIT_SUCCESS;
84 }
85