5
5
#include <dirent.h>
6
6
#include <errno.h>
7
7
8
+ #ifdef HAVE_SELINUX
9
+ #include <selinux/selinux.h>
10
+ #endif
11
+
8
12
#include <sys/stat.h>
9
13
#include <unistd.h>
10
14
#include <time.h>
25
29
#define LIST_SIZE (1 << 4)
26
30
#define LIST_LONG_NUMERIC (1 << 5)
27
31
#define LIST_CLASSIFY (1 << 6)
32
+ #define LIST_MACLABEL (1 << 7)
28
33
29
34
// fwd
30
35
static int listpath (const char * name , int flags );
@@ -234,9 +239,75 @@ static int listfile_long(const char *path, int flags)
234
239
return 0 ;
235
240
}
236
241
242
+ static int listfile_maclabel (const char * path , int flags )
243
+ {
244
+ struct stat s ;
245
+ char mode [16 ];
246
+ char user [16 ];
247
+ char group [16 ];
248
+ char * maclabel = NULL ;
249
+ const char * name ;
250
+
251
+ /* name is anything after the final '/', or the whole path if none*/
252
+ name = strrchr (path , '/' );
253
+ if (name == 0 ) {
254
+ name = path ;
255
+ } else {
256
+ name ++ ;
257
+ }
258
+
259
+ if (lstat (path , & s ) < 0 ) {
260
+ return -1 ;
261
+ }
262
+
263
+ #ifdef HAVE_SELINUX
264
+ lgetfilecon (path , & maclabel );
265
+ #else
266
+ maclabel = strdup ("-" );
267
+ #endif
268
+ if (!maclabel ) {
269
+ return -1 ;
270
+ }
271
+
272
+ mode2str (s .st_mode , mode );
273
+ user2str (s .st_uid , user );
274
+ group2str (s .st_gid , group );
275
+
276
+ switch (s .st_mode & S_IFMT ) {
277
+ case S_IFLNK : {
278
+ char linkto [256 ];
279
+ int len ;
280
+
281
+ len = readlink (path , linkto , sizeof (linkto ));
282
+ if (len < 0 ) return -1 ;
283
+
284
+ if (len > sizeof (linkto )- 1 ) {
285
+ linkto [sizeof (linkto )- 4 ] = '.' ;
286
+ linkto [sizeof (linkto )- 3 ] = '.' ;
287
+ linkto [sizeof (linkto )- 2 ] = '.' ;
288
+ linkto [sizeof (linkto )- 1 ] = 0 ;
289
+ } else {
290
+ linkto [len ] = 0 ;
291
+ }
292
+
293
+ printf ("%s %-8s %-8s %s %s -> %s\n" ,
294
+ mode , user , group , maclabel , name , linkto );
295
+ break ;
296
+ }
297
+ default :
298
+ printf ("%s %-8s %-8s %s %s\n" ,
299
+ mode , user , group , maclabel , name );
300
+
301
+ }
302
+
303
+ free (maclabel );
304
+
305
+ return 0 ;
306
+ }
307
+
237
308
static int listfile (const char * dirname , const char * filename , int flags )
238
309
{
239
- if ((flags & ( LIST_LONG | LIST_SIZE | LIST_CLASSIFY ) ) == 0 ) {
310
+ if ((flags & LIST_LONG | LIST_SIZE | LIST_CLASSIFY | LIST_MACLABEL ) == 0 ) {
240
311
printf ("%s\n" , filename );
241
312
return 0 ;
242
313
}
@@ -251,7 +322,9 @@ static int listfile(const char *dirname, const char *filename, int flags)
251
322
pathname = filename ;
252
323
}
253
324
254
- if ((flags & LIST_LONG ) != 0 ) {
325
+ if ((flags & LIST_MACLABEL ) != 0 ) {
326
+ return listfile_maclabel (pathname , flags );
327
+ } else if ((flags & LIST_LONG ) != 0 ) {
255
328
return listfile_long (pathname , flags );
256
329
} else /*((flags & LIST_SIZE) != 0)*/ {
257
330
return listfile_size (pathname , filename , flags );
@@ -386,6 +459,7 @@ int ls_main(int argc, char **argv)
386
459
case 's' : flags |= LIST_SIZE ; break ;
387
460
case 'R' : flags |= LIST_RECURSIVE ; break ;
388
461
case 'd' : flags |= LIST_DIRECTORIES ; break ;
462
+ case 'Z' : flags |= LIST_MACLABEL ; break ;
389
463
case 'a' : flags |= LIST_ALL ; break ;
390
464
case 'F' : flags |= LIST_CLASSIFY ; break ;
391
465
default :
0 commit comments