/* ECP: FILEname=fig12_15.c */ /* 1*/ #include /* 2*/ #include /* 3*/ #include /* 4*/ static const char *Wtmp = "/var/adm/wtmp"; /* 5*/ static const UtmpSize = sizeof( struct utmp ); /* 6*/ #define NameLen 8 /* 7*/ #define LineLen 8 /* 8*/ int /* 9*/ Logins( const char *SearchName ) /*10*/ { /*11*/ int HowMany = 0; /*12*/ struct utmp UtEntry; /*13*/ FILE *Ufp; /*14*/ if( ( Ufp = fopen( Wtmp, "rb" ) ) == NULL ) /*15*/ { /*16*/ fprintf( stderr, "Can not open %s\n", Wtmp ); /*17*/ return -1; /*18*/ } /*19*/ while( fread( &UtEntry, UtmpSize, 1, Ufp ) == 1 ) /*20*/ { /*21*/ char Name[ NameLen + 1 ] = "", Line[ LineLen + 1 ] = ""; /*22*/ if( strncmp( UtEntry.ut_name, SearchName, NameLen ) == 0 ) /*23*/ { /*24*/ strncpy( Name, UtEntry.ut_name, NameLen ); /*25*/ strncpy( Line, UtEntry.ut_line, LineLen ); /*26*/ printf( "%-*s %-*s %26s", NameLen, Name, LineLen, /*27*/ Line, ctime( &UtEntry.ut_time ) ); /*28*/ HowMany++; /*29*/ } /*30*/ } /*31*/ fclose( Ufp ); /*32*/ return HowMany; /*33*/ }