first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / production / msys / bin / umount
1 #!/bin/sh
2 #
3 # File: umount
4 # $Id$
5 #
6 # =====================================================================
7 #
8 # Copyright (C) 2006, 2007 by Keith Marshall
9 #  mailto:keithmarshall@users.sourceforge.net
10 #
11 # This file is part of MSYS
12 #  http://www.mingw.org/msys.shtml
13 #
14 # MSYS is free software.  It is provided "as is", in the hope that it
15 # may be useful; there is NO WARRANTY OF ANY KIND, not even an implied
16 # warranty of MERCHANTABILITY or FITNESS FOR ANY PARTICULAR PURPOSE.
17 # At no time will the author accept liability for damages, however
18 # caused, resulting from the use of this software.
19 #
20 # Permission is granted to copy and redistribute this software, either
21 # as is, or in modified form, provided that:--
22 #
23 #   1) All such copies are distributed with the same rights
24 #      of redistribution.
25 #
26 #   2) The preceding disclaimer of warranty and liabality is
27 #      retained verbatim, in all copies.
28 #
29 #   3) Accreditation of the original author remains in place.
30 #
31 #   4) Modified copies are clearly identified as such, with
32 #      additional accreditation given to the authors of each
33 #      modified version.
34 #
35 # =====================================================================
36 #
37 # Exactly one argument is required...
38 #
39   if test $# -eq 1
40   then
41 #
42 #   Normally, it specifies the mount point to be released,
43 #   but it may also represent a mounted directory path name,
44 #   for which all bound mount points are to be filtered out
45 #   of the "mount table" file.
46 #
47     MNTPATH=`echo "$1" | tr '\\\\' /`
48     TMPFILE=${TMPDIR-"/tmp"}/mnttab$$.tmp
49     MNTTAB=${MNTTAB-"/etc/fstab"}
50 #
51     if cat "$MNTTAB" | tr '\\' / | awk '
52 #
53 #     Copy the "mount table" to a temporary file, filtering
54 #     out all active mount point records which match MNTPATH,
55 #     (the specified argument); set exit status to:--
56 #       0: if at least one mount point is matched;
57 #       1: if no match is found.
58 #
59       BEGIN { status = 1 }
60       { keep = $0 }
61       /^#/ { print; keep = "no"; $0 = "!'$MNTPATH'" }
62       $2 == "'$MNTPATH'" { keep = "no"; status = 0 }
63       { $2 = "!" } $0 == "'$MNTPATH' !" { keep = "no"; status = 0 }
64       keep != "no" { print keep }
65       END { exit status }' > "$TMPFILE"
66     then
67 #
68 #     At least one mount point was selected to release...
69 #     Replace the active "mount table" file with the regenerated
70 #     copy, so completing the operation.
71 #
72       cp "$TMPFILE" "$MNTTAB"
73       rm -f "$TMPFILE"
74 #
75     else
76 #
77 #     No active mount point matched the specified argument...
78 #     Discard the temporary file, complain, and bail out.
79 #
80       rm -f "$TMPFILE"
81       echo >&2 "$0: '$1' is not mounted"
82       exit 1
83     fi
84 #
85   else
86 #
87 #   The command line did not specify exactly one argument...
88 #   Complain, and bail out.
89 #
90     echo >&2 "$0: incorrect number of arguments"
91     echo >&2 "usage: umount <path>"
92     exit 2
93   fi
94 #
95 # On successful completion, ensure we set the exit status appropriately.
96 #
97   exit 0
98 #
99 # $RCSfile$: end of file