--- a/toys/other/stat.c	2016-04-25 09:29:31.000000000 +0530
+++ b/toys/other/stat.c	2016-04-25 11:08:45.000000000 +0530
@@ -1,35 +1,41 @@
 /* stat.c : display file or file system status
  * Copyright 2012 <warior.linux@gmail.com>
  * Copyright 2013 <anand.sinha85@gmail.com>
+ * Copyright 2016 Bilal Qureshi <bilal.jmi@gmail.com>
 
-USE_STAT(NEWTOY(stat, "<1c:f", TOYFLAG_BIN)) 
+USE_STAT(NEWTOY(stat, "<1c:ftL", TOYFLAG_BIN)) 
 
 config STAT
   bool stat
   default y
   help
-    usage: stat [-f] [-c FORMAT] FILE...
+    usage: stat [-t] [-f] [-c FORMAT] FILE...
 
     Display status of files or filesystems.
 
-    -f display filesystem status instead of file status
     -c Output specified FORMAT string instead of default
+    -f Display filesystem status instead of file status
+    -L Follow links
+    -t Display information in terse form 
+
 
     The valid format escape sequences for files:
-    %a  Access bits (octal) |%A  Access bits (flags)|%b  Blocks allocated
-    %B  Bytes per block     |%d  Device ID (dec)    |%D  Device ID (hex)
-    %f  All mode bits (hex) |%F  File type          |%g  Group ID
-    %G  Group name          |%h  Hard links         |%i  Inode
-    %n  Filename            |%N  Long filename      |%o  I/O block size
-    %s  Size (bytes)        |%u  User ID            |%U  User name
-    %x  Access time         |%X  Access unix time   |%y  File write time
-    %Y  File write unix time|%z  Dir change time    |%Z  Dir change unix time
+    %a  Access bits (octal)      |%A  Access bits (flags) |%b  Blocks allocated
+    %B  Bytes per block          |%d  Device ID (dec)     |%D  Device ID (hex)
+    %f  All mode bits (hex)      |%F  File type           |%g  Group ID
+    %G  Group name               |%h  Hard links          |%i  Inode
+    %m  Mount point              |%n  Filename            |%N  Long filename
+    %o  I/O block size           |%s  Size (bytes)        |%t  Major device type in hex 
+    %T  Minor device type in hex |%u  User ID             |%U  User name 
+    %x  Access time              |%X  Access unix time    |%y  File write time
+    %Y  File write unix time     |%z  Dir change time     |%Z  Dir change unix time
 
     The valid format escape sequences for filesystems:
     %a  Available blocks    |%b  Total blocks       |%c  Total inodes
     %d  Free inodes         |%f  Free blocks        |%i  File system ID
     %l  Max filename length |%n  File name          |%s  Fragment size
-    %S  Best transfer size  |%t  Filesystem type    |%T  Filesystem type name
+    %S  Best transfer size  |%t  File system type   |%T  Type in human readable form
+
 */
 
 #define FOR_stat
@@ -55,31 +61,29 @@
 {
   strftime(toybuf, sizeof(toybuf), "%Y-%m-%d %H:%M:%S",
     localtime(&(ts->tv_sec)));
-  xprintf("%s.%09ld", toybuf, ts->tv_nsec);
+  xprintf("%s.%09d", toybuf, ts->tv_nsec);
 }
 
