Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
Linux Homeworks
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Rafael László
Linux Homeworks
Commits
ae925488
Verified
Commit
ae925488
authored
3 years ago
by
Rafael László
Browse files
Options
Downloads
Patches
Plain Diff
Task2
parent
ad5eaf24
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.c
+45
-48
45 additions, 48 deletions
main.c
with
45 additions
and
48 deletions
main.c
+
45
−
48
View file @
ae925488
...
...
@@ -3,63 +3,60 @@
#include
<dirent.h>
#include
<string.h>
#include
<malloc.h>
#include
<poll.h>
#include
<fcntl.h>
#include
<sys/types.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
int
count_files_in_dir
(
char
*
path
)
{
int
file_count
=
0
;
DIR
*
dir
;
struct
dirent
*
entry
;
dir
=
opendir
(
path
);
while
((
entry
=
readdir
(
dir
))
!=
NULL
)
{
if
(
strcmp
(
entry
->
d_name
,
"."
)
==
0
||
strcmp
(
entry
->
d_name
,
".."
)
==
0
)
{
continue
;
}
file_count
++
;
}
closedir
(
dir
);
return
file_count
;
int
main
(
int
argc
,
char
*
argv
[])
{
struct
pollfd
pfds
[
2
]
=
{{.
fd
=
open
(
"/tmp/opened"
,
O_RDONLY
),
.
events
=
POLLIN
},{.
fd
=
0
,
.
events
=
POLLIN
}};
int
pipeout
=
open
(
"/tmp/write"
,
O_WRONLY
);
while
(
1
)
{
char
buf
[
10
];
int
ready
;
ready
=
poll
(
pfds
,
2
,
-
1
);
if
(
ready
==
-
1
)
{
perror
(
"poll"
);
return
1
;
}
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
==
1
)
{
printf
(
"Not enough parameters provided!"
);
if
(
pfds
[
0
].
revents
!=
0
)
{
if
(
pfds
[
0
].
revents
&
POLLIN
)
{
ssize_t
s
=
read
(
pfds
[
0
].
fd
,
buf
,
sizeof
(
buf
));
if
(
s
==
-
1
){
perror
(
"read 0"
);
return
1
;
}
else
{
DIR
*
dir
;
struct
dirent
*
entry
;
}
dir
=
opendir
(
argv
[
1
]);
if
(
!
dir
)
{
return
0
;
write
(
1
,
buf
,
s
);
}
else
{
break
;
}
while
((
entry
=
readdir
(
dir
))
!=
NULL
)
{
if
(
strcmp
(
entry
->
d_name
,
"."
)
==
0
||
strcmp
(
entry
->
d_name
,
".."
)
==
0
)
{
continue
;
}
char
*
entry_path
;
// string concatenation
if
((
entry_path
=
malloc
(
strlen
(
argv
[
1
])
+
strlen
(
entry
->
d_name
)
+
2
))
!=
NULL
){
entry_path
[
0
]
=
'\0'
;
strcat
(
entry_path
,
argv
[
1
]);
strcat
(
entry_path
,
"/"
);
strcat
(
entry_path
,
entry
->
d_name
);
}
else
{
printf
(
"%d
\n
"
,
pfds
[
1
].
revents
);
if
(
pfds
[
1
].
revents
!=
0
)
{
if
(
pfds
[
1
].
revents
&
POLLIN
)
{
ssize_t
s
=
read
(
pfds
[
1
].
fd
,
buf
,
sizeof
(
buf
));
if
(
s
==
-
1
){
perror
(
"read 0"
);
return
1
;
}
if
(
entry
->
d_type
==
DT_REG
)
{
unlink
(
entry_path
);
}
else
if
(
entry
->
d_type
==
DT_DIR
&&
count_files_in_dir
(
entry_path
)
==
0
)
{
rmdir
(
entry_path
);
write
(
pipeout
,
buf
,
s
);
}
else
{
break
;
}
free
(
entry_path
);
}
closedir
(
dir
);
if
(
count_files_in_dir
(
argv
[
1
])
==
0
)
{
rmdir
(
argv
[
1
]);
}
else
{
printf
(
"Couldn't delete all files, because there was a non empty folder!
\n
"
);
return
1
;
}
close
(
pfds
[
0
].
fd
);
close
(
pipeout
);
return
0
;
}
\ No newline at end of file
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment