1 /* listfile.c -- display a long listing of a file
2 Copyright (C) 1991, 1993, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
43 #include <sys/types.h>
60 #if HAVE_STRING_H || STDC_HEADERS
67 /* The presence of unistd.h is assumed by gnulib these days, so we
68 * might as well assume it too.
70 #include <unistd.h> /* for readlink() */
80 /* Since major is a function on SVR4, we can't use `ifndef major'. */
82 #include <sys/mkdev.h>
85 #ifdef MAJOR_IN_SYSMACROS
86 #include <sys/sysmacros.h>
100 # define _(Text) gettext (Text)
102 # define _(Text) Text
103 #define textdomain(Domain)
104 #define bindtextdomain(Package, Directory)
107 # define N_(String) gettext_noop (String)
109 /* See locate.c for explanation as to why not use (String) */
110 # define N_(String) String
115 #ifdef STAT_MACROS_BROKEN
122 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
125 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
127 #if defined(S_IFLNK) && !defined(S_ISLNK)
128 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
131 /* Get or fake the disk device blocksize.
132 Usually defined by sys/param.h (if at all). */
135 # define DEV_BSIZE BSIZE
137 # define DEV_BSIZE 4096
139 #endif /* !DEV_BSIZE */
141 /* Extract or fake data from a `struct stat'.
142 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
143 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
144 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
145 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
146 # define ST_BLKSIZE(statbuf) DEV_BSIZE
147 # if defined(_POSIX_SOURCE) || !defined(BSIZE) /* fileblocks.c uses BSIZE. */
148 # define ST_NBLOCKS(statbuf) \
149 (S_ISREG ((statbuf).st_mode) \
150 || S_ISDIR ((statbuf).st_mode) \
151 ? (statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0) : 0)
152 # else /* !_POSIX_SOURCE && BSIZE */
153 # define ST_NBLOCKS(statbuf) \
154 (S_ISREG ((statbuf).st_mode) \
155 || S_ISDIR ((statbuf).st_mode) \
156 ? st_blocks ((statbuf).st_size) : 0)
157 # endif /* !_POSIX_SOURCE && BSIZE */
158 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
159 /* Some systems, like Sequents, return st_blksize of 0 on pipes. */
160 # define ST_BLKSIZE(statbuf) ((statbuf).st_blksize > 0 \
161 ? (statbuf).st_blksize : DEV_BSIZE)
162 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
163 /* HP-UX counts st_blocks in 1024-byte units.
164 This loses when mixing HP-UX and BSD filesystems with NFS. */
165 # define ST_NBLOCKSIZE 1024
167 # if defined(_AIX) && defined(_I386)
168 /* AIX PS/2 counts st_blocks in 4K units. */
169 # define ST_NBLOCKSIZE (4 * 1024)
170 # else /* not AIX PS/2 */
172 # define ST_NBLOCKS(statbuf) \
173 (S_ISREG ((statbuf).st_mode) \
174 || S_ISDIR ((statbuf).st_mode) \
175 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
177 # endif /* not AIX PS/2 */
179 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
182 # define ST_NBLOCKS(statbuf) \
183 (S_ISREG ((statbuf).st_mode) \
184 || S_ISDIR ((statbuf).st_mode) \
185 ? (statbuf).st_blocks : 0)
188 #ifndef ST_NBLOCKSIZE
189 # define ST_NBLOCKSIZE 512
192 #ifdef major /* Might be defined in sys/types.h. */
196 #define major(dev) (((dev) >> 8) & 0xff)
197 #define minor(dev) ((dev) & 0xff)
202 static void print_name (register char *p
, FILE *stream
, int literal_control_chars
);
204 extern char * getgroup (gid_t gid
);
205 extern char * getuser (uid_t uid
);
210 file_blocksize(const struct stat
*p
)
212 return ST_NBLOCKSIZE
;
217 /* NAME is the name to print.
218 RELNAME is the path to access it from the current directory.
219 STATP is the results of stat or lstat on it.
220 Use CURRENT_TIME to decide whether to print yyyy or hh:mm.
221 Use OUTPUT_BLOCK_SIZE to determine how to print file block counts
223 STREAM is the stdio stream to print on. */
226 list_file (char *name
,
229 const struct stat
*statp
,
231 int output_block_size
,
232 int literal_control_chars
,
236 struct tm
const *when_local
;
237 char const *user_name
;
238 char const *group_name
;
239 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
242 /* Cray DMF: look at the file's migrated, not real, status */
243 strmode (statp
->st_dm_mode
, modebuf
);
245 strmode (statp
->st_mode
, modebuf
);
249 fprintf (stream
, "%6s ",
250 human_readable ((uintmax_t) statp
->st_ino
, hbuf
,
254 fprintf (stream
, "%4s ",
255 human_readable ((uintmax_t) ST_NBLOCKS (*statp
), hbuf
,
257 ST_NBLOCKSIZE
, output_block_size
));
260 /* The space between the mode and the number of links is the POSIX
261 "optional alternate access method flag". */
262 fprintf (stream
, "%s %3lu ", modebuf
, (unsigned long) statp
->st_nlink
);
264 user_name
= getuser (statp
->st_uid
);
266 fprintf (stream
, "%-8s ", user_name
);
268 fprintf (stream
, "%-8lu ", (unsigned long) statp
->st_uid
);
270 group_name
= getgroup (statp
->st_gid
);
272 fprintf (stream
, "%-8s ", group_name
);
274 fprintf (stream
, "%-8lu ", (unsigned long) statp
->st_gid
);
276 if (S_ISCHR (statp
->st_mode
) || S_ISBLK (statp
->st_mode
))
278 fprintf (stream
, "%3lu, %3lu ",
279 (unsigned long) major (statp
->st_rdev
),
280 (unsigned long) minor (statp
->st_rdev
));
282 fprintf (stream
, " ");
285 fprintf (stream
, "%8s ",
286 human_readable ((uintmax_t) statp
->st_size
, hbuf
,
289 output_block_size
< 0 ? output_block_size
: 1));
291 if ((when_local
= localtime (&statp
->st_mtime
)))
293 char init_bigbuf
[256];
294 char *buf
= init_bigbuf
;
295 size_t bufsize
= sizeof init_bigbuf
;
297 /* Use strftime rather than ctime, because the former can produce
298 locale-dependent names for the month (%b).
300 Output the year if the file is fairly old or in the future.
301 POSIX says the cutoff is 6 months old;
302 approximate this by 6*30 days.
303 Allow a 1 hour slop factor for what is considered "the future",
304 to allow for NFS server/client clock disagreement. */
306 ((current_time
- 6 * 30 * 24 * 60 * 60 <= statp
->st_mtime
307 && statp
->st_mtime
<= current_time
+ 60 * 60)
311 while (!strftime (buf
, bufsize
, fmt
, when_local
))
312 buf
= (char *) alloca (bufsize
*= 2);
314 fprintf (stream
, "%s ", buf
);
318 /* The time cannot be represented as a local time;
319 print it as a huge integer number of seconds. */
322 if (statp
->st_mtime
< 0)
324 char const *num
= human_readable (- (uintmax_t) statp
->st_mtime
,
325 hbuf
, human_ceiling
, 1, 1);
326 int sign_width
= width
- strlen (num
);
327 fprintf (stream
, "%*s%s ",
328 sign_width
< 0 ? 0 : sign_width
, "-", num
);
331 fprintf (stream
, "%*s ", width
,
332 human_readable ((uintmax_t) statp
->st_mtime
, hbuf
,
337 print_name (name
, stream
, literal_control_chars
);
340 if (S_ISLNK (statp
->st_mode
))
342 char *linkname
= get_link_name_at (name
, dirfd
, relname
);
346 fputs (" -> ", stream
);
347 print_name (linkname
, stream
, literal_control_chars
);
357 print_name_without_quoting (char *p
, FILE *stream
)
359 fprintf(stream
, "%s", p
);
364 print_name_with_quoting (register char *p
, FILE *stream
)
366 register unsigned char c
;
368 while ((c
= *p
++) != '\0')
373 fprintf (stream
, "\\\\");
377 fprintf (stream
, "\\n");
381 fprintf (stream
, "\\b");
385 fprintf (stream
, "\\r");
389 fprintf (stream
, "\\t");
393 fprintf (stream
, "\\f");
397 fprintf (stream
, "\\ ");
401 fprintf (stream
, "\\\"");
405 if (c
> 040 && c
< 0177)
408 fprintf (stream
, "\\%03o", (unsigned int) c
);
413 static void print_name (register char *p
, FILE *stream
, int literal_control_chars
)
415 if (literal_control_chars
)
416 print_name_without_quoting(p
, stream
);
418 print_name_with_quoting(p
, stream
);
423 get_link_name (const char *name
, char *relname
)
425 register char *linkname
;
426 register int linklen
;
428 /* st_size is wrong for symlinks on AIX, and on
429 mount points with some automounters.
430 So allocate a pessimistic PATH_MAX + 1 bytes. */
431 #define LINK_BUF PATH_MAX
432 linkname
= (char *) xmalloc (LINK_BUF
+ 1);
433 linklen
= readlink (relname
, linkname
, LINK_BUF
);
436 error (0, errno
, "%s", name
);
440 linkname
[linklen
] = '\0';
444 struct link_name_args
452 get_link_name_cb(void *context
)
454 struct link_name_args
*args
= context
;
455 args
->result
= get_link_name(args
->name
, args
->relname
);
460 get_link_name_at (const char *name
, int dirfd
, char *relname
)
462 struct link_name_args args
;
465 args
.relname
= relname
;
466 if (0 == run_in_dir(dirfd
, get_link_name_cb
, &args
))