#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
+#ifdef HAVE_WCHAR_T
+#include <wchar.h>
+#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h> /* for long options (is this portable?)*/
#include "patchlevel.h"
#ifndef lint
-FILE_RCSID("@(#)$Id: file.c,v 1.90 2004/03/03 17:24:28 christos Exp $")
+FILE_RCSID("@(#)$Id: file.c,v 1.91 2004/03/22 19:12:15 christos Exp $")
#endif /* lint */
else {
int i, wid, nw;
for (wid = 0, i = optind; i < argc; i++) {
- nw = strlen(argv[i]);
+ nw = file_mbswidth(argv[i]);
if (nw > wid)
wid = nw;
}
}
while (fgets(buf, MAXPATHLEN, f) != NULL) {
- cwid = strlen(buf) - 1;
+ cwid = file_mbswidth(buf) - 1;
if (cwid > wid)
wid = cwid;
}
}
while (fgets(buf, MAXPATHLEN, f) != NULL) {
- buf[strlen(buf)-1] = '\0';
+ buf[file_mbswidth(buf)-1] = '\0';
process(buf, wid);
if(nobuffer)
(void) fflush(stdout);
if (wid > 0 && !bflag)
(void) printf("%s%s%*s ", std_in ? "/dev/stdin" : inname,
- separator, (int) (nopad ? 0 : (wid - strlen(inname))), "");
+ separator, (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
type = magic_file(magic, std_in ? NULL : inname);
if (type == NULL)
}
#endif
+size_t
+file_mbswidth(const char *s)
+{
+#ifdef HAVE_WCHAR_H
+ size_t bytesconsumed, old_n, n, width = 0;
+ mbstate_t state;
+ wchar_t nextchar;
+ (void)memset(&state, 0, sizeof(mbstate_t));
+ old_n = n = strlen(s);
+
+ while (n > 0) {
+ bytesconsumed = mbrtowc(&nextchar, s, n, &state);
+ if (bytesconsumed == (size_t)(-1) ||
+ bytesconsumed == (size_t)(-2)) {
+ /* Something went wrong, return something reasonable */
+ return old_n;
+ }
+ if (s[0] == '\n') {
+ /*
+ * do what strlen() would do, so that caller
+ * is always right
+ */
+ width++;
+ } else
+ width += wcwidth(nextchar);
+
+ s += bytesconsumed, n -= bytesconsumed;
+ }
+ return width;
+#else
+ return strlen(s);
+#endif
+}
+
private void
usage(void)
{
*/
/*
* file.h - definitions for file(1) program
- * @(#)$Id: file.h,v 1.59 2003/10/14 19:17:17 christos Exp $
+ * @(#)$Id: file.h,v 1.60 2004/03/22 19:12:15 christos Exp $
*/
#ifndef __file_h__
protected void file_magwarn(const char *, ...);
protected void file_mdump(struct magic *);
protected void file_showstr(FILE *, const char *, size_t);
+protected size_t file_mbswidth(const char *);
protected const char *file_getbuffer(struct magic_set *);
#ifndef HAVE_STRERROR