Add -p flag, contributed by Rob Hoelz

This commit is contained in:
Hisham Muhammad 2012-08-10 21:54:41 +00:00
parent 6c71b7ed2d
commit e6c6d7fbf7
5 changed files with 50 additions and 13 deletions

View File

@ -250,7 +250,11 @@ void Panel_draw(Panel* this, bool focus) {
int scrollH = this->scrollH;
int y = this->y; int x = this->x;
int first = this->scrollV;
int last = MIN(itemCount, this->scrollV + MIN(itemCount, this->h));
if (itemCount > this->h && first > itemCount - this->h) {
first = itemCount - this->h;
this->scrollV = first;
}
int last = MIN(itemCount, first + MIN(itemCount, this->h));
if (this->selected < first) {
first = this->selected;
this->scrollV = first;

View File

@ -114,6 +114,7 @@ typedef struct ProcessList_ {
uid_t userId;
bool filtering;
const char* incFilter;
Hashtable* pidWhiteList;
int cpuCount;
int totalTasks;
@ -180,12 +181,13 @@ const char *ProcessList_treeStrUtf8[TREE_STR_COUNT] = {
"\xe2\x94\x80", // TREE_STR_SHUT ─
};
ProcessList* ProcessList_new(UsersTable* usersTable) {
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList) {
ProcessList* this;
this = calloc(sizeof(ProcessList), 1);
this->processes = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE, Process_compare);
this->processTable = Hashtable_new(140, false);
this->usersTable = usersTable;
this->pidWhiteList = pidWhiteList;
/* tree-view auxiliary buffers */
this->processes2 = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE, Process_compare);
@ -599,6 +601,7 @@ static void ProcessList_readVServerData(Process* process, const char* dirname, c
static bool ProcessList_readCmdlineFile(Process* process, const char* dirname, const char* name) {
if (Process_isKernelThread(process))
return true;
char filename[MAX_NAME+1];
snprintf(filename, MAX_NAME, "%s/%s/cmdline", dirname, name);
FILE* file = fopen(filename, "r");
@ -617,6 +620,7 @@ static bool ProcessList_readCmdlineFile(Process* process, const char* dirname, c
fclose(file);
free(process->comm);
process->comm = strdup(command);
return true;
}
@ -928,7 +932,8 @@ void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, bool
if ( (!p->show)
|| (userOnly && (p->st_uid != userId))
|| (filtering && !(String_contains_i(p->comm, incFilter))) )
|| (filtering && !(String_contains_i(p->comm, incFilter)))
|| (this->pidWhiteList && !Hashtable_get(this->pidWhiteList, p->pid)) )
hidden = true;
if (!hidden) {

View File

@ -97,6 +97,7 @@ typedef struct ProcessList_ {
uid_t userId;
bool filtering;
const char* incFilter;
Hashtable* pidWhiteList;
int cpuCount;
int totalTasks;
@ -144,7 +145,7 @@ extern const char *ProcessList_treeStrAscii[TREE_STR_COUNT];
extern const char *ProcessList_treeStrUtf8[TREE_STR_COUNT];
ProcessList* ProcessList_new(UsersTable* usersTable);
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList);
void ProcessList_delete(ProcessList* this);

View File

@ -29,12 +29,15 @@ Start htop in monochrome mode
\fB\-h \-\-help
Display a help message and exit
.TP
\fB\-u \-\-user=USERNAME\fR
Show only the processes of a given user
\fB\-p \-\-pid=PID,PID...\fR
Show only the given PIDs
.TP
\fB\-s \-\-sort\-key COLUMN\fR
Sort by this column (use \-\-sort\-key help for a column list)
.TP
\fB\-u \-\-user=USERNAME\fR
Show only the processes of a given user
.TP
\fB\-v \-\-version
Output version information and exit
.PP

28
htop.c
View File

@ -56,6 +56,7 @@ static void printHelpFlag() {
"-h --help Print this help screen\n"
"-s --sort-key=COLUMN Sort by COLUMN (try --sort-key=help for a list)\n"
"-u --user=USERNAME Show only processes of a given user\n"
"-p --pid=PID,[,PID,PID...] Show only the given PIDs\n"
"-v --version Print version info\n"
"\n"
"Long options may be passed with a single dash.\n\n"
@ -269,6 +270,9 @@ int main(int argc, char** argv) {
uid_t userId = 0;
int usecolors = 1;
TreeType treeType = TREE_TYPE_AUTO;
char *argCopy;
char *pid;
Hashtable *pidWhiteList = NULL;
int opt, opti=0;
static struct option long_opts[] =
@ -280,6 +284,7 @@ int main(int argc, char** argv) {
{"user", required_argument, 0, 'u'},
{"no-color", no_argument, 0, 'C'},
{"no-colour",no_argument, 0, 'C'},
{"pid", required_argument, 0, 'p'},
{0,0,0,0}
};
int sortKey = 0;
@ -293,7 +298,7 @@ int main(int argc, char** argv) {
setlocale(LC_CTYPE, "");
/* Parse arguments */
while ((opt = getopt_long(argc, argv, "hvCs:d:u:", long_opts, &opti))) {
while ((opt = getopt_long(argc, argv, "hvCs:d:u:p:", long_opts, &opti))) {
if (opt == EOF) break;
switch (opt) {
case 'h':
@ -332,6 +337,22 @@ int main(int argc, char** argv) {
break;
case 'C':
usecolors=0;
break;
case 'p':
argCopy = strdup(optarg);
pid = strtok(argCopy, ",");
if( !pidWhiteList ) {
pidWhiteList = Hashtable_new(8, false);
}
while( pid ) {
unsigned int num_pid = atoi(pid);
Hashtable_put(pidWhiteList, num_pid, (void *) 1);
pid = strtok(NULL, ",");
}
free(argCopy);
break;
default:
exit(1);
@ -356,7 +377,7 @@ int main(int argc, char** argv) {
ProcessList* pl = NULL;
UsersTable* ut = UsersTable_new();
pl = ProcessList_new(ut);
pl = ProcessList_new(ut, pidWhiteList);
Process_getMaxPid();
Header* header = Header_new(pl);
@ -936,5 +957,8 @@ int main(int argc, char** argv) {
((Object*)killPanel)->delete((Object*)killPanel);
UsersTable_delete(ut);
Settings_delete(settings);
if(pidWhiteList) {
Hashtable_delete(pidWhiteList);
}
return 0;
}