Home | History | Annotate | Line # | Download | only in gpt
unset.c revision 1.15
      1   1.1   jnemeth /*-
      2   1.1   jnemeth  * Copyright (c) 2002 Marcel Moolenaar
      3   1.1   jnemeth  * All rights reserved.
      4   1.1   jnemeth  *
      5   1.1   jnemeth  * Redistribution and use in source and binary forms, with or without
      6   1.1   jnemeth  * modification, are permitted provided that the following conditions
      7   1.1   jnemeth  * are met:
      8   1.1   jnemeth  *
      9   1.1   jnemeth  * 1. Redistributions of source code must retain the above copyright
     10   1.1   jnemeth  *    notice, this list of conditions and the following disclaimer.
     11   1.1   jnemeth  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1   jnemeth  *    notice, this list of conditions and the following disclaimer in the
     13   1.1   jnemeth  *    documentation and/or other materials provided with the distribution.
     14   1.1   jnemeth  *
     15   1.1   jnemeth  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16   1.1   jnemeth  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17   1.1   jnemeth  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18   1.1   jnemeth  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19   1.1   jnemeth  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20   1.1   jnemeth  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21   1.1   jnemeth  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22   1.1   jnemeth  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23   1.1   jnemeth  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24   1.1   jnemeth  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25   1.1   jnemeth  */
     26   1.1   jnemeth 
     27   1.3  christos #if HAVE_NBTOOL_CONFIG_H
     28   1.3  christos #include "nbtool_config.h"
     29   1.3  christos #endif
     30   1.3  christos 
     31   1.1   jnemeth #include <sys/cdefs.h>
     32   1.1   jnemeth #ifdef __FBSDID
     33   1.1   jnemeth __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
     34   1.1   jnemeth #endif
     35   1.1   jnemeth #ifdef __RCSID
     36  1.15    martin __RCSID("$NetBSD: unset.c,v 1.15 2019/03/26 14:55:02 martin Exp $");
     37   1.1   jnemeth #endif
     38   1.1   jnemeth 
     39   1.1   jnemeth #include <sys/types.h>
     40   1.1   jnemeth 
     41   1.1   jnemeth #include <err.h>
     42   1.1   jnemeth #include <stddef.h>
     43   1.1   jnemeth #include <stdio.h>
     44   1.1   jnemeth #include <stdlib.h>
     45   1.1   jnemeth #include <string.h>
     46   1.1   jnemeth #include <unistd.h>
     47   1.1   jnemeth 
     48   1.1   jnemeth #include "map.h"
     49   1.1   jnemeth #include "gpt.h"
     50   1.7  christos #include "gpt_private.h"
     51   1.1   jnemeth 
     52   1.8  christos static int cmd_unset(gpt_t, int, char *[]);
     53   1.1   jnemeth 
     54   1.8  christos static const char *unsethelp[] = {
     55  1.10  christos 	"-a attribute -i index",
     56  1.15    martin 	"-a attribute -b startsec",
     57  1.14   mlelstv 	"-l",
     58   1.8  christos };
     59   1.8  christos 
     60   1.8  christos struct gpt_cmd c_unset = {
     61   1.8  christos 	"unset",
     62   1.8  christos 	cmd_unset,
     63   1.8  christos 	unsethelp, __arraycount(unsethelp),
     64  1.14   mlelstv 	GPT_OPTDEV,
     65   1.8  christos };
     66   1.1   jnemeth 
     67   1.8  christos #define usage() gpt_usage(NULL, &c_unset)
     68   1.1   jnemeth 
     69   1.7  christos static int
     70   1.7  christos cmd_unset(gpt_t gpt, int argc, char *argv[])
     71   1.1   jnemeth {
     72   1.7  christos 	int ch;
     73   1.9  christos 	unsigned int entry = 0;
     74   1.9  christos 	uint64_t attributes = 0;
     75  1.15    martin 	off_t start = 0;
     76  1.15    martin 	map_t m;
     77   1.1   jnemeth 
     78  1.15    martin 	while ((ch = getopt(argc, argv, "a:b:i:l")) != -1) {
     79   1.1   jnemeth 		switch(ch) {
     80   1.1   jnemeth 		case 'a':
     81  1.14   mlelstv 			if (gpt == NULL || gpt_attr_get(gpt, &attributes) == -1)
     82   1.8  christos 				return usage();
     83   1.1   jnemeth 			break;
     84  1.15    martin 		case 'b':
     85  1.15    martin 			if (gpt == NULL || gpt_human_get(gpt, &start) == -1)
     86  1.15    martin 				return usage();
     87  1.15    martin 			break;
     88   1.1   jnemeth 		case 'i':
     89  1.14   mlelstv 			if (gpt == NULL || gpt_uint_get(gpt, &entry) == -1)
     90   1.8  christos 				return usage();
     91   1.1   jnemeth 			break;
     92  1.12  christos 		case 'l':
     93  1.12  christos 			gpt_attr_help("\t");
     94  1.12  christos 			return 0;
     95   1.1   jnemeth 		default:
     96   1.8  christos 			return usage();
     97   1.1   jnemeth 		}
     98   1.1   jnemeth 	}
     99   1.1   jnemeth 
    100  1.15    martin 	if (start > 0) {
    101  1.15    martin 		for (m = map_first(gpt); m != NULL; m = m->map_next) {
    102  1.15    martin 			if (m->map_type != MAP_TYPE_GPT_PART ||
    103  1.15    martin 			    m->map_index < 1)
    104  1.15    martin 				continue;
    105  1.15    martin 			if (start != m->map_start)
    106  1.15    martin 				continue;
    107  1.15    martin 			entry = m->map_index;
    108  1.15    martin 			break;
    109  1.15    martin 		}
    110  1.15    martin 	}
    111  1.15    martin 
    112  1.14   mlelstv 	if (gpt == NULL || argc != optind)
    113   1.8  christos 		return usage();
    114   1.1   jnemeth 
    115   1.9  christos 	return gpt_attr_update(gpt, entry, 0, attributes);
    116   1.1   jnemeth }
    117