top(1): partial revert of r334517

In fixing issues with uid > INT_MAX, I broke the uid without username
case. The latter is more important so return the old state.

Discussed with:	allanjude
This commit is contained in:
Eitan Adler 2018-06-03 05:20:11 +00:00
parent b7128a09ce
commit 01a55f006a
7 changed files with 16 additions and 18 deletions

View File

@ -918,7 +918,7 @@ get_process_info(struct system_info *si, struct process_select *sel,
static char fmt[512]; /* static area where result is built */ static char fmt[512]; /* static area where result is built */
char * char *
format_next_process(caddr_t xhandle, char *(*get_userid)(uid_t), int flags) format_next_process(caddr_t xhandle, char *(*get_userid)(int), int flags)
{ {
struct kinfo_proc *pp; struct kinfo_proc *pp;
const struct kinfo_proc *oldp; const struct kinfo_proc *oldp;

View File

@ -76,7 +76,7 @@ struct process_select
/* routines defined by the machine dependent module */ /* routines defined by the machine dependent module */
char *format_header(char *uname_field); char *format_header(char *uname_field);
char *format_next_process(caddr_t handle, char *(*get_userid)(uid_t), char *format_next_process(caddr_t handle, char *(*get_userid)(int),
int flags); int flags);
void toggle_pcpustats(void); void toggle_pcpustats(void);
void get_system_info(struct system_info *si); void get_system_info(struct system_info *si);

View File

@ -206,7 +206,7 @@ main(int argc, char *argv[])
int displays = 0; /* indicates unspecified */ int displays = 0; /* indicates unspecified */
int sel_ret = 0; int sel_ret = 0;
time_t curr_time; time_t curr_time;
char *(*get_userid)(uid_t) = username; char *(*get_userid)(int) = username;
char *uname_field = "USERNAME"; char *uname_field = "USERNAME";
char *header_text; char *header_text;
char *env_top; char *env_top;

View File

@ -37,27 +37,26 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include "utils.h" #include "utils.h"
#include "username.h" #include "username.h"
struct hash_el { struct hash_el {
uid_t uid; int uid;
char name[MAXLOGNAME]; char name[MAXLOGNAME];
}; };
#define is_empty_hash(x) (hash_table[x].name[0] == 0) #define is_empty_hash(x) (hash_table[x].name[0] == 0)
/* simple minded hashing function */ /* simple minded hashing function */
#define hashit(i) (i % Table_size) #define hashit(i) (abs(i) % Table_size)
/* K&R requires that statically declared tables be initialized to zero. */ /* K&R requires that statically declared tables be initialized to zero. */
/* We depend on that for hash_table and YOUR compiler had BETTER do it! */ /* We depend on that for hash_table and YOUR compiler had BETTER do it! */
static struct hash_el hash_table[Table_size]; static struct hash_el hash_table[Table_size];
char *username(uid_t uid) char *username(int uid)
{ {
int hashindex; int hashindex;
@ -70,7 +69,7 @@ char *username(uid_t uid)
return(hash_table[hashindex].name); return(hash_table[hashindex].name);
} }
uid_t userid(char username[]) int userid(char username[])
{ {
struct passwd *pwd; struct passwd *pwd;
@ -91,7 +90,7 @@ uid_t userid(char username[])
} }
/* wecare 1 = enter it always, 0 = nice to have */ /* wecare 1 = enter it always, 0 = nice to have */
int enter_user(uid_t uid, char name[], bool wecare) int enter_user(int uid, char name[], bool wecare)
{ {
int hashindex; int hashindex;
@ -122,7 +121,7 @@ int enter_user(uid_t uid, char name[], bool wecare)
*/ */
int int
get_user(uid_t uid) get_user(int uid)
{ {
struct passwd *pwd; struct passwd *pwd;

View File

@ -12,13 +12,12 @@
#define USERNAME_H #define USERNAME_H
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h>
int enter_user(uid_t uid, char *name, bool wecare); int enter_user(int uid, char *name, bool wecare);
int get_user(uid_t uid); int get_user(int uid);
void init_hash(void); void init_hash(void);
char *username(uid_t uid); char *username(int uid);
uid_t userid(char *username); int userid(char *username);
/* /*
* "Table_size" defines the size of the hash tables used to map uid to * "Table_size" defines the size of the hash tables used to map uid to

View File

@ -89,12 +89,12 @@ char *itoa(unsigned int val)
} }
/* /*
* itoa7(val) - like itoa, except the number is right justified in a 7 * (val) - like itoa, except the number is right justified in a 7
* character field. This code is a duplication of itoa instead of * character field. This code is a duplication of itoa instead of
* a front end to a more general routine for efficiency. * a front end to a more general routine for efficiency.
*/ */
char *itoa7(unsigned int val) char *itoa7(int val)
{ {
char *ptr; char *ptr;
static char buffer[16]; /* result is built here */ static char buffer[16]; /* result is built here */

View File

@ -14,7 +14,7 @@
int atoiwi(const char *); int atoiwi(const char *);
char *itoa(unsigned int); char *itoa(unsigned int);
char *itoa7(unsigned int); char *itoa7(int);
int digits(int); int digits(int);
char **argparse(char *, int *); char **argparse(char *, int *);
long percentages(int, int *, long *, long *, long *); long percentages(int, int *, long *, long *, long *);