Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this message"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
Post Follow-up to this messageOn 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
Post Follow-up to this messageOn 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.