1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:47:45 +00:00

Userland: "touch" can now handle multiple paths as arguments

You can now do:
  touch a.txt b.txt c.txt d.txt

Also now you can't do:
  touch --test  # This created a "./--test" file

Also adds ArgsParser to the mix to better handle arguments :)
This commit is contained in:
Andres Vieira 2020-05-01 14:02:04 +02:00 committed by Andreas Kling
parent 8551c10918
commit 15aedc6866

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <LibCore/ArgsParser.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
@ -55,24 +56,28 @@ int main(int argc, char** argv)
return 1; return 1;
} }
if (argc != 2) { Vector<const char*> paths;
fprintf(stderr, "usage: touch <path>\n");
return 1; Core::ArgsParser args_parser;
} args_parser.add_positional_argument(paths, "Files to touch", "path", Core::ArgsParser::Required::Yes);
if (file_exists(argv[1])) { args_parser.parse(argc, argv);
int rc = utime(argv[1], nullptr);
if (rc < 0) for (auto path : paths) {
perror("utime"); if (file_exists(path)) {
} else { int rc = utime(path, nullptr);
int fd = open(argv[1], O_CREAT, 0100644); if (rc < 0)
if (fd < 0) { perror("utime");
perror("open"); } else {
return 1; int fd = open(path, O_CREAT, 0100644);
} if (fd < 0) {
int rc = close(fd); perror("open");
if (rc < 0) { return 1;
perror("close"); }
return 1; int rc = close(fd);
if (rc < 0) {
perror("close");
return 1;
}
} }
} }
return 0; return 0;