Stricter checks for command-line options

(thanks to Sebastian Pipping)
This commit is contained in:
Hisham Muhammad 2011-08-26 21:04:26 +00:00
parent 7eeb52dfbb
commit 5dfb46e14f
2 changed files with 18 additions and 5 deletions

View File

@ -4,6 +4,8 @@ What's new in version 0.9.1
* Option for counting CPUs from zero * Option for counting CPUs from zero
(thanks to Sean Noonan) (thanks to Sean Noonan)
* Meters update in every screen (no longer halting while on Setup, etc.) * Meters update in every screen (no longer halting while on Setup, etc.)
* Stricter checks for command-line options
(thanks to Sebastian Pipping)
* BUGFIX: Support larger numbers for process times. * BUGFIX: Support larger numbers for process times.
(thanks to Tristan Nakagawa for the report.) (thanks to Tristan Nakagawa for the report.)
* BUGFIX: Segfault in BarMeterMode_draw() for small terminal widths * BUGFIX: Segfault in BarMeterMode_draw() for small terminal widths

21
htop.c
View File

@ -228,12 +228,14 @@ static void addUserToVector(int key, void* userCast, void* panelCast) {
Panel_add(panel, (Object*) ListItem_new(user, key)); Panel_add(panel, (Object*) ListItem_new(user, key));
} }
static void setUserOnly(const char* userName, bool* userOnly, uid_t* userId) { static bool setUserOnly(const char* userName, bool* userOnly, uid_t* userId) {
struct passwd* user = getpwnam(userName); struct passwd* user = getpwnam(userName);
if (user) { if (user) {
*userOnly = true; *userOnly = true;
*userId = user->pw_uid; *userId = user->pw_uid;
return true;
} }
return false;
} }
static inline void setSortKey(ProcessList* pl, ProcessField sortKey, Panel* panel, Settings* settings) { static inline void setSortKey(ProcessList* pl, ProcessField sortKey, Panel* panel, Settings* settings) {
@ -295,16 +297,25 @@ int main(int argc, char** argv) {
} }
break; break;
case 'd': case 'd':
sscanf(optarg, "%d", &delay); if (sscanf(optarg, "%d", &delay) == 1) {
if (delay < 1) delay = 1; if (delay < 1) delay = 1;
if (delay > 100) delay = 100; if (delay > 100) delay = 100;
} else {
fprintf(stderr, "Error: invalid delay value \"%s\".\n", optarg);
exit(1);
}
break; break;
case 'u': case 'u':
setUserOnly(optarg, &userOnly, &userId); if (!setUserOnly(optarg, &userOnly, &userId)) {
fprintf(stderr, "Error: invalid user \"%s\".\n", optarg);
exit(1);
}
break; break;
case 'C': case 'C':
usecolors=0; usecolors=0;
break; break;
default:
exit(1);
} }
} }