/*
* who1.c - a first version of the who program
* open, read UTMP file, and show results
*/
#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#define SHOWHOST /* include remote machine on output */
/*
* show_info()
* displays contents of the utmp struct in human readable form
* *note* these sizes should not be hardwired
*/
show_info(struct utmp *utbufp)
{
if (utbufp->ut_type != USER_PROCESS) /* users only */
return ;
printf("% - 8.8s", utbufp->ut_name); /* the logname */
printf(" "); /* a space */
printf("% - 8.8s", utbufp->ut_line); /* the tty */
printf(" "); /* a space */
printf("% 10ld", utbufp->ut_time); /* login time */
printf(" "); /* a space */
#ifdef SHOWHOST
printf("(%s)", utbufp->ut_host); /* the host */
#endif
printf("\n");
}
int main()
{
struct utmp current_record; /* read info into here */
int utmpfd; /* read from this descriptor */
int reclen = sizeof(current_record);
if ((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1) {
perror(UTMP_FILE); /* UTMP_FILE is in utmp.h */
exit(1);
}
while (read(utmpfd, ¤t_record, reclen) == reclen)
show_info(¤t_record);
close(utmpfd);
return 0; /* went ok */
}
/*
* who2.c - read /etc/run/utmp and list info therein
* - suppresses empty records
* - formats time nicely
*/
#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
/* #define SHOWHOST */ /* include remote machine on output */
void showtime(long);
void show_info(struct utmp *);
int main()
{
struct utmp utbuf; /* read info into here */
int utmpfd; /* read from this descriptor */
int reclen = sizeof(utbuf);
if ((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1) {
perror(UTMP_FILE); /* UTMP_FILE is in utmp.h */
exit(1);
}
while (read(utmpfd, &utbuf, reclen) == reclen)
show_info(&utbuf);
close(utmpfd);
return 0; /* went ok */
}
/*
* show_info()
* displays the contents of the utmp struct
* in human readable form
* * displays nothing if record has no user name
*/
show_info(struct utmp *utbufp)
{
if (utbufp->ut_type != USER_PROCESS) /* users only */
return ;
printf("% - 8.8s", utbufp->ut_name); /* the logname */
printf(" "); /* a space */
printf("% - 8.8s", utbufp->ut_line); /* the tty */
printf(" "); /* a space */
showtime(utbufp->ut_time); /* display time */
#ifdef SHOWHOST
if (utbuf->ut_host[0] != '\0')
printf("(%s)", utbufp->ut_host); /* the host */
#endif
printf("\n");
}
void showtime(long timeval)
/*
* displays time in a format fit for human consumption
* uses ctime to build a string then picks parts out of it
* Note: %12.12s prints a string 12 chars wide an LIMITS
* it to 12chara.
*/
{
char *cp; /* to hold address of time */
cp = ctime(&timeval); /* convert time to string */
/* string looks like */
/* Mon Feb 4 00:46:40 EST 1991 */
/* 012345678912345. */
printf("% 12.12s", cp + 4); /* pick 12 chars from pos 4 */
}
/*
* who3.c - who with buffered reads
* - suppresses empty records
* - formats time nicely
* - buffers input (using utmplib)
*/
#include <stdio.h>
#include <sys/types.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#define SHOWHOST /* include remote machine on output */
void showtime(long);
void show_info(struct utmp *);
int main()
{
struct utmp *utbufp, /* holds pointer to next rec */
*utmp_next(); /* returns pointer to next */
if (utmp_open(UTMP_FILE) == -1) {
perror(UTMP_FILE); /* UTMP_FILE is in utmp.h */
exit(1);
}
while ((utbufp = utmp_next()) != ((struct utmp *)NULL))
show_info(&utbufp);
utmp_close();
return 0; /* went ok */
}
/*
* show_info()
* displays the contents of the utmp struct
* in human readable form
* * displays nothing if record has no user name
*/
show_info(struct utmp *utbufp)
{
if (utbufp->ut_type != USER_PROCESS) /* users only */
return ;
printf("% - 8.8s", utbufp->ut_name); /* the logname */
printf(" "); /* a space */
printf("% - 8.8s", utbufp->ut_line); /* the tty */
printf(" "); /* a space */
showtime(utbufp->ut_time); /* display time */
#ifdef SHOWHOST
if (utbuf->ut_host[0] != '\0')
printf("(%s)", utbufp->ut_host); /* the host */
#endif
printf("\n");
}
void showtime(long timeval)
/*
* displays time in a format fit for human consumption
* uses ctime to build a string then picks parts out of it
* Note: %12.12s prints a string 12 chars wide an LIMITS
* it to 12chara.
*/
{
char *cp; /* to hold address of time */
cp = ctime(&timeval); /* convert time to string */
/* string looks like */
/* Mon Feb 4 00:46:40 EST 1991 */
/* 012345678912345. */
printf("% 12.12s", cp + 4); /* pick 12 chars from pos 4 */
}
/*
* cp1.c
* version 1 of cp - uses read and write with tunable buffer size
*
* usage: cp1 src dest
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define BUFFERSIZE 4096
#define COPYMODE 0644
void oops(char *, char *);
main(int ac, char *av[])
{
int in_fd, out_fd, n_chars;
char buf[BUFFERSIZE];
/* check args */
if (ac != 3)
{
fprintf(stderr, "usage: %s source destination\n", *av);
exit(1);
}
/* open files */
if ((in_fd = open(av[1], O_RDONLY)) == -1)
oops("Cannot open ", av[1]);
if ((out_fd = creat(av[2], COPYMODE)) == -1)
oops("Cannot creat", av[2]);
/* copy files */
while ((n_chars = read(in_fd, buf, BUFFERSIZE)) > 0)
if (write(out_fd, buf, n_chars) != n_chars)
oops("Write error to ", av[2]);
if (n_chars == -1)
oops("Read error from ", av[1]);
/* close files */
if (close(in_fd) == -1 || close(out_fd) == -1)
oops("Error closing files", "");
}
void oops(char *s1, char *s2)
{
fprintf(stderr, "Error: %s ", s1);
perror(s2);
exit(1);
}