init: builtins: Add 'copy' command to init.rc parser
Signed-off-by: San Mehat <san@google.com>
diff --git a/init/builtins.c b/init/builtins.c
index 16601ce..cc7c9d1 100644
--- a/init/builtins.c
+++ b/init/builtins.c
@@ -427,6 +427,68 @@
return write_file(args[1], args[2]);
}
+int do_copy(int nargs, char **args)
+{
+ char *buffer = NULL;
+ int rc = 0;
+ int fd1 = -1, fd2 = -1;
+ struct stat info;
+ int brtw, brtr;
+ char *p;
+
+ if (nargs != 3)
+ return -1;
+
+ if (stat(args[1], &info) < 0)
+ return -1;
+
+ if ((fd1 = open(args[1], O_RDONLY)) < 0)
+ goto out_err;
+
+ if ((fd2 = open(args[2], O_WRONLY|O_CREAT, 0660)) < 0)
+ goto out_err;
+
+ if (!(buffer = malloc(info.st_size)))
+ goto out_err;
+
+ p = buffer;
+ brtr = info.st_size;
+ while(brtr) {
+ rc = read(fd1, p, brtr);
+ if (rc < 0)
+ goto out_err;
+ if (rc == 0)
+ break;
+ p += rc;
+ brtr -= rc;
+ }
+
+ p = buffer;
+ brtw = info.st_size;
+ while(brtw) {
+ rc = write(fd2, p, brtw);
+ if (rc < 0)
+ goto out_err;
+ if (rc == 0)
+ break;
+ p += rc;
+ brtw -= rc;
+ }
+
+ rc = 0;
+ goto out;
+out_err:
+ rc = -1;
+out:
+ if (buffer)
+ free(buffer);
+ if (fd1 >= 0)
+ close(fd1);
+ if (fd2 >= 0)
+ close(fd2);
+ return rc;
+}
+
int do_chown(int nargs, char **args) {
/* GID is optional. */
if (nargs == 3) {
diff --git a/init/keywords.h b/init/keywords.h
index 6f47379..641426c 100644
--- a/init/keywords.h
+++ b/init/keywords.h
@@ -21,6 +21,7 @@
int do_symlink(int nargs, char **args);
int do_sysclktz(int nargs, char **args);
int do_write(int nargs, char **args);
+int do_copy(int nargs, char **args);
int do_chown(int nargs, char **args);
int do_chmod(int nargs, char **args);
int do_loglevel(int nargs, char **args);
@@ -65,6 +66,7 @@
KEYWORD(sysclktz, COMMAND, 1, do_sysclktz)
KEYWORD(user, OPTION, 0, 0)
KEYWORD(write, COMMAND, 2, do_write)
+ KEYWORD(copy, COMMAND, 2, do_copy)
KEYWORD(chown, COMMAND, 2, do_chown)
KEYWORD(chmod, COMMAND, 2, do_chmod)
KEYWORD(loglevel, COMMAND, 1, do_loglevel)
diff --git a/init/parser.c b/init/parser.c
index 33c1a68..affc80c 100644
--- a/init/parser.c
+++ b/init/parser.c
@@ -127,6 +127,7 @@
{
switch (*s++) {
case 'c':
+ if (!strcmp(s, "opy")) return K_copy;
if (!strcmp(s, "apability")) return K_capability;
if (!strcmp(s, "lass")) return K_class;
if (!strcmp(s, "lass_start")) return K_class_start;