xref: /titanic_44/usr/src/cmd/allocate/fd_clean.sh (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1#! /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#
24# Copyright (c) 1992-1993, 1997-2001 by Sun Microsystems, Inc.
25# All rights reserved.
26#
27#ident	"%Z%%M%	%I%	%E% SMI"
28#
29#  This a clean script for the floppy drive
30#
31
32PROG=`basename $0`
33PATH="/usr/sbin:/usr/bin"
34TEXTDOMAIN="SUNW_OST_OSCMD"
35export TEXTDOMAIN
36
37USAGE=`gettext "%s [-I|-s|-f|-i] device"`
38
39#
40# 		*** Shell Function Declarations ***
41#
42
43con_msg() {
44    form=`gettext "%s: Media in %s is ready.  Please, label and store safely."`
45    if [ "$silent" != "y" ] ; then
46	printf "${form}\n" $PROG $DEVICE > /dev/console
47    fi
48}
49
50e_con_msg() {
51    form=`gettext "%s: Error cleaning up device %s."`
52    if [ "$silent" != "y" ] ; then
53	printf "${form}\n" $PROG $DEVICE > /dev/console
54    fi
55}
56
57user_msg() {
58    form=`gettext "%s: Media in %s is ready.  Please, label and store safely."`
59    if [ "$silent" != "y" ] ; then
60	printf "${form}\n" $PROG $DEVICE > /dev/tty
61    fi
62}
63
64e_user_msg() {
65    form=`gettext "%s: Error cleaning up device %s."`
66    if [ "$silent" != "y" ] ; then
67	printf "${form}\n" $PROG $DEVICE > /dev/tty
68	gettext "Please inform system administrator.\n" > /dev/tty
69    fi
70}
71
72mk_error() {
73   chown bin /etc/security/dev/$1
74   chmod 0100 /etc/security/dev/$1
75}
76
77#
78# 		*** Begin main program ***
79#
80
81silent=n
82
83# Parse the argumnets
84
85while getopts Iifs c
86do
87   case $c in
88   I)	FLAG=i
89	silent=y;;
90   i)   FLAG=$c;;
91   f)   FLAG=$c;;
92   s)   FLAG=$c;;
93   \?)   printf "${USAGE}\n" $PROG
94      exit 1 ;;
95   esac
96done
97shift `expr $OPTIND - 1`
98
99# get the map information
100
101FLOPPY=$1
102MAP=`dminfo -v -n $FLOPPY`
103DEVICE=`echo $MAP | cut -f1 -d:`
104TYPE=`echo $MAP | cut -f2 -d:`
105FILES=`echo $MAP | cut -f3 -d:`
106DEVFILE=`echo $FILES | cut -f1 -d" "`
107
108#if init then do once and exit
109
110lform=`gettext "%s error: %s."`
111
112if [ "$FLAG" = "i" ] ; then
113   x="`eject -q $DEVFILE 2>&1`"		# Determine if there is media in drive
114   z="$?"
115
116   case $z in
117   0) 					# Media is in the drive.
118	a="`eject -f $DEVFILE 2>&1`"
119	b="$?"
120
121	case $b in
122	0)				# Media has been ejected
123		con_msg
124		exit 0;;
125	1)				# Media not ejected
126		mk_error $DEVICE
127		if [ "$silent" != "y" ] ; then
128			printf "${lform}\n" $PROG $a >/dev/tty
129		fi
130		e_con_msg
131		exit 1;;
132	2)			# Error
133		mk_error $DEVICE
134		if [ "$silent" != "y" ] ; then
135			printf "${lform}\n" $PROG $a >/dev/tty
136		fi
137		e_con_msg
138		exit 1;;
139	3)			# Error - Perhaps drive doesn't support ejection
140		mk_error $DEVICE
141		if [ "$silent" != "y" ] ; then
142			printf "${lform}\n" $PROG $a >/dev/tty
143		fi
144		e_con_msg
145		exit 1;;
146	esac;;
147   1) 		# No media in drive
148	con_msg
149	exit 0;;
150   2)			# Error
151		mk_error $DEVICE
152		if [ "$silent" != "y" ] ; then
153			printf "${lform}\n" $PROG $x >/dev/tty
154		fi
155		e_con_msg
156		exit 1;;
157   3)			# Error
158		mk_error $DEVICE
159		if [ "$silent" != "y" ] ; then
160			printf "${lform}\n" $PROG $x >/dev/tty
161		fi
162		e_con_msg
163		exit 1;;
164   esac
165else
166# interactive clean up
167   x="`eject -q $DEVFILE 2>&1`"		# Determine if there is media in drive
168   z="$?"
169
170   case $z in
171   0)					# Media is in the drive.
172	a="`eject -f $DEVFILE 2>&1`"
173	b="$?"
174	case $b in
175	0)				# Media has been ejected
176		user_msg
177		exit 0;;
178	1)				# Media didn't eject
179         	mk_error $DEVICE
180		if [ "$silent" != "y" ] ; then
181			printf "${lform}\n" $PROG $a >/dev/tty
182		fi
183         	e_user_msg
184         	exit 1;;
185	2)				# Other Error
186		mk_error $DEVICE
187		if [ "$silent" != "y" ] ; then
188			printf "${lform}\n" $PROG $a >/dev/tty
189		fi
190		e_user_msg
191         	exit 1;;
192	3)
193
194		if echo $a | grep "failed" >/dev/null ; then
195         	while true 		# Drive doesn't support eject, so loop
196         	    do
197			c="`eject -q $DEVFILE 2>&1`"	# Is floppy in drive?
198			d="$?"
199            		if [ $d -eq 0 ] ; then		# Yes, Floppy in drive
200				form=`gettext "Please remove the floppy from %s."`
201				if [ "$silent" != "y" ] ; then
202               				printf "${form}\n" $DEVICE >/dev/tty
203					/usr/5bin/echo \\007 > /dev/tty
204				fi
205               			sleep 3
206            		elif echo $c | grep "NOT" > /dev/null ; then
207							# No,Floppy NOT in drive
208               			user_msg
209               			exit 0
210			else				# Error occurred
211         			mk_error $DEVICE
212				if [ "$silent" != "y" ] ; then
213					printf "${lform}\n" $PROG $a >/dev/tty
214				fi
215				e_user_msg
216         			exit 1
217            		fi
218         	    done
219		else 					# Some other failure
220			if [ "$silent" != "y" ] ; then
221				printf "${lform}\n" $PROG $a >/dev/tty
222			fi
223         		e_user_msg
224         		mk_error $DEVICE
225         		exit 1
226		fi;;
227
228	esac;;
229   1)							# No media in the drive
230         user_msg
231         exit 0;;
232   2)
233       	mk_error $DEVICE
234	if [ "$silent" != "y" ] ; then
235		printf "${lform}\n" $PROG $x >/dev/tty
236	fi
237	e_user_msg
238       	exit 1;;
239   3)
240       	mk_error $DEVICE
241	if [ "$silent" != "y" ] ; then
242		printf "${lform}\n" $PROG $x >/dev/tty
243	fi
244	e_user_msg
245       	exit 1;;
246   esac
247fi
248exit 2
249