From 1ef73ccec2927bf811545c08c0f5fe66d0780cff Mon Sep 17 00:00:00 2001 From: Ray Lai Date: Wed, 8 Aug 2007 07:20:46 +0000 Subject: Show how to use strcspn(3) to trim newlines. OK jmc and millert. --- lib/libc/string/strcspn.3 | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'lib/libc/string/strcspn.3') diff --git a/lib/libc/string/strcspn.3 b/lib/libc/string/strcspn.3 index c5d9e562d25..21c727344dd 100644 --- a/lib/libc/string/strcspn.3 +++ b/lib/libc/string/strcspn.3 @@ -29,9 +29,9 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: strcspn.3,v 1.8 2007/05/31 19:19:32 jmc Exp $ +.\" $OpenBSD: strcspn.3,v 1.9 2007/08/08 07:20:45 ray Exp $ .\" -.Dd $Mdocdate: May 31 2007 $ +.Dd $Mdocdate: August 8 2007 $ .Dt STRCSPN 3 .Os .Sh NAME @@ -72,6 +72,18 @@ size_t span; span = strcspn(s, charset); .Ed +.Pp +The following removes the first (if any) newline character from string +.Fa line . +This is useful for trimming the newline after a +.Xr fgets 3 +call. +.Bd -literal -offset indent +char line[BUFSIZ]; + +if (fgets(line, sizeof(line), fp) != NULL) + line[strcspn(line, "\en")] = '\e0'; +.Ed .Sh SEE ALSO .Xr memchr 3 , .Xr strchr 3 , -- cgit v1.2.3