Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!panda!talcott!harvard!seismo!rochester!rocksanne!sunybcs!kitty!baylor!peter From: peter@baylor.UUCP (Peter da Silva) Newsgroups: net.lang.c Subject: Re: Re: Need strnlen(). Message-ID: <615@baylor.UUCP> Date: Sat, 18-Jan-86 10:55:48 EST Article-I.D.: baylor.615 Posted: Sat Jan 18 10:55:48 1986 Date-Received: Tue, 21-Jan-86 01:00:00 EST References: <207@a.sei.cmu.edu> Organization: The Power Elite, Houston, TX Lines: 28 > 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