Implement -i support for ls

This commit is contained in:
sin 2013-09-27 15:32:50 +01:00
parent 5d35656193
commit 8c87c20054
2 changed files with 15 additions and 4 deletions

5
ls.1
View File

@ -3,7 +3,7 @@
ls \- list directory contents
.SH SYNOPSIS
.B ls
.RB [ \-adltU ]
.RB [ \-adiltU ]
.RI [ file ...]
.SH DESCRIPTION
.B ls
@ -17,6 +17,9 @@ shows hidden files (those beginning with '.').
.B \-d
lists directories themselves, not their contents.
.TP
.B \-i
print the index number of each file.
.TP
.B \-l
lists detailed information about each file, including their type, permissions,
links, owner, group, size, and modification time.

14
ls.c
View File

@ -20,6 +20,7 @@ typedef struct {
gid_t gid;
off_t size;
time_t mtime;
ino_t ino;
} Entry;
static int entcmp(const void *, const void *);
@ -30,6 +31,7 @@ static void output(Entry *);
static bool aflag = false;
static bool dflag = false;
static bool iflag = false;
static bool lflag = false;
static bool tflag = false;
static bool Uflag = false;
@ -39,7 +41,7 @@ static bool many;
static void
usage(void)
{
eprintf("usage: %s [-adltU] [FILE...]\n", argv0);
eprintf("usage: %s [-adiltU] [FILE...]\n", argv0);
}
int
@ -55,6 +57,9 @@ main(int argc, char *argv[])
case 'd':
dflag = true;
break;
case 'i':
iflag = true;
break;
case 'l':
lflag = true;
break;
@ -131,7 +136,7 @@ lsdir(const char *path)
if(d->d_name[0] == '.' && !aflag)
continue;
if(Uflag){
mkent(&ent, d->d_name, lflag);
mkent(&ent, d->d_name, lflag || iflag);
output(&ent);
} else {
if(!(ents = realloc(ents, ++n * sizeof *ents)))
@ -139,7 +144,7 @@ lsdir(const char *path)
if(!(p = malloc((sz = strlen(d->d_name)+1))))
eprintf("malloc:");
memcpy(p, d->d_name, sz);
mkent(&ents[n-1], p, tflag || lflag);
mkent(&ents[n-1], p, tflag || lflag || iflag);
}
}
closedir(dp);
@ -172,6 +177,7 @@ mkent(Entry *ent, char *path, bool dostat)
ent->gid = st.st_gid;
ent->size = st.st_size;
ent->mtime = st.st_mtime;
ent->ino = st.st_ino;
}
void
@ -183,6 +189,8 @@ output(Entry *ent)
struct group *gr;
struct passwd *pw;
if (iflag)
printf("%lu ", (unsigned long)ent->ino);
if(!lflag) {
puts(ent->name);
return;