I read ftp source code in busybox, But I can't under what's meaning: "%s %s\r\n" 3
static int ftpcmd(const char *s1, const char *s2)
{
if (s1) {
fprintf(control_stream, (s2 ? "%s %s\r\n" : "%s %s\r\n" 3),
s1, s2);
fflush(control_stream);
}
do {
strcpy(buf, "EOF"); /* for ftp_die */
if (fgets(buf, BUFSZ - 2, control_stream) == NULL) {
ftp_die(NULL);
}
} while (!isdigit(buf[0]) || buf[3] != ' ');
buf[3] = '\0';
n = xatou(buf);
buf[3] = ' ';
return n;
}
CodePudding user response:
A string literal in an expression decays to a pointer to its first character.
Adding 3 to it results in a pointer to its fourth character.
In this case fprintf()
receives the format string "%s\r\n"
.
The statement:
fprintf(control_stream, (s2 ? "%s %s\r\n" : "%s %s\r\n" 3), s1, s2);
is quite "hacky" and create confusion, as we see here.
It is better expressed as:
if (s2) {
fprintf(control_stream, "%s %s\r\n", s1, s2);
} else {
fprintf(control_stream, "%s\r\n", s1);
}
CodePudding user response:
Lets say we have something like this:
const char format_string[] = "%s %s\r\n";
const char *ptr = &format_string[3];
The pointer ptr
will be somewhat equivalent to "%s %s\r\n" 3
.
When using ptr
as a string it will be "%s\r\n"
, i.e. it will skip the first %s
and the following space.