Fix potential buffer overflow.
[userinfo.git] / contrib / lastusers.sh
blob848844f78511c879df9f88a2c3875d7bed9d5869
1 #!/bin/sh
3 # Find users whose last login is greater or less than the specified arguments.
4 # Needs the userinfo utility from http://userinfo.sourceforge.net/.
6 # Copyright (c) 2003-2004 Ben Kibbey <bjk@arbornet.org>
7 # Distributed under the terms of the General Public License version 2 or later.
9 # $Log: lastusers.sh,v $
10 # Revision 2.0 2004-12-05 19:44:23 bjk
11 # Initial revision
13 # Revision 1.6 2004/11/21 18:48:07 bjk
14 # Updated to work with userinfo 2.0 (included in the archive).
16 # Revision 1.5 2004/05/01 19:10:37 bjk
17 # Changed option -b to -l.
19 # Displaying users who have never logged in is off by default. Specify with -n.
21 # Revision 1.4 2003/03/01 19:30:44 bjk
22 # Changed option -a to -b to specify logins before the arguments.
24 # Added options -s, -m, -r to specify seconds, minutes and hours.
26 # Revision 1.3 2003/03/01 16:14:46 bjk
27 # Added option -a to specify logins after the arguments.
29 # Revision 1.2 2003/02/26 17:43:22 bjk
30 # Dont run userinfo on matches. Dump only the username.
32 # Added option -n to suppress users who have never logged in.
34 # Revision 1.1 2003/02/22 20:57:18 bjk
35 # Initial commit.
38 PN="`basename $0`";
39 MINUTE=60;
40 HOUR=3600;
41 DAY=86400;
42 WEEK=604800;
43 YEAR=31449600;
44 CMD="awk -F: '{print \$1}' < /etc/passwd | ui -O passwd.so -l -- -O login.so -lt --";
46 usage () {
47 cat <<EOF 1>&2
48 Find users whose last login is greater or less than the specified arguments.
49 Usage: $PN [-hnl] [-s seconds] [-m minutes] [-r hours] [-d days]
50 [-w weeks] [-y years]
51 -s Logins seconds old.
52 -m Logins minutes old.
53 -r Logins hours old.
54 -d Logins days old.
55 -w Logins weeks old.
56 -y Logins years old.
57 -l List users whose last login is at least as old as the arguments.
58 -n Also show users who have never logged in.
59 -h This help text.
60 EOF
61 exit 1;
64 while getopts "d:w:hr:m:nls:y:" opt; do
65 case $opt in
66 l) BEFORE=1;;
67 n) NOLOGIN=1;;
68 s) tmp=`expr $OPTARG`;;
69 d) tmp=`expr $OPTARG \* $DAY`;;
70 w) tmp=`expr $OPTARG \* $WEEK`;;
71 r) tmp=`expr $OPTARG \* $HOUR`;;
72 m) tmp=`expr $OPTARG \* $MINUTE`;;
73 y) tmp=`expr $OPTARG \* $YEAR`;;
74 h) usage;;
75 \?) usage;;
76 --) break;;
77 esac;
79 if [ $WHEN ]; then
80 WHEN=`expr $WHEN + $tmp`;
81 else
82 WHEN=$tmp;
83 fi;
84 done;
86 if [ ! $WHEN ]; then
87 usage;
88 fi;
90 shift $(($OPTIND - 1));
92 NOW=`date +%s`;
93 OLDIFS=$IFS;
95 eval $CMD | while read line; do
96 IFS=:;
97 set $line;
98 user="$1";
99 sec="$2";
101 if [ x"$sec" = x- -o x"$sec" = x\! ]; then
102 if [ $NOLOGIN ]; then
103 echo "$user";
106 continue;
109 if [ $BEFORE ]; then
110 if [ `expr $NOW - $sec` -gt $WHEN ]; then
111 echo "$user";
113 else
114 if [ `expr $NOW - $sec` -lt $WHEN ]; then
115 echo "$user";
118 done;
120 IFS=$OLDIFS;
121 exit 0;