write_name_quoted(): make one of the path a counted string.
This is to prepare for ls-tree updates. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Nov 28, 2005 at 22:55 UTC
9ef2b3cbf62d15aa0312bde349347873d7c0f399
4 files changed
+29
-16
ls-files.c
+5
-3
@@ -344,7 +344,7 @@ static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
344
return;
345
346
fputs(tag, stdout);
347
- write_name_quoted("", ent->name + offset, line_terminator, stdout);
347
+ write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
348
putchar(line_terminator);
349
}
350
@@ -433,7 +433,8 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
433
434
if (!show_stage) {
435
fputs(tag, stdout);
436
- write_name_quoted("", ce->name + offset, line_terminator, stdout);
436
+ write_name_quoted("", 0, ce->name + offset,
437
+ line_terminator, stdout);
438
putchar(line_terminator);
439
}
440
else {
@@ -442,7 +443,8 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
443
ntohl(ce->ce_mode),
444
sha1_to_hex(ce->sha1),
445
ce_stage(ce));
445
- write_name_quoted("", ce->name + offset, line_terminator, stdout);
446
+ write_name_quoted("", 0, ce->name + offset,
447
+ line_terminator, stdout);
448
putchar(line_terminator);
449
}
450
}
ls-tree.c
+3
-1
@@ -157,9 +157,11 @@ static int show_entry(struct tree_entry_list *e, int level, char *pathbuf)
157
int err = 0;
158
159
if (e != &root_entry) {
160
+ int pathlen = strlen(pathbuf);
161
printf("%06o %s %s ",
162
e->mode, entry_type(e), entry_hex(e));
162
- write_name_quoted(pathbuf, e->name, line_termination, stdout);
163
+ write_name_quoted(pathbuf, pathlen, e->name,
164
+ line_termination, stdout);
165
putchar(line_termination);
166
}
167
quote.c
+19
-10
@@ -112,7 +112,8 @@ char *sq_dequote(char *arg)
112
* but not enclosed in double-quote pair. Return value is undefined.
113
*/
114
115
-int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
115
+static int quote_c_style_counted(const char *name, int namelen,
116
+ char *outbuf, FILE *outfp, int no_dq)
117
{
118
#undef EMIT
119
#define EMIT(c) \
@@ -125,7 +126,7 @@ int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
126
127
if (!no_dq)
128
EMIT('"');
128
- for (sp = name; (ch = *sp++); ) {
129
+ for (sp = name; (ch = *sp++) && (sp - name) <= namelen; ) {
130
131
if ((ch < ' ') || (ch == '"') || (ch == '\\') ||
132
(ch == 0177)) {
@@ -162,6 +163,12 @@ int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
163
return needquote ? count : 0;
164
}
165
166
+int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
167
+{
168
+ int cnt = strlen(name);
169
+ return quote_c_style_counted(name, cnt, outbuf, outfp, no_dq);
170
+}
171
+
172
/*
173
* C-style name unquoting.
174
*
@@ -227,28 +234,30 @@ char *unquote_c_style(const char *quoted, const char **endp)
234
}
235
}
236
230
-void write_name_quoted(const char *prefix, const char *name,
231
- int quote, FILE *out)
237
+void write_name_quoted(const char *prefix, int prefix_len,
238
+ const char *name, int quote, FILE *out)
239
{
240
int needquote;
241
242
if (!quote) {
243
no_quote:
237
- if (prefix && prefix[0])
238
- fputs(prefix, out);
244
+ if (prefix_len)
245
+ fprintf(out, "%.*s", prefix_len, prefix);
246
fputs(name, out);
247
return;
248
}
249
250
needquote = 0;
244
- if (prefix && prefix[0])
245
- needquote = quote_c_style(prefix, NULL, NULL, 0);
251
+ if (prefix_len)
252
+ needquote = quote_c_style_counted(prefix, prefix_len,
253
+ NULL, NULL, 0);
254
if (!needquote)
255
needquote = quote_c_style(name, NULL, NULL, 0);
256
if (needquote) {
257
fputc('"', out);
250
- if (prefix && prefix[0])
251
- quote_c_style(prefix, NULL, out, 1);
258
+ if (prefix_len)
259
+ quote_c_style_counted(prefix, prefix_len,
260
+ NULL, out, 1);
261
quote_c_style(name, NULL, out, 1);
262
fputc('"', out);
263
}
quote.h
+2
-2
@@ -41,7 +41,7 @@ extern int quote_c_style(const char *name, char *outbuf, FILE *outfp,
41
int nodq);
42
extern char *unquote_c_style(const char *quoted, const char **endp);
43
44
-extern void write_name_quoted(const char *prefix, const char *name,
45
- int quote, FILE *out);
44
+extern void write_name_quoted(const char *prefix, int prefix_len,
45
+ const char *name, int quote, FILE *out);
46
47
#endif