label.c revision 1.22 1 /*-
2 * Copyright (c) 2005 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/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $");
34 #endif
35 #ifdef __RCSID
36 __RCSID("$NetBSD: label.c,v 1.22 2015/12/01 19:25:24 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 #include "gpt_uuid.h"
52
53 static int cmd_label(gpt_t, int, char *[]);
54
55 static const char *labelhelp[] = {
56 "-a <-l label | -f file>",
57 "[-b blocknr] [-i index] [-L label] [-s sectors] [-t uuid] <-l label | -f file>",
58 };
59
60 struct gpt_cmd c_label = {
61 "label",
62 cmd_label,
63 labelhelp, __arraycount(labelhelp),
64 0,
65 };
66
67 #define usage() gpt_usage(NULL, &c_label)
68
69 static void
70 change(struct gpt_ent *ent, void *v)
71 {
72 uint8_t *name = v;
73 utf8_to_utf16(name, ent->ent_name, 36);
74 }
75
76 static char *
77 name_from_file(const char *fn)
78 {
79 FILE *f;
80 char *p;
81 size_t maxlen = 1024;
82 size_t len;
83 char *name;
84
85 if (strcmp(fn, "-") != 0) {
86 f = fopen(fn, "r");
87 if (f == NULL)
88 err(1, "unable to open file %s", fn);
89 } else
90 f = stdin;
91 name = malloc(maxlen);
92 len = fread(name, 1, maxlen - 1, f);
93 if (ferror(f))
94 err(1, "unable to read label from file %s", fn);
95 if (f != stdin)
96 fclose(f);
97 name[len] = '\0';
98 /* Only keep the first line, excluding the newline character. */
99 p = strchr((const char *)name, '\n');
100 if (p != NULL)
101 *p = '\0';
102 return name;
103 }
104
105 static int
106 cmd_label(gpt_t gpt, int argc, char *argv[])
107 {
108 int ch;
109 struct gpt_find find;
110 char *name = NULL;
111
112 memset(&find, 0, sizeof(find));
113 find.msg = "label changed";
114
115 /* Get the label options */
116 while ((ch = getopt(argc, argv, GPT_FIND "f:l:")) != -1) {
117 switch(ch) {
118 case 'f':
119 if (name != NULL)
120 return usage();
121 name = name_from_file(optarg);
122 break;
123 case 'l':
124 if (name != NULL)
125 return usage();
126 name = strdup(optarg);
127 break;
128 default:
129 if (gpt_add_find(gpt, &find, ch) == -1)
130 return usage();
131 break;
132 }
133 }
134
135 if (name == NULL || argc != optind)
136 return usage();
137
138 return gpt_change_ent(gpt, &find, change, name);
139 }
140