From e35f6842e22185ac0f596ea89c4256492468100d Mon Sep 17 00:00:00 2001 From: ray <> Date: Wed, 8 Aug 2007 07:20:45 +0000 Subject: [PATCH] Show how to use strcspn(3) to trim newlines. OK jmc and millert. --- src/lib/libc/string/strcspn.3 | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib/libc/string/strcspn.3 b/src/lib/libc/string/strcspn.3 index c5d9e562..21c72734 100644 --- a/src/lib/libc/string/strcspn.3 +++ b/src/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 ,