From 8c87c200540547202e25342765a8fd4250125469 Mon Sep 17 00:00:00 2001 From: sin Date: Fri, 27 Sep 2013 15:32:50 +0100 Subject: [PATCH] Implement -i support for ls --- ls.1 | 5 ++++- ls.c | 14 +++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ls.1 b/ls.1 index 00ae32b..eb0b015 100644 --- a/ls.1 +++ b/ls.1 @@ -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. diff --git a/ls.c b/ls.c index e3a440f..98b4c3c 100644 --- a/ls.c +++ b/ls.c @@ -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;