From 5087f3dbf68efd615a73dd628ba78d7cd74f8653 Mon Sep 17 00:00:00 2001 From: albert <> Date: Mon, 9 Dec 2002 07:00:07 +0000 Subject: [PATCH] big header clean-up --- proc/alloc.c | 2 +- proc/alloc.h | 14 ++++++++++++++ proc/devname.h | 8 ++++++++ proc/ksym.c | 1 + proc/output.c | 12 ++++++------ proc/output.h | 15 +++++++++++++++ proc/procps.h | 36 +++++++++--------------------------- proc/pwcache.c | 5 +++-- proc/pwcache.h | 14 ++++++++++++++ proc/readproc.c | 2 ++ proc/readproc.h | 3 +++ proc/sig.h | 7 +++++++ proc/status.h | 12 ++++++++++-- proc/sysinfo.h | 7 +++++-- proc/version.h | 6 ++++++ proc/wchan.h | 14 ++++++++++++++ proc/whattime.h | 10 +++++++--- ps/display.c | 2 +- ps/global.c | 1 + ps/output.c | 1 + skill.c | 1 + top.c | 1 + w.c | 29 ++++++++++++++++++++--------- 23 files changed, 150 insertions(+), 53 deletions(-) create mode 100644 proc/alloc.h create mode 100644 proc/output.h create mode 100644 proc/pwcache.h create mode 100644 proc/wchan.h diff --git a/proc/alloc.c b/proc/alloc.c index 0868c68a..a4ed1b19 100644 --- a/proc/alloc.c +++ b/proc/alloc.c @@ -7,7 +7,7 @@ \***********************************************************************/ #include #include -#include "procps.h" +#include "alloc.h" void *xcalloc(void *pointer, int size) { void * ret; diff --git a/proc/alloc.h b/proc/alloc.h new file mode 100644 index 00000000..8c5016de --- /dev/null +++ b/proc/alloc.h @@ -0,0 +1,14 @@ +#ifndef PROCPS_PROC_ALLOC_H +#define PROCPS_PROC_ALLOC_H + +#include "procps.h" + +EXTERN_C_BEGIN + +extern void *xrealloc(void *oldp, unsigned int size) MALLOC; +extern void *xmalloc(unsigned int size) MALLOC; +extern void *xcalloc(void *pointer, int size) MALLOC; + +EXTERN_C_END + +#endif diff --git a/proc/devname.h b/proc/devname.h index 60b8e75a..b04bd905 100644 --- a/proc/devname.h +++ b/proc/devname.h @@ -1,5 +1,10 @@ +#ifndef PROC_DEVNAME_H +#define PROC_DEVNAME_H + #include "procps.h" +EXTERN_C_BEGIN + #define ABBREV_DEV 1 /* remove /dev/ */ #define ABBREV_TTY 2 /* remove tty */ #define ABBREV_PTS 4 /* remove pts/ */ @@ -7,3 +12,6 @@ extern unsigned dev_to_tty(char *restrict ret, unsigned chop, int dev, int pid, unsigned int flags); extern int tty_to_dev(const char *restrict const name); + +EXTERN_C_END +#endif diff --git a/proc/ksym.c b/proc/ksym.c index f572becc..f2eea82e 100644 --- a/proc/ksym.c +++ b/proc/ksym.c @@ -8,6 +8,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. */ +#include #include #include #include diff --git a/proc/output.c b/proc/output.c index 4ea73cf6..3863dc25 100644 --- a/proc/output.c +++ b/proc/output.c @@ -5,16 +5,16 @@ #include #include #include -#include "procps.h" +#include "output.h" -#if 0 +#if 1 /* output a string, converting unprintables to octal as we go, and stopping after processing max chars of output (accounting for expansion due to octal rep). */ unsigned print_str(FILE *restrict file, const char *restrict const s, unsigned max) { unsigned i; - for (i=0; s[i] && i < max; i++) - if (isprint(s[i]) || s[i] == ' ') + for (i=0; likely(s[i]) && likely(i i+3) { @@ -33,9 +33,9 @@ unsigned print_str(FILE *restrict file, const char *restrict const s, unsigned m */ unsigned print_strlist(FILE *restrict file, const char *restrict const *restrict strs, unsigned max) { unsigned i, n; - for (n=0; *strs && n < max; strs++) { + for (n=0; *strs && n n+i+3) { diff --git a/proc/output.h b/proc/output.h new file mode 100644 index 00000000..a2a0541f --- /dev/null +++ b/proc/output.h @@ -0,0 +1,15 @@ +#ifndef PROCPS_PROC_OUTPUT_H +#define PROCPS_PROC_OUTPUT_H + +#include +#include +#include "procps.h" + +EXTERN_C_BEGIN + +extern unsigned print_str (FILE *restrict file, const char *restrict s, unsigned max); +extern unsigned print_strlist(FILE *restrict file, const char *restrict const *restrict strs, unsigned max); + +EXTERN_C_END + +#endif diff --git a/proc/procps.h b/proc/procps.h index 76cc707a..44aab672 100644 --- a/proc/procps.h +++ b/proc/procps.h @@ -1,16 +1,13 @@ #ifndef PROCPS_PROC_PROCPS_H #define PROCPS_PROC_PROCPS_H -/* The shadow of the original with only common prototypes now. */ -#include -#include - -/* The HZ constant from is replaced by the Hertz variable - * available from "proc/sysinfo.h". - */ - -/* get page info */ -#include +#ifdef __cplusplus +#define EXTERN_C_BEGIN extern "C" { +#define EXTERN_C_END } +#else +#define EXTERN_C_BEGIN +#define EXTERN_C_END +#endif #if !defined(restrict) && __STDC_VERSION__ < 199901 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 91 // maybe 92 or 95 ? @@ -27,27 +24,12 @@ // tell gcc what to expect: if(unlikely(err)) die(err); #define likely(x) __builtin_expect(!!(x),1) #define unlikely(x) __builtin_expect(!!(x),0) +#define expected(x,y) __builtin_expect((x),(y)) #else #define MALLOC #define likely(x) (x) #define unlikely(x) (x) +#define expected(x,y) (x) #endif - -extern void *xrealloc(void *oldp, unsigned int size) MALLOC; -extern void *xmalloc(unsigned int size) MALLOC; -extern void *xcalloc(void *pointer, int size) MALLOC; - -extern int mult_lvl_cmp(void* a, void* b); - -extern char *user_from_uid(uid_t uid); -extern char *group_from_gid(gid_t gid); - -extern const char * wchan(unsigned long address); -extern int open_psdb(const char *restrict override); -extern int open_psdb_message(const char *restrict override, void (*message)(const char *, ...)); - -extern unsigned print_str (FILE *restrict file, const char *restrict s, unsigned max); -extern unsigned print_strlist(FILE *restrict file, const char *restrict const *restrict strs, unsigned max); - #endif diff --git a/proc/pwcache.c b/proc/pwcache.c index c8bb18c9..51dd2d20 100644 --- a/proc/pwcache.c +++ b/proc/pwcache.c @@ -10,12 +10,13 @@ #include #include #include -#include "procps.h" +#include "alloc.h" +#include "pwcache.h" #include // might as well fill cache lines... else we waste memory anyway -#define HASHSIZE 32 /* power of 2 */ +#define HASHSIZE 64 /* power of 2 */ #define HASH(x) ((x) & (HASHSIZE - 1)) #define NAMESIZE 20 diff --git a/proc/pwcache.h b/proc/pwcache.h new file mode 100644 index 00000000..244ce6d1 --- /dev/null +++ b/proc/pwcache.h @@ -0,0 +1,14 @@ +#ifndef PROCPS_PROC_PWCACHE_H +#define PROCPS_PROC_PWCACHE_H + +#include +#include "procps.h" + +EXTERN_C_BEGIN + +extern char *user_from_uid(uid_t uid); +extern char *group_from_gid(gid_t gid); + +EXTERN_C_END + +#endif diff --git a/proc/readproc.c b/proc/readproc.c index 4cd0627c..132462f9 100644 --- a/proc/readproc.c +++ b/proc/readproc.c @@ -10,6 +10,8 @@ #endif #include "version.h" #include "readproc.h" +#include "alloc.h" +#include "pwcache.h" #include "devname.h" #include "procps.h" #include diff --git a/proc/readproc.h b/proc/readproc.h index 2b0947f5..4bb24bd5 100644 --- a/proc/readproc.h +++ b/proc/readproc.h @@ -17,6 +17,8 @@ #include #endif +EXTERN_C_BEGIN + /* ld cutime, cstime, priority, nice, timeout, it_real_value, rss, c state, @@ -221,4 +223,5 @@ extern void freeproc(proc_t* p); #define PROC_SPARE_3 0x04000000 #define PROC_SPARE_4 0x08000000 +EXTERN_C_END #endif diff --git a/proc/sig.h b/proc/sig.h index 9640f989..9e8e07b1 100644 --- a/proc/sig.h +++ b/proc/sig.h @@ -1,3 +1,5 @@ +#ifndef PROC_SIG_H +#define PROC_SIG_H /* * Copyright 1998 by Albert Cahalan; all rights resered. * This file may be used subject to the terms and conditions of the @@ -11,6 +13,8 @@ #include "procps.h" +EXTERN_C_BEGIN + /* return -1 on failure */ extern int signal_name_to_number(const char *restrict name); @@ -19,3 +23,6 @@ extern int print_given_signals(int argc, const char *restrict const *restrict ar extern void pretty_print_signals(void); extern void unix_print_signals(void); + +EXTERN_C_END +#endif diff --git a/proc/status.h b/proc/status.h index 8eefe790..f1e141f0 100644 --- a/proc/status.h +++ b/proc/status.h @@ -1,4 +1,12 @@ -#ifndef __PROC_STATUS_H -#define __PROC_STATUS_H +#ifndef PROC_STATUS_H +#define PROC_STATUS_H + +#include "procps.h" + +EXTERN_C_BEGIN + extern const char * status(const proc_t *restrict task); + +EXTERN_C_END + #endif diff --git a/proc/sysinfo.h b/proc/sysinfo.h index 7c81954b..70f681af 100644 --- a/proc/sysinfo.h +++ b/proc/sysinfo.h @@ -1,8 +1,10 @@ -#ifndef SYSINFO_H -#define SYSINFO_H +#ifndef PROC_SYSINFO_H +#define PROC_SYSINFO_H #include "procps.h" +EXTERN_C_BEGIN + extern unsigned long long Hertz; /* clock tick frequency */ extern long smp_num_cpus; /* number of CPUs */ @@ -77,4 +79,5 @@ extern unsigned vm_allocstall; extern void vminfo(void); +EXTERN_C_END #endif /* SYSINFO_H */ diff --git a/proc/version.h b/proc/version.h index 6a93d893..15fa96f7 100644 --- a/proc/version.h +++ b/proc/version.h @@ -1,6 +1,8 @@ #ifndef PROC_VERSION_H #define PROC_VERSION_H +#include "procps.h" + /* Suite version information for procps utilities * Copyright (c) 1995 Martin Schulze * Linux kernel version information for procps utilities @@ -8,6 +10,8 @@ * Distributable under the terms of the GNU Library General Public License */ +EXTERN_C_BEGIN + extern void display_version(void); /* display suite version */ extern const char procps_version[]; /* global buf for suite version */ @@ -20,4 +24,6 @@ extern int linux_version_code; /* runtime version of LINUX_VERSION_CODE #define LINUX_VERSION_MINOR(x) (((x)>> 8) & 0xFF) #define LINUX_VERSION_PATCH(x) ( (x) & 0xFF) +EXTERN_C_END + #endif /* PROC_VERSION_H */ diff --git a/proc/wchan.h b/proc/wchan.h new file mode 100644 index 00000000..ad71b716 --- /dev/null +++ b/proc/wchan.h @@ -0,0 +1,14 @@ +#ifndef PROCPS_PROC_WCHAN_H +#define PROCPS_PROC_WCHAN_H + +#include "procps.h" + +EXTERN_C_BEGIN + +extern const char * wchan(unsigned long address); +extern int open_psdb(const char *restrict override); +extern int open_psdb_message(const char *restrict override, void (*message)(const char *, ...)); + +EXTERN_C_END + +#endif diff --git a/proc/whattime.h b/proc/whattime.h index 3d37bfa2..891ccd35 100644 --- a/proc/whattime.h +++ b/proc/whattime.h @@ -1,9 +1,13 @@ -/* whattime.h --- see whattime.c for explanation */ +#ifndef PROC_WHATTIME_H +#define PROC_WHATTIME_H -#ifndef __WHATTIME_H -#define __WHATTIME_H +#include "procps.h" + +EXTERN_C_BEGIN extern void print_uptime(void); extern char *sprint_uptime(void); +EXTERN_C_END + #endif diff --git a/ps/display.c b/ps/display.c index 3ddd97e4..704606b6 100644 --- a/ps/display.c +++ b/ps/display.c @@ -25,7 +25,7 @@ #include /* catch signals */ #include "common.h" -#include "../proc/procps.h" +#include "../proc/wchan.h" #include "../proc/version.h" #include "../proc/readproc.h" #include "../proc/sysinfo.h" diff --git a/ps/global.c b/ps/global.c index c23ec732..7330f04d 100644 --- a/ps/global.c +++ b/ps/global.c @@ -20,6 +20,7 @@ #include "common.h" #include +#include "../proc/wchan.h" #include "../proc/version.h" #include "../proc/sysinfo.h" diff --git a/ps/output.c b/ps/output.c index 7fb28cdb..16ae3770 100644 --- a/ps/output.c +++ b/ps/output.c @@ -62,6 +62,7 @@ #include "../proc/readproc.h" #include "../proc/sysinfo.h" +#include "../proc/wchan.h" #include "../proc/procps.h" #include "../proc/devname.h" #include "common.h" diff --git a/skill.c b/skill.c index 6d6ed278..b58200bb 100644 --- a/skill.c +++ b/skill.c @@ -21,6 +21,7 @@ #include #include #include +#include "proc/pwcache.h" #include "proc/sig.h" #include "proc/devname.h" #include "proc/procps.h" /* char *user_from_uid(uid_t uid) */ diff --git a/top.c b/top.c index 4399376a..295db89a 100644 --- a/top.c +++ b/top.c @@ -38,6 +38,7 @@ #include #include "proc/devname.h" +#include "proc/wchan.h" #include "proc/procps.h" #include "proc/readproc.h" #include "proc/sig.h" diff --git a/w.c b/w.c index 2806993b..f8d5b511 100644 --- a/w.c +++ b/w.c @@ -8,6 +8,7 @@ #include "proc/readproc.h" #include "proc/devname.h" #include "proc/procps.h" +#include "proc/output.h" #include "proc/sysinfo.h" #include #include @@ -143,7 +144,7 @@ static const proc_t *getproc(const utmp_t *restrict const u, const char *restric *found_utpid = 0; for(; *pptr; pptr++) { const proc_t *restrict const tmp = *pptr; - if(tmp->pid == u->ut_pid) { + if(unlikely(tmp->pid == u->ut_pid)) { *found_utpid = 1; best = tmp; } @@ -212,8 +213,8 @@ static void showinfo(utmp_t *u, int formtype, int maxcmd, int from) { else print_time_ival7(idletime(tty), 0, stdout); } - fputs(" ", stdout); - if (best) { + fputs(" ", stdout); + if (likely(best)) { if (best->cmdline) print_strlist(stdout, best->cmdline, maxcmd); else @@ -276,17 +277,27 @@ int main(int argc, char **argv) { if (from) printf("FROM "); if (longform) - printf(" LOGIN@ IDLE JCPU PCPU WHAT\n"); + printf(" LOGIN@ IDLE JCPU PCPU WHAT\n"); else - printf(" IDLE WHAT\n"); + printf(" IDLE WHAT\n"); } utmpname(UTMP_FILE); setutent(); - while ((u=getutent())) { - if (u->ut_type == USER_PROCESS && - (user ? !strncmp(u->ut_user, user, USERSZ) : *u->ut_user)) - showinfo(u, longform, maxcmd, from); + if (user) { + for (;;) { + u = getutent(); + if (unlikely(!u)) break; + if (u->ut_type != USER_PROCESS) continue; + if (!strncmp(u->ut_user, user, USERSZ)) showinfo(u, longform, maxcmd, from); + } + } else { + for (;;) { + u = getutent(); + if (unlikely(!u)) break; + if (u->ut_type != USER_PROCESS) continue; + if (*u->ut_user) showinfo(u, longform, maxcmd, from); + } } endutent();