Search code examples
clinuxpopenfgets

Command "who -b" behaves differently


I am trying to parse system boot time from "who -b" output.

#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>

void GetSystemTurnOnTime()
{
 FILE *fp;
 char *pTemp = NULL;
 char szBuffer[1024] = {0};
 int iLen;
 struct tm tm;
 char buf[255];

 fp = popen("who -b", "r");
 fgets(szBuffer, sizeof(szBuffer), fp);

 pTemp = szBuffer;
 while(isspace(*pTemp))
    ++pTemp;
 iLen = strlen(pTemp);
 pTemp[iLen - 1] = '\0';
 printf("%s\n", pTemp);

 pclose(fp);
}


int
main(void)
{
 GetSystemTurnOnTime();
 exit(EXIT_SUCCESS);
}

Output: In normal execution it gives : system boot 2015-07-31 11:08

If i integrate with daemon then it gives: Jul 31 11:08

if i execute daemon with gdb then it again gives: system boot 2015-07-31 11:08 can any one help me why it is giving two different format.


Solution

  • you can use following command:

    who -b | awk '{$1=""; $2=""; print $0}' | date -f -