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