summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-04-11 16:54:06 -0400
committerJakob Kaivo <jkk@ung.org>2022-04-11 16:54:06 -0400
commitc2d237b4766d73be4c4897ac61fded67395c8cfe (patch)
tree4e5dccf561f53d5133837d5db7b6708a32f8ede0
parent56008cedc6068d6b6e47c4d724e766525ae858b6 (diff)
add line wrapping to serial output format
-rw-r--r--ls.c67
1 files changed, 47 insertions, 20 deletions
diff --git a/ls.c b/ls.c
index 0f7d498..b6ca33a 100644
--- a/ls.c
+++ b/ls.c
@@ -59,15 +59,13 @@ static enum {
ATIME,
} sort = ALPHA;
-#define DEFAULT_BLOCK_SIZE 512
-
static enum { HIDE_ALL, HIDE_SOME, HIDE_NONE } ls_hidden = HIDE_ALL;
static enum { GROUP_NAME, GROUP_ID, GROUP_NONE } ls_group = GROUP_NAME;
static enum { OWNER_NAME, OWNER_ID, OWNER_NONE } ls_owner = OWNER_NAME;
static enum { MARK_NONE, MARK_DIRS, MARK_ALL } ls_mark = MARK_NONE;
+static enum { BLOCKS_NONE, BLOCKS_POSIX, BLOCKS_KIBI } ls_blocks = BLOCKS_NONE;
static int recurse = 0;
-static int blocksize = DEFAULT_BLOCK_SIZE;
static int reverse = 0;
static int stat_flags = AT_SYMLINK_NOFOLLOW;
@@ -146,6 +144,7 @@ static int ls_dir(char *dir, int showdirname)
size_t nfiles = 0;
struct file_info *files = NULL;
int ret = 0;
+ uintmax_t blocks = 0;
DIR *d = opendir(dir);
if (d == NULL) {
@@ -187,16 +186,22 @@ static int ls_dir(char *dir, int showdirname)
ret = 1;
} else {
nfiles++;
+ blocks += files[nfiles].st.st_blocks;
}
}
closedir(d);
- qsort(files, nfiles, sizeof(*files), ls_compare_files);
if (showdirname) {
printf("%s:\n", dir);
}
- ls_print(nfiles, files);
+ if (ls_blocks != BLOCKS_NONE) {
+ /* TODO: adjust for BLOCK_KIBI */
+ printf("total %ju\n", blocks);
+ }
+
+ qsort(files, nfiles, sizeof(*files), ls_compare_files);
+ ls_print(nfiles, files);
free(files);
return ret;
@@ -325,19 +330,6 @@ static void ls_print_single(size_t n, struct file_info files[static n])
}
}
-static void ls_print_serial(size_t n, struct file_info files[static n])
-{
- if (n == 0) {
- return;
- }
-
- /* TODO: wrap at screen width */
- for (size_t i = 0; i < n - 1; i++) {
- printf("%s, ", ls_filename(files + i));
- }
- printf("%s\n", ls_filename(files + n - 1));
-}
-
static size_t ls_get_columns(void)
{
static size_t columns = 0;
@@ -353,6 +345,27 @@ static size_t ls_get_columns(void)
return columns;
}
+static void ls_print_serial(size_t n, struct file_info files[static n])
+{
+ if (n == 0) {
+ return;
+ }
+
+ size_t columns = ls_get_columns();
+ size_t at = 0;
+
+ /* TODO: wrap at screen width */
+ for (size_t i = 0; i < n - 1; i++) {
+ char *s = ls_filename(files + i);
+ if (at + strlen(s) > columns) {
+ printf("\n");
+ at = 0;
+ }
+ at += printf("%s, ", ls_filename(files + i));
+ }
+ printf("%s\n", ls_filename(files + n - 1));
+}
+
static void ls_print_columns(size_t n, struct file_info files[static n])
{
size_t widest = ls_find_widest(n, files);
@@ -481,6 +494,9 @@ int main(int argc, char *argv[])
break;
case 'g': /** like -l, but without owner **/
+ if (ls_blocks == BLOCKS_NONE) {
+ ls_blocks = BLOCKS_POSIX;
+ }
ls_print = ls_print_long;
ls_owner = OWNER_NONE;
break;
@@ -490,10 +506,13 @@ int main(int argc, char *argv[])
break;
case 'k': /** use a blocksize of 1024 **/
- blocksize = 1024;
+ ls_blocks = BLOCKS_KIBI;
break;
case 'l': /** output full details **/
+ if (ls_blocks == BLOCKS_NONE) {
+ ls_blocks = BLOCKS_POSIX;
+ }
ls_print = ls_print_long;
break;
@@ -502,12 +521,18 @@ int main(int argc, char *argv[])
break;
case 'n': /** like -l, but with numeric UID and GID **/
+ if (ls_blocks == BLOCKS_NONE) {
+ ls_blocks = BLOCKS_POSIX;
+ }
ls_print = ls_print_long;
ls_owner = OWNER_ID;
ls_group = GROUP_ID;
break;
case 'o': /** like -l, but without group **/
+ if (ls_blocks == BLOCKS_NONE) {
+ ls_blocks = BLOCKS_POSIX;
+ }
ls_print = ls_print_long;
ls_group = GROUP_NONE;
break;
@@ -525,7 +550,9 @@ int main(int argc, char *argv[])
break;
case 's': /** outpu total number of blocks **/
- //format |= BLOCKS; // FIXME
+ if (ls_blocks == BLOCKS_NONE) {
+ ls_blocks = BLOCKS_POSIX;
+ }
break;
case 't': /** sort by modified time **/