xref: /titanic_41/usr/src/cmd/calendar/calendar.sh (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1#!/usr/bin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
24#	  All Rights Reserved
25
26
27#	Copyright (c) 1999 by Sun Microsystems, Inc.
28#	All rights reserved.
29
30#ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.8	*/
31#	calendar.sh - calendar command, uses /usr/lib/calprog
32
33PATH=/usr/bin
34USAGE="usage: calendar [ - ]"
35
36# mktmpdir - Create a private (mode 0700) temporary directory inside of /tmp
37mktmpdir() {
38	tmpdir=/tmp/calendar.$$
39	/usr/bin/mkdir -m 700 $tmpdir || exit 1
40}
41mktmpdir
42_tmp=$tmpdir/cal$$
43
44cleanup()
45{
46	/usr/bin/rm -rf $tmpdir
47}
48
49# Trap on SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM
50for i in 1 2 3 13 15
51do
52	# Ignore trap if already set by the shell.  NOTE: If /bin/sh
53	# is made XCU4 compliant, updates will also be required to this
54	# if statement because of XCU4 changes to the trap built-in.
55	TRAP_IGNORE=`trap | egrep -c "^$i:\$"`
56	if [ "$TRAP_IGNORE" != "1" ]
57	then
58		# Cleanup; reset default value; send signal to process.
59		trap "cleanup; trap $i; kill -$i $$" $i
60	fi
61done
62
63# POSIX.2 and XCU4 specify that if a utility accepts an operand,
64# it also handle -- as a delimitor.
65if [ "$1" = -- ]; then
66	shift
67fi
68
69case $# in
700)	if [ -f calendar ]; then
71		/usr/lib/calprog > ${_tmp}
72		egrep -f ${_tmp} calendar
73	else
74		echo >&2 $0: `pwd`/calendar not found
75		exit 1
76	fi;;
77*)	case $* in
78	-)	if (rpcinfo -p | fgrep -s ypbind); then
79			caldata="ypcat passwd.byname | grep /`uname -n`/"
80		else
81			caldata="cat /dev/null"
82		fi
83		/usr/lib/calprog > ${_tmp}
84		eval $caldata | cat /etc/passwd - | \
85		sed 's/\([^:]*\):.*:\(.*\):[^:]*$/_dir=\2 _user=\1/' | \
86		while read _token; do
87			eval ${_token}	# evaluates _dir= and _user=
88			if [ -s ${_dir}/calendar ]; then
89				egrep -f ${_tmp} ${_dir}/calendar 2>/dev/null \
90					> $tmpdir/calendar.$$
91				if [ -s $tmpdir/calendar.$$ ]; then
92					mail ${_user} < $tmpdir/calendar.$$
93				fi
94			fi
95		done;;
96	*)	echo >&2 $0: illegal option -- $@
97		echo >&2 $USAGE
98		exit 1
99	esac
100esac
101cleanup
102exit 0
103