Segmentation fault (core dumped)
#include<stdio.h>
#include<utmp.h>
#include<fcntl.h>
#include<unistd.h>
#define SHOWHOST /* include remote machine on output */
int main()
{
struct utmp current_record; /* read info int here */
int utmpfd; /* read from this descriptor */
int reclen = sizeof(current_record);
if(( utmpfd = open(UTMP_FILE, O_RDONLY)) == -1) {
perror(UTMP_FILE); /* UTMP_FILE is in utmp.h */
exit(1);
}
while(read(utmpfd, ¤t_record, reclen) == reclen)
show_info(¤t_record);
close(utmpfd);
return 0; /* went ok */
}
show_info(struct utmp *utbufp)
{
if( utbufp->ut_type != USER_PROCESS )
return;
printf("% -8.8s", utbufp->ut_name); /* the logname */
printf(" "); /* a space */
printf("% -8.8s", utbufp->ut_line); /* the tty */
printf(" ");
showtime(utbufp->ut_time);
#ifdef SHOWHOST
printf("( % s)", utbufp->ut_host); /* the host */
#endif
printf("\n"); /* new line */
}
void showtime( long timeval )
{
char *cp;
cp = ctime(&timeval);
printf("%12.12s",cp+4);
}
代码如上,调试问题如下:
有大神给个回复嘛,系统是ubuntu16.04,gcc编译