Page MenuHomeFreeBSD

D4239.id10789.diff
No OneTemporary

D4239.id10789.diff

Index: bin/ls/print.c
===================================================================
--- bin/ls/print.c
+++ bin/ls/print.c
@@ -47,12 +47,14 @@
#include <fts.h>
#include <langinfo.h>
#include <libutil.h>
+#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
+#include <wchar.h>
#ifdef COLORLS
#include <ctype.h>
#include <termcap.h>
@@ -105,6 +107,9 @@
} colors[C_NUMCOLORS];
#endif
+static size_t padding_for_month[12];
+static size_t month_max_size = 0;
+
void
printscol(const DISPLAY *dp)
{
@@ -138,6 +143,93 @@
return rc;
}
+static const char
+*get_abmon(int mon)
+{
+ const char *month;
+
+ month = NULL;
+ switch (mon) {
+ case 0:
+ month = nl_langinfo(ABMON_1);
+ break;
+ case 1:
+ month = nl_langinfo(ABMON_2);
+ break;
+ case 2:
+ month = nl_langinfo(ABMON_3);
+ break;
+ case 3:
+ month = nl_langinfo(ABMON_4);
+ break;
+ case 4:
+ month = nl_langinfo(ABMON_5);
+ break;
+ case 5:
+ month = nl_langinfo(ABMON_6);
+ break;
+ case 6:
+ month = nl_langinfo(ABMON_7);
+ break;
+ case 7:
+ month = nl_langinfo(ABMON_8);
+ break;
+ case 8:
+ month = nl_langinfo(ABMON_9);
+ break;
+ case 9:
+ month = nl_langinfo(ABMON_10);
+ break;
+ case 10:
+ month = nl_langinfo(ABMON_11);
+ break;
+ case 11:
+ month = nl_langinfo(ABMON_12);
+ break;
+ }
+
+ return (month);
+}
+
+static size_t
+mbswidth(const char *month)
+{
+ wchar_t wc;
+ size_t width, donelen, clen;
+
+ width = donelen = 0;
+ while ((clen = mbrtowc(&wc, month + donelen, MB_LEN_MAX, NULL)) != 0) {
+ if (clen < 0)
+ return (-1);
+ donelen += clen;
+ width += wcwidth(wc);
+ }
+
+ return (width);
+}
+
+static void
+populate_abbreviated_month_size(void)
+{
+ int i;
+ size_t width;
+ size_t months_width[12];
+
+ for (i = 0; i < 12; i++) {
+ width = mbswidth(get_abmon(i));
+ if (width == (size_t)-1) {
+ month_max_size = -1;
+ return;
+ }
+ months_width[i] = width;
+ if (width > month_max_size)
+ month_max_size = width;
+ }
+
+ for (i = 0; i < 12; i++)
+ padding_for_month[i] = month_max_size - months_width[i];
+}
+
/*
* print name in current style
*/
@@ -425,6 +517,32 @@
xo_emit("{:device/%#*jx} ", (u_int)width, (uintmax_t)dev);
}
+static size_t
+ls_strftime(char *str, size_t len, const char *fmt, const struct tm *tm)
+{
+ char *posb, nfmt[BUFSIZ];
+ const char *format = fmt;
+ size_t ret;
+
+ if ((posb = strstr(fmt, "%b")) != NULL) {
+ if (month_max_size == 0) {
+ printf("iic\n");
+ populate_abbreviated_month_size();
+ }
+ if (month_max_size > 0) {
+ snprintf(nfmt, sizeof(nfmt), "%.*s%s%*s%s",
+ (int)(posb - fmt), fmt,
+ get_abmon(tm->tm_mon),
+ (int)padding_for_month[tm->tm_mon],
+ "",
+ posb + 2);
+ format = nfmt;
+ }
+ }
+ ret = strftime(str, len, format, tm);
+ return (ret);
+}
+
static void
printtime(const char *field, time_t ftime)
{
@@ -451,7 +569,7 @@
else
/* mmm dd yyyy || dd mmm yyyy */
format = d_first ? "%e %b %Y" : "%b %e %Y";
- strftime(longstring, sizeof(longstring), format, localtime(&ftime));
+ ls_strftime(longstring, sizeof(longstring), format, localtime(&ftime));
snprintf(fmt, sizeof(fmt), "{d:%s/%%hs} ", field);
xo_attr("value", "%ld", (long) ftime);

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 22, 2:44 AM (15 h, 6 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
25880642
Default Alt Text
D4239.id10789.diff (3 KB)

Event Timeline