0

Perhaps a trivial question, but I found myself in the situation of needing to strip the leading space off the output of uptime and it got me wondering why it was there in the first place.

An example from a Debian Stretch EC2 instance:

admin@ip-88-88-88-88:~$ uptime
 22:14:50 up 7 min,  1 user,  load average: 1.61, 1.21, 0.60
admin@ip-88-88-88-88:~$

I thought maybe it was padding, but for what? A three-digit hour for the clock?

2

It's possible the present-day GNU/Linux versions of uptime are trying to be compatible with the old BSD uptime from ~1980, which used the %3d format to print the hours portion of the time (even though that value is never more than 24, 2 digits). The same function was used to print both the current time-of-day and the system uptime.

if (tim >= 60) {
    printf("%3d:", tim/60);
    didhrs++;
} else {
    printf("    ");
}
tim %= 60;
if (tim > 0 || didhrs) {
    printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
} else {
    printf("  ");
}

The fact that it printed zero values as sequences of spaces (rather than printing nothing) may indicate the code was taken from another program where precise column positioning was needed.

  • It has been strftime() with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSD ps was using getopt() and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility. – JdeBP Mar 22 at 1:44
2

Because the code explicitly puts it there:

pos = sprintf(buf, " %02d:%02d:%02d ",
  realtime->tm_hour, realtime->tm_min, realtime->tm_sec);

The uptime executable from the procps package (on e.g. Ubuntu) calls a function called print_uptime(). This function lives in proc/whattime.c in the procps sources. The function just outputs a string built by sprint_uptime() in the same C source file which constructs the output string bit by bit.

The first bit that is added to the string is added with an initial space, as shown above.

The space has been there since at least 2002 in the procps implementation of uptime.

Note that uptime -p does not output a space:

$ uptime -p
up 8 weeks, 8 hours, 41 minutes

$ uptime --version
uptime from procps-ng 3.3.12

As noted by maxxvw, the GNU coreutils version of the utility uses a similar kind of output.

The uptime utility on BSD systems does not follow the same output format:

$ uptime
11:56PM  up 23:58, 1 user, load averages: 0.04, 0.02, 0.00
1

Coreutils source code is here: https://github.com/coreutils/coreutils/blob/master/src/uptime.c

As you can see, it is just as the authors decided to print it, a space before current time, two spaces after uptime, none around users connected and two spaces before load average:

fprintftime (stdout, _(" %H:%M:%S  "), tmn, 0, 0);

printf (_("up  %2d:%02d,  "), uphours, upmins);

printf (ngettext ("%lu user", "%lu users", select_plural (entries)),

printf (_(",  load average: %.2f"), avg[0]);
  • @jsotola No, the spaces are explicit in the calls to printf(). – Kusalananda Mar 21 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.