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