diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2011-10-14 04:55:05 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2011-10-14 04:55:05 +0200 |
commit | 6aa0e0017d7d0cddc006da885946934b06949a91 (patch) | |
tree | 66b688ec32e2f91266db760b1762f2a50cc52036 /libslang/examples/utmp.sl | |
parent | a966db5b71328f6adf9dd767e64b322a3bd7ed9c (diff) | |
download | erlang-slang-fork-6aa0e0017d7d0cddc006da885946934b06949a91.tar.gz |
include libslang-1.4.9 and automatically build it and link erlang-slang against it
few (erlang) people will still have libslang-1.4.9 installed or spend time
to get it to link against the driver
Diffstat (limited to 'libslang/examples/utmp.sl')
-rw-r--r-- | libslang/examples/utmp.sl | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/libslang/examples/utmp.sl b/libslang/examples/utmp.sl new file mode 100644 index 0000000..b7a69af --- /dev/null +++ b/libslang/examples/utmp.sl @@ -0,0 +1,67 @@ +% This file illustrates how to read a binary file into a structure. In this +% case, the file is the Unix utmp file. +% +% Note that the format of the utmp file will vary with the OS. The format +% encoded here is for glibc Linux, but even that may be version-dependent. + +variable format, size, fp, buf; + +variable is_glibc = 1; + +#ifeval is_glibc +typedef struct +{ + ut_type, ut_pid, ut_line, ut_id, + ut_user, ut_host, ut_exit, ut_session, ut_tv, ut_addr +} UTMP_Type; +% The ut_tv is a timeval structure which has the format: l2 +% Also the ut_exit field is a struct of h2 +format = pad_pack_format ("h i S32 S4 S32 S256 h2 l l2 k4 x20"); +#else +typedef struct +{ + ut_type, ut_pid, ut_line, ut_id, + ut_time, ut_user, ut_host, ut_addr +} UTMP_Type; +format = pad_pack_format ("h i S12 S2 l S8 S16 l"); +#endif + +size = sizeof_pack (format); +vmessage ("Sizeof of utmp line: %d bytes", size); + +define print_utmp (u) +{ + () = fprintf (stdout, "%-16s %-12s %-16s %s\n", + u.ut_user, u.ut_line, u.ut_host, +#ifeval is_glibc + ctime (u.ut_tv[0]) +#else + ctime (u.ut_time) +#endif + ); +} + +variable Utmp_File; +foreach (["/var/run/utmp", "/var/log/utmp"]) +{ + Utmp_File = (); + fp = fopen (Utmp_File, "rb"); + if (fp != NULL) + break; +} + +if (fp == NULL) error ("Unable to open utmp file"); + +() = fprintf (stdout, "%-16s %-12s %-16s %s\n", + "USER", "TTY", "FROM", "LOGIN@"); + +variable U = @UTMP_Type; + +while (size == fread (&buf, Char_Type, size, fp)) +{ + set_struct_fields (U, unpack (format, buf)); + print_utmp (U); +} + +() = fclose (fp); + |