Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Reading the utmp file
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


Report this thread to moderator Post Follow-up to this message
Old Post
Rodrick Brown
10-17-04 08:56 PM


Re: Reading the utmp file
"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




Report this thread to moderator Post Follow-up to this message
Old Post
Martin Blume
10-17-04 08:56 PM


Re: Reading the utmp file
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

Report this thread to moderator Post Follow-up to this message
Old Post
Rich Teer
10-17-04 08:56 PM


Re: Reading the utmp file
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

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Kerrisk
10-18-04 09:01 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:00 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.