- DIR *d;
- struct dirent *de;
-
- if (!(d = opendir (dn)))
- failed ("opendir", dn);
-
- while ((de = readdir (d))) {
- char *dname = d_name (de);
- if (!strcmp (dname, ".") || !strcmp (dname, ".."))
- continue;
- printf ("%s\n", dname);
- }
-
- if (closedir (d) == -1)
- failed ("closedir", dn);
+ struct stat buf;
+ char permbuf[PERMSTRING_SIZE];
+ struct tm *mt;
+
+ if (do_lstat(fname, &buf) == -1)
+ failed ("stat", fname);
+
+ /* On some BSD platforms the mode bits of a symlink are
+ * undefined. */
+ if (S_ISLNK(buf.st_mode))
+ buf.st_mode &= ~0777;
+
+ /* The size of anything but a regular file is probably not
+ * worth thinking about. */
+ if (!S_ISREG(buf.st_mode))
+ buf.st_size = 0;
+
+ permstring(permbuf, buf.st_mode);
+
+ mt = gmtime(&buf.st_mtime);
+
+ /* TODO: Perhaps escape special characters in fname? */
+
+ /* NB: need to pass size as a double because it might be be
+ * too large for a long. */
+ printf("%s %12.0f %6d.%-6d %04d-%02d-%02d %02d:%02d:%02d %s\n",
+ permbuf, (double) buf.st_size,
+ buf.st_uid, buf.st_gid,
+ mt->tm_year + 1900,
+ mt->tm_mon + 1,
+ mt->tm_mday,
+ mt->tm_hour,
+ mt->tm_min,
+ mt->tm_sec,
+ fname);