1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255 |
- #include "libbb.h"
- #include "unicode.h"
- #if ENABLE_FTPD
- # undef CONFIG_FEATURE_LS_TIMESTAMPS
- # undef ENABLE_FEATURE_LS_TIMESTAMPS
- # undef IF_FEATURE_LS_TIMESTAMPS
- # undef IF_NOT_FEATURE_LS_TIMESTAMPS
- # define CONFIG_FEATURE_LS_TIMESTAMPS 1
- # define ENABLE_FEATURE_LS_TIMESTAMPS 1
- # define IF_FEATURE_LS_TIMESTAMPS(...) __VA_ARGS__
- # define IF_NOT_FEATURE_LS_TIMESTAMPS(...)
- #endif
- enum {
- TERMINAL_WIDTH = 80,
- SPLIT_FILE = 0,
- SPLIT_DIR = 1,
- SPLIT_SUBDIR = 2,
- LIST_INO = 1 << 0,
- LIST_BLOCKS = 1 << 1,
- LIST_MODEBITS = 1 << 2,
- LIST_NLINKS = 1 << 3,
- LIST_ID_NAME = 1 << 4,
- LIST_ID_NUMERIC = 1 << 5,
- LIST_CONTEXT = 1 << 6,
- LIST_SIZE = 1 << 7,
- LIST_DATE_TIME = 1 << 8,
- LIST_FULLTIME = 1 << 9,
- LIST_SYMLINK = 1 << 10,
- LIST_FILETYPE = 1 << 11,
- LIST_CLASSIFY = 1 << 12,
- LIST_MASK = (LIST_CLASSIFY << 1) - 1,
- DISP_DIRNAME = 1 << 13,
- DISP_HIDDEN = 1 << 14,
- DISP_DOT = 1 << 15,
- DISP_NOLIST = 1 << 16,
- DISP_RECURSIVE = 1 << 17,
- DISP_ROWS = 1 << 18,
- DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1),
- STYLE_COLUMNAR = 1 << 19,
- STYLE_LONG = 2 << 19,
- STYLE_SINGLE = 3 << 19,
- STYLE_MASK = STYLE_SINGLE,
- TIME_CHANGE = (1 << 21) * ENABLE_FEATURE_LS_TIMESTAMPS,
- TIME_ACCESS = (2 << 21) * ENABLE_FEATURE_LS_TIMESTAMPS,
- TIME_MASK = (3 << 21) * ENABLE_FEATURE_LS_TIMESTAMPS,
- SORT_REVERSE = 1 << 23,
- SORT_NAME = 0,
- SORT_SIZE = 1 << 24,
- SORT_ATIME = 2 << 24,
- SORT_CTIME = 3 << 24,
- SORT_MTIME = 4 << 24,
- SORT_VERSION = 5 << 24,
- SORT_EXT = 6 << 24,
- SORT_DIR = 7 << 24,
- SORT_MASK = (7 << 24) * ENABLE_FEATURE_LS_SORTFILES,
- LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \
- LIST_DATE_TIME | LIST_SYMLINK,
- };
- static const char ls_options[] ALIGN1 =
- "Cadil1gnsxQAk"
- IF_FEATURE_LS_TIMESTAMPS("cetu")
- IF_FEATURE_LS_SORTFILES("SXrv")
- IF_FEATURE_LS_FILETYPES("Fp")
- IF_FEATURE_LS_RECURSIVE("R")
- IF_SELINUX("KZ")
- IF_FEATURE_LS_FOLLOWLINKS("LH")
- IF_FEATURE_HUMAN_READABLE("h")
- IF_FEATURE_AUTOWIDTH("T:w:")
- ;
- enum {
-
-
-
-
-
-
- OPT_g = (1 << 6),
-
-
-
- OPT_Q = (1 << 10),
-
-
- OPTBIT_c = 13,
- OPTBIT_e,
- OPTBIT_t,
- OPTBIT_u,
- OPTBIT_S = OPTBIT_c + 4 * ENABLE_FEATURE_LS_TIMESTAMPS,
- OPTBIT_X,
- OPTBIT_r,
- OPTBIT_v,
- OPTBIT_F = OPTBIT_S + 4 * ENABLE_FEATURE_LS_SORTFILES,
- OPTBIT_p,
- OPTBIT_R = OPTBIT_F + 2 * ENABLE_FEATURE_LS_FILETYPES,
- OPTBIT_K = OPTBIT_R + 1 * ENABLE_FEATURE_LS_RECURSIVE,
- OPTBIT_Z,
- OPTBIT_L = OPTBIT_K + 2 * ENABLE_SELINUX,
- OPTBIT_H,
- OPTBIT_h = OPTBIT_L + 2 * ENABLE_FEATURE_LS_FOLLOWLINKS,
- OPTBIT_T = OPTBIT_h + 1 * ENABLE_FEATURE_HUMAN_READABLE,
- OPTBIT_w,
- OPTBIT_color = OPTBIT_T + 2 * ENABLE_FEATURE_AUTOWIDTH,
- OPT_c = (1 << OPTBIT_c) * ENABLE_FEATURE_LS_TIMESTAMPS,
- OPT_e = (1 << OPTBIT_e) * ENABLE_FEATURE_LS_TIMESTAMPS,
- OPT_t = (1 << OPTBIT_t) * ENABLE_FEATURE_LS_TIMESTAMPS,
- OPT_u = (1 << OPTBIT_u) * ENABLE_FEATURE_LS_TIMESTAMPS,
- OPT_S = (1 << OPTBIT_S) * ENABLE_FEATURE_LS_SORTFILES,
- OPT_X = (1 << OPTBIT_X) * ENABLE_FEATURE_LS_SORTFILES,
- OPT_r = (1 << OPTBIT_r) * ENABLE_FEATURE_LS_SORTFILES,
- OPT_v = (1 << OPTBIT_v) * ENABLE_FEATURE_LS_SORTFILES,
- OPT_F = (1 << OPTBIT_F) * ENABLE_FEATURE_LS_FILETYPES,
- OPT_p = (1 << OPTBIT_p) * ENABLE_FEATURE_LS_FILETYPES,
- OPT_R = (1 << OPTBIT_R) * ENABLE_FEATURE_LS_RECURSIVE,
- OPT_K = (1 << OPTBIT_K) * ENABLE_SELINUX,
- OPT_Z = (1 << OPTBIT_Z) * ENABLE_SELINUX,
- OPT_L = (1 << OPTBIT_L) * ENABLE_FEATURE_LS_FOLLOWLINKS,
- OPT_H = (1 << OPTBIT_H) * ENABLE_FEATURE_LS_FOLLOWLINKS,
- OPT_h = (1 << OPTBIT_h) * ENABLE_FEATURE_HUMAN_READABLE,
- OPT_T = (1 << OPTBIT_T) * ENABLE_FEATURE_AUTOWIDTH,
- OPT_w = (1 << OPTBIT_w) * ENABLE_FEATURE_AUTOWIDTH,
- OPT_color = (1 << OPTBIT_color) * ENABLE_FEATURE_LS_COLOR,
- };
- static const uint32_t opt_flags[] = {
- STYLE_COLUMNAR,
- DISP_HIDDEN | DISP_DOT,
- DISP_NOLIST,
- LIST_INO,
- LIST_LONG | STYLE_LONG,
- STYLE_SINGLE,
- LIST_LONG | STYLE_LONG,
- LIST_ID_NUMERIC | LIST_LONG | STYLE_LONG,
- LIST_BLOCKS,
- DISP_ROWS | STYLE_COLUMNAR,
- 0,
- DISP_HIDDEN,
- ENABLE_SELINUX * (LIST_CONTEXT|STYLE_SINGLE),
- #if ENABLE_FEATURE_LS_TIMESTAMPS
- TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME),
- LIST_FULLTIME,
- ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME,
- TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME),
- #endif
- #if ENABLE_FEATURE_LS_SORTFILES
- SORT_SIZE,
- SORT_EXT,
- SORT_REVERSE,
- SORT_VERSION,
- #endif
- #if ENABLE_FEATURE_LS_FILETYPES
- LIST_FILETYPE | LIST_CLASSIFY,
- LIST_FILETYPE,
- #endif
- #if ENABLE_FEATURE_LS_RECURSIVE
- DISP_RECURSIVE,
- #endif
- #if ENABLE_SELINUX
- LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME|STYLE_SINGLE,
- LIST_MODEBITS|LIST_ID_NAME|LIST_CONTEXT|STYLE_SINGLE,
- #endif
- (1U << 31)
-
- };
- struct dnode {
- const char *name;
- const char *fullname;
- struct dnode *dn_next;
- IF_SELINUX(security_context_t sid;)
- smallint fname_allocated;
-
- mode_t dn_mode_lstat;
- mode_t dn_mode_stat;
-
- mode_t dn_mode;
- off_t dn_size;
- #if ENABLE_FEATURE_LS_TIMESTAMPS || ENABLE_FEATURE_LS_SORTFILES
- time_t dn_atime;
- time_t dn_mtime;
- time_t dn_ctime;
- #endif
- ino_t dn_ino;
- blkcnt_t dn_blocks;
- nlink_t dn_nlink;
- uid_t dn_uid;
- gid_t dn_gid;
- int dn_rdev_maj;
- int dn_rdev_min;
- };
- struct globals {
- #if ENABLE_FEATURE_LS_COLOR
- smallint show_color;
- # define G_show_color (G.show_color)
- #else
- # define G_show_color 0
- #endif
- smallint exit_code;
- unsigned all_fmt;
- #if ENABLE_FEATURE_AUTOWIDTH
- unsigned terminal_width;
- # define G_terminal_width (G.terminal_width)
- #else
- # define G_terminal_width TERMINAL_WIDTH
- #endif
- #if ENABLE_FEATURE_LS_TIMESTAMPS
-
- time_t current_time_t;
- #endif
- } FIX_ALIASING;
- #define G (*(struct globals*)&bb_common_bufsiz1)
- #define INIT_G() do { \
- \
- memset(&G, 0, sizeof(G)); \
- IF_FEATURE_AUTOWIDTH(G_terminal_width = TERMINAL_WIDTH;) \
- IF_FEATURE_LS_TIMESTAMPS(time(&G.current_time_t);) \
- } while (0)
- #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
- #define APPCHAR(mode) ("\0""|""\0""\0""/""\0""\0""\0""\0""\0""@""\0""=""\0""\0""\0" [TYPEINDEX(mode)])
- #define COLOR(mode) ( \
- \
- "\037\043\043\045\042\045\043\043\000\045\044\045\043\045\045\040" \
- [TYPEINDEX(mode)])
- #define ATTR(mode) ( \
- \
- "\01\00\01\07\01\07\01\07\00\07\01\07\01\07\07\01" \
- [TYPEINDEX(mode)])
- #if ENABLE_FEATURE_LS_COLOR
- static char fgcolor(mode_t mode)
- {
- if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
- return COLOR(0xF000);
- return COLOR(mode);
- }
- static char bold(mode_t mode)
- {
- if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
- return ATTR(0xF000);
- return ATTR(mode);
- }
- #endif
- #if ENABLE_FEATURE_LS_FILETYPES
- static char append_char(mode_t mode)
- {
- if (!(G.all_fmt & LIST_FILETYPE))
- return '\0';
- if (S_ISDIR(mode))
- return '/';
- if (!(G.all_fmt & LIST_CLASSIFY))
- return '\0';
- if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
- return '*';
- return APPCHAR(mode);
- }
- #endif
- static unsigned calc_name_len(const char *name)
- {
- unsigned len;
- uni_stat_t uni_stat;
-
- name = printable_string(&uni_stat, name);
- if (!(option_mask32 & OPT_Q)) {
- return uni_stat.unicode_width;
- }
- len = 2 + uni_stat.unicode_width;
- while (*name) {
- if (*name == '"' || *name == '\\') {
- len++;
- }
- name++;
- }
- return len;
- }
- static unsigned print_name(const char *name)
- {
- unsigned len;
- uni_stat_t uni_stat;
-
- name = printable_string(&uni_stat, name);
- if (!(option_mask32 & OPT_Q)) {
- fputs(name, stdout);
- return uni_stat.unicode_width;
- }
- len = 2 + uni_stat.unicode_width;
- putchar('"');
- while (*name) {
- if (*name == '"' || *name == '\\') {
- putchar('\\');
- len++;
- }
- putchar(*name);
- name++;
- }
- putchar('"');
- return len;
- }
- static NOINLINE unsigned display_single(const struct dnode *dn)
- {
- unsigned column = 0;
- char *lpath;
- #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
- struct stat statbuf;
- char append;
- #endif
- #if ENABLE_FEATURE_LS_FILETYPES
- append = append_char(dn->dn_mode);
- #endif
-
- lpath = NULL;
- if (G.all_fmt & LIST_SYMLINK)
- if (S_ISLNK(dn->dn_mode))
- lpath = xmalloc_readlink_or_warn(dn->fullname);
- if (G.all_fmt & LIST_INO)
- column += printf("%7llu ", (long long) dn->dn_ino);
- if (G.all_fmt & LIST_BLOCKS)
- column += printf("%6"OFF_FMT"u ", (off_t) (dn->dn_blocks >> 1));
- if (G.all_fmt & LIST_MODEBITS)
- column += printf("%-10s ", (char *) bb_mode_string(dn->dn_mode));
- if (G.all_fmt & LIST_NLINKS)
- column += printf("%4lu ", (long) dn->dn_nlink);
- if (G.all_fmt & LIST_ID_NUMERIC) {
- if (option_mask32 & OPT_g)
- column += printf("%-8u ", (int) dn->dn_gid);
- else
- column += printf("%-8u %-8u ",
- (int) dn->dn_uid,
- (int) dn->dn_gid);
- }
- #if ENABLE_FEATURE_LS_USERNAME
- else if (G.all_fmt & LIST_ID_NAME) {
- if (option_mask32 & OPT_g) {
- column += printf("%-8.8s ",
- get_cached_groupname(dn->dn_gid));
- } else {
- column += printf("%-8.8s %-8.8s ",
- get_cached_username(dn->dn_uid),
- get_cached_groupname(dn->dn_gid));
- }
- }
- #endif
- if (G.all_fmt & LIST_SIZE) {
- if (S_ISBLK(dn->dn_mode) || S_ISCHR(dn->dn_mode)) {
- column += printf("%4u, %3u ",
- dn->dn_rdev_maj,
- dn->dn_rdev_min);
- } else {
- if (option_mask32 & OPT_h) {
- column += printf("%"HUMAN_READABLE_MAX_WIDTH_STR"s ",
-
- make_human_readable_str(dn->dn_size, 1, 0)
- );
- } else {
- column += printf("%9"OFF_FMT"u ", dn->dn_size);
- }
- }
- }
- #if ENABLE_FEATURE_LS_TIMESTAMPS
- if (G.all_fmt & (LIST_FULLTIME|LIST_DATE_TIME)) {
- char *filetime;
- time_t ttime = dn->dn_mtime;
- if (G.all_fmt & TIME_ACCESS)
- ttime = dn->dn_atime;
- if (G.all_fmt & TIME_CHANGE)
- ttime = dn->dn_ctime;
- filetime = ctime(&ttime);
-
- if (G.all_fmt & LIST_FULLTIME) {
-
- column += printf("%.24s ", filetime);
- } else {
-
- time_t age = G.current_time_t - ttime;
- printf("%.6s ", filetime + 4);
- if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
-
- printf("%.5s ", filetime + 11);
- } else {
- printf(" %.4s ", filetime + 20);
- }
- column += 13;
- }
- }
- #endif
- #if ENABLE_SELINUX
- if (G.all_fmt & LIST_CONTEXT) {
- column += printf("%-32s ", dn->sid ? dn->sid : "unknown");
- freecon(dn->sid);
- }
- #endif
- #if ENABLE_FEATURE_LS_COLOR
- if (G_show_color) {
- mode_t mode = dn->dn_mode_lstat;
- if (!mode)
- if (lstat(dn->fullname, &statbuf) == 0)
- mode = statbuf.st_mode;
- printf("\033[%u;%um", bold(mode), fgcolor(mode));
- }
- #endif
- column += print_name(dn->name);
- if (G_show_color) {
- printf("\033[0m");
- }
- if (lpath) {
- printf(" -> ");
- #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
- if ((G.all_fmt & LIST_FILETYPE) || G_show_color) {
- mode_t mode = dn->dn_mode_stat;
- if (!mode)
- if (stat(dn->fullname, &statbuf) == 0)
- mode = statbuf.st_mode;
- # if ENABLE_FEATURE_LS_FILETYPES
- append = append_char(mode);
- # endif
- # if ENABLE_FEATURE_LS_COLOR
- if (G_show_color) {
- printf("\033[%u;%um", bold(mode), fgcolor(mode));
- }
- # endif
- }
- #endif
- column += print_name(lpath) + 4;
- free(lpath);
- if (G_show_color) {
- printf("\033[0m");
- }
- }
- #if ENABLE_FEATURE_LS_FILETYPES
- if (G.all_fmt & LIST_FILETYPE) {
- if (append) {
- putchar(append);
- column++;
- }
- }
- #endif
- return column;
- }
- static void display_files(struct dnode **dn, unsigned nfiles)
- {
- unsigned i, ncols, nrows, row, nc;
- unsigned column;
- unsigned nexttab;
- unsigned column_width = 0;
- if (G.all_fmt & STYLE_LONG) {
- ncols = 1;
- } else {
-
- for (i = 0; dn[i]; i++) {
- int len = calc_name_len(dn[i]->name);
- if (column_width < len)
- column_width = len;
- }
- column_width += 1 +
- IF_SELINUX( ((G.all_fmt & LIST_CONTEXT) ? 33 : 0) + )
- ((G.all_fmt & LIST_INO) ? 8 : 0) +
- ((G.all_fmt & LIST_BLOCKS) ? 5 : 0);
- ncols = (unsigned)G_terminal_width / column_width;
- }
- if (ncols > 1) {
- nrows = nfiles / ncols;
- if (nrows * ncols < nfiles)
- nrows++;
- } else {
- nrows = nfiles;
- ncols = 1;
- }
- column = 0;
- nexttab = 0;
- for (row = 0; row < nrows; row++) {
- for (nc = 0; nc < ncols; nc++) {
-
- if (G.all_fmt & DISP_ROWS)
- i = (row * ncols) + nc;
- else
- i = (nc * nrows) + row;
- if (i < nfiles) {
- if (column > 0) {
- nexttab -= column;
- printf("%*s ", nexttab, "");
- column += nexttab + 1;
- }
- nexttab = column + column_width;
- column += display_single(dn[i]);
- }
- }
- putchar('\n');
- column = 0;
- }
- }
- static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
- {
- struct stat statbuf;
- struct dnode *cur;
- cur = xzalloc(sizeof(*cur));
- cur->fullname = fullname;
- cur->name = name;
- if ((option_mask32 & OPT_L) || force_follow) {
- #if ENABLE_SELINUX
- if (is_selinux_enabled()) {
- getfilecon(fullname, &cur->sid);
- }
- #endif
- if (stat(fullname, &statbuf)) {
- bb_simple_perror_msg(fullname);
- G.exit_code = EXIT_FAILURE;
- free(cur);
- return NULL;
- }
- cur->dn_mode_stat = statbuf.st_mode;
- } else {
- #if ENABLE_SELINUX
- if (is_selinux_enabled()) {
- lgetfilecon(fullname, &cur->sid);
- }
- #endif
- if (lstat(fullname, &statbuf)) {
- bb_simple_perror_msg(fullname);
- G.exit_code = EXIT_FAILURE;
- free(cur);
- return NULL;
- }
- cur->dn_mode_lstat = statbuf.st_mode;
- }
-
- cur->dn_mode = statbuf.st_mode ;
- cur->dn_size = statbuf.st_size ;
- #if ENABLE_FEATURE_LS_TIMESTAMPS || ENABLE_FEATURE_LS_SORTFILES
- cur->dn_atime = statbuf.st_atime ;
- cur->dn_mtime = statbuf.st_mtime ;
- cur->dn_ctime = statbuf.st_ctime ;
- #endif
- cur->dn_ino = statbuf.st_ino ;
- cur->dn_blocks = statbuf.st_blocks;
- cur->dn_nlink = statbuf.st_nlink ;
- cur->dn_uid = statbuf.st_uid ;
- cur->dn_gid = statbuf.st_gid ;
- cur->dn_rdev_maj = major(statbuf.st_rdev);
- cur->dn_rdev_min = minor(statbuf.st_rdev);
- return cur;
- }
- static unsigned count_dirs(struct dnode **dn, int which)
- {
- unsigned dirs, all;
- if (!dn)
- return 0;
- dirs = all = 0;
- for (; *dn; dn++) {
- const char *name;
- all++;
- if (!S_ISDIR((*dn)->dn_mode))
- continue;
- name = (*dn)->name;
- if (which != SPLIT_SUBDIR
-
- || name[0] != '.'
- || (name[1] && (name[1] != '.' || name[2]))
- ) {
- dirs++;
- }
- }
- return which != SPLIT_FILE ? dirs : all - dirs;
- }
- static struct dnode **dnalloc(unsigned num)
- {
- if (num < 1)
- return NULL;
- num++;
- return xzalloc(num * sizeof(struct dnode *));
- }
- #if ENABLE_FEATURE_LS_RECURSIVE
- static void dfree(struct dnode **dnp)
- {
- unsigned i;
- if (dnp == NULL)
- return;
- for (i = 0; dnp[i]; i++) {
- struct dnode *cur = dnp[i];
- if (cur->fname_allocated)
- free((char*)cur->fullname);
- free(cur);
- }
- free(dnp);
- }
- #else
- #define dfree(...) ((void)0)
- #endif
- static struct dnode **splitdnarray(struct dnode **dn, int which)
- {
- unsigned dncnt, d;
- struct dnode **dnp;
- if (dn == NULL)
- return NULL;
-
- dncnt = count_dirs(dn, which);
-
- dnp = dnalloc(dncnt);
-
- for (d = 0; *dn; dn++) {
- if (S_ISDIR((*dn)->dn_mode)) {
- const char *name;
- if (which == SPLIT_FILE)
- continue;
- name = (*dn)->name;
- if ((which & SPLIT_DIR)
-
- || name[0] != '.'
- || (name[1] && (name[1] != '.' || name[2]))
- ) {
- dnp[d++] = *dn;
- }
- } else
- if (which == SPLIT_FILE) {
- dnp[d++] = *dn;
- }
- }
- return dnp;
- }
- #if ENABLE_FEATURE_LS_SORTFILES
- static int sortcmp(const void *a, const void *b)
- {
- struct dnode *d1 = *(struct dnode **)a;
- struct dnode *d2 = *(struct dnode **)b;
- unsigned sort_opts = G.all_fmt & SORT_MASK;
- off_t dif;
- dif = 0;
-
-
- if (sort_opts == SORT_SIZE) {
- dif = (d2->dn_size - d1->dn_size);
- } else
- if (sort_opts == SORT_ATIME) {
- dif = (d2->dn_atime - d1->dn_atime);
- } else
- if (sort_opts == SORT_CTIME) {
- dif = (d2->dn_ctime - d1->dn_ctime);
- } else
- if (sort_opts == SORT_MTIME) {
- dif = (d2->dn_mtime - d1->dn_mtime);
- } else
- if (sort_opts == SORT_DIR) {
- dif = S_ISDIR(d2->dn_mode) - S_ISDIR(d1->dn_mode);
- } else
- #if defined(HAVE_STRVERSCMP) && HAVE_STRVERSCMP == 1
- if (sort_opts == SORT_VERSION) {
- dif = strverscmp(d1->name, d2->name);
- } else
- #endif
- if (sort_opts == SORT_EXT) {
- dif = strcmp(strchrnul(d1->name, '.'), strchrnul(d2->name, '.'));
- }
- if (dif == 0) {
-
- if (ENABLE_LOCALE_SUPPORT)
- dif = strcoll(d1->name, d2->name);
- else
- dif = strcmp(d1->name, d2->name);
- }
-
- if (sizeof(dif) > sizeof(int)) {
- enum { BITS_TO_SHIFT = 8 * (sizeof(dif) - sizeof(int)) };
-
- if (dif != 0) {
- dif = 1 | (int)((uoff_t)dif >> BITS_TO_SHIFT);
- }
- }
- return (G.all_fmt & SORT_REVERSE) ? -(int)dif : (int)dif;
- }
- static void dnsort(struct dnode **dn, int size)
- {
- qsort(dn, size, sizeof(*dn), sortcmp);
- }
- static void sort_and_display_files(struct dnode **dn, unsigned nfiles)
- {
- dnsort(dn, nfiles);
- display_files(dn, nfiles);
- }
- #else
- # define dnsort(dn, size) ((void)0)
- # define sort_and_display_files(dn, nfiles) display_files(dn, nfiles)
- #endif
- static struct dnode **scan_one_dir(const char *path, unsigned *nfiles_p)
- {
- struct dnode *dn, *cur, **dnp;
- struct dirent *entry;
- DIR *dir;
- unsigned i, nfiles;
- *nfiles_p = 0;
- dir = warn_opendir(path);
- if (dir == NULL) {
- G.exit_code = EXIT_FAILURE;
- return NULL;
- }
- dn = NULL;
- nfiles = 0;
- while ((entry = readdir(dir)) != NULL) {
- char *fullname;
-
- if (entry->d_name[0] == '.') {
- if ((!entry->d_name[1] || (entry->d_name[1] == '.' && !entry->d_name[2]))
- && !(G.all_fmt & DISP_DOT)
- ) {
- continue;
- }
- if (!(G.all_fmt & DISP_HIDDEN))
- continue;
- }
- fullname = concat_path_file(path, entry->d_name);
- cur = my_stat(fullname, bb_basename(fullname), 0);
- if (!cur) {
- free(fullname);
- continue;
- }
- cur->fname_allocated = 1;
- cur->dn_next = dn;
- dn = cur;
- nfiles++;
- }
- closedir(dir);
- if (dn == NULL)
- return NULL;
-
- *nfiles_p = nfiles;
- dnp = dnalloc(nfiles);
- for (i = 0; ; i++) {
- dnp[i] = dn;
- dn = dn->dn_next;
- if (!dn)
- break;
- }
- return dnp;
- }
- #if ENABLE_DESKTOP
- static off_t calculate_blocks(struct dnode **dn)
- {
- uoff_t blocks = 1;
- if (dn) {
- while (*dn) {
-
- blocks += (*dn)->dn_blocks;
- dn++;
- }
- }
-
-
- return blocks >> 1;
- }
- #endif
- static void scan_and_display_dirs_recur(struct dnode **dn, int first)
- {
- unsigned nfiles;
- struct dnode **subdnp;
- for (; *dn; dn++) {
- if (G.all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
- if (!first)
- bb_putchar('\n');
- first = 0;
- printf("%s:\n", (*dn)->fullname);
- }
- subdnp = scan_one_dir((*dn)->fullname, &nfiles);
- #if ENABLE_DESKTOP
- if ((G.all_fmt & STYLE_MASK) == STYLE_LONG)
- printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp));
- #endif
- if (nfiles > 0) {
-
- sort_and_display_files(subdnp, nfiles);
- if (ENABLE_FEATURE_LS_RECURSIVE
- && (G.all_fmt & DISP_RECURSIVE)
- ) {
- struct dnode **dnd;
- unsigned dndirs;
-
- dnd = splitdnarray(subdnp, SPLIT_SUBDIR);
- dndirs = count_dirs(subdnp, SPLIT_SUBDIR);
- if (dndirs > 0) {
- dnsort(dnd, dndirs);
- scan_and_display_dirs_recur(dnd, 0);
-
- free(dnd);
- }
- }
-
- dfree(subdnp);
- }
- }
- }
- int ls_main(int argc UNUSED_PARAM, char **argv)
- {
- struct dnode **dnd;
- struct dnode **dnf;
- struct dnode **dnp;
- struct dnode *dn;
- struct dnode *cur;
- unsigned opt;
- unsigned nfiles;
- unsigned dnfiles;
- unsigned dndirs;
- unsigned i;
- #if ENABLE_FEATURE_LS_COLOR
-
-
- static const char ls_longopts[] ALIGN1 =
- "color\0" Optional_argument "\xff";
- static const char color_str[] ALIGN1 =
- "always\0""yes\0""force\0"
- "auto\0""tty\0""if-tty\0";
-
- const char *color_opt = color_str;
- #endif
- INIT_G();
- init_unicode();
- if (ENABLE_FEATURE_LS_SORTFILES)
- G.all_fmt = SORT_NAME;
- #if ENABLE_FEATURE_AUTOWIDTH
-
- get_terminal_width_height(STDIN_FILENO, &G_terminal_width, NULL);
-
- G_terminal_width--;
- #endif
-
- IF_FEATURE_LS_COLOR(applet_long_options = ls_longopts;)
- opt_complementary =
-
- IF_FEATURE_LS_TIMESTAMPS("el")
-
- IF_FEATURE_LS_TIMESTAMPS(IF_FEATURE_LS_SORTFILES(":t-S:S-t"))
-
- IF_FEATURE_LS_FOLLOWLINKS(":H-L:L-H")
- ":C-xl:x-Cl:l-xC"
- ":C-1:1-C"
- ":x-1:1-x"
- IF_FEATURE_LS_TIMESTAMPS(":c-u:u-c")
-
- IF_FEATURE_AUTOWIDTH(":w+");
- opt = getopt32(argv, ls_options
- IF_FEATURE_AUTOWIDTH(, NULL, &G_terminal_width)
- IF_FEATURE_LS_COLOR(, &color_opt)
- );
- for (i = 0; opt_flags[i] != (1U << 31); i++) {
- if (opt & (1 << i)) {
- uint32_t flags = opt_flags[i];
- if (flags & STYLE_MASK)
- G.all_fmt &= ~STYLE_MASK;
- if (flags & SORT_MASK)
- G.all_fmt &= ~SORT_MASK;
- if (flags & TIME_MASK)
- G.all_fmt &= ~TIME_MASK;
- G.all_fmt |= flags;
- }
- }
- #if ENABLE_FEATURE_LS_COLOR
-
- if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
- char *p = getenv("LS_COLORS");
-
- if (!p || (p[0] && strcmp(p, "none") != 0))
- G_show_color = 1;
- }
- if (opt & OPT_color) {
- if (color_opt[0] == 'n')
- G_show_color = 0;
- else switch (index_in_substrings(color_str, color_opt)) {
- case 3:
- case 4:
- case 5:
- if (isatty(STDOUT_FILENO)) {
- case 0:
- case 1:
- case 2:
- G_show_color = 1;
- }
- }
- }
- #endif
-
- if (ENABLE_FEATURE_LS_RECURSIVE && (G.all_fmt & DISP_NOLIST))
- G.all_fmt &= ~DISP_RECURSIVE;
- if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
- if (G.all_fmt & TIME_CHANGE)
- G.all_fmt = (G.all_fmt & ~SORT_MASK) | SORT_CTIME;
- if (G.all_fmt & TIME_ACCESS)
- G.all_fmt = (G.all_fmt & ~SORT_MASK) | SORT_ATIME;
- }
- if ((G.all_fmt & STYLE_MASK) != STYLE_LONG)
- G.all_fmt &= ~(LIST_ID_NUMERIC|LIST_ID_NAME|LIST_FULLTIME);
-
- if (!(G.all_fmt & STYLE_MASK))
- G.all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNAR : STYLE_SINGLE);
- argv += optind;
- if (!argv[0])
- *--argv = (char*)".";
- if (argv[1])
- G.all_fmt |= DISP_DIRNAME;
-
- dn = NULL;
- nfiles = 0;
- do {
- cur = my_stat(*argv, *argv,
-
- !((G.all_fmt & STYLE_MASK) == STYLE_LONG
- || (G.all_fmt & LIST_BLOCKS)
- || (option_mask32 & OPT_F)
- )
-
- || (option_mask32 & OPT_H)
-
- );
- argv++;
- if (!cur)
- continue;
-
- cur->dn_next = dn;
- dn = cur;
- nfiles++;
- } while (*argv);
-
- if (nfiles == 0)
- return G.exit_code;
-
- dnp = dnalloc(nfiles);
- for (i = 0; ; i++) {
- dnp[i] = dn;
- dn = dn->dn_next;
- if (!dn)
- break;
- }
- if (G.all_fmt & DISP_NOLIST) {
- sort_and_display_files(dnp, nfiles);
- } else {
- dnd = splitdnarray(dnp, SPLIT_DIR);
- dnf = splitdnarray(dnp, SPLIT_FILE);
- dndirs = count_dirs(dnp, SPLIT_DIR);
- dnfiles = nfiles - dndirs;
- if (dnfiles > 0) {
- sort_and_display_files(dnf, dnfiles);
- if (ENABLE_FEATURE_CLEAN_UP)
- free(dnf);
- }
- if (dndirs > 0) {
- dnsort(dnd, dndirs);
- scan_and_display_dirs_recur(dnd, dnfiles == 0);
- if (ENABLE_FEATURE_CLEAN_UP)
- free(dnd);
- }
- }
- if (ENABLE_FEATURE_CLEAN_UP)
- dfree(dnp);
- return G.exit_code;
- }
|