Hi there,
I recently found myself wanting ls to show file permissions in octal format, so
I decided to implement the feature. Being somewhat unfamiliar with C coding and
C coding standards in particular, I've hunched my way through the implementation
method, but seeing that it's only a few lines of code, it shouldn't break
anything as far as I can see.
Anywho, I've attached a diff to this mail, which may or may not be applicable to
the most recent version (I just did an apt-get source in Trisquel 6.0 to get the
source for coreutils).
Cheers,
Sakse Dalum
*** coreutils-8.13/src/ls.c 2011-07-28 12:38:27.000000000 +0200
--- coreutils/src/ls.c 2013-03-18 16:57:01.040463873 +0100
***************
*** 470,475 ****
--- 470,480 ----
static bool print_group = true;
+ /* True means to display permissions in octal format instead of the normal
+ * display mode. -O turn this on. */
+
+ static bool print_octal = false;
+
/* True means print the user and group id's as numbers rather
than as names. -n */
***************
*** 841,846 ****
--- 846,852 ----
{"block-size", required_argument, NULL, BLOCK_SIZE_OPTION},
{"context", no_argument, 0, 'Z'},
{"author", no_argument, NULL, AUTHOR_OPTION},
+ {"octal", no_argument, NULL, 'O'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
***************
*** 1644,1650 ****
{
int oi = -1;
int c = getopt_long (argc, argv,
! "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UXZ1",
long_options, &oi);
if (c == -1)
break;
--- 1650,1656 ----
{
int oi = -1;
int c = getopt_long (argc, argv,
! "abcdfghiklmnopqrstuvw:xABCDFGHI:LNOQRST:UXZ1",
long_options, &oi);
if (c == -1)
break;
***************
*** 1720,1725 ****
--- 1726,1736 ----
print_group = false;
break;
+ case 'O':
+ format = long_format;
+ print_octal = true;
+ break;
+
case 'p':
indicator_style = slash;
break;
***************
*** 3716,3722 ****
"optional alternate access method flag". */
{
char hbuf[INT_BUFSIZE_BOUND (uintmax_t)];
! sprintf (p, "%s %*s ", modebuf, nlink_width,
! f->stat_ok ? "?" : umaxtostr (f->stat.st_nlink, hbuf));
}
/* Increment by strlen (p) here, rather than by, e.g.,
--- 3727,3734 ----
"optional alternate access method flag". */
{
char hbuf[INT_BUFSIZE_BOUND (uintmax_t)];
! sprintf (p, print_octal ? "%o %*s " : "%s %*s ",
! print_octal ? (0777 & f->stat.st_mode) : modebuf, nlink_width,
! f->stat_ok ? "?" : umaxtostr (f->stat.st_nlink, hbuf));
}
/* Increment by strlen (p) here, rather than by, e.g.,
***************
*** 4682,4687 ****
--- 4694,4701 ----
-N, --literal print raw entry names (don't treat e.g. control\n\
characters specially)\n\
-o like -l, but do not list group information\n\
+ -O, --octal like -l, but prints permissions in octal format\n\
+ instead\n\
-p, --indicator-style=slash\n\
append / indicator to directories\n\
"), stdout);