aboutsummaryrefslogtreecommitdiffstats
path: root/misc/lsattr.c
blob: 6ce190c4328f2cd784cae00978116e8ed9a53d90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * lsattr.c		- List file attributes on an ext2 file system
 *
 * Copyright (C) 1993, 1994  Remy Card <card@masi.ibp.fr>
 *                           Laboratoire MASI, Institut Blaise Pascal
 *                           Universite Pierre et Marie Curie (Paris VI)
 *
 * This file can be redistributed under the terms of the GNU General
 * Public License
 */

/*
 * History:
 * 93/10/30	- Creation
 * 93/11/13	- Replace stat() calls by lstat() to avoid loops
 * 94/02/27	- Integrated in Ted's distribution
 */

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <linux/ext2_fs.h>

#include "et/com_err.h"
#include "e2p/e2p.h"

#include "../version.h"

const char * program_name = "lsattr";

int all = 0;
int d_opt = 0;
int l_opt = 0;
int recursive = 0;
int v_opt = 0;

static void volatile usage (void)
{
	fprintf (stderr, "Usage: %s [-Radlv] [files...]\n", program_name);
	exit (1);
}

static void list_attributes (const char * name)
{
	unsigned long flags;
	unsigned long version;

	if (fgetflags (name, &flags) == -1)
		com_err (program_name, errno, "While reading flags on %s",
			 name);
	else if (fgetversion (name, &version) == -1)
		com_err (program_name, errno, "While reading version on %s",
			 name);
	else
	{
		if (v_opt)
			printf ("%5lu ", version);
		print_flags (stdout, flags, l_opt);
		printf (" %s\n", name);
	}
}

static int lsattr_dir_proc (const char *, struct dirent *, void *);

static void lsattr_args (const char * name)
{
	struct stat st;

	if (lstat (name, &st) == -1)
		com_err (program_name, errno, "while stating %s", name);
	else
	{
		if (S_ISDIR(st.st_mode) && !d_opt)
			iterate_on_dir (name, lsattr_dir_proc, (void *) NULL);
		else
			list_attributes (name);
	}
}

static int lsattr_dir_proc (const char * dir_name, struct dirent * de, void * private)
{
	char path [MAXPATHLEN];
	struct stat st;

	sprintf (path, "%s/%s", dir_name, de->d_name);
	if (lstat (path, &st) == -1)
		perror (path);
	else
	{
		if (de->d_name[0] != '.' || all)
		{
			list_attributes (path);
			if (S_ISDIR(st.st_mode) && recursive &&
			    strcmp (de->d_name, ".") && strcmp (de->d_name, ".."))
			{
				printf ("\n%s:\n", path);
				iterate_on_dir (path, lsattr_dir_proc, (void *) NULL);
				printf ("\n");
			}
		}
	}
	return 0;
}

void main (int argc, char ** argv)
{
	char c;
	int i;

	fprintf (stderr, "lsattr %s, %s for EXT2 FS %s, %s\n",
		 E2FSPROGS_VERSION, E2FSPROGS_DATE,
		 EXT2FS_VERSION, EXT2FS_DATE);
	if (argc && *argv)
		program_name = *argv;
	while ((c = getopt (argc, argv, "Radlv")) != EOF)
		switch (c)
		{
			case 'R':
				recursive = 1;
				break;
			case 'a':
				all = 1;
				break;
			case 'd':
				d_opt = 1;
				break;
			case 'l':
				l_opt = 1;
				break;
			case 'v':
				v_opt = 1;
				break;
			default:
				usage ();
		}

	if (optind > argc - 1)
		lsattr_args (".");
	else
		for (i = optind; i < argc; i++)
			lsattr_args (argv[i]);
}