/* ECP: FILEname=fig12_17.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*/ fseek( Ufp, -UtmpSize, SEEK_END ); /*20*/ while( fread( &UtEntry, UtmpSize, 1, Ufp ) == 1 ) /*21*/ { /*22*/ char Name[ NameLen + 1 ] = "", Line[ LineLen + 1 ] = ""; /*23*/ if( !strncmp( UtEntry.ut_name, SearchName, NameLen ) ) /*24*/ { /*25*/ strncpy( Name, UtEntry.ut_name, NameLen ); /*26*/ strncpy( Line, UtEntry.ut_line, LineLen ); /*27*/ printf( "%-*s %-*s %26s", NameLen, Name, LineLen, /*28*/ Line, ctime( &UtEntry.ut_time ) ); /*29*/ HowMany++; /*30*/ } /*31*/ /* Back Up Past Current Structure */ /*32*/ if( fseek( Ufp, -UtmpSize * 2, SEEK_CUR ) == -1 ) /*33*/ break; /*34*/ } /*35*/ fclose( Ufp ); /*36*/ return HowMany; /*37*/ }