File: snprintf.c

package info (click to toggle)
samba 2%3A4.23.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 188,088 kB
  • sloc: ansic: 2,007,520; python: 272,673; sh: 72,258; xml: 51,653; perl: 36,094; makefile: 6,353; yacc: 5,323; exp: 1,582; lex: 1,504; cpp: 1,224; awk: 589; java: 119; csh: 58; pascal: 54; sed: 45; asm: 30
file content (29 lines) | stat: -rw-r--r-- 749 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void foo(const char *format, ...)
{
	va_list ap;
	int len;
	char buf[20];
	long long l = 1234567890;
	l *= 100;

	va_start(ap, format);
	len = vsnprintf(buf, 0, format, ap);
	va_end(ap);
	if (len != 5) exit(1);

	va_start(ap, format);
	len = vsnprintf(0, 0, format, ap);
	va_end(ap);
	if (len != 5) exit(2);

	if (snprintf(buf, 3, "hello") != 5 || strcmp(buf, "he") != 0) exit(3);

	if (snprintf(buf, 20, "%lld", l) != 12 || strcmp(buf, "123456789000") != 0) exit(4);
	if (snprintf(buf, 20, "%zu", 123456789) != 9 || strcmp(buf, "123456789") != 0) exit(5);
	if (snprintf(buf, 20, "%2\$d %1\$d", 3, 4) != 3 || strcmp(buf, "4 3") != 0) exit(6);
	if (snprintf(buf, 20, "%s", 0) < 3) exit(7);

	printf("1");
	exit(0);
}
int main(void) { foo("hello"); }