-
Notifications
You must be signed in to change notification settings - Fork 0
/
emptydir.c
124 lines (101 loc) · 2.77 KB
/
emptydir.c
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
/**************************************************************************
emptydir - check if directories are empty or not.
Copyright (C) 2010 Victor Zamanian
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
/*
* Returns 1 if the directory at `path' is empty.
* Returns 0 if the directory at `path' is not empty.
*/
int emptydir(DIR* dp);
/*
* Wrapper for fprintf that also prints out the error message
* corresponding to the current value of `errno'. The error function
* prints to stderr.
*/
int error(const char* format, ...);
/*
* Prints usage information.
*/
void usage(const char* prog_name);
int main(int argc, char** argv)
{
DIR* dp;
int arg;
char* prog_name;
prog_name = argv[0];
if (argc < 2) {
usage(prog_name);
return 1;
}
for (arg = 1; arg < argc; arg++) {
dp = opendir(argv[arg]);
if (dp == NULL)
error("%s: Error when opening `%s'", prog_name, argv[arg]);
else
{
int result = emptydir(dp);
/* No need to close the directory unless dp wasn't NULL. */
closedir(dp);
if (result)
printf("`%s' is empty.\n", argv[arg]);
else
printf("`%s' is not empty.\n", argv[arg]);
}
}
return 0;
}
int emptydir(DIR* dp)
{
struct dirent* entry;
size_t files = 0;
do
{
/* Read next directory entry. */
entry = readdir(dp);
/* If there was an entry and it wasn't "." nor "..". */
if (entry != NULL && strcmp(entry->d_name, ".") &&
strcmp(entry->d_name, ".."))
files++;
}
while (entry != NULL && files == 0);
return !files;
}
int error(const char* format, ...)
{
va_list arg;
int printed;
char* errmsg;
/* Copy error message first. */
{
char* tmp;
tmp = strerror(errno);
errmsg = malloc((strlen(tmp) + 1)*sizeof(char));
strcpy(errmsg, tmp);
}
va_start(arg, format);
printed = vfprintf(stderr, format, arg);
va_end(arg);
printed += fprintf(stderr, ": %s\n", errmsg);
free(errmsg);
return printed;
}
void usage(const char* prog_name)
{
fprintf(stderr, "Usage: %s DIRECTORY...\n", prog_name);
}