LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   localtime() function is having problem with getenv(), received signal SIGSEGV, so seg (https://www.linuxquestions.org/questions/linux-software-2/localtime-function-is-having-problem-with-getenv-received-signal-sigsegv-so-seg-586027/)

lovelylinux 09-20-2007 06:00 AM

localtime() having problem with getenv(), received signal SIGSEGV, segmentation fault
 
Hello to all,

I am using localtime() funtion to get information of of time.

But the program is having segmentation fault.

It gives the following type of error from debugger.

/******************************************************************/

Program received signal SIGSEGV, Segmentation fault.
0x00a46100 in getenv () from /lib/tls/libc.so.6

#0 0x00a46100 in getenv () from /lib/tls/libc.so.6
#1 0x00aafbcf in tzset_internal () from /lib/tls/libc.so.6
#2 0x00ab07a8 in __tz_convert () from /lib/tls/libc.so.6

/******************************************************************/

Same problem is there is I am using either ctime() or gmtime().

So what should I have to use instead of these function to get time.

Or What precautions I have to take.

Please help me if possible.

Any good reply is apprecialble.

Thanking You in advance.

tronayne 09-20-2007 07:42 AM

Here's a little demo:
Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main    (void)
{
        time_t  now;
        struct  tm      *tm;

        (void) time (&now);
        tm = localtime (&now);
        /*      show a two-digit year          */
        (void) fprintf (stdout, "Today is %02d/%02d/%02d\n",
            tm->tm_mon+1, tm->tm_mday, tm->tm_year % 100);
        /*      show a four-digit year          */
        (void) fprintf (stdout, "Today is %02d/%02d/%d\n",
            tm->tm_mon+1, tm->tm_mday, tm->tm_year + 1900);
        exit (EXIT_SUCCESS);
}


lovelylinux 09-20-2007 09:14 AM

Program for Understanding the problem clearly
 
Quote:

Originally Posted by tronayne (Post 2897802)
Here's a little demo:
Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main    (void)
{
        time_t  now;
        struct  tm      *tm;

        (void) time (&now);
        tm = localtime (&now);
        /*      show a two-digit year          */
        (void) fprintf (stdout, "Today is %02d/%02d/%02d\n",
            tm->tm_mon+1, tm->tm_mday, tm->tm_year % 100);
        /*      show a four-digit year          */
        (void) fprintf (stdout, "Today is %02d/%02d/%d\n",
            tm->tm_mon+1, tm->tm_mday, tm->tm_year + 1900);
        exit (EXIT_SUCCESS);
}



First of all Very Very Thanks for your reply :

But problem is somewhat different.

My Program is like Below :

with my program I am having problem with localtime() function.

so what should be the solution or what is the alternative for this localtime(), ctime(), and gmtime() so that the problem or getenv() is solved and segmentation fault problem is solved.

Please help me if possible.

/************************************************** ********************/

#include <stdio.h>
#include <string.h>
#include <utmp.h>
#include <paths.h>
#include <time.h>

struct login
{
char username[20];
char logintime[30];
char logouttime[30];
}user[100];

int main(int argc, char *argv[])
{
int i = 0;
char cTime[64] = { '\0' };
struct tm *Time;
struct utmp *entry;
utmpname(_PATH_WTMP);
setutent();

while(entry = getutent())
{
if( strcmp(entry->ut_line,":0") == 0)
{
Time = localtime(&(entry->ut_time));// with localtime the program is having problem as mentioned above
strftime(cTime,sizeof cTime,"%F %T",Time);
if(strcmp(entry->ut_user,"\0") != 0)
{
strcpy(user[i].username, "");
strcpy(user[i].logintime, "");
strcpy(user[i].username, entry->ut_user);
strcpy(user[i].logintime, cTime);
}
else
{
strcpy(user[i].logouttime, "");
strcpy(user[i].logouttime, cTime);
i++;
}
}
}
int p;
for(p = 0 ; p < i ; p++)
{
printf(" %s\t\t %s\t\t %s\n", user[p].username, user[p].logintime, user[p].logouttime);
}
endutent();
return 0;
}

/************************************************** ************************/

tronayne 09-20-2007 10:01 AM

You haven't called the time() function anywhere to get the time. The localtime() function is called with the number-of-seconds since the epoch and converts that into the elements of the tm structure. The example above is how to use time() and localtime().


All times are GMT -5. The time now is 04:11 AM.