annotate toys/ls.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents 31215cc6c9f2
children 44abf4d901f3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * ls.c - list files
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
7 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
9 USE_LS(NEWTOY(ls, "AnRlF1a", TOYFLAG_BIN))
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config LS
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "ls"
502
763d581badae The aboriginal linux build needs ls -ditc and probably some more unimplemented options yet...
Rob Landley <rob@landley.net>
parents: 491
diff changeset
13 default n
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
15 usage: ls [-lFaA1] [directory...]
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 list files
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
17
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
18 -1 list one file per line
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 -a list all files
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
20 -A list all files except . and ..
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
21 -F append a character as a file type indicator
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
22 -l show full details for each file
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 */
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 #include "toys.h"
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
26
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 #define FLAG_a 1
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 #define FLAG_1 2
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 #define FLAG_F 4
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
30 #define FLAG_l 8
476
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
31 #define FLAG_R 16
491
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
32 #define FLAG_n 32
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
33 #define FLAG_A 64
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
34
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 static int dir_filter(const struct dirent *d)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 {
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
37 /* Skip over all '.*' entries, unless -a is given */
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
38 if (!(toys.optflags & FLAG_a)) {
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
39 /* -A means show everything except the . & .. entries */
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
40 if (toys.optflags & FLAG_A) {
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
41 if (strcmp(d->d_name, ".") == 0 ||
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
42 strcmp(d->d_name, "..") == 0)
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
43 return 0;
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
44 } else if (d->d_name[0] == '.')
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 return 0;
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
46 }
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 return 1;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
49
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 static void do_ls(int fd, char *name)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 {
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 struct dirent **entries;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 int nentries;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 int i;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 int maxwidth = -1;
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
56 int ncolumns = 1;
476
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
57 struct dirent file_dirent;
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
58 struct dirent *file_direntp;
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
59
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
60 if (!name || strcmp(name, "-") == 0)
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 name = ".";
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
62
476
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
63 if (toys.optflags & FLAG_R)
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
64 xprintf("\n%s:\n", name);
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
65
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
66 /* Get all the files in this directory */
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 nentries = scandir(name, &entries, dir_filter, alphasort);
476
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
68 if (nentries < 0) {
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
69 /* We've just selected a single file, so create a single-length list */
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
70 /* FIXME: This means that ls *.x results in a whole bunch of single
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
71 * listings, not one combined listing.
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
72 */
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
73 if (errno == ENOTDIR) {
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
74 nentries = 1;
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
75 strcpy(file_dirent.d_name, name);
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
76 file_direntp = &file_dirent;
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
77 entries = &file_direntp;
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
78 } else
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
79 perror_exit("ls: cannot access %s'", name);
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
80 }
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
81
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
82
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 /* Determine the widest entry so we can flow them properly */
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 if (!(toys.optflags & FLAG_1)) {
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 int columns;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
86 char *columns_str;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
87
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 for (i = 0; i < nentries; i++) {
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 struct dirent *ent = entries[i];
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 int width;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
91
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 width = strlen(ent->d_name);
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 if (width > maxwidth)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 maxwidth = width;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 /* We always want at least a single space for each entry */
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 maxwidth++;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 if (toys.optflags & FLAG_F)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 maxwidth++;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
100
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 columns_str = getenv("COLUMNS");
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
102 columns = columns_str ? atoi(columns_str) : 80;
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
103 ncolumns = maxwidth ? columns / maxwidth : 1;
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
105
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 for (i = 0; i < nentries; i++) {
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 struct dirent *ent = entries[i];
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 int len = strlen(ent->d_name);
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
109 struct stat st;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
110 int stat_valid = 0;
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
111
476
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
112 sprintf(toybuf, "%s/%s", name, ent->d_name);
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
113
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
114 /* Provide the ls -l long output */
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
115 if (toys.optflags & FLAG_l) {
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
116 char type;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
117 char timestamp[64];
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
118 struct tm mtime;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
119
476
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
120 if (lstat(toybuf, &st))
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
121 perror_exit("Can't stat %s", toybuf);
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
122 stat_valid = 1;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
123 if (S_ISDIR(st.st_mode))
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
124 type = 'd';
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
125 else if (S_ISCHR(st.st_mode))
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
126 type = 'c';
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
127 else if (S_ISBLK(st.st_mode))
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
128 type = 'b';
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
129 else if (S_ISLNK(st.st_mode))
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
130 type = 'l';
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
131 else
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
132 type = '-';
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
133
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
134 xprintf("%c%c%c%c%c%c%c%c%c%c ", type,
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
135 (st.st_mode & S_IRUSR) ? 'r' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
136 (st.st_mode & S_IWUSR) ? 'w' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
137 (st.st_mode & S_IXUSR) ? 'x' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
138 (st.st_mode & S_IRGRP) ? 'r' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
139 (st.st_mode & S_IWGRP) ? 'w' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
140 (st.st_mode & S_IXGRP) ? 'x' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
141 (st.st_mode & S_IROTH) ? 'r' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
142 (st.st_mode & S_IWOTH) ? 'w' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
143 (st.st_mode & S_IXOTH) ? 'x' : '-');
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
144
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
145 xprintf("%2d ", st.st_nlink);
491
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
146 if (toys.optflags & FLAG_n) {
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
147 xprintf("%4d ", st.st_uid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
148 xprintf("%4d ", st.st_gid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
149 } else {
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
150 struct passwd *pwd = getpwuid(st.st_uid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
151 struct group *grp = getgrgid(st.st_gid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
152 if (!pwd)
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
153 xprintf("%4d ", st.st_uid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
154 else
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
155 xprintf("%-10s ", pwd->pw_name);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
156 if (!grp)
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
157 xprintf("%4d ", st.st_gid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
158 else
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
159 xprintf("%-10s ", grp->gr_name);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
160 }
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
161 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
162 xprintf("%3d, %3d ", major(st.st_rdev), minor(st.st_rdev));
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
163 else
491
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
164 xprintf("%12lld ", st.st_size);
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
165
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
166 localtime_r(&st.st_mtime, &mtime);
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
167
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
168 strftime(timestamp, sizeof(timestamp), "%b %e %H:%M", &mtime);
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
169 xprintf("%s ", timestamp);
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
170 }
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
171
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
172 xprintf("%s", ent->d_name);
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
173
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
174 /* Append the file-type indicator character */
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 if (toys.optflags & FLAG_F) {
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
176 if (!stat_valid) {
476
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
177 if (lstat(toybuf, &st))
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
178 perror_exit("Can't stat %s", toybuf);
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
179 stat_valid = 1;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
180 }
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
181 if (S_ISDIR(st.st_mode)) {
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
182 xprintf("/");
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
183 len++;
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
184 } else if (S_ISREG(st.st_mode) &&
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
186 xprintf("*");
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
187 len++;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
188 } else if (S_ISLNK(st.st_mode)) {
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
189 xprintf("@");
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
190 len++;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
192 }
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
193 if (toys.optflags & FLAG_1) {
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
194 xprintf("\n");
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
195 } else {
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
196 if (i % ncolumns == ncolumns - 1)
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
197 xprintf("\n");
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 else
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
199 xprintf("%*s", maxwidth - len, "");
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
200 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
201 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
202 /* Make sure we put at a trailing new line in */
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
203 if (!(toys.optflags & FLAG_1) && (i % ncolumns))
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
204 xprintf("\n");
476
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
205
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
206 if (toys.optflags & FLAG_R) {
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
207 for (i = 0; i < nentries; i++) {
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
208 struct dirent *ent = entries[i];
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
209 struct stat st;
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
210 char dirname[PATH_MAX];
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
211
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
212 sprintf(dirname, "%s/%s", name, ent->d_name);
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
213 if (lstat(dirname, &st))
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
214 perror_exit("Can't stat %s", dirname);
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
215 if (S_ISDIR(st.st_mode))
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
216 do_ls(0, dirname);
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
217 }
d10b58563cff More ls updates from Andre Renaud: Add -R and initial support for listing files on the command line.
Rob Landley <rob@landley.net>
parents: 459
diff changeset
218 }
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
220
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
221 void ls_main(void)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
222 {
491
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
223 /* If the output is not a TTY, then just do one-file per line
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
224 * This makes ls easier to use with other command line tools (grep/awk etc...)
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
225 */
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
226 if (!isatty(fileno(stdout)))
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
227 toys.optflags |= FLAG_1;
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
228 /* Long output must be one-file per line */
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
229 if (toys.optflags & FLAG_l)
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
230 toys.optflags |= FLAG_1;
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
231 loopfiles(toys.optargs, do_ls);
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
232 }