Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top