/src/unit/src/nxt_file_name.c
Line | Count | Source (jump to first uncovered line) |
1 | | |
2 | | /* |
3 | | * Copyright (C) Igor Sysoev |
4 | | * Copyright (C) NGINX, Inc. |
5 | | */ |
6 | | |
7 | | #include <nxt_main.h> |
8 | | |
9 | | |
10 | | /* |
11 | | * Supported formats: |
12 | | * %s null-terminated string |
13 | | * %*s length and string |
14 | | * %FN nxt_file_name_t * |
15 | | * %V nxt_str_t * |
16 | | * %Z '\0', this null is not counted in file name lenght. |
17 | | */ |
18 | | |
19 | | nxt_int_t |
20 | | nxt_file_name_create(nxt_mp_t *mp, nxt_file_name_str_t *file_name, |
21 | | const char *format, ...) |
22 | 0 | { |
23 | 0 | u_char ch, *p; |
24 | 0 | size_t length; |
25 | 0 | va_list args; |
26 | 0 | nxt_str_t *v; |
27 | 0 | nxt_bool_t zero; |
28 | 0 | const char *fmt; |
29 | 0 | nxt_file_name_t *dst, *fn; |
30 | |
|
31 | 0 | va_start(args, format); |
32 | 0 | fmt = format; |
33 | 0 | zero = 0; |
34 | 0 | length = 0; |
35 | |
|
36 | 0 | for ( ;; ) { |
37 | 0 | ch = *fmt++; |
38 | |
|
39 | 0 | if (ch != '%') { |
40 | |
|
41 | 0 | if (ch != '\0') { |
42 | 0 | length++; |
43 | 0 | continue; |
44 | 0 | } |
45 | | |
46 | 0 | break; |
47 | 0 | } |
48 | | |
49 | 0 | ch = *fmt++; |
50 | |
|
51 | 0 | switch (ch) { |
52 | | |
53 | 0 | case 'V': |
54 | 0 | v = va_arg(args, nxt_str_t *); |
55 | |
|
56 | 0 | if (nxt_fast_path(v != NULL)) { |
57 | 0 | length += v->length; |
58 | 0 | } |
59 | |
|
60 | 0 | continue; |
61 | | |
62 | 0 | case 's': |
63 | 0 | p = va_arg(args, u_char *); |
64 | |
|
65 | 0 | if (nxt_fast_path(p != NULL)) { |
66 | 0 | while (*p != '\0') { |
67 | 0 | p++; |
68 | 0 | length++; |
69 | 0 | } |
70 | 0 | } |
71 | |
|
72 | 0 | continue; |
73 | | |
74 | 0 | case '*': |
75 | 0 | length += va_arg(args, u_int); |
76 | 0 | fmt++; |
77 | |
|
78 | 0 | continue; |
79 | | |
80 | 0 | case 'F': |
81 | 0 | ch = *fmt++; |
82 | |
|
83 | 0 | if (nxt_fast_path(ch == 'N')) { |
84 | 0 | fn = va_arg(args, nxt_file_name_t *); |
85 | |
|
86 | 0 | if (nxt_fast_path(fn != NULL)) { |
87 | 0 | while (*fn != '\0') { |
88 | 0 | fn++; |
89 | 0 | length += sizeof(nxt_file_name_t); |
90 | 0 | } |
91 | 0 | } |
92 | 0 | } |
93 | |
|
94 | 0 | continue; |
95 | | |
96 | 0 | case 'Z': |
97 | 0 | zero = 1; |
98 | 0 | length++; |
99 | 0 | continue; |
100 | | |
101 | 0 | default: |
102 | 0 | continue; |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | 0 | va_end(args); |
107 | |
|
108 | 0 | if (length == 0) { |
109 | 0 | return NXT_ERROR; |
110 | 0 | } |
111 | | |
112 | 0 | file_name->len = length - zero; |
113 | |
|
114 | 0 | fn = nxt_file_name_alloc(mp, length); |
115 | 0 | if (nxt_slow_path(fn == NULL)) { |
116 | 0 | return NXT_ERROR; |
117 | 0 | } |
118 | | |
119 | 0 | file_name->start = fn; |
120 | 0 | dst = fn; |
121 | |
|
122 | 0 | va_start(args, format); |
123 | 0 | fmt = format; |
124 | |
|
125 | 0 | for ( ;; ) { |
126 | 0 | ch = *fmt++; |
127 | |
|
128 | 0 | if (ch != '%') { |
129 | |
|
130 | 0 | if (ch != '\0') { |
131 | 0 | *dst++ = (nxt_file_name_t) ch; |
132 | 0 | continue; |
133 | 0 | } |
134 | | |
135 | 0 | break; |
136 | 0 | } |
137 | | |
138 | 0 | ch = *fmt++; |
139 | |
|
140 | 0 | switch (ch) { |
141 | | |
142 | 0 | case 'V': |
143 | 0 | v = va_arg(args, nxt_str_t *); |
144 | |
|
145 | 0 | if (nxt_fast_path(v != NULL)) { |
146 | 0 | dst = nxt_file_name_add(dst, v->start, v->length); |
147 | 0 | } |
148 | |
|
149 | 0 | continue; |
150 | | |
151 | 0 | case 's': |
152 | 0 | p = va_arg(args, u_char *); |
153 | |
|
154 | 0 | if (nxt_fast_path(p != NULL)) { |
155 | 0 | while (*p != '\0') { |
156 | 0 | *dst++ = (nxt_file_name_t) (*p++); |
157 | 0 | } |
158 | 0 | } |
159 | |
|
160 | 0 | continue; |
161 | | |
162 | 0 | case '*': |
163 | 0 | length += va_arg(args, u_int); |
164 | |
|
165 | 0 | ch = *fmt++; |
166 | |
|
167 | 0 | if (nxt_fast_path(ch == 's')) { |
168 | 0 | p = va_arg(args, u_char *); |
169 | 0 | dst = nxt_file_name_add(dst, p, length); |
170 | 0 | } |
171 | |
|
172 | 0 | continue; |
173 | | |
174 | 0 | case 'F': |
175 | 0 | ch = *fmt++; |
176 | |
|
177 | 0 | if (nxt_fast_path(ch == 'N')) { |
178 | 0 | fn = va_arg(args, nxt_file_name_t *); |
179 | |
|
180 | 0 | if (nxt_fast_path(fn != NULL)) { |
181 | 0 | while (*fn != '\0') { |
182 | 0 | *dst++ = *fn++; |
183 | 0 | } |
184 | 0 | } |
185 | 0 | } |
186 | |
|
187 | 0 | continue; |
188 | | |
189 | 0 | case 'Z': |
190 | 0 | *dst++ = '\0'; |
191 | 0 | continue; |
192 | | |
193 | 0 | default: |
194 | 0 | continue; |
195 | 0 | } |
196 | 0 | } |
197 | | |
198 | 0 | va_end(args); |
199 | |
|
200 | 0 | return NXT_OK; |
201 | 0 | } |