Home > Archive > Unix Programming > October 2004 > Reading the utmp file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Reading the utmp file
|
|
| Rodrick Brown 2004-10-17, 3:56 pm |
| Why does the data come out all garbled
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <utmp.h>
int
main(void)
{
FILE *fp;
struct utmp *llog;
if ( (llog = malloc(sizeof(*llog))) == NULL)
{
perror("fatal: malloc() failed");
exit(EXIT_FAILURE);
}
if ( (fp = fopen(_PATH_LASTLOG,"r")) == NULL)
{
perror("fatal: fopen() failed");
exit(EXIT_FAILURE);
}
while(fread(&llog,sizeof(llog),1,fp) == 1)
{
fwrite(&llog,sizeof(llog),1,stdout);
}
return EXIT_SUCCESS;
}
--
Unix Systems Engineer
The City of New York
Dept. of Information Technology
http://www.nyc.gov/doitt
rbrown[(@)]doitt.nyc.gov
http://www.rodrickbrown.com
| |
| Martin Blume 2004-10-17, 3:56 pm |
| "Rodrick Brown" schrieb
> [reading utmp]
>
> while(fread(&llog,sizeof(llog),1,fp) == 1)
> {
> fwrite(&llog,sizeof(llog),1,stdout);
> }
>
> Why does the data come out all garbled
>
Because the file is binary, not a text file.
You'd have have to print out each member, like e.g.:
while(fread(&llog,sizeof(llog),1,fp) == 1)
{
printf("type %d\n", llog->ut_type);
printf("line %s\n", llog->ut_line);
// etc.etc.
} // as long as there are entries
HTH
Martin
| |
| Rich Teer 2004-10-17, 3:56 pm |
| On Sun, 17 Oct 2004, it was written:
> Why does the data come out all garbled
Becuase you're not using the correct funtions, e.g., getutxent,
and so on.
--
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming",
published in August 2004.
President,
Rite Online Inc.
Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
| |
| Michael Kerrisk 2004-10-18, 4:01 am |
| On Sun, 17 Oct 2004 15:39:22 GMT, Rodrick Brown
<rbrown[@]doitt.nyc.gov> wrote:
>Why does the data come out all garbled
>
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <unistd.h>
>#include <time.h>
>#include <utmp.h>
>
>int
>main(void)
>{
> FILE *fp;
> struct utmp *llog;
>
> if ( (llog = malloc(sizeof(*llog))) == NULL)
> {
> perror("fatal: malloc() failed");
> exit(EXIT_FAILURE);
> }
>
> if ( (fp = fopen(_PATH_LASTLOG,"r")) == NULL)
> {
> perror("fatal: fopen() failed");
> exit(EXIT_FAILURE);
> }
>
> while(fread(&llog,sizeof(llog),1,fp) == 1)
> {
> fwrite(&llog,sizeof(llog),1,stdout);
> }
>
> return EXIT_SUCCESS;
>}
You are reading from the lastlog file, but using utmp records. This
isn't right. Which do you intend:
1. reading utmp records from the utmp file, in which case you should
use getutent() and friends.
2. Reading from the lastlog file -- in which case you must read into
lastlog records. In this case, you can use read() (and ls () if
appropriate). You will need to check for unused records (which will
have empty string fields and 0 timestamp values).
As Martin Blume points out, you must also use suitable stdio I/O
functions to write output to stdaout in ASCII form.
Cheers,
Michael
|
|
|
|
|