Home | History | Annotate | Line # | Download | only in gpt
main.c revision 1.1
      1  1.1  christos /*	$NetBSD: main.c,v 1.1 2015/11/30 19:59:34 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.1  christos __RCSID("$NetBSD: main.c,v 1.1 2015/11/30 19:59:34 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.1  christos 	int (*fptr)(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.1  christos 	    "[-nrqv] [-m <mediasize>] [-p <partitionnum>] [-s <sectorsize>]";
     94  1.1  christos 
     95  1.1  christos 	fprintf(stderr,
     96  1.1  christos 	    "Usage: %s %s <command> [<args>]\n", p, f);
     97  1.1  christos 	fprintf(stderr,
     98  1.1  christos 	    "Commands:\n"
     99  1.1  christos #ifndef HAVE_NBTOOL_CONFIG_H
    100  1.1  christos 	    "       %s\n"
    101  1.1  christos 	    "       %s\n"
    102  1.1  christos #endif
    103  1.1  christos 	    "       %s\n"
    104  1.1  christos 	    "       %s\n"
    105  1.1  christos 	    "       %s\n"
    106  1.1  christos 	    "       %s\n"
    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 	    addmsg1, addmsg2,
    125  1.1  christos #ifndef HAVE_NBTOOL_CONFIG_H
    126  1.1  christos 	    backupmsg,
    127  1.1  christos #endif
    128  1.1  christos 	    biosbootmsg, createmsg, destroymsg,
    129  1.1  christos 	    headermsg, labelmsg1, labelmsg2, labelmsg3,
    130  1.1  christos 	    migratemsg, recovermsg,
    131  1.1  christos 	    removemsg1, removemsg2,
    132  1.1  christos 	    resizemsg, resizediskmsg,
    133  1.1  christos #ifndef HAVE_NBTOOL_CONFIG_H
    134  1.1  christos 	    restoremsg,
    135  1.1  christos #endif
    136  1.1  christos 	    setmsg, showmsg,
    137  1.1  christos 	    typemsg1, typemsg2, typemsg3,
    138  1.1  christos 	    unsetmsg);
    139  1.1  christos 	exit(1);
    140  1.1  christos }
    141  1.1  christos 
    142  1.1  christos static void
    143  1.1  christos prefix(const char *cmd)
    144  1.1  christos {
    145  1.1  christos 	char *pfx;
    146  1.1  christos 
    147  1.1  christos 	if (asprintf(&pfx, "%s %s", getprogname(), cmd) < 0)
    148  1.1  christos 		pfx = NULL;
    149  1.1  christos 	else
    150  1.1  christos 		setprogname(pfx);
    151  1.1  christos }
    152  1.1  christos 
    153  1.1  christos int
    154  1.1  christos main(int argc, char *argv[])
    155  1.1  christos {
    156  1.1  christos 	char *cmd, *p;
    157  1.1  christos 	int ch, i;
    158  1.1  christos 
    159  1.1  christos 	/* Get the generic options */
    160  1.1  christos 	while ((ch = getopt(argc, argv, "m:np:qrs:v")) != -1) {
    161  1.1  christos 		switch(ch) {
    162  1.1  christos 		case 'm':
    163  1.1  christos 			if (mediasz > 0)
    164  1.1  christos 				usage();
    165  1.1  christos 			mediasz = strtoul(optarg, &p, 10);
    166  1.1  christos 			if (*p != 0 || mediasz < 1)
    167  1.1  christos 				usage();
    168  1.1  christos 			break;
    169  1.1  christos 		case 'n':
    170  1.1  christos 			nosync = 1;
    171  1.1  christos 			break;
    172  1.1  christos 		case 'p':
    173  1.1  christos 			if (parts > 0)
    174  1.1  christos 				usage();
    175  1.1  christos 			parts = strtoul(optarg, &p, 10);
    176  1.1  christos 			if (*p != 0 || parts < 1)
    177  1.1  christos 				usage();
    178  1.1  christos 			break;
    179  1.1  christos 		case 'r':
    180  1.1  christos 			readonly = 1;
    181  1.1  christos 			break;
    182  1.1  christos 		case 'q':
    183  1.1  christos 			quiet = 1;
    184  1.1  christos 			break;
    185  1.1  christos 		case 's':
    186  1.1  christos 			if (secsz > 0)
    187  1.1  christos 				usage();
    188  1.1  christos 			secsz = strtoul(optarg, &p, 10);
    189  1.1  christos 			if (*p != 0 || secsz < 1)
    190  1.1  christos 				usage();
    191  1.1  christos 			break;
    192  1.1  christos 		case 'v':
    193  1.1  christos 			verbose++;
    194  1.1  christos 			break;
    195  1.1  christos 		default:
    196  1.1  christos 			usage();
    197  1.1  christos 		}
    198  1.1  christos 	}
    199  1.1  christos 	if (!parts)
    200  1.1  christos 		parts = 128;
    201  1.1  christos 
    202  1.1  christos 	if (argc == optind)
    203  1.1  christos 		usage();
    204  1.1  christos 
    205  1.1  christos 	cmd = argv[optind++];
    206  1.1  christos 	for (i = 0; cmdsw[i].name != NULL && strcmp(cmd, cmdsw[i].name); i++);
    207  1.1  christos 
    208  1.1  christos 	if (cmdsw[i].fptr == NULL)
    209  1.1  christos 		errx(1, "unknown command: %s", cmd);
    210  1.1  christos 
    211  1.1  christos 	prefix(cmd);
    212  1.1  christos 	return ((*cmdsw[i].fptr)(argc, argv));
    213  1.1  christos }
    214