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

📄 snprintf.c

📁 samba服务器!
💻 C
📖 第 1 页 / 共 3 页
字号:
		/* printf ("%llf, %f, %x\n", temp, intpart, idx); */		iconvert[iplace++] =			(caps? "0123456789ABCDEF":"0123456789abcdef")[idx];	} while (intpart && (iplace < 311));	if (iplace == 311) iplace--;	iconvert[iplace] = 0;	/* Convert fractional part */	if (fracpart)	{		do {			temp = fracpart*0.1;			my_modf(temp, &fracpart);			idx = (int) ((temp -fracpart +0.05)* 10.0);			/* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */			/* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */			fconvert[fplace++] =			(caps? "0123456789ABCDEF":"0123456789abcdef")[idx];		} while(fracpart && (fplace < 311));		if (fplace == 311) fplace--;	}	fconvert[fplace] = 0;  	/* -1 for decimal point, another -1 if we are printing a sign */	padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); 	zpadlen = max - fplace;	if (zpadlen < 0) zpadlen = 0;	if (padlen < 0) 		padlen = 0;	if (flags & DP_F_MINUS) 		padlen = -padlen; /* Left Justifty */		if ((flags & DP_F_ZERO) && (padlen > 0)) {		if (signvalue) {			dopr_outch (buffer, currlen, maxlen, signvalue);			--padlen;			signvalue = 0;		}		while (padlen > 0) {			dopr_outch (buffer, currlen, maxlen, '0');			--padlen;		}	}	while (padlen > 0) {		dopr_outch (buffer, currlen, maxlen, ' ');		--padlen;	}	if (signvalue) 		dopr_outch (buffer, currlen, maxlen, signvalue);		while (iplace > 0) 		dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);#ifdef DEBUG_SNPRINTF	printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);#endif	/*	 * Decimal point.  This should probably use locale to find the correct	 * char to print out.	 */	if (max > 0) {		dopr_outch (buffer, currlen, maxlen, '.');				while (zpadlen > 0) {			dopr_outch (buffer, currlen, maxlen, '0');			--zpadlen;		}		while (fplace > 0) 			dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);	}	while (padlen < 0) {		dopr_outch (buffer, currlen, maxlen, ' ');		++padlen;	}}static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c){	if (*currlen < maxlen) {		buffer[(*currlen)] = c;	}	(*currlen)++;}static struct pr_chunk *new_chunk(void) {	struct pr_chunk *new_c = (struct pr_chunk *)malloc(sizeof(struct pr_chunk));	if (!new_c)		return NULL;	new_c->type = 0;	new_c->num = 0;	new_c->min = 0;	new_c->min_star = NULL;	new_c->max = -1;	new_c->max_star = NULL;	new_c->flags = 0;	new_c->cflags = 0;	new_c->start = 0;	new_c->len = 0;	new_c->value = 0;	new_c->fvalue = 0;	new_c->strvalue = NULL;	new_c->pnum = NULL;	new_c->next = NULL;	return new_c;}static int add_cnk_list_entry(struct pr_chunk_x **list,				int max_num, struct pr_chunk *chunk) {	struct pr_chunk_x *l;	struct pr_chunk **c;	int max;	int cnum;	int i, pos;	if (chunk->num > max_num) {		max = chunk->num;			if (*list == NULL) {			l = (struct pr_chunk_x *)malloc(sizeof(struct pr_chunk_x) * max);			pos = 0;		} else {			l = (struct pr_chunk_x *)realloc(*list, sizeof(struct pr_chunk_x) * max);			pos = max_num;		}		if (l == NULL) {			for (i = 0; i < max; i++) {				if ((*list)[i].chunks) free((*list)[i].chunks);			}			return 0;		}		for (i = pos; i < max; i++) {			l[i].chunks = NULL;			l[i].num = 0;		}	} else {		l = *list;		max = max_num;	}	i = chunk->num - 1;	cnum = l[i].num + 1;	if (l[i].chunks == NULL) {		c = (struct pr_chunk **)malloc(sizeof(struct pr_chunk *) * cnum); 	} else {		c = (struct pr_chunk **)realloc(l[i].chunks, sizeof(struct pr_chunk *) * cnum);	}	if (c == NULL) {		for (i = 0; i < max; i++) {			if (l[i].chunks) free(l[i].chunks);		}		return 0;	}	c[l[i].num] = chunk;	l[i].chunks = c;	l[i].num = cnum;	*list = l;	return max;} int vsnprintf (char *str, size_t count, const char *fmt, va_list args){	return dopr(str, count, fmt, args);}#endif/* yes this really must be a ||. Don't muck with this (tridge) * * The logic for these two is that we need our own definition if the * OS *either* has no definition of *sprintf, or if it does have one * that doesn't work properly according to the autoconf test. */#if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF) int 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;}#endif#ifndef HAVE_C99_VSNPRINTF int printf(const char *fmt, ...){	va_list ap;	int ret;	char *s;	s = NULL;	va_start(ap, fmt);	ret = vasprintf(&s, fmt, ap);	va_end(ap);	if (s) {		fwrite(s, 1, strlen(s), stdout);	}	free(s);	return ret;}#endif#ifndef HAVE_C99_VSNPRINTF int fprintf(FILE *stream, const char *fmt, ...){	va_list ap;	int ret;	char *s;	s = NULL;	va_start(ap, fmt);	ret = vasprintf(&s, fmt, ap);	va_end(ap);	if (s) {		fwrite(s, 1, strlen(s), stream);	}	free(s);	return ret;}#endif#endif #ifndef HAVE_VASPRINTF int vasprintf(char **ptr, const char *format, va_list ap){	int ret;	va_list ap2;	VA_COPY(ap2, ap);	ret = vsnprintf(NULL, 0, format, ap2);	va_end(ap2);	if (ret <= 0) return ret;	(*ptr) = (char *)malloc(ret+1);	if (!*ptr) return -1;	VA_COPY(ap2, ap);	ret = vsnprintf(*ptr, ret+1, format, ap2);	va_end(ap2);	return ret;}#endif#ifndef HAVE_ASPRINTF int asprintf(char **ptr, const char *format, ...){	va_list ap;	int ret;		*ptr = NULL;	va_start(ap, format);	ret = vasprintf(ptr, format, ap);	va_end(ap);	return ret;}#endif#ifdef TEST_SNPRINTF int sprintf(char *str,const char *fmt,...); int printf(const char *fmt,...); int main (void){	char buf1[1024];	char buf2[1024];	char *buf3;	char *fp_fmt[] = {		"%1.1f",		"%-1.5f",		"%1.5f",		"%123.9f",		"%10.5f",		"% 10.5f",		"%+22.9f",		"%+4.9f",		"%01.3f",		"%4f",		"%3.1f",		"%3.2f",		"%.0f",		"%f",		"%-8.8f",		"%-9.9f",		NULL	};	double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 203.9, 0.96, 0.996, 			     0.9996, 1.996, 4.136, 5.030201, 0.00205,			     /* END LIST */ 0};	char *int_fmt[] = {		"%-1.5d",		"%1.5d",		"%123.9d",		"%5.5d",		"%10.5d",		"% 10.5d",		"%+22.33d",		"%01.3d",		"%4d",		"%d",		NULL	};	long int_nums[] = { -1, 134, 91340, 341, 0203, 1234567890, 0};	char *str_fmt[] = {		"%10.5s",		"%-10.5s",		"%5.10s",		"%-5.10s",		"%10.1s",		"%0.10s",		"%10.0s",		"%1.10s",		"%s",		"%.1s",		"%.10s",		"%10s",		NULL	};	char *str_vals[] = {"hello", "a", "", "a longer string", NULL};#ifdef HAVE_LONG_LONG	char *ll_fmt[] = {		"%llu",		NULL	};	LLONG ll_nums[] = { 134, 91340, 341, 0203, 1234567890, 128006186140000000LL, 0};#endif	int x, y;	int fail = 0;	int num = 0;	int l1, l2;	char *ss_fmt[] = {		"%zd",		"%zu",		NULL	};	size_t ss_nums[] = {134, 91340, 123456789, 0203, 1234567890, 0};	printf ("Testing snprintf format codes against system sprintf...\n");	for (x = 0; fp_fmt[x] ; x++) {		for (y = 0; fp_nums[y] != 0 ; y++) {			buf1[0] = buf2[0] = '\0';			l1 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);			l2 = sprintf (buf2, fp_fmt[x], fp_nums[y]);			buf1[1023] = buf2[1023] = '\0';			if (strcmp (buf1, buf2) || (l1 != l2)) {				printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n", 				       fp_fmt[x], l1, buf1, l2, buf2);				fail++;			}			num++;		}	}	for (x = 0; int_fmt[x] ; x++) {		for (y = 0; int_nums[y] != 0 ; y++) {			buf1[0] = buf2[0] = '\0';			l1 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);			l2 = sprintf (buf2, int_fmt[x], int_nums[y]);			buf1[1023] = buf2[1023] = '\0';			if (strcmp (buf1, buf2) || (l1 != l2)) {				printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n", 				       int_fmt[x], l1, buf1, l2, buf2);				fail++;			}			num++;		}	}	for (x = 0; str_fmt[x] ; x++) {		for (y = 0; str_vals[y] != 0 ; y++) {			buf1[0] = buf2[0] = '\0';			l1 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);			l2 = sprintf (buf2, str_fmt[x], str_vals[y]);			buf1[1023] = buf2[1023] = '\0';			if (strcmp (buf1, buf2) || (l1 != l2)) {				printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n", 				       str_fmt[x], l1, buf1, l2, buf2);				fail++;			}			num++;		}	}#ifdef HAVE_LONG_LONG	for (x = 0; ll_fmt[x] ; x++) {		for (y = 0; ll_nums[y] != 0 ; y++) {			buf1[0] = buf2[0] = '\0';			l1 = snprintf(buf1, sizeof(buf1), ll_fmt[x], ll_nums[y]);			l2 = sprintf (buf2, ll_fmt[x], ll_nums[y]);			buf1[1023] = buf2[1023] = '\0';			if (strcmp (buf1, buf2) || (l1 != l2)) {				printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n", 				       ll_fmt[x], l1, buf1, l2, buf2);				fail++;			}			num++;		}	}#endif#define BUFSZ 2048	buf1[0] = buf2[0] = '\0';	if ((buf3 = malloc(BUFSZ)) == NULL) {		fail++;	} else {		num++;		memset(buf3, 'a', BUFSZ);		snprintf(buf1, sizeof(buf1), "%.*s", 1, buf3);		buf1[1023] = '\0';		if (strcmp(buf1, "a") != 0) {			printf("length limit buf1 '%s' expected 'a'\n", buf1);			fail++;		}        }	buf1[0] = buf2[0] = '\0';	l1 = snprintf(buf1, sizeof(buf1), "%4$*1$d %2$s %3$*1$.*1$f", 3, "pos test", 12.3456, 9);	l2 = sprintf(buf2, "%4$*1$d %2$s %3$*1$.*1$f", 3, "pos test", 12.3456, 9);	buf1[1023] = buf2[1023] = '\0';	if (strcmp(buf1, buf2) || (l1 != l2)) {		printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",				"%4$*1$d %2$s %3$*1$.*1$f", l1, buf1, l2, buf2);		fail++;	}	buf1[0] = buf2[0] = '\0';	l1 = snprintf(buf1, sizeof(buf1), "%4$*4$d %2$s %3$*4$.*4$f", 3, "pos test", 12.3456, 9);	l2 = sprintf(buf2, "%4$*4$d %2$s %3$*4$.*4$f", 3, "pos test", 12.3456, 9);	buf1[1023] = buf2[1023] = '\0';	if (strcmp(buf1, buf2)) {		printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",				"%4$*1$d %2$s %3$*1$.*1$f", l1, buf1, l2, buf2);		fail++;	}	for (x = 0; ss_fmt[x] ; x++) {		for (y = 0; ss_nums[y] != 0 ; y++) {			buf1[0] = buf2[0] = '\0';			l1 = snprintf(buf1, sizeof(buf1), ss_fmt[x], ss_nums[y]);			l2 = sprintf (buf2, ss_fmt[x], ss_nums[y]);			buf1[1023] = buf2[1023] = '\0';			if (strcmp (buf1, buf2) || (l1 != l2)) {				printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n", 				       ss_fmt[x], l1, buf1, l2, buf2);				fail++;			}			num++;		}	}#if 0	buf1[0] = buf2[0] = '\0';	l1 = snprintf(buf1, sizeof(buf1), "%lld", (LLONG)1234567890);	l2 = sprintf(buf2, "%lld", (LLONG)1234567890);	buf1[1023] = buf2[1023] = '\0';	if (strcmp(buf1, buf2)) {		printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",				"%lld", l1, buf1, l2, buf2);		fail++;	}	buf1[0] = buf2[0] = '\0';	l1 = snprintf(buf1, sizeof(buf1), "%Lf", (LDOUBLE)890.1234567890123);	l2 = sprintf(buf2, "%Lf", (LDOUBLE)890.1234567890123);	buf1[1023] = buf2[1023] = '\0';	if (strcmp(buf1, buf2)) {		printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",				"%Lf", l1, buf1, l2, buf2);		fail++;	}#endif	printf ("%d tests failed out of %d.\n", fail, num);	printf("seeing how many digits we support\n");	{		double v0 = 0.12345678901234567890123456789012345678901;		for (x=0; x<100; x++) {			double p = pow(10, x); 			double r = v0*p;			snprintf(buf1, sizeof(buf1), "%1.1f", r);			sprintf(buf2,                "%1.1f", r);			if (strcmp(buf1, buf2)) {				printf("we seem to support %d digits\n", x-1);				break;			}		}	}	return 0;}#endif /* TEST_SNPRINTF */

⌨️ 快捷键说明

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