Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 21 Mar 2001 07:53:10 +0000
From:      thinker <thinker@branda.to>
To:        freebsd-current@freebsd.org
Cc:        keichii@iteration.net
Subject:   Re: /bin/ls mb patch again
Message-ID:  <20010321075310.A21539@hell.branda.to>
In-Reply-To: <200103210125.f2L1PU919996@harmony.village.org>; from imp@harmony.village.org on Tue, Mar 20, 2001 at 06:25:30PM -0700
References:  <20010320023657.B47174@peorth.iteration.net> <20010320163153.A14341@hell.branda.to> <20010320023657.B47174@peorth.iteration.net> <200103210125.f2L1PU919996@harmony.village.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Mar 20, 2001 at 06:25:30PM -0700, Warner Losh wrote:
> In message <20010320023657.B47174@peorth.iteration.net> "Michael C . Wu" writes:
> : Thinker thinks that memset() is too costly to use here
> : to modify one or two bytes. I agreed with him in that
> : filenames can't be that long to justify the memset()
> : overhead.  However, with today's CPU power, I think
> : memset()'s overhead will only be noticeable with a
> : large directory filled with data.  Please tell Thinker
> : what you think.
> 
> It is mostly a conceptual thing.  You are setting memory or copying
> memory.  You should use the API for that rather than roll your own.
I don't know what does it make different. I think it likes adding
a comment at right of variable 'len' to tell people it is meaning 'length'.
Should we crazy functionize every thing? Every one have a choice.
No matter what, the code should be accepted by most people here. I
provide another version as another choice.
---------- begin ---------------
--- util.c.orig	Sun Mar 18 16:35:12 2001
+++ util.c	Wed Mar 21 07:29:51 2001
@@ -60,15 +60,41 @@
 prn_printable(s)
 	const char *s;
 {
-	unsigned char c;
-	int n;
+	const char *p;	/* String walker. */
+	char *r, *ri;	/* Ptr for result string & walker of it. */
+	int len;
+	int dc;		/* Count down of length after 'p' . */
+	size_t sz;	/* Number of bytes been processed. */
+	wchar_t c;
 
-	for (n = 0; (c = *s) != '\0'; ++s, ++n)
-		if (isprint(c))
-			putchar(c);
-		else
-			putchar('?');
-	return n;
+	if (s == NULL)
+		return (0);
+	p = s;
+	dc = len = strlen(s);
+	ri = r = (char *)malloc(len + 1);
+	if (r == NULL)
+		return (0);
+
+	while (dc > 0) {
+		sz = mbtowc(&c, p, dc);
+		if (sz < 0) {		/* Not be recognized. */
+			p++;
+			dc--;
+			*ri++ = '?';
+		} else {
+			if (isprint(c))
+				memcpy(ri, p, sz);
+			else
+				memset(ri, (int)'?', sz);
+			ri += sz;
+			p += sz;
+			dc -= sz;
+		}
+	}
+	*ri = '\0';
+	printf("%s", r);
+	free(r);
+	return (len);
 }
 
 /*

---------- end -----------------
-- 
thinker@branda.to		Branda Open Site (BOS)
thinker.bbs@bbs.yzu.edu.tw	http://www.branda.to/

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




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