From owner-freebsd-current Tue Mar 20 23:14: 0 2001 Delivered-To: freebsd-current@freebsd.org Received: from hell.branda.to (61-216-80-21.HINET-IP.hinet.net [61.216.80.21]) by hub.freebsd.org (Postfix) with ESMTP id 6A39937B71F for ; Tue, 20 Mar 2001 23:13:53 -0800 (PST) (envelope-from thinker@branda.to) Received: from localhost (localhost [127.0.0.1]) (uid 1000) by hell.branda.to with local; Wed, 21 Mar 2001 07:53:10 +0000 Date: Wed, 21 Mar 2001 07:53:10 +0000 From: thinker To: freebsd-current@freebsd.org Cc: keichii@iteration.net Subject: Re: /bin/ls mb patch again Message-ID: <20010321075310.A21539@hell.branda.to> References: <20010320023657.B47174@peorth.iteration.net> <20010320163153.A14341@hell.branda.to> <20010320023657.B47174@peorth.iteration.net> <200103210125.f2L1PU919996@harmony.village.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <200103210125.f2L1PU919996@harmony.village.org>; from imp@harmony.village.org on Tue, Mar 20, 2001 at 06:25:30PM -0700 Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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