title: How to get user's last login including the year in C
body: <p>I'm trying to write a program that can get the user's last login time. Then I want to do something with the data. E.g. check which user haven't logged in for 90 days. </p>
  
  <p>So I tried using <code>last</code> command, this works great. Just that it doesn't display the year. So if I would do the checking on January 2014, how would I know the last login is actually would be on Dec 2013 or Dec 2012?</p>
  
  <p>This is an example of the code:</p>
  
  <pre><code>FILE *file1;
  extern FILE *popen();
  char command[1024];
  char *username;
  char result[1024];
  char buff[1024];
  
  username = "fikrie";
  sprintf(command,"last %s",username);
  file1 = popen(command, "r");
  while(fgets(result, sizeof(result), file1)!=NULL){
     sprintf(buff,"%s",result);
  }
  pclose(file1);
  </code></pre>
  
  <p>After some googling, I found out there's a -F flag to display the year. Unfortunately, it doesn't work on HP-UX, Solaris. These platform doesn't recognize that flag. </p>
  
  <p>I've also tested to used utmp based from this link <a href="http://www.unix.com/emergency-unix-and-linux-support/138430-last-command-year.html" rel="nofollow">here</a> .The problem is this code will fail on Solaris and HP-UX. Seems like ut_tv doesn't exist in utmp struct.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;utmp.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  
  int main(void) {
      struct utmp *line;
      time_t timestamp;
      utmpname("/var/log/wtmp");
      setutent();
      while( (line = getutent()) != NULL) {
              if (line-&gt;ut_type == USER_PROCESS ) {
                      timestamp = line-&gt;ut_tv.tv_sec;
                      printf("%s %s", line-&gt;ut_user, asctime(localtime(&amp;timestamp)));
              }
      }
      endutent();
      return 0;
  }
  </code></pre>
  
  <p>So, is there a way to do a C code that can generally get the last login for all the Unix platform. (Solaris, Redhat/Linux/CentOS, AIX, HP-UX, Ubuntu). <br />
  Seems like when I manage to get it on 1 platform, then it fails on the others. </p>