/* utxreader -- reads binary wtmpx-format files; prints attributes Copyright (C) 1999 Ed Cashin This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* utxreader * Ed Cashin, OIT, Sep. 1999 * * reads wtmpx-format binary files and prints fields in * colon-delimited ASCII format. */ #include #include #include #include #include #include #define TIMEBUFSIZ 128 int main(int argc, char *argv[]) { struct utmpx utx; int fd; char timebuf[TIMEBUFSIZ]; timebuf[TIMEBUFSIZ] = '\0'; if (argc < 2) { fprintf(stderr, "usage: %s {filename}\n", argv[0]); exit(EXIT_FAILURE); } if ( (fd = open(argv[1], O_RDONLY)) == -1) { perror(argv[1]); exit(EXIT_FAILURE); } while (read(fd, &utx, sizeof(utx)) == sizeof(utx)) { strftime(timebuf, TIMEBUFSIZ - 1, "%Y%m%d-%H%M%S", localtime(&utx.ut_tv.tv_sec)); printf("user=%s:fmttime=%s:host=%s:" "init_id=%s:device=%s:pid=%ld:proc_type=%d:" "term_status=%d:exit_status=%d:" "\n", utx.ut_user, timebuf, utx.ut_host, utx.ut_id, utx.ut_line, utx.ut_pid, utx.ut_type, utx.ut_exit.e_termination, utx.ut_exit.e_exit); } return 0; } /* from utmpx.h or something: char ut_user[32]; // user login name char ut_id[4]; // /etc/inittab id // (usually line #) char ut_line[32]; // device name (console, lnxx) pid_t ut_pid; // process id short ut_type; // type of entry struct exit_status ut_exit; // exit status of a process // marked as DEAD_PROCESS struct timeval ut_tv; // time entry was made long ut_session; // session ID, used for windowing long pad[5]; // reserved for future use short ut_syslen; // significant length of ut_host // including terminating null char ut_host[257]; // host name, if remote The structure exit status includes the following members: short e_termination; // termination status short e_exit; // exit status from /usr/include/sys/time.h: struct timeval { long tv_sec; // seconds long tv_usec; // and microseconds }; */