mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:07:45 +00:00
cat: Updated to handle multiple input files (#694)
Now concatenates multiple files at once with a for loop iterating through a vector of file arguments.
This commit is contained in:
parent
fa69b9fbb7
commit
5442e365c9
1 changed files with 30 additions and 18 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
#include <AK/Vector.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -8,11 +9,20 @@
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
int fd = argc > 1 ? open(argv[1], O_RDONLY) : 0;
|
Vector<int> fds;
|
||||||
if (fd == -1) {
|
if (argc > 1) {
|
||||||
fprintf(stderr, "Failed to open %s: %s\n", argv[1], strerror(errno));
|
for (int i = 0; i < argc; i++) {
|
||||||
return 1;
|
int fd;
|
||||||
|
if ((fd = open(argv[i], O_RDONLY)) == -1) {
|
||||||
|
fprintf(stderr, "Failed to open %s: %s\n", argv[i], strerror(errno));
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
fds.append(fd);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fds.append(0);
|
||||||
|
}
|
||||||
|
for (auto& fd : fds) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
ssize_t nread = read(fd, buf, sizeof(buf));
|
ssize_t nread = read(fd, buf, sizeof(buf));
|
||||||
|
@ -29,5 +39,7 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
ASSERT(nwritten == nread);
|
ASSERT(nwritten == nread);
|
||||||
}
|
}
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue