From 7c44fe5925b6dd51166f73d30de0a2f22d66373e Mon Sep 17 00:00:00 2001 From: San Mehat Date: Wed, 26 Aug 2009 16:39:25 -0700 Subject: [PATCH] init: builtins: Add 'copy' command to init.rc parser Signed-off-by: San Mehat --- init/builtins.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ init/keywords.h | 2 ++ init/parser.c | 1 + 3 files changed, 65 insertions(+) diff --git a/init/builtins.c b/init/builtins.c index 16601ce0c..cc7c9d169 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 6f47379c6..641426caf 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 33c1a6813..affc80c9a 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;