Need strnlen().

Peter da Silva peter at baylor.UUCP
Sun Jan 19 01:55:48 AEST 1986


> One should never allow a character array to not have a null terminating byte.

Except that in lots of places in UNIX you find character arrays that may or
may not be null-terminated. Examples: directory entries and entries in
/etc/utmp. The following program fragment lists all files in the current
directory seperated by commas:

	if(!(fp = fopen(".", "r"))) {
		perror(".");
		return(ERROR);
	}
	commastring = "";
	while(fread(dirp, 14, 1, fp))
		if(dirp->d_ino) {
			printf("%s%.14s", commastring, dirp->d_name);
			commastring = ", ";
		}
	if(commastring[0])
		putchar('\n');

dirp->d_name may or may not be null terminated. Printf doesn't get bent out
of shape over it, now does it? (please, no flames from 4.2 people who want
me to use their routines. I know all about them. (1) I'm on a 4.2 system
right now. (2) I posted a generic UNIX implementation of them to net.sources
a few months ago (and had to deal with non-null-terminated strings then)).
-- 
-- Peter da Silva
-- UUCP: ...!shell!{baylor,graffiti}!peter; MCI: PDASILVA; CIS: 70216,1076



More information about the Comp.lang.c mailing list