blob: 5ccac7a95264915b8e6ec42999051a7843891c66 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenef8b6c71999-10-20 08:05:35 +00002/*
Erik Andersen246cc6d2000-03-07 07:41:42 +00003 * Mini ps implementation(s) for busybox
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
Eric Andersenef8b6c71999-10-20 08:05:35 +00007 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00008 * 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
Eric Andersenbdfd0d72001-10-24 05:00:29 +000021 */
22
Erik Andersen246cc6d2000-03-07 07:41:42 +000023#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000024#include <stdlib.h>
Eric Andersenef8b6c71999-10-20 08:05:35 +000025#include <unistd.h>
26#include <dirent.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +000027#include <errno.h>
Eric Andersend23f9ba1999-10-20 19:18:15 +000028#include <fcntl.h>
29#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000030#include <string.h>
Eric Andersen8d79ce82001-07-22 23:00:15 +000031#include <termios.h>
Erik Andersen2ac2fae2000-03-07 23:32:17 +000032#include <sys/ioctl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000033#include "busybox.h"
Erik Andersen2ac2fae2000-03-07 23:32:17 +000034
Mark Whitley59ab0252001-01-23 22:30:04 +000035static const int TERMINAL_WIDTH = 79; /* not 80 in case terminal has linefold bug */
Eric Andersen86ab8a32000-06-02 03:21:42 +000036
37
Eric Andersend23f9ba1999-10-20 19:18:15 +000038
Eric Andersenef8b6c71999-10-20 08:05:35 +000039extern int ps_main(int argc, char **argv)
40{
Eric Andersen44608e92002-10-22 12:21:15 +000041 procps_status_t * p;
42 int i, len;
Eric Andersenbdfd0d72001-10-24 05:00:29 +000043#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersenfad04fd2000-07-14 06:49:52 +000044 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen86ab8a32000-06-02 03:21:42 +000045 int terminal_width = TERMINAL_WIDTH;
Erik Andersen2ac2fae2000-03-07 23:32:17 +000046#else
Eric Andersen86ab8a32000-06-02 03:21:42 +000047#define terminal_width TERMINAL_WIDTH
Erik Andersen2ac2fae2000-03-07 23:32:17 +000048#endif
49
50
Eric Andersenbdfd0d72001-10-24 05:00:29 +000051#ifdef CONFIG_FEATURE_AUTOWIDTH
Erik Andersen2ac2fae2000-03-07 23:32:17 +000052 ioctl(fileno(stdout), TIOCGWINSZ, &win);
53 if (win.ws_col > 0)
54 terminal_width = win.ws_col - 1;
55#endif
56
Eric Andersen13727802002-04-27 06:06:11 +000057 printf(" PID Uid VmSize Stat Command\n");
Eric Andersen44608e92002-10-22 12:21:15 +000058 while ((p = procps_scan(1)) != 0) {
59 char *namecmd = p->cmd;
Erik Andersene49d5ec2000-02-08 19:58:47 +000060
Eric Andersen44608e92002-10-22 12:21:15 +000061 if(p->rss == 0)
62 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
Eric Andersen13727802002-04-27 06:06:11 +000063 else
Eric Andersen44608e92002-10-22 12:21:15 +000064 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);
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 }
Eric Andersen44608e92002-10-22 12:21:15 +000081 free(p->cmd);
Eric Andersend23f9ba1999-10-20 19:18:15 +000082 }
Matt Kraai3e856ce2000-12-01 02:55:13 +000083 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +000084}
Erik Andersen246cc6d2000-03-07 07:41:42 +000085