10 const char umount_usage[] = "umount {filesystem|directory}\n"
13 "\tUnmount a filesystem.\n"
14 "\t-a:\tUnmounts all mounted filesystems.\n";
17 stralloc(const char * string)
19 int length = strlen(string) + 1;
20 char * n = malloc(length);
21 memcpy(n, string, length);
26 erase_mtab(const char * name)
28 struct mntent entries[100];
30 FILE * mountTable = setmntent("/etc/mtab", "r");
34 && (mountTable = setmntent("/proc/mounts", "r")) == 0 ) {
35 name_and_error("/etc/mtab");
39 while ( (m = getmntent(mountTable)) != 0 ) {
40 entries[count].mnt_fsname = stralloc(m->mnt_fsname);
41 entries[count].mnt_dir = stralloc(m->mnt_dir);
42 entries[count].mnt_type = stralloc(m->mnt_type);
43 entries[count].mnt_opts = stralloc(m->mnt_opts);
44 entries[count].mnt_freq = m->mnt_freq;
45 entries[count].mnt_passno = m->mnt_passno;
48 endmntent(mountTable);
49 if ( (mountTable = setmntent("/etc/mtab", "w")) ) {
51 for ( i = 0; i < count; i++ ) {
52 int result = ( strcmp(entries[i].mnt_fsname, name) == 0
53 || strcmp(entries[i].mnt_dir, name) == 0 );
58 addmntent(mountTable, &entries[i]);
60 endmntent(mountTable);
62 else if ( errno != EROFS )
63 name_and_error("/etc/mtab");
67 umount_all(int noMtab)
69 struct mntent entries[100];
71 FILE * mountTable = setmntent("/etc/mtab", "r");
76 && (mountTable = setmntent("/proc/mounts", "r")) == 0 ) {
77 name_and_error("/etc/mtab");
81 while ( (m = getmntent(mountTable)) != 0 ) {
82 entries[count].mnt_fsname = stralloc(m->mnt_fsname);
85 endmntent(mountTable);
88 int result = umount(entries[--count].mnt_fsname) == 0;
89 /* free(entries[count].mnt_fsname); */
92 erase_mtab(entries[count].mnt_fsname);
96 name_and_error(entries[count].mnt_fsname);
103 do_umount(const char * name, int noMtab)
105 if ( umount(name) == 0 ) {
114 umount_main(struct FileInfo * i, int argc, char * * argv)
118 if ( argv[1][0] == '-' ) {
119 switch ( argv[1][1] ) {
121 return umount_all(noMtab);
130 if ( do_umount(argv[1],noMtab) != 0 ) {
131 fprintf(stderr, "%s: %s.\n", argv[1], strerror(errno));