⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snprintftest.c

📁 openssh47的源码
💻 C
字号:
/* * Copyright (c) 2005 Darren Tucker * Copyright (c) 2005 Damien Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */#define BUFSZ 2048#include <sys/types.h>#include <stdlib.h>#include <stdio.h>#include <stdarg.h>#include <string.h>static int failed = 0;static voidfail(const char *m){	fprintf(stderr, "snprintftest: %s\n", m);	failed = 1;}int x_snprintf(char *str, size_t count, const char *fmt, ...){	size_t ret;	va_list ap;	va_start(ap, fmt);	ret = vsnprintf(str, count, fmt, ap);	va_end(ap);	return ret;}intmain(void){	char b[5];	char *src;	snprintf(b,5,"123456789");	if (b[4] != '\0')		fail("snprintf does not correctly terminate long strings");	/* check for read overrun on unterminated string */	if ((src = malloc(BUFSZ)) == NULL) {		fail("malloc failed");	} else {		memset(src, 'a', BUFSZ);		snprintf(b, sizeof(b), "%.*s", 1, src);		if (strcmp(b, "a") != 0)			fail("failed with length limit '%%.s'");	}	/* check that snprintf and vsnprintf return sane values */	if (snprintf(b, 1, "%s %d", "hello", 12345) != 11)		fail("snprintf does not return required length");	if (x_snprintf(b, 1, "%s %d", "hello", 12345) != 11)		fail("vsnprintf does not return required length");	return failed;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -