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

📄 strfield.c

📁 Many C samples. It is a good sample for students to learn C language.
💻 C
字号:
/*  File   : strfield.c
    Author : Richard A. O'Keefe.
    Updated: 21 April 1984
    Defines: strfield()

    strfield(src, fields, chars, blanks, tabch)
	is based on the key specifications of the sort(1) command.

	tabch corresponds to 'x' in -t'x'.  If it is NUL, a field
	is leading layout (spaces, tabs &c) followed by at least
	one non-layout character, and is terminated by the next
	layout character or NUL.  If it is not NUL, a field is
	terminated by tabch or NUL.

	fields is the number of fields to skip over.  It corresponds
	to m in -m.n or +m.n .  There must be at least this many
	fields, and only the last may be terminated by NUL.

	chars is the number of characters to skip after the fields
	have been skipped.  At least this many non-NUL characters
	must remain after the fields have been skipped.  Note that
	it is entirely possible for this skip to cross one or more
	field boundaries.  This corresponds to n in +m.n or -m.n .

	Finally, if blanks is not 0, any layout characters will be
	skipped.  There need not be any.  This corresponds to the
	letter b in +2.0b or -0.4b .

	The result is NullS if the source ran out of fields or ran
	out of chars.  Otherwise it is a pointer to the first
	character of src which was not skipped.  It is quite possible
	for this character to be the terminating NUL.

    Example:
	to skip to the user-id field of /etc/passwd:
	    user_id = strfield(line, 2, 0, 0, ':');

	to check whether "line" is at least 27 characters long:
	    if (strfield(line, 0, 27, 0, 0)) then-it-is;

	to select the third blank-delimited field in a line:
	    head = strfield(line, 2, 0, 1, 0);
	    tail = strfield(head, 1, 0, 0, 0);
	    (* the field is the tail-head characters starting at head *)

    It's not a bug, it's a feature: "layout" means any ASCII character
	in the range '\1' .. ' ', including '\n', '\f' and so on.
*/

#include "strings.h"

char *strfield(src, fields, chars, blanks, tabch)
    register char *src;
    int fields, chars, blanks, tabch;
    {
	if (tabch <= 0) {
	    while (--fields >= 0) {
		while (*src <= ' ') if (!*src++) return NullS;
		while (*++src > ' ') ;
	    }
	} else
	if (fields > 0) {
	    do if (!*src) return NullS;
	    while (*src++ != tabch || --fields > 0);
	}
	while (--chars >= 0) if (!*src++) return NullS;
	if (blanks) while (*src && *src++ <= ' ') ;
	return src;
    }

⌨️ 快捷键说明

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