-// Force numeric output to long long instead of manually typecasting everything
-static void out(char c, long long val)
+static int get_stat(char *path, struct stat *st)
 {
-  sprintf(toybuf, "%%ll%c", c);
-  printf(toybuf, val);
+  return (((toys.optflags&FLAG_L) ? stat : lstat)(path,st));
 }
 
 static void print_stat(char type)
 {
   struct stat *stat = (struct stat *)&TT.stat;
 
-  if (type == 'a') out('o', stat->st_mode&~S_IFMT);
+  if (type == 'a') xprintf("%lo", stat->st_mode & ~S_IFMT);
   else if (type == 'A') {
     char str[11];
 
     mode_to_string(stat->st_mode, str);
     xprintf("%s", str);
-  } else if (type == 'b') out('u', stat->st_blocks);
-  else if (type == 'B') out('u', stat->st_blksize);
-  else if (type == 'd') out('d', stat->st_dev);
-  else if (type == 'D') out('x', stat->st_dev);
-  else if (type == 'f') out('x', stat->st_mode);
+  } else if (type == 'b') xprintf("%llu", stat->st_blocks);
+  else if (type == 'B') xprintf("%lu", stat->st_blksize);
+  else if (type == 'd') xprintf("%ld", stat->st_dev);
+  else if (type == 'D') xprintf("%llx", stat->st_dev);
+  else if (type == 'f') xprintf("%lx", stat->st_mode);
   else if (type == 'F') {
     char *t = "character device\0directory\0block device\0" \
               "regular file\0symbolic link\0socket\0FIFO (named pipe)";
@@ -88,38 +92,60 @@
     for (i = 1; filetype != (i*8192) && i < 7; i++) t += strlen(t)+1;
     if (!stat->st_size && filetype == S_IFREG) t = "regular empty file";
     xprintf("%s", t);
-  } else if (type == 'g') out('u', stat->st_gid);
+  } else if (type == 'g') xprintf("%lu", stat->st_gid);
   else if (type == 'G') xprintf("%8s", TT.group_name->gr_name);
-  else if (type == 'h') out('u', stat->st_nlink);
-  else if (type == 'i') out('u', stat->st_ino);
+  else if (type == 'h') xprintf("%lu", stat->st_nlink);
+  else if (type == 'i') xprintf("%llu", stat->st_ino);
+  else if (type == 'm') { 
+    struct mtab_list *mt, *mtstart, *mtend;
+    struct stat st;
+
+    if (!(mtstart = xgetmountlist(0))) return;
+    mtend = dlist_terminate(mtstart);
+    
+    if (get_stat(*toys.optargs, &st)) 
+      perror_msg("can't stat '%s'", *toys.optargs);
+
+    for (mt = mtend; mt; mt = mt->prev) {
+      if (st.st_dev == mt->stat.st_dev
+          || (st.st_rdev && (st.st_rdev == mt->stat.st_dev))) {
+        xprintf("%s", mt->dir);
+        break;
+      }
+    }
+
+    if (CFG_TOYBOX_FREE) llist_traverse(mtstart, free);
+  }
   else if (type == 'N') {
-    xprintf("`%s'", *toys.optargs);
+    xprintf("%s", *toys.optargs);
     if (S_ISLNK(stat->st_mode))
       if (0<readlink(*toys.optargs, toybuf, sizeof(toybuf)))
-        xprintf(" -> `%s'", toybuf);
-  } else if (type == 'o') out('u', stat->st_blksize);
-  else if (type == 's') out('u', stat->st_size);
-  else if (type == 'u') out('u', stat->st_uid);
+        xprintf(" -> %s", toybuf);
+  } else if (type == 'o') xprintf("%lu", stat->st_blksize);
+  else if (type == 's') xprintf("%llu", stat->st_size);
+  else if (type == 't') xprintf("%x", major(stat->st_rdev));
+  else if (type == 'T') xprintf("%x", minor(stat->st_rdev));
+  else if (type == 'u') xprintf("%lu", stat->st_uid);
   else if (type == 'U') xprintf("%8s", TT.user_name->pw_name);
   else if (type == 'x') date_stat_format((void *)&stat->st_atime);
-  else if (type == 'X') out('u', stat->st_atime);
+  else if (type == 'X') xprintf("%llu", (long long)stat->st_atime);
   else if (type == 'y') date_stat_format((void *)&stat->st_mtime);
