X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=coreutils%2Fls.c;h=862da436854de79f2222a60804fe3c25f2e62359;hb=96f1f33e560295e0ef290d96ec2ca54a6ebee23e;hp=2566beea0667fde23f79fef2ed442ac201e42415;hpb=cc8ed39b240180b58810784f844e253263594ac3;p=oweals%2Fbusybox.git diff --git a/coreutils/ls.c b/coreutils/ls.c index 2566beea0..862da4368 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -1,4 +1,3 @@ -#include "internal.h" /* * tiny-ls.c version 0.1.0: A minimalist 'ls' * Copyright (C) 1996 Brian Candler @@ -41,22 +40,15 @@ * 1. requires lstat (BSD) - how do you do it without? */ -#define FEATURE_USERNAME /* show username/groupnames (libc6 uses NSS) */ -#define FEATURE_TIMESTAMPS /* show file timestamps */ -#define FEATURE_AUTOWIDTH /* calculate terminal & column widths */ -#define FEATURE_FILETYPECHAR /* enable -p and -F */ - -#undef OP_BUF_SIZE 1024 /* leave undefined for unbuffered output */ - #define TERMINAL_WIDTH 80 /* use 79 if your terminal has linefold bug */ #define COLUMN_WIDTH 14 /* default if AUTOWIDTH not defined */ #define COLUMN_GAP 2 /* includes the file type char, if present */ +#define HAS_REWINDDIR /************************************************************************/ -#define HAS_REWINDDIR - -#if 1 /* FIXME libc 6 */ +#include "internal.h" +#if !defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) # include #else # include @@ -67,17 +59,13 @@ #include #include #include -#ifdef FEATURE_USERNAME -#include -#include -#endif -#ifdef FEATURE_TIMESTAMPS +#ifdef BB_FEATURE_LS_TIMESTAMPS #include #endif #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) -#ifdef FEATURE_FILETYPECHAR +#ifdef BB_FEATURE_LS_FILETYPES #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)]) #endif @@ -86,27 +74,6 @@ #define MINOR(dev) ((dev)&0xff) #endif -#define MODE1 "rwxrwxrwx" -#define MODE0 "---------" -#define SMODE1 "..s..s..t" -#define SMODE0 "..S..S..T" - -/* The 9 mode bits to test */ - -static const umode_t MBIT[] = { - S_IRUSR, S_IWUSR, S_IXUSR, - S_IRGRP, S_IWGRP, S_IXGRP, - S_IROTH, S_IWOTH, S_IXOTH -}; - -/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */ - -static const umode_t SBIT[] = { - 0, 0, S_ISUID, - 0, 0, S_ISGID, - 0, 0, S_ISVTX -}; - #define FMT_AUTO 0 #define FMT_LONG 1 /* one record per line, extended info */ #define FMT_SINGLE 2 /* one record per line */ @@ -131,14 +98,14 @@ static unsigned char display_fmt = FMT_AUTO; static unsigned short opts = 0; static unsigned short column = 0; -#ifdef FEATURE_AUTOWIDTH +#ifdef BB_FEATURE_AUTOWIDTH static unsigned short terminal_width = 0, column_width = 0; #else #define terminal_width TERMINAL_WIDTH #define column_width COLUMN_WIDTH #endif -#ifdef FEATURE_TIMESTAMPS +#ifdef BB_FEATURE_LS_TIMESTAMPS static unsigned char time_fmt = TIME_MOD; #endif @@ -146,7 +113,7 @@ static unsigned char time_fmt = TIME_MOD; static void writenum(long val, short minwidth) { - char scratch[20]; + char scratch[128]; char *p = scratch + sizeof(scratch); short len = 0; @@ -191,8 +158,8 @@ static void tab(short col) #undef nspaces } -#ifdef FEATURE_FILETYPECHAR -static char append_char(umode_t mode) +#ifdef BB_FEATURE_LS_FILETYPES +static char append_char(mode_t mode) { if (!(opts & DISP_FTYPE)) return '\0'; @@ -209,55 +176,55 @@ static char append_char(umode_t mode) ** **/ -static void list_single(const char *name, struct stat *info) +static void list_single(const char *name, struct stat *info, const char *fullname) { - char scratch[20]; + char scratch[PATH_MAX]; short len = strlen(name); -#ifdef FEATURE_FILETYPECHAR +#ifdef BB_FEATURE_LS_FILETYPES char append = append_char(info->st_mode); #endif if (display_fmt == FMT_LONG) { - umode_t mode = info->st_mode; - int i; - - scratch[0] = TYPECHAR(mode); - for (i=0; i<9; i++) - if (mode & SBIT[i]) - scratch[i+1] = (mode & MBIT[i]) - ? SMODE1[i] - : SMODE0[i]; - else - scratch[i+1] = (mode & MBIT[i]) - ? MODE1[i] - : MODE0[i]; + mode_t mode = info->st_mode; newline(); - wr(scratch, 10); + wr(modeString(mode), 10); column=10; - writenum((long)info->st_nlink,(short)4); + writenum((long)info->st_nlink,(short)5); fputs(" ", stdout); -#ifdef FEATURE_USERNAME +#ifdef BB_FEATURE_LS_USERNAME if (!(opts & DISP_NUMERIC)) { - struct passwd *pw = getpwuid(info->st_uid); - if (pw) - fputs(pw->pw_name, stdout); - else - writenum((long)info->st_uid,(short)0); + memset ( scratch, 0, sizeof (scratch)); + my_getpwuid( scratch, info->st_uid); + if (*scratch) { + fputs(scratch, stdout); + if ( strlen( scratch) <= 8 ) + wr(" ", 8-strlen( scratch)); + } + else { + writenum((long) info->st_uid,(short)8); + fputs(" ", stdout); + } } else #endif - writenum((long)info->st_uid,(short)0); - tab(24); -#ifdef FEATURE_USERNAME + { + writenum((long) info->st_uid,(short)8); + fputs(" ", stdout); + } +#ifdef BB_FEATURE_LS_USERNAME if (!(opts & DISP_NUMERIC)) { - struct group *gr = getgrgid(info->st_gid); - if (gr) - fputs(gr->gr_name, stdout); - else - writenum((long)info->st_gid,(short)0); + memset ( scratch, 0, sizeof (scratch)); + my_getgrgid( scratch, info->st_gid); + if (*scratch) { + fputs(scratch, stdout); + if ( strlen( scratch) <= 8 ) + wr(" ", 8-strlen( scratch)); + } + else + writenum((long) info->st_gid,(short)8); } else #endif - writenum((long)info->st_gid,(short)0); - tab(33); + writenum((long) info->st_gid,(short)8); + //tab(26); if (S_ISBLK(mode) || S_ISCHR(mode)) { writenum((long)MAJOR(info->st_rdev),(short)3); fputs(", ", stdout); @@ -266,7 +233,8 @@ static void list_single(const char *name, struct stat *info) else writenum((long)info->st_size,(short)8); fputs(" ", stdout); -#ifdef FEATURE_TIMESTAMPS + //tab(32); +#ifdef BB_FEATURE_LS_TIMESTAMPS { time_t cal; char *string; @@ -300,12 +268,12 @@ static void list_single(const char *name, struct stat *info) wr(name, len); if (S_ISLNK(mode)) { wr(" -> ", 4); - len = readlink(name, scratch, sizeof scratch); + len = readlink(fullname, scratch, sizeof scratch); if (len > 0) fwrite(scratch, 1, len, stdout); -#ifdef FEATURE_FILETYPECHAR +#ifdef BB_FEATURE_LS_FILETYPES /* show type of destination */ if (opts & DISP_FTYPE) { - if (!stat(name, info)) { + if (!stat(fullname, info)) { append = append_char(info->st_mode); if (append) fputc(append, stdout); @@ -313,7 +281,7 @@ static void list_single(const char *name, struct stat *info) } #endif } -#ifdef FEATURE_FILETYPECHAR +#ifdef BB_FEATURE_LS_FILETYPES else if (append) wr(&append, 1); #endif @@ -327,7 +295,7 @@ static void list_single(const char *name, struct stat *info) newline(); else { if (nexttab + column_width > terminal_width -#ifndef FEATURE_AUTOWIDTH +#ifndef BB_FEATURE_AUTOWIDTH || nexttab + len >= terminal_width #endif ) @@ -336,7 +304,7 @@ static void list_single(const char *name, struct stat *info) tab(nexttab); } /* work out where next column starts */ -#ifdef FEATURE_AUTOWIDTH +#ifdef BB_FEATURE_AUTOWIDTH /* we know the calculated width is big enough */ nexttab = column + column_width + COLUMN_GAP; #else @@ -349,7 +317,7 @@ static void list_single(const char *name, struct stat *info) /* now write the data */ wr(name, len); column = column + len; -#ifdef FEATURE_FILETYPECHAR +#ifdef BB_FEATURE_LS_FILETYPES if (append) wr(&append, 1), column++; #endif @@ -375,7 +343,7 @@ static int list_item(const char *name) if (!S_ISDIR(info.st_mode) || (opts & DIR_NOLIST)) { - list_single(name, &info); + list_single(name, &info, name); return 0; } @@ -390,7 +358,7 @@ static int list_item(const char *name) dir = opendir(name); if (!dir) goto listerr; -#ifdef FEATURE_AUTOWIDTH +#ifdef BB_FEATURE_AUTOWIDTH column_width = 0; while ((entry = readdir(dir)) != NULL) { short w = strlen(entry->d_name); @@ -427,7 +395,7 @@ static int list_item(const char *name) strcpy(fnend, entry->d_name); if (lstat(fullname, &info)) goto direrr; /* (shouldn't fail) */ - list_single(entry->d_name, &info); + list_single(entry->d_name, &info, fullname); } closedir(dir); return 0; @@ -436,27 +404,27 @@ direrr: closedir(dir); listerr: newline(); - name_and_error(name); + perror(name); return 1; } -const char ls_usage[] = "Usage: ls [-1a" -#ifdef FEATURE_TIMESTAMPS +static const char ls_usage[] = "ls [-1a" +#ifdef BB_FEATURE_LS_TIMESTAMPS "c" #endif "d" -#ifdef FEATURE_TIMESTAMPS +#ifdef BB_FEATURE_LS_TIMESTAMPS "e" #endif "ln" -#ifdef FEATURE_FILETYPECHAR +#ifdef BB_FEATURE_LS_FILETYPES "p" #endif -#ifdef FEATURE_TIMESTAMPS +#ifdef BB_FEATURE_LS_TIMESTAMPS "u" #endif "xAC" -#ifdef FEATURE_FILETYPECHAR +#ifdef BB_FEATURE_LS_FILETYPES "F" #endif #ifdef FEATURE_RECURSIVE @@ -465,7 +433,7 @@ const char ls_usage[] = "Usage: ls [-1a" "] [filenames...]\n"; extern int -ls_main(struct FileInfo * not_used, int argc, char * * argv) +ls_main(int argc, char * * argv) { int argi=1, i; @@ -489,7 +457,7 @@ ls_main(struct FileInfo * not_used, int argc, char * * argv) case '1': display_fmt = FMT_SINGLE; break; case 'x': display_fmt = FMT_ROWS; break; case 'C': display_fmt = FMT_COLUMNS; break; -#ifdef FEATURE_FILETYPECHAR +#ifdef BB_FEATURE_LS_FILETYPES case 'p': opts |= DISP_FTYPE; break; case 'F': opts |= DISP_FTYPE|DISP_EXEC; break; #endif @@ -500,7 +468,7 @@ ls_main(struct FileInfo * not_used, int argc, char * * argv) #ifdef FEATURE_RECURSIVE case 'R': opts |= DIR_RECURSE; break; #endif -#ifdef FEATURE_TIMESTAMPS +#ifdef BB_FEATURE_LS_TIMESTAMPS case 'u': time_fmt = TIME_ACCESS; break; case 'c': time_fmt = TIME_CHANGE; break; case 'e': opts |= DISP_FULLTIME; break; @@ -513,10 +481,10 @@ ls_main(struct FileInfo * not_used, int argc, char * * argv) /* choose a display format */ if (display_fmt == FMT_AUTO) - display_fmt = isatty(STDOUT_FILENO) ? FMT_COLUMNS : FMT_SINGLE; + display_fmt = isatty(fileno(stdout)) ? FMT_COLUMNS : FMT_SINGLE; if (argi < argc - 1) opts |= DISP_DIRNAME; /* 2 or more items? label directories */ -#ifdef FEATURE_AUTOWIDTH +#ifdef BB_FEATURE_AUTOWIDTH /* could add a -w option and/or TIOCGWINSZ call */ if (terminal_width < 1) terminal_width = TERMINAL_WIDTH; @@ -534,9 +502,10 @@ ls_main(struct FileInfo * not_used, int argc, char * * argv) while (argi < argc) i |= list_item(argv[argi++]); newline(); - return i; + exit( i); print_usage_message: - usage(ls_usage); - return 1; + usage (ls_usage); + exit( FALSE); } +