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 #ifndef _POSIX_VERSION
193 struct passwd
*getpwuid ();
194 struct group
*getgrgid ();
197 #ifdef major /* Might be defined in sys/types.h. */
201 #define major(dev) (((dev) >> 8) & 0xff)
202 #define minor(dev) ((dev) & 0xff)
207 static void print_name (register char *p
, FILE *stream
, int literal_control_chars
);
209 extern char * getgroup (gid_t gid
);
210 extern char * getuser (uid_t uid
);
215 file_blocksize(const struct stat
*p
)
217 return ST_NBLOCKSIZE
;
222 /* NAME is the name to print.
223 RELNAME is the path to access it from the current directory.
224 STATP is the results of stat or lstat on it.
225 Use CURRENT_TIME to decide whether to print yyyy or hh:mm.
226 Use OUTPUT_BLOCK_SIZE to determine how to print file block counts
228 STREAM is the stdio stream to print on. */
231 list_file (char *name
,
234 const struct stat
*statp
,
236 int output_block_size
,
237 int literal_control_chars
,
241 struct tm
const *when_local
;
242 char const *user_name
;
243 char const *group_name
;
244 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
247 /* Cray DMF: look at the file's migrated, not real, status */
248 strmode (statp
->st_dm_mode
, modebuf
);
250 strmode (statp
->st_mode
, modebuf
);
254 fprintf (stream
, "%6s ",
255 human_readable ((uintmax_t) statp
->st_ino
, hbuf
,
259 fprintf (stream
, "%4s ",
260 human_readable ((uintmax_t) ST_NBLOCKS (*statp
), hbuf
,
262 ST_NBLOCKSIZE
, output_block_size
));
265 /* The space between the mode and the number of links is the POSIX
266 "optional alternate access method flag". */
267 fprintf (stream
, "%s %3lu ", modebuf
, (unsigned long) statp
->st_nlink
);
269 user_name
= getuser (statp
->st_uid
);
271 fprintf (stream
, "%-8s ", user_name
);
273 fprintf (stream
, "%-8lu ", (unsigned long) statp
->st_uid
);
275 group_name
= getgroup (statp
->st_gid
);
277 fprintf (stream
, "%-8s ", group_name
);
279 fprintf (stream
, "%-8lu ", (unsigned long) statp
->st_gid
);
281 if (S_ISCHR (statp
->st_mode
) || S_ISBLK (statp
->st_mode
))
283 fprintf (stream
, "%3lu, %3lu ",
284 (unsigned long) major (statp
->st_rdev
),
285 (unsigned long) minor (statp
->st_rdev
));
287 fprintf (stream
, " ");
290 fprintf (stream
, "%8s ",
291 human_readable ((uintmax_t) statp
->st_size
, hbuf
,
294 output_block_size
< 0 ? output_block_size
: 1));
296 if ((when_local
= localtime (&statp
->st_mtime
)))
298 char init_bigbuf
[256];
299 char *buf
= init_bigbuf
;
300 size_t bufsize
= sizeof init_bigbuf
;
302 /* Use strftime rather than ctime, because the former can produce
303 locale-dependent names for the month (%b).
305 Output the year if the file is fairly old or in the future.
306 POSIX says the cutoff is 6 months old;
307 approximate this by 6*30 days.
308 Allow a 1 hour slop factor for what is considered "the future",
309 to allow for NFS server/client clock disagreement. */
311 ((current_time
- 6 * 30 * 24 * 60 * 60 <= statp
->st_mtime
312 && statp
->st_mtime
<= current_time
+ 60 * 60)
316 while (!strftime (buf
, bufsize
, fmt
, when_local
))
317 buf
= (char *) alloca (bufsize
*= 2);
319 fprintf (stream
, "%s ", buf
);
323 /* The time cannot be represented as a local time;
324 print it as a huge integer number of seconds. */
327 if (statp
->st_mtime
< 0)
329 char const *num
= human_readable (- (uintmax_t) statp
->st_mtime
,
330 hbuf
, human_ceiling
, 1, 1);
331 int sign_width
= width
- strlen (num
);
332 fprintf (stream
, "%*s%s ",
333 sign_width
< 0 ? 0 : sign_width
, "-", num
);
336 fprintf (stream
, "%*s ", width
,
337 human_readable ((uintmax_t) statp
->st_mtime
, hbuf
,
342 print_name (name
, stream
, literal_control_chars
);
345 if (S_ISLNK (statp
->st_mode
))
347 char *linkname
= get_link_name_at (name
, dirfd
, relname
);
351 fputs (" -> ", stream
);
352 print_name (linkname
, stream
, literal_control_chars
);
362 print_name_without_quoting (char *p
, FILE *stream
)
364 fprintf(stream
, "%s", p
);
369 print_name_with_quoting (register char *p
, FILE *stream
)
371 register unsigned char c
;
373 while ((c
= *p
++) != '\0')
378 fprintf (stream
, "\\\\");
382 fprintf (stream
, "\\n");
386 fprintf (stream
, "\\b");
390 fprintf (stream
, "\\r");
394 fprintf (stream
, "\\t");
398 fprintf (stream
, "\\f");
402 fprintf (stream
, "\\ ");
406 fprintf (stream
, "\\\"");
410 if (c
> 040 && c
< 0177)
413 fprintf (stream
, "\\%03o", (unsigned int) c
);
418 static void print_name (register char *p
, FILE *stream
, int literal_control_chars
)
420 if (literal_control_chars
)
421 print_name_without_quoting(p
, stream
);
423 print_name_with_quoting(p
, stream
);
428 get_link_name (const char *name
, char *relname
)
430 register char *linkname
;
431 register int linklen
;
433 /* st_size is wrong for symlinks on AIX, and on
434 mount points with some automounters.
435 So allocate a pessimistic PATH_MAX + 1 bytes. */
436 #define LINK_BUF PATH_MAX
437 linkname
= (char *) xmalloc (LINK_BUF
+ 1);
438 linklen
= readlink (relname
, linkname
, LINK_BUF
);
441 error (0, errno
, "%s", name
);
445 linkname
[linklen
] = '\0';
449 struct link_name_args
457 get_link_name_cb(void *context
)
459 struct link_name_args
*args
= context
;
460 args
->result
= get_link_name(args
->name
, args
->relname
);
465 get_link_name_at (const char *name
, int dirfd
, char *relname
)
467 struct link_name_args args
;
470 args
.relname
= relname
;
471 if (0 == run_in_dir(dirfd
, get_link_name_cb
, &args
))