Re: Using [uw]tmp
Re: Using [uw]tmp
- Subject: Re: Using [uw]tmp
- From: Jason Townsend <email@hidden>
- Date: Tue, 13 Dec 2005 20:47:34 -0800
On Dec 13, 2005, at 7:38 AM, Graham J Lee wrote:
I want to get login information for users (basically my goal is a
Bonjour-enabled equivalent to the old GNU or ICSI distributed
finger server[*], where any systems on the local network register
themselves with the server so I can find out if a user has logged
in anywhere on the local network) and as neither of the distributed
fingers come anywhere near close to compiling on Darwin (8.3.0)
have tried to start from scratch. Having observed that /var/run/
utmpx is empty, and that there's no getutent on Darwin, I'm doing
it the same way that programs like w(1) do and reading struct utmps
from the appropriate file. However, what I get back doesn't seem
consistent with the idea that utmp(5) [or indeed wtmp(5)] is just a
bunch of struct utmps stored back to back. Here's my code:
Graham,
It looks like your problem is that you're using sizeof on a pointer,
which is going to be 4, rather than the correct size, so you're
looking at the file four bytes at a time.
If you change to use sizeof(struct utmp) then this works as expected.
-Jason
#import <Foundation/Foundation.h>
#import <utmp.h>
#import <stdlib.h>
#import <stdio.h>
extern int errno;
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
FILE *utmp_file;
struct utmp *utmp_store;
errno=0;
if((utmp_file=fopen(_PATH_UTMP,"rb"))==NULL)
{
perror("csalutd: couldn't open utmp");
exit(EXIT_FAILURE);
}
if((utmp_store=(struct utmp *)malloc(sizeof(utmp_store)))==NULL)
{
perror("csalutd: couldn't malloc struct");
exit(EXIT_FAILURE);
}
while(fread(utmp_store,sizeof(utmp_store),1,utmp_file)==1)
{
printf("That fool %s, over %s way, was responsible.\nIt was at %
d on %s.\n",utmp_store->ut_name,utmp_store->ut_host,utmp_store-
>ut_time,utmp_store->ut_line);
}
fclose(utmp_file);
[pool release];
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden