473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to read from the binary file of wtmpx.dat (unix log information )

Helllo.
this is biginner programmer.
Would u check the file that I programed on c#(winform)?

I tryed to make the program reading the binary file by C# programming but I
failed.

If u have more free time, would u program for ur skill up and me ^^;
below contens is programed by unix c.
#include <stdio.h>
#include <utmpx.h>
#include <errno.h>
#include <time.h>

main(int argc,char *argv[])
{
FILE *fp;
struct utmpx buffer;
int count = 0;
char yn;
struct tm *wtm;
char title[1024];

if(argc != 2){
printf("Usage : %s wtmpx\n",argv[0]);
exit(-1);
}

if((fp = fopen(argv[1],"r")) == NULL){
printf("FILE OPEN FAIL errno[%d]\n",errno);
exit(-1);
}
memset(title,0x00,sizeof(title));
sprintf(title,"%10s %32s %4s %32s %4s %4s %4s %4s %32s %257s\n",
"COUNT","USER","ID","DEVICE NAME","PID",
"TYPE","TERM","EXIT","TIME","HOST");
memset(&buffer,0x00,sizeof(buffer));
printf("%s",title);

printf("========================================== ================\n");
while(fread(&buffer,sizeof(buffer),1,fp) != 0){
count++;
printf("%10d ",count);
printf("%32s ",buffer.ut_user);
printf("%4s ",buffer.ut_id);
printf("%32s ",buffer.ut_line);
printf("%4d ",buffer.ut_pid);
printf("%4d ",buffer.ut_type);
printf("%4d ",buffer.ut_exit.e_termination);
printf("%4d ",buffer.ut_exit.e_exit);
printf("%32s ",ctime((const time_t *)&buffer.ut_tv));
printf("%257s ",buffer.ut_host);
printf("\n");
if(!(count%10)){

printf("========================================== =====;
printf("continue([y]/n)?");
yn=getchar();
if(yn == 'N' || yn == 'n'){
break;
}
printf("%s",title);
}
memset(&buffer,0x00,sizeof(buffer));
}
printf("END\n");
}
************************************************** **************************
****************************************
UTMPX Structure.
************************************************** **************************
****************************************
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */

/* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T */
/* The copyright notice above does not evidence any */
/* actual or intended publication of such source code. */

/*
* Copyright (c) 1996-1997, by Sun Microsystems, Inc.
* All rights reserved.
*/
#ifndef _UTMP_H
#define _UTMP_H

#pragma ident "@(#)utmp.h 1.17 97/08/23 SMI" /* SVr4.0 1.5.1.7 */

/*
* Note: The getutent(3c) family of interfaces are obsolete.
* The getutxent(3c) family provide a superset of this functionality
* and should be used in place of getutent(3c).
*/

#include <sys/types.h>

#ifdef __cplusplus
extern "C" {
#endif

#if !defined(_XPG4_2) || defined(__EXTENSIONS__)
#define UTMP_FILE "/var/adm/utmp"
#define WTMP_FILE "/var/adm/wtmp"
#endif

#define ut_name ut_user

#if !defined(_XPG4_2) || defined(__EXTENSIONS__)
struct exit_status {
short e_termination; /* Process termination status */
short e_exit; /* Process exit status */
};
#else
struct ut_exit_status {
short ut_e_termination; /* Process termination status */
short ut_e_exit; /* Process exit status */
};
#endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */

#if !defined(_XPG4_2) || defined(__EXTENSIONS__)

/*
* This data structure describes the utmp entries returned by
* the getutent(3c) family of APIs. It does not (necessarily)
* correspond to the contents of the utmp or wtmp files.
*
* Applications should only interact with this subsystem via
* the getutxent(3c) family of APIs, as the getutent(3c) family
* are obsolete.
*/
struct utmp {
char ut_user[8]; /* User login name */
char ut_id[4]; /* /etc/inittab id(usually line #)
*/
char ut_line[12]; /* device name (console, lnxx) */
short ut_pid; /* short for compat. - process id */
short ut_type; /* type of entry */
struct exit_status ut_exit; /* The exit status of a process */
/* marked as DEAD_PROCESS. */
time_t ut_time; /* time entry was made */
};

#include <sys/types32.h>
#include <inttypes.h>

/*
* This data structure describes the utmp *file* contents using
* fixed-width data types. It should only be used by the implementation.
*
* Applications should use the getutxent(3c) family of routines to interact
* with this database.
*/

struct futmp {
char ut_user[8]; /* User login name */
char ut_id[4]; /* /etc/inittab id */
char ut_line[12]; /* device name (console, lnxx) */
int16_t ut_pid; /* process id */
int16_t ut_type; /* type of entry */
struct {
int16_t e_termination; /* Process termination status */
int16_t e_exit; /* Process exit status */
} ut_exit; /* The exit status of a process */
time32_t ut_time; /* time entry was made */
};

#endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */

/* Definitions for ut_type */

#define EMPTY 0
#define RUN_LVL 1
#define BOOT_TIME 2
#define OLD_TIME 3
#define NEW_TIME 4
#define INIT_PROCESS 5 /* Process spawned by "init" */
#define LOGIN_PROCESS 6 /* A "getty" process waiting for login */
#define USER_PROCESS 7 /* A user process */
#define DEAD_PROCESS 8

#if !defined(_XPG4_2) || defined(__EXTENSIONS__)

#define ACCOUNTING 9

#define UTMAXTYPE ACCOUNTING /* Largest legal value of ut_type */

/* Special strings or formats used in the "ut_line" field when */
/* accounting for something other than a process. */
/* No string for the ut_line field can be more than 11 chars + */
/* a NULL in length. */

#define RUNLVL_MSG "run-level %c"
#define BOOT_MSG "system boot"
#define OTIME_MSG "old time"
#define NTIME_MSG "new time"
#define PSRADM_MSG "%03d %s" /* processor on or off */

/* Define and macro for determing if a normal user wrote the entry */
/* and marking the utmpx entry as a normal user */
#define NONROOT_USR 2
#define nonuser(ut) ((ut).ut_exit.e_exit == NONROOT_USR ? 1 : 0)
#define setuser(ut) ((ut).ut_exit.e_exit = NONROOT_USR)
#if defined(__STDC__)
extern void endutent(void);
extern struct utmp *getutent(void);
extern struct utmp *getutid(const struct utmp *);
extern struct utmp *getutline(const struct utmp *);
extern struct utmp *pututline(const struct utmp *);
extern void setutent(void);
extern int utmpname(const char *);
#else
extern void endutent();
extern struct utmp *getutent();
extern struct utmp *getutid();
extern struct utmp *getutline();
extern struct utmp *pututline();
extern void setutent();
extern int utmpname();
#endif

#endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */

#ifdef __cplusplus
}
#endif

#endif /* _UTMP_H */
Nov 15 '05 #1
0 4804

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Phil Powell | last post by:
I have a PHP script that would read in a binary file and display it as if it were <img src>, how would you do that w/o changing the header's MIME type? The entire file does not need to be changed....
3
by: DrewM | last post by:
I'm using ASP to generate an RTF (rich text) file, via the FSO. No problems with text, but when it comes to inserting images, the files have to be embedded as either binary or hex. Try as I...
2
by: Pete | last post by:
Hi Could someone kindly help with the C# equivilent of the following 4 lines of C code. I'm *really* struggling with this. ( Colours.dat contains 300 RGB values ) COLORREF Colours;
8
by: S Shulman | last post by:
Hi All I need to read a file to an array of bytes (any type of file) I tried using FileStream and BinaryReader but it doesn't seem to work Thank you, Shmuel
1
by: Quinn | last post by:
Hi all, I have some binary files in the following format: text line 1 text line 2 .... text line N end of text single in binary 1 single in binary 2 single N EOF
4
by: Matrixinline | last post by:
Hi All Here is my problem I am using a Unicode project and I tried to read the File like sPath = LPCTSTR; FILE* oFp = _tfopen(sPath,L"r"); while(!feof(oFp))
2
by: kowndinya | last post by:
Hello, i want to read values from a binary file in VisualBasic 6.0 Initially i want to read only header of this binary file. I know this structure for this header. It is 100 bytes long and...
2
by: tulasisridhar | last post by:
Hi All, I have binary file created in Unix I now need to read it using VB.net and doo some procesing. There is some conversion between bigendian and little endians. I know the structure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.