diff --git a/Makefile.am b/Makefile.am index 2026b5e4..bcad91b5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -101,7 +101,7 @@ top_top_SOURCES = \ top/top.c \ top/top_nls.h \ top/top_nls.c \ - lib/fileutils.c + lib/fileutils.c \ lib/signals.c top_top_LDADD = $(LDADD) @NCURSES_LIBS@ $(DL_LIB) endif diff --git a/proc/libprocps.sym b/proc/libprocps.sym index 7b215194..c47f8356 100644 --- a/proc/libprocps.sym +++ b/proc/libprocps.sym @@ -40,7 +40,6 @@ global: meminfo; openproc; page_bytes; - print_uptime; put_slabinfo; readeither; readproc; @@ -49,12 +48,8 @@ global: readproctab; readtask; smp_num_cpus; - sprint_uptime; tty_to_dev; user_from_uid; - uptime; - sprint_uptime; - sprint_uptime_short; procps_hertz_get; procps_linux_version; procps_meminfo_new; @@ -76,6 +71,9 @@ global: procps_stat_get_jiffs_hist_all; procps_stat_get_sys; procps_stat_get_sys_chain; + procps_uptime; + procps_uptime_sprint; + procps_uptime_sprint_short; procps_vmstat_new; procps_vmstat_read; procps_vmstat_ref; diff --git a/proc/uptime.c b/proc/uptime.c index 2287ebd5..a1628915 100644 --- a/proc/uptime.c +++ b/proc/uptime.c @@ -47,13 +47,14 @@ static int count_users(void) setutent(); while ((ut = getutent())) { - if ((ut->ut_type == USER_PROCESS) && (ut->ut_name[0] != '\0')) - numuser++; + if ((ut->ut_type == USER_PROCESS) && (ut->ut_name[0] != '\0')) + numuser++; } endutent(); return numuser; } + /* * uptime: * @@ -64,7 +65,9 @@ static int count_users(void) * * Returns: uptime_secs on success and <0 on failure */ -PROCPS_EXPORT int uptime(double *restrict uptime_secs, double *restrict idle_secs) +PROCPS_EXPORT int procps_uptime( + double *restrict uptime_secs, + double *restrict idle_secs) { double up=0, idle=0; char *savelocale; @@ -72,33 +75,34 @@ PROCPS_EXPORT int uptime(double *restrict uptime_secs, double *restrict idle_sec FILE *fp; if ((fp = fopen(UPTIME_FILE, "r")) == NULL) - return -errno; + return -errno; + savelocale = strdup(setlocale(LC_NUMERIC, NULL)); setlocale(LC_NUMERIC, "C"); if (fscanf(fp, "%lf %lf", &up, &idle) < 2) { - setlocale(LC_NUMERIC, savelocale); - free(savelocale); - fclose(fp); - return -ERANGE; + setlocale(LC_NUMERIC, savelocale); + free(savelocale); + fclose(fp); + return -ERANGE; } fclose(fp); setlocale(LC_NUMERIC, savelocale); free(savelocale); if (uptime_secs) - *uptime_secs = up; + *uptime_secs = up; if (idle_secs) - *idle_secs = idle; + *idle_secs = idle; return up; } /* - * sprint_uptime: + * procps_uptime_sprint: * * Print current time in nice format * * Returns a statically allocated upbuf or NULL on error */ -PROCPS_EXPORT char *sprint_uptime(void) +PROCPS_EXPORT char *procps_uptime_sprint(void) { int upminutes, uphours, updays, users; int pos; @@ -109,34 +113,41 @@ PROCPS_EXPORT char *sprint_uptime(void) upbuf[0] = '\0'; if (time(&realseconds) < 0) - return upbuf; + return upbuf; realtime = localtime(&realseconds); - if (uptime(&uptime_secs, &idle_secs) < 0) - return upbuf; + if (procps_uptime(&uptime_secs, &idle_secs) < 0) + return upbuf; updays = ((int) uptime_secs / (60*60*24)); uphours = ((int) uptime_secs / (60*60)) % 24; upminutes = ((int) uptime_secs / (60)) % 60; pos = sprintf(upbuf, " %02d:%02d:%02d up %d %s, ", - realtime->tm_hour, realtime->tm_min, realtime->tm_sec, - updays, (updays != 1) ? "days" : "day"); + realtime->tm_hour, realtime->tm_min, realtime->tm_sec, + updays, (updays != 1) ? "days" : "day"); if (uphours) - pos += sprintf(upbuf + pos, "%2d:%02d, ", uphours, upminutes); + pos += sprintf(upbuf + pos, "%2d:%02d, ", uphours, upminutes); else - pos += sprintf(upbuf + pos, "%d min, ", uphours, upminutes); + pos += sprintf(upbuf + pos, "%d min, ", uphours, upminutes); users = count_users(); loadavg(&av1, &av5, &av15); pos += sprintf(upbuf + pos, "%2d user%s, load average: %.2f, %.2f, %.2f", - users, users == 1 ? "" : "s", - av1, av5, av15); + users, users == 1 ? "" : "s", + av1, av5, av15); return upbuf; } -PROCPS_EXPORT char *sprint_uptime_short(void) +/* + * procps_uptime_sprint_short: + * + * Print current time in nice format + * + * Returns a statically allocated buffer or NULL on error + */ +PROCPS_EXPORT char *procps_uptime_sprint_short(void) { int updecades, upyears, upweeks, updays, uphours, upminutes; int pos = 3; @@ -146,8 +157,8 @@ PROCPS_EXPORT char *sprint_uptime_short(void) double uptime_secs, idle_secs; shortbuf[0] = '\0'; - if (uptime(&uptime_secs, &idle_secs) < 0) - return shortbuf; + if (procps_uptime(&uptime_secs, &idle_secs) < 0) + return shortbuf; updecades = (int) uptime_secs / (60*60*24*365*10); upyears = ((int) uptime_secs / (60*60*24*365)) % 10; @@ -159,44 +170,44 @@ PROCPS_EXPORT char *sprint_uptime_short(void) strcat(shortbuf, "up "); if (updecades) { - pos += sprintf(shortbuf + pos, "%d %s", - updecades, updecades > 1 ? "decades" : "decade"); - comma +1; + pos += sprintf(shortbuf + pos, "%d %s", + updecades, updecades > 1 ? "decades" : "decade"); + comma +1; } if (upyears) { - pos += sprintf(shortbuf + pos, "%s%d %s", - comma > 0 ? ", " : "", upyears, - upyears > 1 ? "years" : "year"); - comma += 1; + pos += sprintf(shortbuf + pos, "%s%d %s", + comma > 0 ? ", " : "", upyears, + upyears > 1 ? "years" : "year"); + comma += 1; } if (upweeks) { - pos += sprintf(shortbuf + pos, "%s%d %s", - comma > 0 ? ", " : "", upweeks, - upweeks > 1 ? "weeks" : "week"); - comma += 1; + pos += sprintf(shortbuf + pos, "%s%d %s", + comma > 0 ? ", " : "", upweeks, + upweeks > 1 ? "weeks" : "week"); + comma += 1; } if (updays) { - pos += sprintf(shortbuf + pos, "%s%d %s", - comma > 0 ? ", " : "", updays, - updays > 1 ? "days" : "day"); - comma += 1; + pos += sprintf(shortbuf + pos, "%s%d %s", + comma > 0 ? ", " : "", updays, + updays > 1 ? "days" : "day"); + comma += 1; } if (uphours) { - pos += sprintf(shortbuf + pos, "%s%d %s", - comma > 0 ? ", " : "", uphours, - uphours > 1 ? "hours" : "hour"); - comma += 1; + pos += sprintf(shortbuf + pos, "%s%d %s", + comma > 0 ? ", " : "", uphours, + uphours > 1 ? "hours" : "hour"); + comma += 1; } if (upminutes) { - pos += sprintf(shortbuf + pos, "%s%d %s", - comma > 0 ? ", " : "", upminutes, - upminutes > 1 ? "minutes" : "minute"); - comma += 1; + pos += sprintf(shortbuf + pos, "%s%d %s", + comma > 0 ? ", " : "", upminutes, + upminutes > 1 ? "minutes" : "minute"); + comma += 1; } return shortbuf; } diff --git a/proc/uptime.h b/proc/uptime.h index 1288dccb..6175937a 100644 --- a/proc/uptime.h +++ b/proc/uptime.h @@ -29,9 +29,9 @@ __BEGIN_DECLS -int uptime(double *uptime_secs, double *idle_secs); -char *sprint_uptime(void); -char *sprint_uptime_short(void); +int procps_uptime(double *uptime_secs, double *idle_secs); +char *procps_uptime_sprint(void); +char *procps_uptime_sprint_short(void); __END_DECLS diff --git a/ps/global.c b/ps/global.c index 0724066d..1b4b3af0 100644 --- a/ps/global.c +++ b/ps/global.c @@ -34,6 +34,7 @@ #include "../proc/wchan.h" #include "../proc/version.h" #include "../proc/sysinfo.h" +#include #include "../include/c.h" #include "common.h" @@ -390,7 +391,7 @@ void reset_global(void){ negate_selection = 0; page_size = getpagesize(); running_only = 0; - seconds_since_boot = uptime(0,0); + seconds_since_boot = procps_uptime(0,0); selection_list = NULL; simple_select = 0; sort_list = NULL; diff --git a/top/top.c b/top/top.c index 710e95b2..9e76aed2 100644 --- a/top/top.c +++ b/top/top.c @@ -2516,7 +2516,7 @@ static void procs_hlp (proc_t *this) { float et; void *v; - uptime(&uptime_cur, NULL); + procps_uptime(&uptime_cur, NULL); et = uptime_cur - uptime_sav; if (et < 0.01) et = 0.005; uptime_sav = uptime_cur; @@ -5102,10 +5102,10 @@ static void summary_show (void) { // Display Uptime and Loadavg if (isROOM(View_LOADAV, 1)) { if (!Rc.mode_altscr) - show_special(0, fmtmk(LOADAV_line, Myname, sprint_uptime())); + show_special(0, fmtmk(LOADAV_line, Myname, procps_uptime_sprint())); else show_special(0, fmtmk(CHKw(w, Show_TASKON)? LOADAV_line_alt : LOADAV_line - , w->grpname, sprint_uptime())); + , w->grpname, procps_uptime_sprint())); Msg_row += 1; } // end: View_LOADAV diff --git a/uptime.c b/uptime.c index 205bc325..64ff6ac4 100644 --- a/uptime.c +++ b/uptime.c @@ -1,6 +1,6 @@ /* * uptime.c - display system uptime - * Copyright (C) 2012 Craig Small + * Copyright (C) 2012-2015 Craig Small * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -33,80 +33,80 @@ static void print_uptime_since() { - double now, uptime_secs, idle_secs; - time_t up_since_secs; - struct tm *up_since; - struct timeval tim; + double now, uptime_secs, idle_secs; + time_t up_since_secs; + struct tm *up_since; + struct timeval tim; - /* Get the current time and convert it to a double */ - gettimeofday(&tim, NULL); - now = tim.tv_sec + (tim.tv_usec / 1000000.0); + /* Get the current time and convert it to a double */ + gettimeofday(&tim, NULL); + now = tim.tv_sec + (tim.tv_usec / 1000000.0); - /* Get the uptime and calculate when that was */ - uptime(&uptime_secs, &idle_secs); - up_since_secs = (time_t) ((now - uptime_secs) + 0.5); + /* Get the uptime and calculate when that was */ + procps_uptime(&uptime_secs, &idle_secs); + up_since_secs = (time_t) ((now - uptime_secs) + 0.5); - /* Show this */ - up_since = localtime(&up_since_secs); - printf("%04d-%02d-%02d %02d:%02d:%02d\n", - up_since->tm_year + 1900, up_since->tm_mon + 1, up_since->tm_mday, - up_since->tm_hour, up_since->tm_min, up_since->tm_sec); + /* Show this */ + up_since = localtime(&up_since_secs); + printf("%04d-%02d-%02d %02d:%02d:%02d\n", + up_since->tm_year + 1900, up_since->tm_mon + 1, up_since->tm_mday, + up_since->tm_hour, up_since->tm_min, up_since->tm_sec); } static void __attribute__ ((__noreturn__)) usage(FILE * out) { - fputs(USAGE_HEADER, out); - fprintf(out, _(" %s [options]\n"), program_invocation_short_name); - fputs(USAGE_OPTIONS, out); - fputs(_(" -p, --pretty show uptime in pretty format\n"), out); - fputs(USAGE_HELP, out); - fputs(_(" -s, --since system up since\n"), out); - fputs(USAGE_VERSION, out); - fprintf(out, USAGE_MAN_TAIL("uptime(1)")); + fputs(USAGE_HEADER, out); + fprintf(out, _(" %s [options]\n"), program_invocation_short_name); + fputs(USAGE_OPTIONS, out); + fputs(_(" -p, --pretty show uptime in pretty format\n"), out); + fputs(USAGE_HELP, out); + fputs(_(" -s, --since system up since\n"), out); + fputs(USAGE_VERSION, out); + fprintf(out, USAGE_MAN_TAIL("uptime(1)")); - exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); + exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); } int main(int argc, char **argv) { - int c, p = 0; + int c, p = 0; - static const struct option longopts[] = { - {"pretty", no_argument, NULL, 'p'}, - {"help", no_argument, NULL, 'h'}, - {"since", no_argument, NULL, 's'}, - {"version", no_argument, NULL, 'V'}, - {NULL, 0, NULL, 0} - }; + static const struct option longopts[] = { + {"pretty", no_argument, NULL, 'p'}, + {"help", no_argument, NULL, 'h'}, + {"since", no_argument, NULL, 's'}, + {"version", no_argument, NULL, 'V'}, + {NULL, 0, NULL, 0} + }; #ifdef HAVE_PROGRAM_INVOCATION_NAME - program_invocation_name = program_invocation_short_name; + program_invocation_name = program_invocation_short_name; #endif - setlocale (LC_ALL, ""); - bindtextdomain(PACKAGE, LOCALEDIR); - textdomain(PACKAGE); - atexit(close_stdout); + setlocale (LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + atexit(close_stdout); - while ((c = getopt_long(argc, argv, "phsV", longopts, NULL)) != -1) - switch (c) { - case 'p': - p = 1; - break; - case 'h': - usage(stdout); - case 's': - print_uptime_since(); - return EXIT_SUCCESS; - case 'V': - printf(PROCPS_NG_VERSION); - return EXIT_SUCCESS; - default: - usage(stderr); - } + while ((c = getopt_long(argc, argv, "phsV", longopts, NULL)) != -1) + switch (c) { + case 'p': + p = 1; + break; + case 'h': + usage(stdout); + case 's': + print_uptime_since(); + return EXIT_SUCCESS; + case 'V': + printf(PROCPS_NG_VERSION); + return EXIT_SUCCESS; + default: + usage(stderr); + } - if (p) - printf("%s\n", sprint_uptime_short()); - else - printf("%s\n", sprint_uptime()); - return EXIT_SUCCESS; + if (p) + printf("%s\n", procps_uptime_sprint_short()); + else + printf("%s\n", procps_uptime_sprint()); + return EXIT_SUCCESS; } diff --git a/w.c b/w.c index 4aff8fb6..bf921555 100644 --- a/w.c +++ b/w.c @@ -586,7 +586,7 @@ int main(int argc, char **argv) if (header) { /* print uptime and headers */ - printf("%s\n", sprint_uptime()); + printf("%s\n", procps_uptime_sprint()); /* Translation Hint: Following five uppercase messages are * headers. Try to keep alignment intact. */ printf(_("%-*s TTY "), userlen, _("USER"));