Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Oct 2021 13:51:35 GMT
From:      Baptiste Daroussin <bapt@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 1a4d5f13ba19 - main - ident: replace sbuf(9) with open_memstream(3)
Message-ID:  <202110281351.19SDpZiK080528@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=1a4d5f13ba19308f9909ef712c5d7eebaf1f9806

commit 1a4d5f13ba19308f9909ef712c5d7eebaf1f9806
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2021-10-28 13:39:24 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2021-10-28 13:51:23 +0000

    ident: replace sbuf(9) with open_memstream(3)
    
    This change makes ident only dependant on libc functions
    
    This makes our ident(1) more portable, also the fact that we only
    depend on libc which is maintained with excellent backward compatibility
    means that if one day ident is removed from base, someone using FreeBSD
    22 will be able to fetch ident from FreeBSD 14 to run ident against
    FreeBSD 1.0 binary
    
    MFC After:      1 week
---
 usr.bin/ident/Makefile |  2 --
 usr.bin/ident/ident.c  | 35 +++++++++++++++++++++++------------
 2 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/usr.bin/ident/Makefile b/usr.bin/ident/Makefile
index e0a3a80f7b09..0e0734081607 100644
--- a/usr.bin/ident/Makefile
+++ b/usr.bin/ident/Makefile
@@ -4,8 +4,6 @@
 
 PROG=	ident
 
-LIBADD=	sbuf
-
 HAS_TESTS=
 SUBDIR.${MK_TESTS}+= tests
 
diff --git a/usr.bin/ident/ident.c b/usr.bin/ident/ident.c
index aa9612d2fce1..14ff8ec8f335 100644
--- a/usr.bin/ident/ident.c
+++ b/usr.bin/ident/ident.c
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
+ * Copyright (c) 2015-2021 Baptiste Daroussin <bapt@FreeBSD.org>
  * Copyright (c) 2015 Xin LI <delphij@FreeBSD.org>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$");
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <xlocale.h>
 
@@ -58,10 +59,17 @@ scan(FILE *fp, const char *name, bool quiet)
 	bool hasid = false;
 	bool subversion = false;
 	analyzer_states state = INIT;
-	struct sbuf *id = sbuf_new_auto();
+	FILE* buffp;
+	char *buf;
+	size_t sz;
 	locale_t l;
 
 	l = newlocale(LC_ALL_MASK, "C", NULL);
+	sz = 0;
+	buf = NULL;
+	buffp = open_memstream(&buf, &sz);
+	if (buffp == NULL)
+		err(EXIT_FAILURE, "open_memstream()");
 
 	if (name != NULL)
 		printf("%s:\n", name);
@@ -80,9 +88,11 @@ scan(FILE *fp, const char *name, bool quiet)
 		case DELIM_SEEN:
 			if (isalpha_l(c, l)) {
 				/* Transit to KEYWORD if we see letter */
-				sbuf_clear(id);
-				sbuf_putc(id, '$');
-				sbuf_putc(id, c);
+				if (buf != NULL)
+					memset(buf, 0, sz);
+				rewind(buffp);
+				fputc('$', buffp);
+				fputc(c, buffp);
 				state = KEYWORD;
 
 				continue;
@@ -95,7 +105,7 @@ scan(FILE *fp, const char *name, bool quiet)
 			}
 			break;
 		case KEYWORD:
-			sbuf_putc(id, c);
+			fputc(c, buffp);
 
 			if (isalpha_l(c, l)) {
 				/*
@@ -125,7 +135,7 @@ scan(FILE *fp, const char *name, bool quiet)
 			break;
 		case PUNC_SEEN:
 		case PUNC_SEEN_SVN:
-			sbuf_putc(id, c);
+			fputc(c, buffp);
 
 			switch (c) {
 			case ':':
@@ -159,13 +169,13 @@ scan(FILE *fp, const char *name, bool quiet)
 			}
 			break;
 		case TEXT:
-			sbuf_putc(id, c);
+			fputc(c, buffp);
 
 			if (iscntrl_l(c, l)) {
 				/* Control characters are not allowed in this state */
 				state = INIT;
 			} else if (c == '$') {
-				sbuf_finish(id);
+				fflush(buffp);
 				/*
 				 * valid ident should end with a space.
 				 *
@@ -175,9 +185,9 @@ scan(FILE *fp, const char *name, bool quiet)
 				 * subversion mode.  No length check is enforced
 				 * because GNU RCS ident(1) does not do it either.
 				 */
-				c = sbuf_data(id)[sbuf_len(id) - 2];
+				c = buf[strlen(buf) -2 ];
 				if (c == ' ' || (subversion && c == '#')) {
-					printf("     %s\n", sbuf_data(id));
+					printf("     %s\n", buf);
 					hasid = true;
 				}
 				state = INIT;
@@ -186,7 +196,8 @@ scan(FILE *fp, const char *name, bool quiet)
 			break;
 		}
 	}
-	sbuf_delete(id);
+	fclose(buffp);
+	free(buf);
 	freelocale(l);
 
 	if (!hasid) {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202110281351.19SDpZiK080528>