Skip to content

Commit

Permalink
Merge pull request #63 from KarlSchu/feature/add_processname_filter
Browse files Browse the repository at this point in the history
feat: add process name filter
  • Loading branch information
gsass1 committed Feb 21, 2024
2 parents 1dfb3c6 + f525b1d commit 3ceac49
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ $ winget install gsass1.NTop
| `-C` | Use monochrome color scheme. |
| `-h` | Display help info. |
| `-p` PID, PID... | Show only the given PIDs. |
| `-n` NamePart, NamePart... | Show only processes containing at least one of the name parts. |
| `-s` COLUMN | Sort by this column. |
| `-u` USERNAME | Only display processes belonging to this user. |
| `-v` | Print version. |
Expand Down
36 changes: 36 additions & 0 deletions ntop.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#define SCROLL_INTERVAL 20ULL
#define INPUT_LOOP_DELAY 30
#define CARET_INTERVAL 500
#define MAX_NAMEPARTS 10
#define MAX_NAMEPARTSIZE 255

static int Width;
static int Height;
Expand Down Expand Up @@ -508,6 +510,10 @@ static BOOL FilterByPID = FALSE;
static DWORD PidFilterList[1024];
static DWORD PidFilterCount;

static BOOL FilterByName = FALSE;
static TCHAR NameFilterList[MAX_NAMEPARTS][MAX_NAMEPARTSIZE+1];
static DWORD NameFilterCount;

static void SelectProcess(DWORD Index)
{
SelectedProcessIndex = Index;
Expand Down Expand Up @@ -687,6 +693,19 @@ static void PollProcessList(DWORD UpdateTime)
}
}

if (FilterByName) {
BOOL InFilter = FALSE;
for(DWORD NameIndex = 0; NameIndex < NameFilterCount; NameIndex++) {
if(strstr(Process.ExeName, NameFilterList[NameIndex]) != NULL) {
InFilter = TRUE;
}
}

if (!InFilter) {
continue;
}
}

NewProcessList[i++] = Process;

if(i >= ProcessListSize) {
Expand Down Expand Up @@ -1254,6 +1273,7 @@ static void PrintHelp(const TCHAR *argv0)
{ _T("-C"), _T("Use a monochrome color scheme.") },
{ _T("-h"), _T("Display this help info.") },
{ _T("-p PID,PID...\n"), _T("\tShow only the given PIDs.") },
{ _T("-n NamePart,NamePart...\n"), _T("\tShow only processes containing at least one of the name parts.") },
{ _T("-s COLUMN\n"), _T("\tSort by this column.") },
{ _T("-u USERNAME\n"), _T("\tDisplay only processes of this user.") },
{ _T("-v"), _T("Print version.") },
Expand Down Expand Up @@ -1624,6 +1644,22 @@ int _tmain(int argc, TCHAR *argv[])
}
}
break;
case _T('n'):
if(++i < argc) {
const TCHAR *Delim = _T(",");
TCHAR *Context;
TCHAR *Token = _tcstok_s(argv[i], Delim, &Context);
while(Token && NameFilterCount < MAX_NAMEPARTS) {
strcpy_s(NameFilterList[NameFilterCount++], sizeof(Token) < MAX_NAMEPARTSIZE ? sizeof(Token) : MAX_NAMEPARTSIZE, Token);
Token = _tcstok_s(0, Delim, &Context);
}

if (NameFilterCount != 0) {
FilterByName = TRUE;
}
}
break;

default:
ConPrintf(_T("Unknown option: '%c'"), argv[i][1]);
return EXIT_FAILURE;
Expand Down
2 changes: 1 addition & 1 deletion ntop.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<id>ntop.portable</id>
<title>NTop</title>
<version>0.3.2</version>
<version>0.3.3</version>
<authors>Gian Sass</authors>
<owners>Gian Sass</owners>
<summary>htop-like system-monitor for Windows with Vi-keybindings</summary>
Expand Down

0 comments on commit 3ceac49

Please sign in to comment.