Home | History | Annotate | Line # | Download | only in gpt
main.c revision 1.2
      1  1.2  christos /*	$NetBSD: main.c,v 1.2 2015/12/01 09:05:33 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2002 Marcel Moolenaar
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  *
     11  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.1  christos  *
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  christos  *
     28  1.1  christos  * CRC32 code derived from work by Gary S. Brown.
     29  1.1  christos  */
     30  1.1  christos 
     31  1.1  christos #if HAVE_NBTOOL_CONFIG_H
     32  1.1  christos #include "nbtool_config.h"
     33  1.1  christos #endif
     34  1.1  christos 
     35  1.1  christos #include <sys/cdefs.h>
     36  1.1  christos #ifdef __RCSID
     37  1.2  christos __RCSID("$NetBSD: main.c,v 1.2 2015/12/01 09:05:33 christos Exp $");
     38  1.1  christos #endif
     39  1.1  christos 
     40  1.1  christos #include <stdio.h>
     41  1.1  christos #include <stdlib.h>
     42  1.1  christos #include <unistd.h>
     43  1.1  christos #include <err.h>
     44  1.1  christos 
     45  1.1  christos #include "map.h"
     46  1.1  christos #include "gpt.h"
     47  1.1  christos 
     48  1.1  christos static struct {
     49  1.2  christos 	int (*fptr)(gpt_t, int, char *[]);
     50  1.1  christos 	const char *name;
     51  1.1  christos } cmdsw[] = {
     52  1.1  christos 	{ cmd_add, "add" },
     53  1.1  christos #ifndef HAVE_NBTOOL_CONFIG_H
     54  1.1  christos 	{ cmd_backup, "backup" },
     55  1.1  christos #endif
     56  1.1  christos 	{ cmd_biosboot, "biosboot" },
     57  1.1  christos 	{ cmd_create, "create" },
     58  1.1  christos 	{ cmd_destroy, "destroy" },
     59  1.1  christos 	{ cmd_header, "header" },
     60  1.1  christos 	{ NULL, "help" },
     61  1.1  christos 	{ cmd_label, "label" },
     62  1.1  christos 	{ cmd_migrate, "migrate" },
     63  1.1  christos 	{ cmd_recover, "recover" },
     64  1.1  christos 	{ cmd_remove, "remove" },
     65  1.1  christos 	{ NULL, "rename" },
     66  1.1  christos 	{ cmd_resize, "resize" },
     67  1.1  christos 	{ cmd_resizedisk, "resizedisk" },
     68  1.1  christos #ifndef HAVE_NBTOOL_CONFIG_H
     69  1.1  christos 	{ cmd_restore, "restore" },
     70  1.1  christos #endif
     71  1.1  christos 	{ cmd_set, "set" },
     72  1.1  christos 	{ cmd_show, "show" },
     73  1.1  christos 	{ cmd_type, "type" },
     74  1.1  christos 	{ cmd_unset, "unset" },
     75  1.1  christos 	{ NULL, "verify" },
     76  1.1  christos 	{ NULL, NULL }
     77  1.1  christos };
     78  1.1  christos 
     79  1.1  christos __dead static void
     80  1.1  christos usage(void)
     81  1.1  christos {
     82  1.1  christos 	extern const char addmsg1[], addmsg2[], biosbootmsg[];
     83  1.1  christos 	extern const char createmsg[], destroymsg[], headermsg[], labelmsg1[];
     84  1.1  christos 	extern const char labelmsg2[], labelmsg3[], migratemsg[], recovermsg[];
     85  1.1  christos 	extern const char removemsg1[], removemsg2[], resizemsg[];
     86  1.1  christos 	extern const char resizediskmsg[], setmsg[], showmsg[], typemsg1[];
     87  1.1  christos 	extern const char typemsg2[], typemsg3[], unsetmsg[];
     88  1.1  christos #ifndef HAVE_NBTOOL_CONFIG_H
     89  1.1  christos 	extern const char backupmsg[], restoremsg[];
     90  1.1  christos #endif
     91  1.1  christos 	const char *p = getprogname();
     92  1.1  christos 	const char *f =
     93  1.2  christos 	    "[-nrqv] [-m <mediasize>] [-s <sectorsize>]";
     94  1.1  christos 
     95  1.2  christos 	if (strcmp(p, "gpt") == 0)
     96  1.2  christos 		fprintf(stderr,
     97  1.2  christos 		    "Usage: %s %s <command> [<args>] <device>\n", p, f);
     98  1.2  christos 	else
     99  1.2  christos 		fprintf(stderr,
    100  1.2  christos 		    "Usage: %s %s <device> <command> [<args>]\n", p, f);
    101  1.1  christos 	fprintf(stderr,
    102  1.1  christos 	    "Commands:\n"
    103  1.1  christos #ifndef HAVE_NBTOOL_CONFIG_H
    104  1.1  christos 	    "       %s\n"
    105  1.1  christos 	    "       %s\n"
    106  1.1  christos #endif
    107  1.1  christos 	    "       %s\n"
    108  1.1  christos 	    "       %s\n"
    109  1.1  christos 	    "       %s\n"
    110  1.1  christos 	    "       %s\n"
    111  1.1  christos 	    "       %s\n"
    112  1.1  christos 	    "       %s\n"
    113  1.1  christos 	    "       %s\n"
    114  1.1  christos 	    "       %s\n"
    115  1.1  christos 	    "       %s\n"
    116  1.1  christos 	    "       %s\n"
    117  1.1  christos 	    "       %s\n"
    118  1.1  christos 	    "       %s\n"
    119  1.1  christos 	    "       %s\n"
    120  1.1  christos 	    "       %s\n"
    121  1.1  christos 	    "       %s\n"
    122  1.1  christos 	    "       %s\n"
    123  1.1  christos 	    "       %s\n"
    124  1.1  christos 	    "       %s\n"
    125  1.1  christos 	    "       %s\n"
    126  1.1  christos 	    "       %s\n"
    127  1.1  christos 	    "       %s\n",
    128  1.1  christos 	    addmsg1, addmsg2,
    129  1.1  christos #ifndef HAVE_NBTOOL_CONFIG_H
    130  1.1  christos 	    backupmsg,
    131  1.1  christos #endif
    132  1.1  christos 	    biosbootmsg, createmsg, destroymsg,
    133  1.1  christos 	    headermsg, labelmsg1, labelmsg2, labelmsg3,
    134  1.1  christos 	    migratemsg, recovermsg,
    135  1.1  christos 	    removemsg1, removemsg2,
    136  1.1  christos 	    resizemsg, resizediskmsg,
    137  1.1  christos #ifndef HAVE_NBTOOL_CONFIG_H
    138  1.1  christos 	    restoremsg,
    139  1.1  christos #endif
    140  1.1  christos 	    setmsg, showmsg,
    141  1.1  christos 	    typemsg1, typemsg2, typemsg3,
    142  1.1  christos 	    unsetmsg);
    143  1.1  christos 	exit(1);
    144  1.1  christos }
    145  1.1  christos 
    146  1.1  christos static void
    147  1.1  christos prefix(const char *cmd)
    148  1.1  christos {
    149  1.1  christos 	char *pfx;
    150  1.1  christos 
    151  1.1  christos 	if (asprintf(&pfx, "%s %s", getprogname(), cmd) < 0)
    152  1.1  christos 		pfx = NULL;
    153  1.1  christos 	else
    154  1.1  christos 		setprogname(pfx);
    155  1.1  christos }
    156  1.1  christos 
    157  1.1  christos int
    158  1.1  christos main(int argc, char *argv[])
    159  1.1  christos {
    160  1.2  christos 	char *cmd, *p, *dev = NULL;
    161  1.1  christos 	int ch, i;
    162  1.2  christos 	u_int secsz = 0;
    163  1.2  christos 	off_t mediasz = 0;
    164  1.2  christos 	int flags = 0;
    165  1.2  christos 	int verbose = 0;
    166  1.2  christos 	gpt_t gpt;
    167  1.2  christos 
    168  1.2  christos 	setprogname(argv[0]);
    169  1.2  christos 
    170  1.2  christos 	if (strcmp(getprogname(), "gpt") == 0) {
    171  1.2  christos 		if (argc < 3)
    172  1.2  christos 			usage();
    173  1.2  christos 		dev = argv[--argc];
    174  1.2  christos 	}
    175  1.1  christos 
    176  1.1  christos 	/* Get the generic options */
    177  1.2  christos 	while ((ch = getopt(argc, argv, "m:nqrs:v")) != -1) {
    178  1.1  christos 		switch(ch) {
    179  1.1  christos 		case 'm':
    180  1.1  christos 			if (mediasz > 0)
    181  1.1  christos 				usage();
    182  1.1  christos 			mediasz = strtoul(optarg, &p, 10);
    183  1.1  christos 			if (*p != 0 || mediasz < 1)
    184  1.1  christos 				usage();
    185  1.1  christos 			break;
    186  1.1  christos 		case 'n':
    187  1.2  christos 			flags |= GPT_NOSYNC;
    188  1.1  christos 			break;
    189  1.1  christos 		case 'r':
    190  1.2  christos 			flags |= GPT_READONLY;
    191  1.1  christos 			break;
    192  1.1  christos 		case 'q':
    193  1.2  christos 			flags |= GPT_QUIET;
    194  1.1  christos 			break;
    195  1.1  christos 		case 's':
    196  1.1  christos 			if (secsz > 0)
    197  1.1  christos 				usage();
    198  1.1  christos 			secsz = strtoul(optarg, &p, 10);
    199  1.1  christos 			if (*p != 0 || secsz < 1)
    200  1.1  christos 				usage();
    201  1.1  christos 			break;
    202  1.1  christos 		case 'v':
    203  1.1  christos 			verbose++;
    204  1.1  christos 			break;
    205  1.1  christos 		default:
    206  1.1  christos 			usage();
    207  1.1  christos 		}
    208  1.1  christos 	}
    209  1.2  christos 
    210  1.2  christos 	if (argc == optind)
    211  1.2  christos 		usage();
    212  1.2  christos 
    213  1.2  christos 	if (dev == NULL)
    214  1.2  christos 		dev = argv[optind++];
    215  1.1  christos 
    216  1.1  christos 	if (argc == optind)
    217  1.1  christos 		usage();
    218  1.1  christos 
    219  1.1  christos 	cmd = argv[optind++];
    220  1.2  christos 	for (i = 0; cmdsw[i].name != NULL && strcmp(cmd, cmdsw[i].name); i++)
    221  1.2  christos 		continue;
    222  1.1  christos 
    223  1.1  christos 	if (cmdsw[i].fptr == NULL)
    224  1.2  christos 		errx(EXIT_FAILURE, "Unknown command: %s", cmd);
    225  1.1  christos 
    226  1.1  christos 	prefix(cmd);
    227  1.2  christos 
    228  1.2  christos 	gpt = gpt_open(dev, flags, verbose, mediasz, secsz);
    229  1.2  christos 	if (gpt == NULL)
    230  1.2  christos 		return EXIT_FAILURE;
    231  1.2  christos 
    232  1.2  christos 	if ((*cmdsw[i].fptr)(gpt, argc, argv) == -1)
    233  1.2  christos 		return EXIT_FAILURE;
    234  1.2  christos 
    235  1.2  christos 	gpt_close(gpt);
    236  1.2  christos 	return EXIT_SUCCESS;
    237  1.1  christos }
    238