label.c revision 1.23 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.23 2015/12/01 23:29:07 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 int
77 name_from_file(gpt_t gpt, void *v)
78 {
79 FILE *f;
80 char *p;
81 size_t maxlen = 1024;
82 size_t len;
83 const char *fn = optarg;
84 char **name = v;
85
86 if (*name != NULL)
87 return -1;
88
89 if (strcmp(fn, "-") != 0) {
90 f = fopen(fn, "r");
91 if (f == NULL) {
92 gpt_warn(gpt, "Can't open `%s'", fn);
93 return -1;
94 }
95 } else
96 f = stdin;
97
98 if ((*name = malloc(maxlen)) == NULL) {
99 gpt_warn(gpt, "Can't copy string");
100 return -1;
101 }
102 len = fread(*name, 1, maxlen - 1, f);
103 if (ferror(f)) {
104 free(*name);
105 gpt_warn(gpt, "Can't label from `%s'", fn);
106 return -1;
107 }
108 if (f != stdin)
109 fclose(f);
110 (*name)[len] = '\0';
111 /* Only keep the first line, excluding the newline character. */
112 p = strchr(*name, '\n');
113 if (p != NULL)
114 *p = '\0';
115 return 0;
116 }
117
118 static int
119 cmd_label(gpt_t gpt, int argc, char *argv[])
120 {
121 int ch;
122 struct gpt_find find;
123 char *name = NULL;
124
125 memset(&find, 0, sizeof(find));
126 find.msg = "label changed";
127
128 /* Get the label options */
129 while ((ch = getopt(argc, argv, GPT_FIND "f:l:")) != -1) {
130 switch(ch) {
131 case 'f':
132 if (name_from_file(gpt, &name) == -1)
133 return usage();
134 break;
135 case 'l':
136 if (gpt_name_get(gpt, &name) == -1)
137 return usage();
138 break;
139 default:
140 if (gpt_add_find(gpt, &find, ch) == -1)
141 return usage();
142 break;
143 }
144 }
145
146 if (name == NULL || argc != optind)
147 return usage();
148
149 return gpt_change_ent(gpt, &find, change, name);
150 }
151