Category : C Source Code
Archive   : E_C_STR.ZIP
Filename : STRFIELD.C

 
Output of file : STRFIELD.C contained in archive : E_C_STR.ZIP
/* 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;
}


  3 Responses to “Category : C Source Code
Archive   : E_C_STR.ZIP
Filename : STRFIELD.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/