-  else if (type == 'Y') out('u', stat->st_mtime);
+  else if (type == 'Y') xprintf("%llu", (long long)stat->st_mtime);
   else if (type == 'z') date_stat_format((void *)&stat->st_ctime);
-  else if (type == 'Z') out('u', stat->st_ctime);
+  else if (type == 'Z') xprintf("%llu", (long long)stat->st_ctime);
   else xprintf("?");
 }
 
 static void print_statfs(char type) {
   struct statfs *statfs = (struct statfs *)&TT.stat;
 
-  if (type == 'a') out('u', statfs->f_bavail);
-  else if (type == 'b') out('u', statfs->f_blocks);
-  else if (type == 'c') out('u', statfs->f_files);
-  else if (type == 'd') out('u', statfs->f_ffree);
-  else if (type == 'f') out('u', statfs->f_bfree);
-  else if (type == 'l') out('d', statfs->f_namelen);
-  else if (type == 't') out('x', statfs->f_type);
+  if (type == 'a') xprintf("%llu", statfs->f_bavail);
+  else if (type == 'b') xprintf("%llu", statfs->f_blocks);
+  else if (type == 'c') xprintf("%llu", statfs->f_files);
+  else if (type == 'd') xprintf("%llu", statfs->f_ffree);
+  else if (type == 'f') xprintf("%llu", statfs->f_bfree);
+  else if (type == 'l') xprintf("%ld", statfs->f_namelen);
+  else if (type == 't') xprintf("%lx", statfs->f_type);
   else if (type == 'T') {
     char *s = "unknown";
     struct {unsigned num; char *name;} nn[] = {
@@ -136,26 +162,34 @@
 
     for (i=0; i<ARRAY_LEN(nn); i++)
       if (nn[i].num == statfs->f_type) s = nn[i].name;
-    fputs(s, stdout);
+    xprintf(s);
   } else if (type == 'i')
     xprintf("%08x%08x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]);
-  else if (type == 's') out('d', statfs->f_frsize);
-  else if (type == 'S') out('d', statfs->f_bsize);
+  else if (type == 's') xprintf("%d", statfs->f_frsize);
+  else if (type == 'S') xprintf("%d", statfs->f_bsize);
   else xprintf("?");
 }
 
 void stat_main(void)
 {
   int flagf = toys.optflags & FLAG_f;
-  char *format = flagf
-    ? "  File: \"%n\"\n    ID: %i Namelen: %l    Type: %t\n"
+  char *format;
+
+  if ((toys.optflags & FLAG_t) && flagf)
+    format = "%n %i %l %t %s %S %b %f %a %c %d";
+  else if (toys.optflags & FLAG_t)
+    format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
+  else {
+    format = flagf
+      ? "  File: \"%n\"\n    ID: %i Namelen: %l    Type: %T\n"
       "Block Size: %s    Fundamental block size: %S\n"
       "Blocks: Total: %b\tFree: %f\tAvailable: %a\n"
       "Inodes: Total: %c\tFree: %d"
-    : "  File: %N\n  Size: %s\t Blocks: %b\t IO Blocks: %B\t%F\n"
+      : "  File: %N\n  Size: %s\t Blocks: %b\t IO Blocks: %B\t%F\n"
       "Device: %Dh/%dd\t Inode: %i\t Links: %h\n"
       "Access: (%a/%A)\tUid: (%u/%U)\tGid: (%g/%G)\n"
       "Access: %x\nModify: %y\nChange: %z";
+  }
 
   if (toys.optflags & FLAG_c) format = TT.fmt;
 
@@ -163,7 +197,7 @@
     char *f;
 
     if (flagf && !statfs(*toys.optargs, (void *)&TT.stat));
-    else if (!flagf && !lstat(*toys.optargs, (void *)&TT.stat)) {
+    else if (!flagf && !get_stat(*toys.optargs, (void *)&TT.stat)) {
       struct stat *stat = (struct stat*)&TT.stat;
 
       // check user and group name
