File: umount_z.c

package info (click to toggle)
jazip 0.34-15.1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 540 kB
  • ctags: 208
  • sloc: ansic: 2,632; perl: 343; makefile: 90; sh: 32
file content (70 lines) | stat: -rw-r--r-- 1,837 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * jaZip for Linux  (c) 1996  Jarrod A. Smith
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

#include <stdio.h>
#include <mntent.h>
#include <forms.h>
#include <errno.h>
#include <sys/mount.h>
#include <fcntl.h>
#include "jazip.h"

int z_umount (char *dev)
{
	FILE	*mtab, *newmtab;
	struct mntent *mp;

	if (!is_mounted(dev)){
		sprintf (mesg, "No disks are mounted!");
		return (0);
	}

	seteuid(0);

	if (!(newmtab = fdopen (open (LOCK_FILE, O_WRONLY|O_CREAT|O_EXCL, 0666), "w"))) {
		sprintf (mesg, "A lock-file exists.  umount denied.");
		seteuid(getuid());
		return (0);
	}
	if (!(mtab = setmntent (MOUNTED, "r"))) {
		sprintf (mesg, "%s : %s", MOUNTED, strerror (errno));
		endmntent (newmtab);
		unlink (LOCK_FILE);
		seteuid(getuid());
		return (0);
	}
	while ((mp = getmntent (mtab))) {
		if (!mp->mnt_opts || !mp->mnt_opts[0]) mp->mnt_opts = "defaults";
		if (!strcmp (mnt_fsname, mp->mnt_fsname) ||
			 !strcmp (mnt_dir, mp->mnt_dir)) { 
			if (umount (mnt_dir) < 0) {
				sprintf (mesg, "umount %s : %s", mnt_dir, strerror (errno));
				endmntent (mtab);
				endmntent (newmtab);
				unlink (LOCK_FILE);
				seteuid(getuid());
				return (0);
			} else {
				sprintf (mesg, "Unmounted %s", mp->mnt_dir);
			}
		} else {
			addmntent (newmtab, mp);
		}
	}
	endmntent (newmtab);
	endmntent (mtab);
	unlink (MOUNTED);
	link (LOCK_FILE, MOUNTED);
	unlink (LOCK_FILE);
	seteuid(getuid());
	return (1);
}