[BACK]Return to label.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sbin / gpt

Annotation of src/sbin/gpt/label.c, Revision 1.30

1.1       christos    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:
1.16      christos   27: #if HAVE_NBTOOL_CONFIG_H
                     28: #include "nbtool_config.h"
                     29: #endif
                     30:
1.1       christos   31: #include <sys/cdefs.h>
1.2       christos   32: #ifdef __FBSDID
1.1       christos   33: __FBSDID("$FreeBSD: src/sbin/gpt/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $");
1.2       christos   34: #endif
                     35: #ifdef __RCSID
1.30    ! jnemeth    36: __RCSID("$NetBSD: label.c,v 1.29 2018/07/03 03:41:24 jnemeth Exp $");
1.2       christos   37: #endif
1.1       christos   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"
1.20      christos   50: #include "gpt_private.h"
1.18      christos   51: #include "gpt_uuid.h"
1.1       christos   52:
1.21      christos   53: static int cmd_label(gpt_t, int, char *[]);
1.5       riz        54:
1.21      christos   55: static const char *labelhelp[] = {
1.25      christos   56:        "-a <-l label | -f file>",
                     57:        "[-b blocknr] [-i index] [-L label] [-s sectors] [-t uuid] "
                     58:            "<-l label | -f file>",
1.21      christos   59: };
                     60:
                     61: struct gpt_cmd c_label = {
                     62:        "label",
                     63:        cmd_label,
                     64:        labelhelp, __arraycount(labelhelp),
1.29      jnemeth    65:        GPT_SYNC,
1.21      christos   66: };
                     67:
                     68: #define usage() gpt_usage(NULL, &c_label)
1.1       christos   69:
1.22      christos   70: static void
1.30    ! jnemeth    71: change(struct gpt_ent *ent, void *v, int backup)
1.1       christos   72: {
1.22      christos   73:        uint8_t *name = v;
1.24      christos   74:        utf8_to_utf16(name, ent->ent_name, __arraycount(ent->ent_name));
1.1       christos   75: }
                     76:
1.23      christos   77: static int
                     78: name_from_file(gpt_t gpt, void *v)
1.1       christos   79: {
                     80:        FILE *f;
                     81:        char *p;
                     82:        size_t maxlen = 1024;
                     83:        size_t len;
1.23      christos   84:        const char *fn = optarg;
                     85:        char **name = v;
                     86:
                     87:        if (*name != NULL)
                     88:                return -1;
1.1       christos   89:
                     90:        if (strcmp(fn, "-") != 0) {
                     91:                f = fopen(fn, "r");
1.23      christos   92:                if (f == NULL) {
                     93:                        gpt_warn(gpt, "Can't open `%s'", fn);
                     94:                        return -1;
                     95:                }
1.1       christos   96:        } else
                     97:                f = stdin;
1.23      christos   98:
                     99:        if ((*name = malloc(maxlen)) == NULL) {
                    100:                gpt_warn(gpt, "Can't copy string");
1.26      christos  101:                goto cleanup;
1.23      christos  102:        }
                    103:        len = fread(*name, 1, maxlen - 1, f);
                    104:        if (ferror(f)) {
                    105:                gpt_warn(gpt, "Can't label from `%s'", fn);
1.26      christos  106:                goto cleanup;
1.23      christos  107:        }
1.1       christos  108:        if (f != stdin)
                    109:                fclose(f);
1.23      christos  110:        (*name)[len] = '\0';
1.1       christos  111:        /* Only keep the first line, excluding the newline character. */
1.23      christos  112:        p = strchr(*name, '\n');
1.1       christos  113:        if (p != NULL)
                    114:                *p = '\0';
1.23      christos  115:        return 0;
1.26      christos  116: cleanup:
                    117:        free(*name);
                    118:        if (f != stdin)
                    119:                fclose(f);
                    120:        return -1;
1.1       christos  121: }
                    122:
1.21      christos  123: static int
1.20      christos  124: cmd_label(gpt_t gpt, int argc, char *argv[])
1.1       christos  125: {
1.20      christos  126:        int ch;
1.22      christos  127:        struct gpt_find find;
                    128:        char *name = NULL;
                    129:
                    130:        memset(&find, 0, sizeof(find));
                    131:        find.msg = "label changed";
1.1       christos  132:
                    133:        /* Get the label options */
1.22      christos  134:        while ((ch = getopt(argc, argv, GPT_FIND "f:l:")) != -1) {
1.1       christos  135:                switch(ch) {
                    136:                case 'f':
1.23      christos  137:                        if (name_from_file(gpt, &name) == -1)
1.27      christos  138:                                goto usage;
1.15      jnemeth   139:                        break;
1.1       christos  140:                case 'l':
1.23      christos  141:                        if (gpt_name_get(gpt, &name) == -1)
1.27      christos  142:                                goto usage;
1.1       christos  143:                        break;
1.22      christos  144:                default:
                    145:                        if (gpt_add_find(gpt, &find, ch) == -1)
1.27      christos  146:                                goto usage;
1.1       christos  147:                        break;
                    148:                }
                    149:        }
                    150:
1.20      christos  151:        if (name == NULL || argc != optind)
1.27      christos  152:                goto usage;
1.1       christos  153:
1.22      christos  154:        return gpt_change_ent(gpt, &find, change, name);
1.27      christos  155: usage:
1.28      christos  156:        usage();
1.27      christos  157:        free(name);
                    158:        return -1;
1.1       christos  159: }

CVSweb <webmaster@jp.NetBSD.org>