summaryrefslogtreecommitdiffstats
path: root/init
diff options
context:
space:
mode:
authorSan Mehat <san@google.com>2009-08-26 16:39:25 -0700
committerSan Mehat <san@google.com>2009-08-26 16:39:59 -0700
commit7c44fe5925b6dd51166f73d30de0a2f22d66373e (patch)
tree6f9e8e3fc3ee500e4e1122ed395e77d19206c017 /init
parenta3e8a6ad56d60d2ad6c580004f86920d73acc62c (diff)
downloadsystem_core-7c44fe5925b6dd51166f73d30de0a2f22d66373e.zip
system_core-7c44fe5925b6dd51166f73d30de0a2f22d66373e.tar.gz
system_core-7c44fe5925b6dd51166f73d30de0a2f22d66373e.tar.bz2
init: builtins: Add 'copy' command to init.rc parser
Signed-off-by: San Mehat <san@google.com>
Diffstat (limited to 'init')
-rw-r--r--init/builtins.c62
-rw-r--r--init/keywords.h2
-rw-r--r--init/parser.c1
3 files changed, 65 insertions, 0 deletions
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 @@ int do_write(int nargs, char **args)
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_trigger(int nargs, char **args);
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 @@ enum {
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 @@ int lookup_keyword(const char *s)
{
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;