xref: /openbsd/sbin/mount_udf/mount_udf.c (revision 17df1aa7)
1 /*	$OpenBSD: mount_udf.c,v 1.5 2007/12/09 20:54:01 jmc Exp $	*/
2 
3 /*
4  * Copyright (c) 2005 Pedro Martelletto <pedro@ambientworks.net>
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/param.h>
21 #include <sys/mount.h>
22 #include <sys/ioctl.h>
23 #include <sys/cdio.h>
24 
25 #include <err.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 
31 #include "mntopts.h"
32 
33 const struct mntopt opts[] = { MOPT_STDOPTS, { NULL } };
34 
35 __dead void
36 usage(void)
37 {
38 	extern char *__progname;
39 
40 	fprintf(stderr, "usage: %s [-o options] special node\n", __progname);
41 
42 	exit(EXIT_FAILURE);
43 }
44 
45 /* Find out media's last block by looking at the LBA of the lead-out track. */
46 u_int32_t
47 lastblock(char *dev)
48 {
49 	int fd, error;
50 	struct ioc_read_toc_entry t;
51 	struct cd_toc_entry te;
52 
53 	fd = open(dev, O_RDONLY, 0);
54 	if (fd < 0)
55 		err(1, "open");
56 
57 	t.address_format = CD_LBA_FORMAT;
58 	t.starting_track = CD_TRACK_LEADOUT;
59 	t.data_len = sizeof(struct cd_toc_entry);
60 	t.data = &te;
61 
62 	error = ioctl(fd, CDIOREADTOCENTRIES, &t);
63 
64 	close(fd);
65 
66 	return (error == -1 ? 0 : te.addr.lba);
67 }
68 
69 int
70 main(int argc, char **argv)
71 {
72 	struct udf_args args;
73 	char node[MAXPATHLEN];
74 	int ch, flags = 0;
75 
76 	while ((ch = getopt(argc, argv, "o:")) != -1)
77 		switch (ch) {
78 		case 'o':
79 			getmntopts(optarg, opts, &flags);
80 			break;
81 		default:
82 			usage();
83 		}
84 
85 	argc -= optind;
86 	argv += optind;
87 
88 	if (argc != 2)
89 		usage();
90 
91 	args.fspec = argv[0];
92 	args.lastblock = lastblock(argv[0]);
93 
94 	if (realpath(argv[1], node) == NULL)
95 		err(1, "realpath %s", argv[1]);
96 
97 	if (mount(MOUNT_UDF, node, flags, &args) < 0)
98 		err(1, "mount");
99 
100 	exit(0);
101 }
102