当前位置: 技术问答>linux和unix
编写who命令的错误!
来源: 互联网 发布时间:2016-04-02
本文导语: 源代码如下: 1 /*who2.c - a second version of the who program 2 * open ,read utmp file , and show results 3 */ 4 #include 5 #include 6 #include 7 #include ...
源代码如下:
错误提示:
/root/tmp/ccXmrdRj.o: In function `show_info':
/root/u_l_me/chapter_2/who2.c:42: undefined reference to `showtime'
collect2: ld 返回 1
自己的确是不知道那里错误了,请高手指教!
1 /*who2.c - a second version of the who program
2 * open ,read utmp file , and show results
3 */
4 #include
5 #include
6 #include
7 #include
8 #include
9 #define SHOWHOST
10
11 int main()
12 {
13 void show_info(struct utmp * utbufp );
14 void _showtime(long);
15 struct utmp current_record;
16 int utmpfd;
17 int reclen = sizeof ( current_record );
18 if (( utmpfd = open ( UTMP_FILE , O_RDONLY ) ) == -1 ) /* int fd = open ( char * name , int how ) */
19 {
20 perror( UTMP_FILE );
21 exit(1);
22 }
23 while ( read (utmpfd , ¤t_record, reclen ) == reclen ) /*ssize_t read(int fd, void *buf, size_t count);8*/
24 show_info( ¤t_record );
25 close( utmpfd );
26 return 0;
27 }
28
29 /*show info ()
30 * displays contexts of the utmp struct in human readdable form
31 * NOTE these sizes should not be hardwired
32 */
33
34 void show_info(struct utmp * utbufp )
35 {
36 if( utbufp->ut_type != USER_PROCESS)
37 return ;
38 printf( " %-8.8s " ,utbufp->ut_user );
39 printf( " " );
40 printf( " %-8.8s " ,utbufp->ut_line );
41 printf( " " );
42 showtime(utbufp->ut_time );
43 printf( " " );
44
45 #ifdef SHOWHOST
46 if ( utbufp->ut_user[0] != '' )
47 printf( " (%s)" ,utbufp->ut_host );
48 #endif
49 printf(" n " );
50 }
51
52
53 void showtime(long timeval )
54 /* displays time in a format fit for human consumption
55 * use ctime to build a string then picks parts out of it
56 * Note : % 12,12s prints a string 12 chars wide and LIMITS
57 * it to 12chars
58 */
59 {
60 char *cp ;
61 cp = ctime ( &timeval ) ; /* char *ctime(const time_t *clock);**/
62 printf(" %12.12s" , cp +4 );
63 }
64
65
错误提示:
/root/tmp/ccXmrdRj.o: In function `show_info':
/root/u_l_me/chapter_2/who2.c:42: undefined reference to `showtime'
collect2: ld 返回 1
自己的确是不知道那里错误了,请高手指教!
|
void showtime(long );
把这个声明加到main函数前面
把这个声明加到main函数前面
|
void _showtime(long);
showtime(utbufp->ut_time );
认真检查下代码..
showtime(utbufp->ut_time );
认真检查下代码..