2

I've been able to retrieve the uptime value from /proc/uptime (which is in seconds). However, I need to retrieve the last boot time stamp using C. (I cannot use system(...) function to call uptime.)

For example, when I run the uptime command, the value I get is:

15:31:35 up 2 days,  4:14,  3 users,  load average: 0.04, 0.05, 0.05

I need that first part: 15:31:35. Is there a built-in C function that can get me this?

Jay Sullivan
  • 17,332
  • 11
  • 62
  • 86
Jake Z
  • 1,113
  • 1
  • 12
  • 22

2 Answers2

1

Open /proc/uptime and read it. The first number is the uptime in seconds.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 2
    I know..I have that value. That's easy. I just don't know how to get the exact HH:MM:SS time stamp. If I convert the seconds in the file, to HH:MM:SS, it will give me total hours and minutes and seconds the computer has been on, instead of the actual time stamp of when the computer started, ya know what I Mean? – Jake Z Oct 06 '13 at 20:54
  • @JakeZ So then is your question "How can I determine what time it was some number of seconds ago?"? If so, read up on `time(3)` and `localtime(3)`. – David Schwartz Oct 06 '13 at 20:58
0

Reading /proc/uptime is good.

Or you can use FILE *fp = popen("uptime", "r") and read from fp the string you want.

That's a little more portable. ( By that I mean different unix's like MacOS ).

Oh, I just saw your lack of "system" requirement. Never mind.

Charlie Burns
  • 6,994
  • 20
  • 29