hi..pls help me with this ...when i run this c program i get in result segmentation fault message (i run in ubuntu terminal):

#include<stdio.h>
#include<utmp.h>
int main()  {
       char *s,*c;
       struct utmp *u;
       int i;
       c=getlogin();
       setutent();
       u=getutent();
       while(u!=NULL)  {
          if(u->ut_type==7 && strcmp(u->ut_user,c)==0) {
               printf("%-12s",u->ut_user);
               printf("%-9s",u->ut_line);
               s=ctime(&u->ut_time);
               for(i=4;i<16;i++)
               printf("%c",s[i]);
               printf("(%s",u->ut_host);
               printf(") ");
          }
       u=getutent();
       }
}

and when compile i get warnings:
gcc -o who who.c
who.c: In function ‘main’:
who.c:7: warning: assignment makes pointer from integer without a cast
who.c:14: warning: assignment makes pointer from integer without a cast

i must mention that programs must return the same as the command who (linux)
thx...and sry for bad english

hi..pls help me with this ...when i run this c program i get in result segmentation fault message (i run in ubuntu terminal):

#include<stdio.h>
#include<utmp.h>
int main()  {
       char *s,*c;
       struct utmp *u;
       int i;
       c=getlogin();
       setutent();
       u=getutent();
       while(u!=NULL)  {
          if(u->ut_type==7 && strcmp(u->ut_user,c)==0) {
               printf("%-12s",u->ut_user);
               printf("%-9s",u->ut_line);
               s=ctime(&u->ut_time);
               for(i=4;i<16;i++)
               printf("%c",s[i]);
               printf("(%s",u->ut_host);
               printf(") ");
          }
       u=getutent();
       }
}

and when compile i get warnings:
gcc -o who who.c
who.c: In function ‘main’:
who.c:7: warning: assignment makes pointer from integer without a cast
who.c:14: warning: assignment makes pointer from integer without a cast

i must mention that programs must return the same as the command who (linux)
thx...and sry for bad english

I quickly look at your program and this works(I would clean up the hack)

#include<stdio.h>
#include<utmp.h>
#include <unistd.h>
#include <time.h>

int main()  {
       char *s,*c;
       struct utmp *u;
       int i;
       c=getlogin();
       setutent();
       u=getutent();
       while(u!=NULL)  {
          if(u->ut_type==7 && strcmp(u->ut_user,c)==0) {
               printf("%-12s",u->ut_user);
               printf("%-9s",u->ut_line);
               s=ctime((const time_t*)&u->ut_time);	//this was a quick hack
							//you could probably do it better
               for(i=4;i<16;i++)
               printf("%c",s[i]);
               printf("(%s",u->ut_host);
               printf(") ");
          }
       u=getutent();
       }
}

Also main should return 0;
and strcmp should include <string.h>

ok thx...problem solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.