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

14
ls.c
View File

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