Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F139362176
D4239.id10485.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
D4239.id10485.diff
View Options
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,12 @@
} colors[C_NUMCOLORS];
#endif
+#define MAX_ABMON_WIDTH 5
+/* static list of month */
+static char ab_months[12][MAX_ABMON_WIDTH * 2 * MB_LEN_MAX];
+static wchar_t wab_months[12][MAX_ABMON_WIDTH * 2 * MB_LEN_MAX];
+static size_t max_width_month = 0;
+
void
printscol(const DISPLAY *dp)
{
@@ -138,6 +146,57 @@
return rc;
}
+static void
+populate_abbreviated_month(void)
+{
+ int i;
+ size_t len, col, j, nbcol, width;
+
+ for (i = 0; i < 12; i++) {
+ len = mbstowcs(wab_months[i], nl_langinfo(ABMON_1 + i),
+ sizeof(wab_months[i]));
+ if (len == (size_t)-1)
+ xo_err(1, "mbstowcs()");
+ /*
+ * count visible columns and pad if needed with spaces until
+ * MAX_ABMON_WIDTH
+ */
+ for (j = 0, col = 0, nbcol = 0; col < MAX_ABMON_WIDTH; j++) {
+ if (j >= len) {
+ wab_months[i][j] = L' ';
+ col++;
+ continue;
+ }
+ /* skip invisible columns */
+ width = wcswidth(&wab_months[i][j], 1);
+ col += width;
+ nbcol += width;
+ }
+ if (max_width_month < nbcol)
+ max_width_month = nbcol;
+ }
+
+ /*
+ * for each month, terminate the widechar version at discovered max
+ * visible column
+ */
+ for (i = 0; i < 12; i++) {
+ for (j= max_width_month; j < sizeof(wab_months); j++) {
+ col = wcswidth(wab_months[i], j);
+ if (col == max_width_month)
+ break;
+ if (col > max_width_month) {
+ j--;
+ break;
+ }
+ }
+ wab_months[i][j] = L'\0';
+ if (wcstombs(ab_months[i], wab_months[i],
+ sizeof(ab_months[i])) == (size_t)-1)
+ xo_err(1, "wcstombs()");
+ }
+}
+
/*
* print name in current style
*/
@@ -425,6 +484,22 @@
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 (max_width_month > 0 && (posb = strstr(fmt, "%b")) != NULL) {
+ snprintf(nfmt, sizeof(nfmt), "%.*s%s%s", (int)(posb - fmt),
+ fmt, ab_months[tm->tm_mon], posb + 2);
+ format = nfmt;
+ }
+ ret = strftime(str, len, format, tm);
+ return (ret);
+}
+
static void
printtime(const char *field, time_t ftime)
{
@@ -439,6 +514,9 @@
if (now == 0)
now = time(NULL);
+ if (max_width_month == 0)
+ populate_abbreviated_month();
+
#define SIXMONTHS ((365 / 2) * 86400)
if (f_timeformat) /* user specified format */
format = f_timeformat;
@@ -451,7 +529,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
Details
Attached
Mime Type
text/plain
Expires
Fri, Dec 12, 6:10 AM (9 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
26890777
Default Alt Text
D4239.id10485.diff (3 KB)
Attached To
Mode
D4239: Fix alignment in ls -l with cldr locales
Attached
Detach File
Event Timeline
Log In to Comment