MCPcopy Create free account
hub / github.com/coreutils/coreutils / print_uptime

Function print_uptime

src/uptime.c:39–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37 proper_name ("Kaveh Ghazi")
38
39static int
40print_uptime (idx_t n, STRUCT_UTMP const *utmp_buf)
41{
42 int status = EXIT_SUCCESS;
43 time_t boot_time = 0;
44
45 /* Loop through all the utmp entries we just read and count up the valid
46 ones, also in the process possibly gleaning boottime. */
47 idx_t entries = 0;
48 for (idx_t i = 0; i < n; i++)
49 {
50 STRUCT_UTMP const *this = &utmp_buf[i];
51 entries += IS_USER_PROCESS (this);
52 if (UT_TYPE_BOOT_TIME (this))
53 boot_time = this->ut_ts.tv_sec;
54 }
55 /* The gnulib module 'readutmp' is supposed to provide a BOOT_TIME entry
56 on all platforms. */
57 if (boot_time == 0)
58 {
59 error (0, errno, _("couldn't get boot time"));
60 status = EXIT_FAILURE;
61 }
62
63 time_t time_now = time (NULL);
64 struct tm *tmn = time_now == (time_t) -1 ? NULL : localtime (&time_now);
65 /* procps' version of uptime also prints the seconds field, but
66 previous versions of coreutils don't. */
67 if (tmn)
68 /* TRANSLATORS: This prints the current clock time. */
69 fprintftime (stdout, _(" %H:%M:%S "), tmn, (timezone_t) 0, 0);
70 else
71 {
72 printf (_(" ??:???? "));
73 status = EXIT_FAILURE;
74 }
75
76 intmax_t uptime;
77 if (time_now == (time_t) -1 || boot_time == 0
78 || ckd_sub (&uptime, time_now, boot_time) || uptime < 0)
79 {
80 printf (_("up ???? days ??:??, "));
81 status = EXIT_FAILURE;
82 }
83 else
84 {
85 intmax_t updays = uptime / 86400;
86 int uphours = uptime % 86400 / 3600;
87 int upmins = uptime % 86400 % 3600 / 60;
88 if (0 < updays)
89 printf (ngettext ("up %jd day %2d:%02d, ",
90 "up %jd days %2d:%02d, ",
91 select_plural (updays)),
92 updays, uphours, upmins);
93 else
94 printf (_("up %2d:%02d, "), uphours, upmins);
95 }
96

Callers 1

uptimeFunction · 0.85

Calls 1

select_pluralFunction · 0.85

Tested by

no test coverage detected