+#include <sys/times.h> /* for times() */
+//#include <sys/sysinfo.h> /* for sysinfo() */
+#ifndef AT_CLKTCK
+#define AT_CLKTCK 17
+#endif
+
+
+#if ENABLE_SELINUX
+#define SELINUX_O_PREFIX "label,"
+#define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" USE_FEATURE_PS_TIME(",time") ",args")
+#else
+#define DEFAULT_O_STR ("pid,user" USE_FEATURE_PS_TIME(",time") ",args")
+#endif
+
+typedef struct {
+ uint16_t width;
+ char name[6];
+ const char *header;
+ void (*f)(char *buf, int size, const procps_status_t *ps);
+ int ps_flags;
+} ps_out_t;
+
+struct globals {
+ ps_out_t* out;
+ int out_cnt;
+ int print_header;
+ int need_flags;
+ char *buffer;
+ unsigned terminal_width;
+#if ENABLE_FEATURE_PS_TIME
+ unsigned kernel_HZ;
+ unsigned long long seconds_since_boot;
+#endif
+ char default_o[sizeof(DEFAULT_O_STR)];
+};
+#define G (*(struct globals*)&bb_common_bufsiz1)
+#define out (G.out )
+#define out_cnt (G.out_cnt )
+#define print_header (G.print_header )
+#define need_flags (G.need_flags )
+#define buffer (G.buffer )
+#define terminal_width (G.terminal_width )
+#define kernel_HZ (G.kernel_HZ )
+#define seconds_since_boot (G.seconds_since_boot)
+#define default_o (G.default_o )
+
+#if ENABLE_FEATURE_PS_TIME
+/* for ELF executables, notes are pushed before environment and args */
+static ptrdiff_t find_elf_note(ptrdiff_t findme)
+{
+ ptrdiff_t *ep = (ptrdiff_t *) environ;
+
+ while (*ep++);
+ while (*ep) {
+ if (ep[0] == findme) {
+ return ep[1];
+ }
+ ep += 2;
+ }
+ return -1;
+}
+
+#if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
+static unsigned get_HZ_by_waiting(void)
+{
+ struct timeval tv1, tv2;
+ unsigned t1, t2, r, hz;
+ unsigned cnt = cnt; /* for compiler */
+ int diff;
+
+ r = 0;
+
+ /* Wait for times() to reach new tick */
+ t1 = times(NULL);
+ do {
+ t2 = times(NULL);
+ } while (t2 == t1);
+ gettimeofday(&tv2, NULL);
+
+ do {
+ t1 = t2;
+ tv1.tv_usec = tv2.tv_usec;
+
+ /* Wait exactly one times() tick */
+ do {
+ t2 = times(NULL);
+ } while (t2 == t1);
+ gettimeofday(&tv2, NULL);
+
+ /* Calculate ticks per sec, rounding up to even */
+ diff = tv2.tv_usec - tv1.tv_usec;
+ if (diff <= 0) diff += 1000000;
+ hz = 1000000u / (unsigned)diff;
+ hz = (hz+1) & ~1;
+
+ /* Count how many same hz values we saw */
+ if (r != hz) {
+ r = hz;
+ cnt = 0;
+ }
+ cnt++;
+ } while (cnt < 3); /* exit if saw 3 same values */
+
+ return r;
+}
+#else
+static inline unsigned get_HZ_by_waiting(void)
+{
+ /* Better method? */
+ return 100;
+}
+#endif
+
+static unsigned get_kernel_HZ(void)
+{
+ //char buf[64];
+ struct sysinfo info;
+
+ if (kernel_HZ)
+ return kernel_HZ;
+
+ /* Works for ELF only, Linux 2.4.0+ */
+ kernel_HZ = find_elf_note(AT_CLKTCK);
+ if (kernel_HZ == (unsigned)-1)
+ kernel_HZ = get_HZ_by_waiting();
+
+ //if (open_read_close("/proc/uptime", buf, sizeof(buf) <= 0)
+ // bb_perror_msg_and_die("cannot read %s", "/proc/uptime");
+ //buf[sizeof(buf)-1] = '\0';
+ ///sscanf(buf, "%llu", &seconds_since_boot);
+ sysinfo(&info);
+ seconds_since_boot = info.uptime;
+
+ return kernel_HZ;
+}
+#endif
+