big header clean-up

This commit is contained in:
albert 2002-12-09 07:00:07 +00:00
parent 4382008ad0
commit 5087f3dbf6
23 changed files with 150 additions and 53 deletions

View File

@ -7,7 +7,7 @@
\***********************************************************************/ \***********************************************************************/
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "procps.h" #include "alloc.h"
void *xcalloc(void *pointer, int size) { void *xcalloc(void *pointer, int size) {
void * ret; void * ret;

14
proc/alloc.h Normal file
View File

@ -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

View File

@ -1,5 +1,10 @@
#ifndef PROC_DEVNAME_H
#define PROC_DEVNAME_H
#include "procps.h" #include "procps.h"
EXTERN_C_BEGIN
#define ABBREV_DEV 1 /* remove /dev/ */ #define ABBREV_DEV 1 /* remove /dev/ */
#define ABBREV_TTY 2 /* remove tty */ #define ABBREV_TTY 2 /* remove tty */
#define ABBREV_PTS 4 /* remove pts/ */ #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 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 int tty_to_dev(const char *restrict const name);
EXTERN_C_END
#endif

View File

@ -8,6 +8,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details. * GNU Library General Public License for more details.
*/ */
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>

View File

@ -5,16 +5,16 @@
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include "procps.h" #include "output.h"
#if 0 #if 1
/* output a string, converting unprintables to octal as we go, and stopping after /* 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). 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 print_str(FILE *restrict file, const char *restrict const s, unsigned max) {
unsigned i; unsigned i;
for (i=0; s[i] && i < max; i++) for (i=0; likely(s[i]) && likely(i<max); i++)
if (isprint(s[i]) || s[i] == ' ') if (likely(isprint(s[i]) || s[i] == ' '))
fputc(s[i], file); fputc(s[i], file);
else { else {
if (max > i+3) { if (max > 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 print_strlist(FILE *restrict file, const char *restrict const *restrict strs, unsigned max) {
unsigned i, n; unsigned i, n;
for (n=0; *strs && n < max; strs++) { for (n=0; *strs && n<max; strs++) {
for (i=0; strs[0][i] && n+i < max; i++) for (i=0; strs[0][i] && n+i < max; i++)
if (isprint(strs[0][i]) || strs[0][i] == ' ') if (likely(isprint(strs[0][i]) || strs[0][i] == ' '))
fputc(strs[0][i], file); fputc(strs[0][i], file);
else { else {
if (max > n+i+3) { if (max > n+i+3) {

15
proc/output.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef PROCPS_PROC_OUTPUT_H
#define PROCPS_PROC_OUTPUT_H
#include <stdio.h>
#include <sys/types.h>
#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

View File

@ -1,16 +1,13 @@
#ifndef PROCPS_PROC_PROCPS_H #ifndef PROCPS_PROC_PROCPS_H
#define PROCPS_PROC_PROCPS_H #define PROCPS_PROC_PROCPS_H
/* The shadow of the original with only common prototypes now. */ #ifdef __cplusplus
#include <stdio.h> #define EXTERN_C_BEGIN extern "C" {
#include <sys/types.h> #define EXTERN_C_END }
#else
/* The HZ constant from <asm/param.h> is replaced by the Hertz variable #define EXTERN_C_BEGIN
* available from "proc/sysinfo.h". #define EXTERN_C_END
*/ #endif
/* get page info */
#include <asm/page.h>
#if !defined(restrict) && __STDC_VERSION__ < 199901 #if !defined(restrict) && __STDC_VERSION__ < 199901
#if __GNUC__ > 2 || __GNUC_MINOR__ >= 91 // maybe 92 or 95 ? #if __GNUC__ > 2 || __GNUC_MINOR__ >= 91 // maybe 92 or 95 ?
@ -27,27 +24,12 @@
// tell gcc what to expect: if(unlikely(err)) die(err); // tell gcc what to expect: if(unlikely(err)) die(err);
#define likely(x) __builtin_expect(!!(x),1) #define likely(x) __builtin_expect(!!(x),1)
#define unlikely(x) __builtin_expect(!!(x),0) #define unlikely(x) __builtin_expect(!!(x),0)
#define expected(x,y) __builtin_expect((x),(y))
#else #else
#define MALLOC #define MALLOC
#define likely(x) (x) #define likely(x) (x)
#define unlikely(x) (x) #define unlikely(x) (x)
#define expected(x,y) (x)
#endif #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 #endif

View File

@ -10,12 +10,13 @@
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h> #include <stdlib.h>
#include <pwd.h> #include <pwd.h>
#include "procps.h" #include "alloc.h"
#include "pwcache.h"
#include <grp.h> #include <grp.h>
// might as well fill cache lines... else we waste memory anyway // 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 HASH(x) ((x) & (HASHSIZE - 1))
#define NAMESIZE 20 #define NAMESIZE 20

14
proc/pwcache.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef PROCPS_PROC_PWCACHE_H
#define PROCPS_PROC_PWCACHE_H
#include <sys/types.h>
#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

View File

@ -10,6 +10,8 @@
#endif #endif
#include "version.h" #include "version.h"
#include "readproc.h" #include "readproc.h"
#include "alloc.h"
#include "pwcache.h"
#include "devname.h" #include "devname.h"
#include "procps.h" #include "procps.h"
#include <stdio.h> #include <stdio.h>

View File

@ -17,6 +17,8 @@
#include <fs_secure.h> #include <fs_secure.h>
#endif #endif
EXTERN_C_BEGIN
/* /*
ld cutime, cstime, priority, nice, timeout, it_real_value, rss, ld cutime, cstime, priority, nice, timeout, it_real_value, rss,
c state, c state,
@ -221,4 +223,5 @@ extern void freeproc(proc_t* p);
#define PROC_SPARE_3 0x04000000 #define PROC_SPARE_3 0x04000000
#define PROC_SPARE_4 0x08000000 #define PROC_SPARE_4 0x08000000
EXTERN_C_END
#endif #endif

View File

@ -1,3 +1,5 @@
#ifndef PROC_SIG_H
#define PROC_SIG_H
/* /*
* Copyright 1998 by Albert Cahalan; all rights resered. * Copyright 1998 by Albert Cahalan; all rights resered.
* This file may be used subject to the terms and conditions of the * This file may be used subject to the terms and conditions of the
@ -11,6 +13,8 @@
#include "procps.h" #include "procps.h"
EXTERN_C_BEGIN
/* return -1 on failure */ /* return -1 on failure */
extern int signal_name_to_number(const char *restrict name); 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 pretty_print_signals(void);
extern void unix_print_signals(void); extern void unix_print_signals(void);
EXTERN_C_END
#endif

View File

@ -1,4 +1,12 @@
#ifndef __PROC_STATUS_H #ifndef PROC_STATUS_H
#define __PROC_STATUS_H #define PROC_STATUS_H
#include "procps.h"
EXTERN_C_BEGIN
extern const char * status(const proc_t *restrict task); extern const char * status(const proc_t *restrict task);
EXTERN_C_END
#endif #endif

View File

@ -1,8 +1,10 @@
#ifndef SYSINFO_H #ifndef PROC_SYSINFO_H
#define SYSINFO_H #define PROC_SYSINFO_H
#include "procps.h" #include "procps.h"
EXTERN_C_BEGIN
extern unsigned long long Hertz; /* clock tick frequency */ extern unsigned long long Hertz; /* clock tick frequency */
extern long smp_num_cpus; /* number of CPUs */ extern long smp_num_cpus; /* number of CPUs */
@ -77,4 +79,5 @@ extern unsigned vm_allocstall;
extern void vminfo(void); extern void vminfo(void);
EXTERN_C_END
#endif /* SYSINFO_H */ #endif /* SYSINFO_H */

View File

@ -1,6 +1,8 @@
#ifndef PROC_VERSION_H #ifndef PROC_VERSION_H
#define PROC_VERSION_H #define PROC_VERSION_H
#include "procps.h"
/* Suite version information for procps utilities /* Suite version information for procps utilities
* Copyright (c) 1995 Martin Schulze <joey@infodrom.north.de> * Copyright (c) 1995 Martin Schulze <joey@infodrom.north.de>
* Linux kernel version information for procps utilities * Linux kernel version information for procps utilities
@ -8,6 +10,8 @@
* Distributable under the terms of the GNU Library General Public License * Distributable under the terms of the GNU Library General Public License
*/ */
EXTERN_C_BEGIN
extern void display_version(void); /* display suite version */ extern void display_version(void); /* display suite version */
extern const char procps_version[]; /* global buf for 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_MINOR(x) (((x)>> 8) & 0xFF)
#define LINUX_VERSION_PATCH(x) ( (x) & 0xFF) #define LINUX_VERSION_PATCH(x) ( (x) & 0xFF)
EXTERN_C_END
#endif /* PROC_VERSION_H */ #endif /* PROC_VERSION_H */

14
proc/wchan.h Normal file
View File

@ -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

View File

@ -1,9 +1,13 @@
/* whattime.h --- see whattime.c for explanation */ #ifndef PROC_WHATTIME_H
#define PROC_WHATTIME_H
#ifndef __WHATTIME_H #include "procps.h"
#define __WHATTIME_H
EXTERN_C_BEGIN
extern void print_uptime(void); extern void print_uptime(void);
extern char *sprint_uptime(void); extern char *sprint_uptime(void);
EXTERN_C_END
#endif #endif

View File

@ -25,7 +25,7 @@
#include <signal.h> /* catch signals */ #include <signal.h> /* catch signals */
#include "common.h" #include "common.h"
#include "../proc/procps.h" #include "../proc/wchan.h"
#include "../proc/version.h" #include "../proc/version.h"
#include "../proc/readproc.h" #include "../proc/readproc.h"
#include "../proc/sysinfo.h" #include "../proc/sysinfo.h"

View File

@ -20,6 +20,7 @@
#include "common.h" #include "common.h"
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#include "../proc/wchan.h"
#include "../proc/version.h" #include "../proc/version.h"
#include "../proc/sysinfo.h" #include "../proc/sysinfo.h"

View File

@ -62,6 +62,7 @@
#include "../proc/readproc.h" #include "../proc/readproc.h"
#include "../proc/sysinfo.h" #include "../proc/sysinfo.h"
#include "../proc/wchan.h"
#include "../proc/procps.h" #include "../proc/procps.h"
#include "../proc/devname.h" #include "../proc/devname.h"
#include "common.h" #include "common.h"

View File

@ -21,6 +21,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include "proc/pwcache.h"
#include "proc/sig.h" #include "proc/sig.h"
#include "proc/devname.h" #include "proc/devname.h"
#include "proc/procps.h" /* char *user_from_uid(uid_t uid) */ #include "proc/procps.h" /* char *user_from_uid(uid_t uid) */

1
top.c
View File

@ -38,6 +38,7 @@
#include <values.h> #include <values.h>
#include "proc/devname.h" #include "proc/devname.h"
#include "proc/wchan.h"
#include "proc/procps.h" #include "proc/procps.h"
#include "proc/readproc.h" #include "proc/readproc.h"
#include "proc/sig.h" #include "proc/sig.h"

29
w.c
View File

@ -8,6 +8,7 @@
#include "proc/readproc.h" #include "proc/readproc.h"
#include "proc/devname.h" #include "proc/devname.h"
#include "proc/procps.h" #include "proc/procps.h"
#include "proc/output.h"
#include "proc/sysinfo.h" #include "proc/sysinfo.h"
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
@ -143,7 +144,7 @@ static const proc_t *getproc(const utmp_t *restrict const u, const char *restric
*found_utpid = 0; *found_utpid = 0;
for(; *pptr; pptr++) { for(; *pptr; pptr++) {
const proc_t *restrict const tmp = *pptr; const proc_t *restrict const tmp = *pptr;
if(tmp->pid == u->ut_pid) { if(unlikely(tmp->pid == u->ut_pid)) {
*found_utpid = 1; *found_utpid = 1;
best = tmp; best = tmp;
} }
@ -212,8 +213,8 @@ static void showinfo(utmp_t *u, int formtype, int maxcmd, int from) {
else else
print_time_ival7(idletime(tty), 0, stdout); print_time_ival7(idletime(tty), 0, stdout);
} }
fputs(" ", stdout); fputs(" ", stdout);
if (best) { if (likely(best)) {
if (best->cmdline) if (best->cmdline)
print_strlist(stdout, best->cmdline, maxcmd); print_strlist(stdout, best->cmdline, maxcmd);
else else
@ -276,17 +277,27 @@ int main(int argc, char **argv) {
if (from) if (from)
printf("FROM "); printf("FROM ");
if (longform) if (longform)
printf(" LOGIN@ IDLE JCPU PCPU WHAT\n"); printf(" LOGIN@ IDLE JCPU PCPU WHAT\n");
else else
printf(" IDLE WHAT\n"); printf(" IDLE WHAT\n");
} }
utmpname(UTMP_FILE); utmpname(UTMP_FILE);
setutent(); setutent();
while ((u=getutent())) { if (user) {
if (u->ut_type == USER_PROCESS && for (;;) {
(user ? !strncmp(u->ut_user, user, USERSZ) : *u->ut_user)) u = getutent();
showinfo(u, longform, maxcmd, from); 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(); endutent();