LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-20-2007, 06:00 AM   #1
lovelylinux
LQ Newbie
 
Registered: Dec 2006
Posts: 17

Rep: Reputation: 0
Thumbs up 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.

Last edited by lovelylinux; 09-20-2007 at 06:07 AM. Reason: title is having less space, so problem with title length
 
Old 09-20-2007, 07:42 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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);
}
 
Old 09-20-2007, 09:14 AM   #3
lovelylinux
LQ Newbie
 
Registered: Dec 2006
Posts: 17

Original Poster
Rep: Reputation: 0
Thumbs up Program for Understanding the problem clearly

Quote:
Originally Posted by tronayne View Post
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;
}

/************************************************** ************************/
 
Old 09-20-2007, 10:01 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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().
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Program received signal SIGSEGV, Segmentation fault go939 Programming 4 08-22-2007 06:54 PM
Program received signal SIGSEGV, Segmentation fault dayalan_cse Linux - Newbie 8 12-15-2006 12:05 AM
Program received signal SIGSEGV, Segmentation fault ims_mca Linux - Distributions 0 03-09-2005 04:16 AM
Kaffeine 0.5 crashes - signal 11 (SIGSEGV) Toonses82 Linux - Software 2 02-02-2005 09:11 AM
signal handling (SIGSEGV) in threads Sinner6 Programming 1 11-09-2004 04:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration