bufargs.c

来自「apue第二版每一章节的c语言源码」· C语言 代码 · 共 35 行

C
35
字号
#include	"ourhdr.h"#define	MAXARGC		50	/* max number of arguments in buf */#define	WHITE	" \t\n"	/* white space for tokenizing arguments *//* buf[] contains white-space separated arguments.  We convert it * to an argv[] style array of pointers, and call the user's * function (*optfunc)() to process the argv[] array. * We return -1 to the caller if there's a problem parsing buf, * else we return whatever optfunc() returns.  Note that user's * buf[] array is modified (nulls placed after each token). */intbuf_args(char *buf, int (*optfunc)(int, char **)){	char	*ptr, *argv[MAXARGC];	int		argc;	if (strtok(buf, WHITE) == NULL)		/* an argv[0] is required */		return(-1);	argv[argc = 0] = buf;	while ( (ptr = strtok(NULL, WHITE)) != NULL) {		if (++argc >= MAXARGC-1)	/* -1 for room for NULL at end */			return(-1);		argv[argc] = ptr;	}	argv[++argc] = NULL;	return( (*optfunc)(argc, argv) );			/* Since argv[] pointers point into the user's buf[],			   user's function can just copy the pointers, even			   though argv[] array will disappear on return. */}

⌨️ 快捷键说明

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