Displaying fields in wtmp file??

This is a discussion on Displaying fields in wtmp file?? within the Linux Programming forums, part of the Platform Specific Boards category; Anyone have any idea on how to display the fields stored in the wtmp file using C? Am I correct ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    2

    Displaying fields in wtmp file??

    Anyone have any idea on how to display the fields stored in the wtmp file using C? Am I correct in thinking that the info stored in wtmp is in binary and that utmpx will be of some help?

    Thanks.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I don't know what a wtmp file is besides what I just looked up. I have never heard of it, but on the man page they have a structure there. I would assume you fread that structure and then you would be able to get the info you want to display.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    2

    Reporting IP address

    Thanks. Ok, this code in C performs the functions of the last command. It displays some of the fields from the wtmp file. Instead of reporting the hostname, I'd like to report the IP address. From the man pages, it looks as though the host command will find IP addresses. But, do I need to use pipes to perform this action? Maybe something like read_fp = popen("host", "r"). Of course, the IP address field may be empty and in this case I can report something such as "empty" and perform no DNS lookup.

    Code:
    #include <utmp.h>
    #include <utmpx.h>
    #include <stdio.h>
    #include <iostream>
    
    #define WTMP_FILENAME "/var/log/wtmp"
    
    int main()
    {
    
            struct utmp record;
            FILE *fd = fopen(WTMP_FILENAME, "r");
    
            if (fd == NULL) {
                    perror(WTMP_FILENAME);
                    return 1;
            }
            while (fread(&record, sizeof record, 1, fd) != 0) {
                    printf("%s: %s\t: %s\t", record.ut_line, record.ut_user, record.ut_id);
            }
            return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21