ladybird/Userland/ln.cpp
Andreas Kling 2c5a378ccc Kernel+Userland: Add symlink() syscall and add "-s" flag to /bin/ln.
It's now possible to create symbolic links! :^)

This exposed an issue in Ext2FS where we'd write uninitialized data past
the end of an inode's content. Fix this by zeroing out the tail end of
the last block in a file.
2019-03-02 01:52:24 +01:00

46 lines
886 B
C++

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
[[noreturn]] static void print_usage_and_exit()
{
printf("usage: ln [-s] <target> <link-path>\n");
exit(0);
}
int main(int argc, char** argv)
{
bool flag_symlink = false;
int opt;
while ((opt = getopt(argc, argv, "s")) != -1) {
switch (opt) {
case 's':
flag_symlink = true;
break;
default:
print_usage_and_exit();
}
}
if ((optind + 1) >= argc)
print_usage_and_exit();
if (flag_symlink) {
int rc = symlink(argv[optind], argv[optind + 1]);
if (rc < 0) {
perror("symlink");
return 1;
}
return 0;
}
int rc = link(argv[1], argv[2]);
if (rc < 0) {
perror("link");
return 1;
}
return 0;
}