Coverage Report

Created: 2025-07-11 06:25

/src/p11-kit/common/argv.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2012 Red Hat Inc.
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions
6
 * are met:
7
 *
8
 *     * Redistributions of source code must retain the above
9
 *       copyright notice, this list of conditions and the
10
 *       following disclaimer.
11
 *     * Redistributions in binary form must reproduce the
12
 *       above copyright notice, this list of conditions and
13
 *       the following disclaimer in the documentation and/or
14
 *       other materials provided with the distribution.
15
 *     * The names of contributors to this software may not be
16
 *       used to endorse or promote products derived from this
17
 *       software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30
 * DAMAGE.
31
 *
32
 * Author: Stef Walter <stefw@redhat.com>
33
 */
34
35
#include "config.h"
36
37
#include "argv.h"
38
#include "debug.h"
39
40
#include <ctype.h>
41
#include <stdlib.h>
42
#include <string.h>
43
44
bool
45
p11_argv_parse (const char *string,
46
                void (*sink) (char *, void *),
47
                void *argument)
48
0
{
49
0
  char quote = '\0';
50
0
  char *src, *dup, *at, *arg;
51
0
  bool ret = true;
52
53
0
  return_val_if_fail (string != NULL, false);
54
0
  return_val_if_fail (sink != NULL, false);
55
56
0
  src = dup = strdup (string);
57
0
  return_val_if_fail (dup != NULL, false);
58
59
0
  arg = at = src;
60
0
  for (src = dup; *src; src++) {
61
62
    /* Matching quote */
63
0
    if (quote == *src) {
64
0
      quote = '\0';
65
66
    /* Inside of quotes */
67
0
    } else if (quote != '\0') {
68
0
      if (*src == '\\') {
69
0
        src++;
70
0
        if (!*src) {
71
0
          ret = false;
72
0
          goto done;
73
0
        }
74
0
        if (*src != quote)
75
0
          *at++ = '\\';
76
0
      }
77
0
      *at++ = *src;
78
79
    /* Space, not inside of quotes */
80
0
    } else if (isspace (*src)) {
81
0
      *at = 0;
82
0
      sink (arg, argument);
83
0
      arg = at;
84
85
    /* Other character outside of quotes */
86
0
    } else {
87
0
      switch (*src) {
88
0
      case '\'':
89
0
      case '"':
90
0
        quote = *src;
91
0
        break;
92
0
      case '\\':
93
0
        *at++ = *src++;
94
0
        if (!*src) {
95
0
          ret = false;
96
0
          goto done;
97
0
        }
98
      /* fall through */
99
0
      default:
100
0
        *at++ = *src;
101
0
        break;
102
0
      }
103
0
    }
104
0
  }
105
106
107
0
  if (at != arg) {
108
0
    *at = 0;
109
0
    sink (arg, argument);
110
0
  }
111
112
0
done:
113
0
  free (dup);
114
0
  return ret;
115
0
}