Home | History | Annotate | Line # | Download | only in eject
eject.c revision 1.8
      1  1.8    tron /*	$NetBSD: eject.c,v 1.8 1999/02/17 22:59:14 tron Exp $	*/
      2  1.8    tron 
      3  1.8    tron /*-
      4  1.8    tron  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.8    tron  * All rights reserved.
      6  1.8    tron  *
      7  1.8    tron  * This code is derived from software contributed to The NetBSD Foundation
      8  1.8    tron  * by Chris Jones.
      9  1.2      pk  *
     10  1.2      pk  * Redistribution and use in source and binary forms, with or without
     11  1.2      pk  * modification, are permitted provided that the following conditions
     12  1.2      pk  * are met:
     13  1.2      pk  * 1. Redistributions of source code must retain the above copyright
     14  1.2      pk  *    notice, this list of conditions and the following disclaimer.
     15  1.2      pk  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2      pk  *    notice, this list of conditions and the following disclaimer in the
     17  1.2      pk  *    documentation and/or other materials provided with the distribution.
     18  1.2      pk  * 3. All advertising materials mentioning features or use of this software
     19  1.2      pk  *    must display the following acknowledgement:
     20  1.8    tron  *	This product includes software developed by the NetBSD
     21  1.8    tron  *	Foundation, Inc. and its contributors.
     22  1.8    tron  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.8    tron  *    contributors may be used to endorse or promote products derived
     24  1.8    tron  *    from this software without specific prior written permission.
     25  1.2      pk  *
     26  1.8    tron  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.8    tron  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.8    tron  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.8    tron  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.8    tron  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.8    tron  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.8    tron  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.8    tron  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.8    tron  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.8    tron  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.8    tron  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2      pk  */
     38  1.2      pk 
     39  1.4   lukem #include <sys/cdefs.h>
     40  1.4   lukem #ifndef lint
     41  1.8    tron __COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\n\
     42  1.8    tron 	All rights reserved.\n");
     43  1.8    tron #endif /* not lint */
     44  1.4   lukem 
     45  1.8    tron #ifndef lint
     46  1.8    tron __RCSID("$NetBSD: eject.c,v 1.8 1999/02/17 22:59:14 tron Exp $");
     47  1.8    tron #endif /* not lint */
     48  1.2      pk 
     49  1.2      pk #include <sys/param.h>
     50  1.8    tron #include <sys/wait.h>
     51  1.2      pk 
     52  1.8    tron #include <ctype.h>
     53  1.8    tron #include <err.h>
     54  1.8    tron #include <locale.h>
     55  1.8    tron #include <paths.h>
     56  1.2      pk #include <stdio.h>
     57  1.2      pk #include <stdlib.h>
     58  1.8    tron #include <string.h>
     59  1.2      pk #include <unistd.h>
     60  1.2      pk 
     61  1.8    tron #define DEFAULT_CC		"cc"
     62  1.8    tron #define DEFAULT_PATH		_PATH_DEFPATH
     63  1.8    tron #define DEFAULT_FILENAME	".depend"
     64  1.8    tron 
     65  1.8    tron void	usage __P((void));
     66  1.8    tron char   *findcc __P((const char *));
     67  1.8    tron int	main __P((int, char **));
     68  1.4   lukem 
     69  1.8    tron void
     70  1.2      pk usage()
     71  1.2      pk {
     72  1.8    tron 	(void)fprintf(stderr,
     73  1.8    tron 	    "usage: mkdep [-a] [-p] [-f file ...] flags file ...\n");
     74  1.8    tron 	exit(EXIT_FAILURE);
     75  1.2      pk }
     76  1.2      pk 
     77  1.8    tron char *
     78  1.8    tron findcc(progname)
     79  1.8    tron 	const char	*progname;
     80  1.8    tron {
     81  1.8    tron 	char   *path, *dir, *next;
     82  1.8    tron 	char   buffer[MAXPATHLEN];
     83  1.8    tron 
     84  1.8    tron 	if (strchr(progname, '/') != NULL)
     85  1.8    tron 		return access(progname, X_OK) ? NULL : strdup(progname);
     86  1.2      pk 
     87  1.8    tron 	if (((path = getenv("PATH")) == NULL) ||
     88  1.8    tron 	    ((path = strdup(path)) == NULL))
     89  1.8    tron 		return NULL;
     90  1.8    tron 
     91  1.8    tron 	dir = path;
     92  1.8    tron 	while (dir != NULL) {
     93  1.8    tron 		if ((next = strchr(dir, ':')) != NULL)
     94  1.8    tron 			*next++ = '\0';
     95  1.8    tron 
     96  1.8    tron 		if (snprintf(buffer, sizeof(buffer),
     97  1.8    tron 			     "%s/%s", dir, progname) < sizeof(buffer)) {
     98  1.8    tron 			if (!access(buffer, X_OK)) {
     99  1.8    tron 				free(path);
    100  1.8    tron 				return strdup(buffer);
    101  1.8    tron 			}
    102  1.2      pk 		}
    103  1.8    tron 		dir = next;
    104  1.2      pk 	}
    105  1.8    tron 
    106  1.8    tron 	free(path);
    107  1.2      pk 	return NULL;
    108  1.2      pk }
    109  1.2      pk 
    110  1.8    tron int
    111  1.8    tron main(argc, argv)
    112  1.8    tron 	int     argc;
    113  1.8    tron 	char  **argv;
    114  1.8    tron {
    115  1.8    tron 	int 	aflag, pflag, index, tmpfd, status;
    116  1.8    tron 	pid_t	cpid, pid;
    117  1.8    tron 	char   *filename, *CC, *pathname, tmpfilename[MAXPATHLEN], **args;
    118  1.8    tron 	FILE   *tmpfile, *dependfile;
    119  1.8    tron 	char	buffer[32768];
    120  1.8    tron 
    121  1.8    tron 	setlocale(LC_ALL, "");
    122  1.8    tron 
    123  1.8    tron 	aflag = 0;
    124  1.8    tron 	pflag = 0;
    125  1.8    tron 	filename = DEFAULT_FILENAME;
    126  1.8    tron 	for (index=1; index< argc; index++)
    127  1.8    tron 		if (strcmp(argv[index], "-a") == 0)
    128  1.8    tron 			aflag = 1;
    129  1.8    tron 		else
    130  1.8    tron 			if (strcmp(argv[index], "-f") == 0) {
    131  1.8    tron 				if (++index < argc)
    132  1.8    tron 					filename = argv[index];
    133  1.8    tron 			}
    134  1.8    tron 			else
    135  1.8    tron 				if (strcmp(argv[index], "-p") == 0)
    136  1.8    tron 					pflag = 1;
    137  1.8    tron 				else
    138  1.8    tron 					break;
    139  1.8    tron 
    140  1.8    tron 	argc -= index;
    141  1.8    tron 	argv += index;
    142  1.8    tron 	if (argc == 0)
    143  1.8    tron 		usage();
    144  1.8    tron 
    145  1.8    tron 	if ((CC = getenv("CC")) == NULL)
    146  1.8    tron 		CC = DEFAULT_CC;
    147  1.8    tron 	if ((pathname = findcc(CC)) == NULL)
    148  1.8    tron 		if (!setenv("PATH", DEFAULT_PATH, 1))
    149  1.8    tron 			pathname = findcc(CC);
    150  1.8    tron 	if (pathname == NULL) {
    151  1.8    tron 		(void)fprintf(stderr, "mkdep: %s: not found\n", CC);
    152  1.8    tron 		return EXIT_FAILURE;
    153  1.8    tron 	}
    154  1.8    tron 
    155  1.8    tron 	if ((args = malloc((argc + 3) * sizeof(char *))) == NULL) {
    156  1.8    tron 		perror("mkdep");
    157  1.8    tron 		exit(EXIT_FAILURE);
    158  1.8    tron 	}
    159  1.8    tron 	args[0] = CC;
    160  1.8    tron 	args[1] = "-M";
    161  1.8    tron 	(void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
    162  1.8    tron 
    163  1.8    tron 	(void)strcpy(tmpfilename, _PATH_TMP "mkdepXXXXXX");
    164  1.8    tron 	if ((tmpfd = mkstemp (tmpfilename)) < 0) {
    165  1.8    tron 		warn("unable to create temporary file %s", tmpfilename);
    166  1.8    tron 		return EXIT_FAILURE;
    167  1.8    tron 	}
    168  1.8    tron 
    169  1.8    tron #ifdef __GNUC__			/* to shut up gcc warnings */
    170  1.8    tron 	(void)&aflag;
    171  1.8    tron 	(void)&pflag;
    172  1.8    tron 	(void)&filename;
    173  1.8    tron 	(void)&pathname;
    174  1.8    tron #endif
    175  1.8    tron 
    176  1.8    tron 	switch (cpid = vfork()) {
    177  1.8    tron 	case 0:
    178  1.8    tron 	    (void)dup2(tmpfd, STDOUT_FILENO);
    179  1.8    tron 	    (void)close(tmpfd);
    180  1.8    tron 
    181  1.8    tron 	    (void)execv(pathname, args);
    182  1.8    tron 	    _exit(EXIT_FAILURE);
    183  1.8    tron 
    184  1.8    tron 	case -1:
    185  1.8    tron 	    (void)fputs("mkdep: unable to fork.\n", stderr);
    186  1.8    tron 	    (void)close(tmpfd);
    187  1.8    tron 	    (void)unlink(tmpfilename);
    188  1.8    tron 	    return EXIT_FAILURE;
    189  1.8    tron 	}
    190  1.8    tron 
    191  1.8    tron 	while (((pid = wait(&status)) != cpid) && (pid >= 0));
    192  1.8    tron 
    193  1.8    tron 	if (status) {
    194  1.8    tron 	    (void)fputs("mkdep: compile failed.\n", stderr);
    195  1.8    tron 	    (void)close(tmpfd);
    196  1.8    tron 	    (void)unlink(tmpfilename);
    197  1.8    tron 	    return EXIT_FAILURE;
    198  1.8    tron 	}
    199  1.8    tron 
    200  1.8    tron 	(void)lseek(tmpfd, 0, SEEK_SET);
    201  1.8    tron 	if ((tmpfile = fdopen(tmpfd, "r")) == NULL) {
    202  1.8    tron 	    (void)fprintf(stderr,
    203  1.8    tron 			  "mkdep: unable to read temporary file %s\n",
    204  1.8    tron 			  tmpfilename);
    205  1.8    tron 	    (void)close(tmpfd);
    206  1.8    tron 	    (void)unlink(tmpfilename);
    207  1.8    tron 	    return EXIT_FAILURE;
    208  1.8    tron 	}
    209  1.8    tron 
    210  1.8    tron 	if ((dependfile = fopen(filename, aflag ? "a" : "w")) == NULL) {
    211  1.8    tron 		(void)fprintf(stderr,
    212  1.8    tron 			      "mkdep: unable to %s to file %s\n",
    213  1.8    tron 			      aflag ? "append" : "write", filename);
    214  1.8    tron 		(void)fclose(tmpfile);
    215  1.8    tron 		(void)unlink(tmpfilename);
    216  1.8    tron 		return EXIT_FAILURE;
    217  1.8    tron 	}
    218  1.8    tron 
    219  1.8    tron 	while (fgets(buffer, sizeof(buffer), tmpfile) != NULL) {
    220  1.8    tron 		char   *ptr;
    221  1.8    tron 
    222  1.8    tron 		if (pflag && ((ptr = strstr(buffer, ".o")) != NULL)) {
    223  1.8    tron 			char   *colon;
    224  1.8    tron 
    225  1.8    tron 			colon = ptr + 2;
    226  1.8    tron 			while (isspace(*colon)) colon++;
    227  1.8    tron 			if (*colon == ':')
    228  1.8    tron 				(void)strcpy(ptr, colon);
    229  1.2      pk 		}
    230  1.8    tron 
    231  1.8    tron 		ptr = buffer;
    232  1.8    tron 		while (*ptr)
    233  1.8    tron 			if (isspace(*ptr++))
    234  1.8    tron 				if ((ptr[0] == '.') && (ptr[1] == '/'))
    235  1.8    tron 					(void)strcpy(ptr, ptr + 2);
    236  1.8    tron 
    237  1.8    tron 		(void)fputs(buffer, dependfile);
    238  1.2      pk 	}
    239  1.8    tron 
    240  1.8    tron 	(void)fclose(dependfile);
    241  1.8    tron 	(void)fclose(tmpfile);
    242  1.8    tron 	(void)unlink(tmpfilename);
    243  1.8    tron 
    244  1.8    tron 	return EXIT_SUCCESS;
    245  1.2      pk }
    246  1.2      pk 
    247  1.2      pk /*
    248  1.8    tron  * Eject program.  Deals with floppies, cdroms, and tapes.  We could
    249  1.8    tron  * really use a generic GDIOCEJECT ioctl, that would work with
    250  1.8    tron  * everything.  This eject also knows how to load media into cdrom
    251  1.8    tron  * drives, and it will attempt to extrapolate a device name from a
    252  1.8    tron  * nickname and number.
    253  1.7  bouyer  */
    254  1.8    tron 
    255  1.8    tron #include <sys/cdefs.h>
    256  1.8    tron #ifndef lint
    257  1.8    tron __RCSID("$NetBSD: eject.c,v 1.8 1999/02/17 22:59:14 tron Exp $");
    258  1.8    tron #endif
    259  1.8    tron 
    260  1.8    tron #include <ctype.h>
    261  1.8    tron #include <err.h>
    262  1.8    tron #include <fcntl.h>
    263  1.8    tron #include <stdio.h>
    264  1.8    tron #include <stdlib.h>
    265  1.8    tron #include <string.h>
    266  1.8    tron #include <unistd.h>
    267  1.8    tron #include <sys/cdio.h>
    268  1.8    tron #include <sys/disklabel.h>
    269  1.8    tron #include <sys/ioctl.h>
    270  1.8    tron #include <sys/param.h>
    271  1.8    tron #include <sys/ucred.h>
    272  1.8    tron #include <sys/mount.h>
    273  1.8    tron #include <sys/mtio.h>
    274  1.8    tron 
    275  1.8    tron struct nicknames_s {
    276  1.8    tron     char *name;			/* The name given on the command line. */
    277  1.8    tron     char *devname;		/* The base name of the device */
    278  1.8    tron     int type;			/* The type of device, for determining
    279  1.8    tron 				   what ioctl to use. */
    280  1.8    tron #define TAPE 0x10
    281  1.8    tron #define DISK 0x20
    282  1.8    tron     /* OR one of the above with one of the below: */
    283  1.8    tron #define NOTLOADABLE 0x00
    284  1.8    tron #define LOADABLE 0x01
    285  1.8    tron #define TYPEMASK ((int)~0x01)
    286  1.8    tron } nicknames[] = {
    287  1.8    tron     { "diskette", "fd", DISK | NOTLOADABLE },
    288  1.8    tron     { "floppy", "fd", DISK | NOTLOADABLE },
    289  1.8    tron     { "fd", "fd", DISK | NOTLOADABLE },
    290  1.8    tron     { "cdrom", "cd", DISK | LOADABLE },
    291  1.8    tron     { "cd", "cd", DISK | LOADABLE },
    292  1.8    tron     { "mcd", "mcd", DISK | LOADABLE }, /* XXX Is this true? */
    293  1.8    tron     { "tape", "st", TAPE | NOTLOADABLE },
    294  1.8    tron     { "st", "st", TAPE | NOTLOADABLE },
    295  1.8    tron     { "dat", "st", TAPE | NOTLOADABLE },
    296  1.8    tron     { "exabyte", "st", TAPE | NOTLOADABLE },
    297  1.8    tron };
    298  1.8    tron #define MAXNICKLEN 12		/* at least enough room for the
    299  1.8    tron 				   longest nickname */
    300  1.8    tron #define MAXDEVLEN (MAXNICKLEN + 7) /* "/dev/r" ... "a" */
    301  1.8    tron 
    302  1.8    tron struct devtypes_s {
    303  1.8    tron     char *name;
    304  1.8    tron     int type;
    305  1.8    tron } devtypes[] = {
    306  1.8    tron     { "diskette", DISK | NOTLOADABLE },
    307  1.8    tron     { "floppy", DISK | NOTLOADABLE },
    308  1.8    tron     { "cdrom", DISK | LOADABLE },
    309  1.8    tron     { "disk", DISK | NOTLOADABLE },
    310  1.8    tron     { "tape", TAPE | NOTLOADABLE },
    311  1.8    tron };
    312  1.8    tron 
    313  1.8    tron int verbose_f = 0;
    314  1.8    tron int umount_f = 1;
    315  1.8    tron int load_f = 0;
    316  1.8    tron 
    317  1.8    tron int main __P((int, char *[]));
    318  1.8    tron void usage __P((void));
    319  1.8    tron char *nick2dev __P((char *));
    320  1.8    tron char *nick2rdev __P((char *));
    321  1.8    tron int guess_devtype __P((char *));
    322  1.8    tron char *guess_nickname __P((char *));
    323  1.8    tron void eject_tape __P((char *));
    324  1.8    tron void eject_disk __P((char *));
    325  1.8    tron void unmount_dev __P((char *));
    326  1.8    tron 
    327  1.8    tron int
    328  1.8    tron main(int argc,
    329  1.8    tron      char *argv[])
    330  1.7  bouyer {
    331  1.8    tron     int ch;
    332  1.8    tron     int devtype = -1;
    333  1.8    tron     int n, i;
    334  1.8    tron     char *devname = NULL;
    335  1.8    tron 
    336  1.8    tron     while((ch = getopt(argc, argv, "d:flnt:v")) != -1) {
    337  1.8    tron 	switch(ch) {
    338  1.8    tron 	case 'd':
    339  1.8    tron 	    devname = optarg;
    340  1.8    tron 	    break;
    341  1.8    tron 	case 'f':
    342  1.8    tron 	    umount_f = 0;
    343  1.8    tron 	    break;
    344  1.8    tron 	case 'l':
    345  1.8    tron 	    load_f = 1;
    346  1.8    tron 	    break;
    347  1.8    tron 	case 'n':
    348  1.8    tron 	    for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
    349  1.8    tron 		n++) {
    350  1.8    tron 		struct nicknames_s *np = &nicknames[n];
    351  1.8    tron 
    352  1.8    tron 		printf("%s -> %s\n", np->name, nick2dev(np->name));
    353  1.8    tron 	    }
    354  1.8    tron 	    return(0);
    355  1.8    tron 	case 't':
    356  1.8    tron 	    for(i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]);
    357  1.8    tron 		i++) {
    358  1.8    tron 		if(strcasecmp(devtypes[i].name, optarg) == 0) {
    359  1.8    tron 		    devtype = devtypes[i].type;
    360  1.8    tron 		    break;
    361  1.8    tron 		}
    362  1.8    tron 	    }
    363  1.8    tron 	    if(devtype == -1) {
    364  1.8    tron 		errx(1, "%s: unknown device type\n", optarg);
    365  1.8    tron 	    }
    366  1.8    tron 	    break;
    367  1.8    tron 	case 'v':
    368  1.8    tron 	    verbose_f = 1;
    369  1.8    tron 	    break;
    370  1.8    tron 	default:
    371  1.8    tron 	    warnx("-%c: unknown switch", ch);
    372  1.8    tron 	    usage();
    373  1.8    tron 	    /* NOTREACHED */
    374  1.8    tron 	}
    375  1.8    tron     }
    376  1.8    tron     argc -= optind;
    377  1.8    tron     argv += optind;
    378  1.8    tron 
    379  1.8    tron     if(devname == NULL) {
    380  1.8    tron 	if(argc == 0) {
    381  1.8    tron 	    usage();
    382  1.8    tron 	    /* NOTREACHED */
    383  1.8    tron 	} else
    384  1.8    tron 	    devname = argv[0];
    385  1.8    tron     }
    386  1.7  bouyer 
    387  1.7  bouyer 
    388  1.8    tron     if(devtype == -1) {
    389  1.8    tron 	devtype = guess_devtype(devname);
    390  1.8    tron     }
    391  1.8    tron     if(devtype == -1) {
    392  1.8    tron 	errx(1, "%s: unable to determine type of device",
    393  1.8    tron 	     devname);
    394  1.8    tron     }
    395  1.8    tron     if(verbose_f) {
    396  1.8    tron 	printf("device type == ");
    397  1.8    tron 	if((devtype & TYPEMASK) == TAPE)
    398  1.8    tron 	    printf("tape\n");
    399  1.8    tron 	else
    400  1.8    tron 	    printf("disk, floppy, or cdrom\n");
    401  1.8    tron     }
    402  1.8    tron 
    403  1.8    tron     if(umount_f)
    404  1.8    tron 	unmount_dev(devname);
    405  1.8    tron 
    406  1.8    tron     /* XXX Tapes and disks have different ioctl's: */
    407  1.8    tron     if((devtype & TYPEMASK) == TAPE)
    408  1.8    tron 	eject_tape(devname);
    409  1.8    tron     else
    410  1.8    tron 	eject_disk(devname);
    411  1.8    tron 
    412  1.8    tron     if(verbose_f)
    413  1.8    tron 	printf("done.\n");
    414  1.8    tron 
    415  1.8    tron     return(0);
    416  1.7  bouyer }
    417  1.7  bouyer 
    418  1.8    tron void
    419  1.8    tron usage(void)
    420  1.8    tron {
    421  1.8    tron     errx(1, "Usage: eject [-n][-f][-v][-l][-t type][-d] device | nickname");
    422  1.8    tron }
    423  1.2      pk 
    424  1.8    tron int
    425  1.8    tron guess_devtype(char *devname)
    426  1.2      pk {
    427  1.8    tron     int n;
    428  1.2      pk 
    429  1.8    tron     /* Nickname match: */
    430  1.8    tron     for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
    431  1.8    tron 	n++) {
    432  1.8    tron 	if(strncasecmp(nicknames[n].name, devname,
    433  1.8    tron 		       strlen(nicknames[n].name)) == 0)
    434  1.8    tron 	    return(nicknames[n].type);
    435  1.8    tron     }
    436  1.8    tron 
    437  1.8    tron     /*
    438  1.8    tron      * If we still don't know it, then try to compare vs. dev
    439  1.8    tron      * and rdev names that we know.
    440  1.8    tron      */
    441  1.8    tron     /* dev first: */
    442  1.8    tron     for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
    443  1.8    tron 	char *name;
    444  1.8    tron 	name = nick2dev(nicknames[n].name);
    445  1.8    tron 	/*
    446  1.8    tron 	 * Assume that the part of the name that distinguishes the
    447  1.8    tron 	 * instance of this device begins with a 0.
    448  1.8    tron 	 */
    449  1.8    tron 	*(strchr(name, '0')) = '\0';
    450  1.8    tron 	if(strncmp(name, devname, strlen(name)) == 0)
    451  1.8    tron 	    return(nicknames[n].type);
    452  1.8    tron     }
    453  1.8    tron 
    454  1.8    tron     /* Now rdev: */
    455  1.8    tron     for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
    456  1.8    tron 	char *name = nick2rdev(nicknames[n].name);
    457  1.8    tron 	*(strchr(name, '0')) = '\0';
    458  1.8    tron 	if(strncmp(name, devname, strlen(name)) == 0)
    459  1.8    tron 	    return(nicknames[n].type);
    460  1.8    tron     }
    461  1.8    tron 
    462  1.8    tron     /* Not found. */
    463  1.8    tron     return(-1);
    464  1.8    tron }
    465  1.8    tron 
    466  1.8    tron /* "floppy5" -> "/dev/fd5a".  Yep, this uses a static buffer. */
    467  1.8    tron char *
    468  1.8    tron nick2dev(char *nn)
    469  1.8    tron {
    470  1.8    tron     int n;
    471  1.8    tron     static char devname[MAXDEVLEN];
    472  1.8    tron     int devnum = 0;
    473  1.8    tron 
    474  1.8    tron     for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
    475  1.8    tron 	if(strncasecmp(nicknames[n].name, nn,
    476  1.8    tron 		       strlen(nicknames[n].name)) == 0) {
    477  1.8    tron 	    sscanf(nn, "%*[^0-9]%d", &devnum);
    478  1.8    tron 	    sprintf(devname, "/dev/%s%d", nicknames[n].devname,
    479  1.8    tron 		    devnum);
    480  1.8    tron 	    if((nicknames[n].type & TYPEMASK) != TAPE)
    481  1.8    tron 		strcat(devname, "a");
    482  1.8    tron 	    return(devname);
    483  1.2      pk 	}
    484  1.8    tron     }
    485  1.2      pk 
    486  1.8    tron     return(NULL);
    487  1.8    tron }
    488  1.2      pk 
    489  1.8    tron /* "floppy5" -> "/dev/rfd5c".  Static buffer. */
    490  1.8    tron char *
    491  1.8    tron nick2rdev(char *nn)
    492  1.8    tron {
    493  1.8    tron     int n;
    494  1.8    tron     static char devname[MAXDEVLEN];
    495  1.8    tron     int devnum = 0;
    496  1.8    tron 
    497  1.8    tron     for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
    498  1.8    tron 	if(strncasecmp(nicknames[n].name, nn,
    499  1.8    tron 		       strlen(nicknames[n].name)) == 0) {
    500  1.8    tron 	    sscanf(nn, "%*[^0-9]%d", &devnum);
    501  1.8    tron 	    sprintf(devname, "/dev/r%s%d", nicknames[n].devname,
    502  1.8    tron 		    devnum);
    503  1.8    tron 	    if((nicknames[n].type & TYPEMASK) != TAPE) {
    504  1.8    tron 		strcat(devname, "a");
    505  1.8    tron 		devname[strlen(devname) - 1] += RAW_PART;
    506  1.8    tron 	    }
    507  1.8    tron 	    return(devname);
    508  1.2      pk 	}
    509  1.8    tron     }
    510  1.2      pk 
    511  1.8    tron     return(NULL);
    512  1.2      pk }
    513  1.2      pk 
    514  1.8    tron /* Unmount all filesystems attached to dev. */
    515  1.8    tron void
    516  1.8    tron unmount_dev(char *name)
    517  1.2      pk {
    518  1.8    tron     struct statfs *mounts;
    519  1.8    tron     int i, nmnts, len;
    520  1.8    tron     char *dn;
    521  1.8    tron 
    522  1.8    tron     nmnts = getmntinfo(&mounts, MNT_NOWAIT);
    523  1.8    tron     if(nmnts == 0) {
    524  1.8    tron 	err(1, "getmntinfo");
    525  1.8    tron     }
    526  1.8    tron 
    527  1.8    tron     /* Make sure we have a device name: */
    528  1.8    tron     dn = nick2dev(name);
    529  1.8    tron     if(dn == NULL)
    530  1.8    tron 	dn = name;
    531  1.8    tron 
    532  1.8    tron     /* Set len to strip off the partition name: */
    533  1.8    tron     len = strlen(dn);
    534  1.8    tron     if(!isdigit(dn[len - 1]))
    535  1.8    tron 	len--;
    536  1.8    tron     if(!isdigit(dn[len - 1])) {
    537  1.8    tron 	errx(1, "Can't figure out base name for dev name %s", dn);
    538  1.8    tron     }
    539  1.8    tron 
    540  1.8    tron     for(i = 0; i < nmnts; i++) {
    541  1.8    tron 	if(strncmp(mounts[i].f_mntfromname, dn, len) == 0) {
    542  1.8    tron 	    if(verbose_f)
    543  1.8    tron 		printf("Unmounting %s from %s...\n",
    544  1.8    tron 		       mounts[i].f_mntfromname,
    545  1.8    tron 		       mounts[i].f_mntonname);
    546  1.8    tron 
    547  1.8    tron 	    if(unmount(mounts[i].f_mntonname, 0) == -1) {
    548  1.8    tron 		err(1, "unmount: %s", mounts[i].f_mntonname);
    549  1.8    tron 	    }
    550  1.2      pk 	}
    551  1.8    tron     }
    552  1.8    tron 
    553  1.8    tron     return;
    554  1.8    tron }
    555  1.2      pk 
    556  1.8    tron void
    557  1.8    tron eject_tape(char *name)
    558  1.8    tron {
    559  1.8    tron     struct mtop m;
    560  1.8    tron     int fd;
    561  1.8    tron     char *dn;
    562  1.8    tron 
    563  1.8    tron     dn = nick2rdev(name);
    564  1.8    tron     if(dn == NULL) {
    565  1.8    tron 	dn = name;		/* Hope for the best. */
    566  1.8    tron     }
    567  1.8    tron 
    568  1.8    tron     fd = open(dn, O_RDONLY);
    569  1.8    tron     if(fd == -1) {
    570  1.8    tron 	err(1, "open: %s", dn);
    571  1.8    tron     }
    572  1.8    tron 
    573  1.8    tron     if(verbose_f)
    574  1.8    tron 	printf("Ejecting %s...\n", dn);
    575  1.8    tron 
    576  1.8    tron     m.mt_op = MTOFFL;
    577  1.8    tron     m.mt_count = 0;
    578  1.8    tron     if(ioctl(fd, MTIOCTOP, &m) == -1) {
    579  1.8    tron 	err(1, "ioctl: MTIOCTOP: %s", dn);
    580  1.8    tron     }
    581  1.2      pk 
    582  1.8    tron     close(fd);
    583  1.8    tron     return;
    584  1.8    tron }
    585  1.2      pk 
    586  1.8    tron void
    587  1.8    tron eject_disk(char *name)
    588  1.8    tron {
    589  1.8    tron     int fd;
    590  1.8    tron     char *dn;
    591  1.8    tron     int arg;
    592  1.8    tron 
    593  1.8    tron     dn = nick2rdev(name);
    594  1.8    tron     if(dn == NULL) {
    595  1.8    tron 	dn = name;		/* Hope for the best. */
    596  1.8    tron     }
    597  1.8    tron 
    598  1.8    tron     fd = open(dn, O_RDONLY);
    599  1.8    tron     if(fd == -1) {
    600  1.8    tron 	err(1, "open: %s", dn);
    601  1.8    tron     }
    602  1.8    tron 
    603  1.8    tron     if(load_f) {
    604  1.8    tron 	if(verbose_f)
    605  1.8    tron 	    printf("Closing %s...\n", dn);
    606  1.8    tron 
    607  1.8    tron 	if(ioctl(fd, CDIOCCLOSE, NULL) == -1) {
    608  1.8    tron 	    err(1, "ioctl: CDIOCCLOSE: %s", dn);
    609  1.8    tron 	}
    610  1.8    tron     } else {
    611  1.8    tron 	if(verbose_f)
    612  1.8    tron 	    printf("Ejecting %s...\n", dn);
    613  1.8    tron 
    614  1.8    tron 	arg = 0;
    615  1.8    tron 	if(ioctl(fd, DIOCLOCK, (char *)&arg) == -1)
    616  1.8    tron 	    err(1, "ioctl: DIOCLOCK: %s", dn);
    617  1.8    tron 	if(ioctl(fd, DIOCEJECT, &arg) == -1)
    618  1.8    tron 	    err(1, "ioctl: DIOCEJECT: %s", dn);
    619  1.8    tron     }
    620  1.2      pk 
    621  1.8    tron     close(fd);
    622  1.8    tron     return;
    623  1.2      pk }
    624