2011-05-26 03:01:20 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 18:54:28 +00:00
|
|
|
#include <sys/stat.h>
|
2011-05-26 03:01:20 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-05-26 03:01:20 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *name;
|
2014-12-08 01:33:52 +00:00
|
|
|
mode_t mode, tmode;
|
2011-05-26 03:01:20 +00:00
|
|
|
nlink_t nlink;
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
off_t size;
|
|
|
|
time_t mtime;
|
2013-09-27 14:32:50 +00:00
|
|
|
ino_t ino;
|
2011-05-26 03:01:20 +00:00
|
|
|
} Entry;
|
|
|
|
|
2013-07-18 19:14:53 +00:00
|
|
|
static int entcmp(const void *, const void *);
|
|
|
|
static void ls(Entry *);
|
2011-05-26 03:01:20 +00:00
|
|
|
static void lsdir(const char *);
|
2014-12-08 01:33:52 +00:00
|
|
|
static void mkent(Entry *, char *, int, int);
|
2011-05-26 03:01:20 +00:00
|
|
|
static void output(Entry *);
|
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
static int aflag = 0;
|
|
|
|
static int dflag = 0;
|
|
|
|
static int Fflag = 0;
|
2014-12-08 01:33:52 +00:00
|
|
|
static int Hflag = 0;
|
2014-11-13 20:24:47 +00:00
|
|
|
static int hflag = 0;
|
|
|
|
static int iflag = 0;
|
2014-12-08 01:33:52 +00:00
|
|
|
static int Lflag = 0;
|
2014-11-13 20:24:47 +00:00
|
|
|
static int lflag = 0;
|
|
|
|
static int rflag = 0;
|
|
|
|
static int tflag = 0;
|
|
|
|
static int Uflag = 0;
|
|
|
|
static int first = 1;
|
|
|
|
static int many;
|
2011-05-26 03:01:20 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-10-19 09:48:04 +00:00
|
|
|
eprintf("usage: %s [-1adFhilrtU] [FILE...]\n", argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 03:01:20 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2013-07-18 19:14:53 +00:00
|
|
|
int i;
|
2011-06-16 00:13:46 +00:00
|
|
|
Entry *ents;
|
2011-05-26 03:01:20 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
2014-07-05 14:35:34 +00:00
|
|
|
case '1':
|
|
|
|
/* ignore */
|
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
case 'a':
|
2014-11-13 20:24:47 +00:00
|
|
|
aflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2014-11-13 20:24:47 +00:00
|
|
|
dflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
2014-02-17 14:41:09 +00:00
|
|
|
case 'F':
|
2014-11-13 20:24:47 +00:00
|
|
|
Fflag = 1;
|
2014-02-17 14:41:09 +00:00
|
|
|
break;
|
2014-12-08 01:33:52 +00:00
|
|
|
case 'H':
|
|
|
|
Hflag = 1;
|
|
|
|
break;
|
2014-10-19 09:48:04 +00:00
|
|
|
case 'h':
|
2014-11-13 20:24:47 +00:00
|
|
|
hflag = 1;
|
2014-10-19 09:48:04 +00:00
|
|
|
break;
|
2013-09-27 14:32:50 +00:00
|
|
|
case 'i':
|
2014-11-13 20:24:47 +00:00
|
|
|
iflag = 1;
|
2013-09-27 14:32:50 +00:00
|
|
|
break;
|
2014-12-08 01:33:52 +00:00
|
|
|
case 'L':
|
|
|
|
Lflag = 1;
|
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
case 'l':
|
2014-11-13 20:24:47 +00:00
|
|
|
lflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
2013-10-04 10:12:11 +00:00
|
|
|
case 'r':
|
2014-11-13 20:24:47 +00:00
|
|
|
rflag = 1;
|
2013-10-04 10:12:11 +00:00
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
case 't':
|
2014-11-13 20:24:47 +00:00
|
|
|
tflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
2013-07-18 19:14:53 +00:00
|
|
|
case 'U':
|
2014-11-13 20:24:47 +00:00
|
|
|
Uflag = 1;
|
2013-07-18 19:14:53 +00:00
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
many = (argc > 1);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc == 0)
|
2013-07-18 19:14:53 +00:00
|
|
|
*--argv = ".", argc++;
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2014-11-16 10:07:26 +00:00
|
|
|
ents = emalloc(argc * sizeof(*ents));
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
2014-12-08 01:33:52 +00:00
|
|
|
mkent(&ents[i], argv[i], 1, Hflag || Lflag);
|
2013-07-18 19:14:53 +00:00
|
|
|
qsort(ents, argc, sizeof *ents, entcmp);
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
2013-10-06 18:52:18 +00:00
|
|
|
ls(&ents[rflag ? argc-i-1 : i]);
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2011-05-26 03:01:20 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static int
|
2013-07-18 19:14:53 +00:00
|
|
|
entcmp(const void *va, const void *vb)
|
2011-05-26 03:01:20 +00:00
|
|
|
{
|
2013-07-18 19:14:53 +00:00
|
|
|
const Entry *a = va, *b = vb;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (tflag)
|
2013-10-06 18:52:18 +00:00
|
|
|
return b->mtime - a->mtime;
|
2011-05-26 03:01:20 +00:00
|
|
|
else
|
2013-10-06 18:52:18 +00:00
|
|
|
return strcmp(a->name, b->name);
|
2011-05-26 03:01:20 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2013-07-18 19:14:53 +00:00
|
|
|
ls(Entry *ent)
|
2011-05-26 03:01:20 +00:00
|
|
|
{
|
2014-12-08 01:33:52 +00:00
|
|
|
if ((S_ISDIR(ent->mode) || (S_ISLNK(ent->mode) && S_ISDIR(ent->tmode) && !Fflag && !lflag)) && !dflag) {
|
2013-07-18 19:14:53 +00:00
|
|
|
lsdir(ent->name);
|
|
|
|
} else {
|
|
|
|
output(ent);
|
|
|
|
}
|
2011-05-26 03:01:20 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2011-05-26 03:01:20 +00:00
|
|
|
lsdir(const char *path)
|
|
|
|
{
|
2011-05-26 03:17:06 +00:00
|
|
|
char *cwd, *p;
|
2011-05-26 03:01:20 +00:00
|
|
|
long i, n = 0;
|
|
|
|
struct dirent *d;
|
|
|
|
DIR *dp;
|
2013-07-18 19:14:53 +00:00
|
|
|
Entry ent, *ents = NULL;
|
2013-08-15 11:34:38 +00:00
|
|
|
size_t sz;
|
2011-05-26 03:01:20 +00:00
|
|
|
|
2011-05-26 03:17:06 +00:00
|
|
|
cwd = agetcwd();
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!(dp = opendir(path)))
|
2011-05-26 03:01:20 +00:00
|
|
|
eprintf("opendir %s:", path);
|
2014-11-19 19:59:37 +00:00
|
|
|
if (chdir(path) < 0)
|
2011-05-26 03:01:20 +00:00
|
|
|
eprintf("chdir %s:", path);
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (many) {
|
|
|
|
if (!first)
|
2011-05-26 04:03:37 +00:00
|
|
|
putchar('\n');
|
2011-05-26 03:01:20 +00:00
|
|
|
printf("%s:\n", path);
|
2014-11-13 20:24:47 +00:00
|
|
|
first = 0;
|
2011-05-26 04:03:37 +00:00
|
|
|
}
|
2013-07-18 19:14:53 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((d = readdir(dp))) {
|
|
|
|
if (d->d_name[0] == '.' && !aflag)
|
2013-07-18 19:14:53 +00:00
|
|
|
continue;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (Uflag){
|
2014-12-08 01:33:52 +00:00
|
|
|
mkent(&ent, d->d_name, Fflag || lflag || iflag, Lflag);
|
2013-07-18 19:14:53 +00:00
|
|
|
output(&ent);
|
|
|
|
} else {
|
2014-11-16 10:07:26 +00:00
|
|
|
ents = erealloc(ents, ++n * sizeof *ents);
|
|
|
|
p = emalloc((sz = strlen(d->d_name)+1));
|
2013-08-15 11:34:38 +00:00
|
|
|
memcpy(p, d->d_name, sz);
|
2014-12-08 01:33:52 +00:00
|
|
|
mkent(&ents[n-1], p, tflag || Fflag || lflag || iflag, Lflag);
|
2013-07-18 19:14:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dp);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!Uflag){
|
2013-07-18 19:14:53 +00:00
|
|
|
qsort(ents, n, sizeof *ents, entcmp);
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2013-10-06 18:52:18 +00:00
|
|
|
output(&ents[rflag ? n-i-1 : i]);
|
|
|
|
free(ents[rflag ? n-i-1 : i].name);
|
2013-07-18 19:14:53 +00:00
|
|
|
}
|
2011-05-26 03:01:20 +00:00
|
|
|
}
|
2014-11-19 19:59:37 +00:00
|
|
|
if (chdir(cwd) < 0)
|
2011-05-26 03:17:06 +00:00
|
|
|
eprintf("chdir %s:", cwd);
|
2011-05-26 03:01:20 +00:00
|
|
|
free(ents);
|
2011-05-26 03:17:06 +00:00
|
|
|
free(cwd);
|
2011-05-26 03:01:20 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2014-12-08 01:33:52 +00:00
|
|
|
mkent(Entry *ent, char *path, int dostat, int follow)
|
2011-05-26 03:01:20 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2013-07-18 19:14:53 +00:00
|
|
|
ent->name = path;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!dostat)
|
2013-07-18 19:14:53 +00:00
|
|
|
return;
|
2014-12-08 01:33:52 +00:00
|
|
|
if ((follow ? stat : lstat)(path, &st) < 0)
|
|
|
|
eprintf("%s %s:", follow ? "stat" : "lstat", path);
|
2011-05-26 03:01:20 +00:00
|
|
|
ent->mode = st.st_mode;
|
|
|
|
ent->nlink = st.st_nlink;
|
|
|
|
ent->uid = st.st_uid;
|
|
|
|
ent->gid = st.st_gid;
|
|
|
|
ent->size = st.st_size;
|
|
|
|
ent->mtime = st.st_mtime;
|
2013-09-27 14:32:50 +00:00
|
|
|
ent->ino = st.st_ino;
|
2014-12-08 01:33:52 +00:00
|
|
|
if (S_ISLNK(ent->mode))
|
|
|
|
ent->tmode = stat(path, &st) == 0 ? st.st_mode : 0;
|
2011-05-26 03:01:20 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static char *
|
2014-02-17 14:41:09 +00:00
|
|
|
indicator(mode_t mode)
|
|
|
|
{
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!Fflag)
|
2014-02-17 14:41:09 +00:00
|
|
|
return "";
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (S_ISLNK(mode))
|
2014-02-17 14:41:09 +00:00
|
|
|
return "@";
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (S_ISDIR(mode))
|
2014-02-17 14:41:09 +00:00
|
|
|
return "/";
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (S_ISFIFO(mode))
|
2014-02-17 14:41:09 +00:00
|
|
|
return "|";
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (S_ISSOCK(mode))
|
2014-02-17 14:41:09 +00:00
|
|
|
return "=";
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (mode & S_IXUSR ||
|
|
|
|
mode & S_IXGRP ||
|
|
|
|
mode & S_IXOTH)
|
2014-02-17 14:41:09 +00:00
|
|
|
return "*";
|
|
|
|
else
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2011-05-26 03:01:20 +00:00
|
|
|
output(Entry *ent)
|
|
|
|
{
|
2011-05-27 22:56:43 +00:00
|
|
|
char buf[BUFSIZ], *fmt;
|
|
|
|
char mode[] = "----------";
|
2011-06-04 11:40:05 +00:00
|
|
|
ssize_t len;
|
2011-05-26 03:01:20 +00:00
|
|
|
struct group *gr;
|
|
|
|
struct passwd *pw;
|
2014-04-05 18:22:56 +00:00
|
|
|
char pwname[_SC_LOGIN_NAME_MAX];
|
|
|
|
char grname[_SC_LOGIN_NAME_MAX];
|
2011-05-26 03:01:20 +00:00
|
|
|
|
2013-09-27 14:32:50 +00:00
|
|
|
if (iflag)
|
|
|
|
printf("%lu ", (unsigned long)ent->ino);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!lflag) {
|
2014-02-17 14:41:09 +00:00
|
|
|
printf("%s%s\n", ent->name, indicator(ent->mode));
|
2011-05-26 03:01:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (S_ISREG(ent->mode))
|
2011-05-26 03:01:20 +00:00
|
|
|
mode[0] = '-';
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (S_ISBLK(ent->mode))
|
2011-05-26 03:01:20 +00:00
|
|
|
mode[0] = 'b';
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (S_ISCHR(ent->mode))
|
2011-05-26 03:01:20 +00:00
|
|
|
mode[0] = 'c';
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (S_ISDIR(ent->mode))
|
2011-05-26 03:01:20 +00:00
|
|
|
mode[0] = 'd';
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (S_ISFIFO(ent->mode))
|
2011-05-26 03:01:20 +00:00
|
|
|
mode[0] = 'p';
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (S_ISLNK(ent->mode))
|
2011-05-26 03:01:20 +00:00
|
|
|
mode[0] = 'l';
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (S_ISSOCK(ent->mode))
|
2011-05-26 03:01:20 +00:00
|
|
|
mode[0] = 's';
|
|
|
|
else
|
|
|
|
mode[0] = '?';
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (ent->mode & S_IRUSR) mode[1] = 'r';
|
|
|
|
if (ent->mode & S_IWUSR) mode[2] = 'w';
|
|
|
|
if (ent->mode & S_IXUSR) mode[3] = 'x';
|
|
|
|
if (ent->mode & S_IRGRP) mode[4] = 'r';
|
|
|
|
if (ent->mode & S_IWGRP) mode[5] = 'w';
|
|
|
|
if (ent->mode & S_IXGRP) mode[6] = 'x';
|
|
|
|
if (ent->mode & S_IROTH) mode[7] = 'r';
|
|
|
|
if (ent->mode & S_IWOTH) mode[8] = 'w';
|
|
|
|
if (ent->mode & S_IXOTH) mode[9] = 'x';
|
2011-05-27 22:56:43 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (ent->mode & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
|
|
|
|
if (ent->mode & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
|
|
|
|
if (ent->mode & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
|
2011-05-26 03:01:20 +00:00
|
|
|
|
|
|
|
pw = getpwuid(ent->uid);
|
2014-12-21 21:58:09 +00:00
|
|
|
if (pw)
|
2014-04-05 18:22:56 +00:00
|
|
|
snprintf(pwname, sizeof(pwname), "%s", pw->pw_name);
|
2014-12-21 21:58:09 +00:00
|
|
|
else
|
|
|
|
snprintf(pwname, sizeof(pwname), "%d", ent->uid);
|
2011-05-26 03:01:20 +00:00
|
|
|
|
|
|
|
gr = getgrgid(ent->gid);
|
2014-12-21 21:58:09 +00:00
|
|
|
if (gr)
|
2014-04-05 18:22:56 +00:00
|
|
|
snprintf(grname, sizeof(grname), "%s", gr->gr_name);
|
2014-12-21 21:58:09 +00:00
|
|
|
else
|
|
|
|
snprintf(grname, sizeof(grname), "%d", ent->gid);
|
2011-05-26 03:01:20 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (time(NULL) > ent->mtime + (180*24*60*60)) /* 6 months ago? */
|
2011-06-04 11:40:05 +00:00
|
|
|
fmt = "%b %d %Y";
|
2011-05-26 03:01:20 +00:00
|
|
|
else
|
|
|
|
fmt = "%b %d %H:%M";
|
|
|
|
|
|
|
|
strftime(buf, sizeof buf, fmt, localtime(&ent->mtime));
|
2014-10-19 09:48:04 +00:00
|
|
|
printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (hflag)
|
2014-10-19 09:48:04 +00:00
|
|
|
printf("%10s ", humansize((unsigned long)ent->size));
|
|
|
|
else
|
|
|
|
printf("%10lu ", (unsigned long)ent->size);
|
|
|
|
printf("%s %s%s", buf, ent->name, indicator(ent->mode));
|
2014-11-13 17:29:30 +00:00
|
|
|
if (S_ISLNK(ent->mode)) {
|
2014-12-08 02:19:34 +00:00
|
|
|
if ((len = readlink(ent->name, buf, sizeof buf - 1)) < 0)
|
2011-06-04 11:40:05 +00:00
|
|
|
eprintf("readlink %s:", ent->name);
|
|
|
|
buf[len] = '\0';
|
2014-12-08 01:33:52 +00:00
|
|
|
printf(" -> %s%s", buf, indicator(ent->tmode));
|
2011-06-04 11:40:05 +00:00
|
|
|
}
|
|
|
|
putchar('\n');
|
2011-05-26 03:01:20 +00:00
|
|
|
}
|