varnish-cache/lib/libvarnish/vin.c
1
/*-
2
 * Copyright (c) 2007-2010 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Dag-Erling Smørgrav <des@des.no>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * XXX: NB: also used in libvarnishapi
31
 */
32
33
#include "config.h"
34
35
#include <limits.h>
36
#include <stdint.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <unistd.h>
41
42
#include "vdef.h"
43
44
#include "vas.h"
45
#include "vin.h"
46
#include "vsb.h"
47
48
int
49 109200
VIN_n_Arg(const char *n_arg, char **dir)
50
{
51
        char nm[PATH_MAX];
52
        char dn[PATH_MAX];
53
        struct vsb vsb[1];
54
        int i;
55
56 109200
        AN(dir);
57
58
        /* First: determine the name */
59
60 109200
        if (n_arg == NULL || *n_arg == '\0') {
61 0
                if (gethostname(nm, sizeof nm) != 0)
62 0
                        return (-1);
63 109200
        } else if (strlen(n_arg) >= sizeof nm) {
64
                /* preliminary length check to avoid overflowing nm */
65 0
                errno = ENAMETOOLONG;
66 0
                return (-1);
67
        } else
68 109200
                bprintf(nm, "%s", n_arg);
69
70
        /* Second: find the directory name */
71
72 109200
        AN(VSB_init(vsb, dn, sizeof dn));
73
74 109200
        if (*nm == '/')
75 109160
                i = VSB_printf(vsb, "%s/", nm);
76
        else
77 40
                i = VSB_printf(vsb, "%s/%s/", VARNISH_STATE_DIR, nm);
78
79 109200
        if (i != 0) {
80 0
                errno = ENAMETOOLONG;
81 0
                return (-1);
82
        }
83
84 109200
        AZ(VSB_finish(vsb));
85
86 109200
        *dir = strdup(VSB_data(vsb));
87 109200
        if (*dir == NULL)
88 0
                return (-1);
89
90 109200
        VSB_fini(vsb);
91 109200
        return (0);
92 109200
}