changeset 23:1bb75d2408ae

Support basic commands
author atton
date Thu, 26 Nov 2015 18:16:25 +0900
parents 2f199ccd8e2d
children 08da58e4a988
files ie-docker.c ie-docker.h
diffstat 2 files changed, 53 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/ie-docker.c	Thu Nov 26 17:52:30 2015 +0900
+++ b/ie-docker.c	Thu Nov 26 18:16:25 2015 +0900
@@ -128,7 +128,13 @@
     PSLISTPTR p = list;
     p->name[0] = 0;
     p->next = 0;
-    FILE *fp = popen(ps_command,"r");
+    FILE *fp = popen(ps_exec_command,"r");
+
+    /* get header */
+    fgets(p->name,PSNAME_MAX,fp);
+    p->next = NEW(PSLIST);
+    p = p->next;
+
     while(fgets(p->name,PSNAME_MAX,fp)!=NULL) {
         if (regexec(list_pattern, p->name, (size_t) 0, NULL, 0)) continue;
         p->next = NEW(PSLIST);
@@ -284,35 +290,69 @@
      * Check argv for proper arguments and run
      * the corresponding script, if invoked.
      */
-    char *execl_command = malloc(sizeof(char) * 1024);
+    char *execv_args[16];
 
     if (command_is_matched(argv[1], ps_command)) {
         PSLISTPTR pslist = get_pslist(pattern);
         print_pslist(pslist); // TODO check work correctly
+        return 0;
     } else if (command_is_matched(argv[1], run_command)) {
-        sprintf(execl_command, "%s %s %s --name %s %s %s",
-                base_exec_command, run_command, run_options, name, base_image_name, run_shell);
+        execv_args[0] = base_exec_command;
+        execv_args[1] = run_command;
+        execv_args[2] = "-it";
+        execv_args[3] = "-u";
+        execv_args[4] = "2015";
+        execv_args[5] = "--name";
+        execv_args[6] = name;
+        execv_args[7] = base_image_name;
+        execv_args[8] = run_shell;
+        execv_args[9] = NULL;
     } else if (command_is_matched(argv[1], start_command)) {
-        sprintf(execl_command, "%s %s %s", base_exec_command, start_command, name);
+        execv_args[0] = base_exec_command;
+        execv_args[1] = start_command;
+        execv_args[2] = name;
+        execv_args[3] = NULL;
     } else if (command_is_matched(argv[1], stop_command)) {
-        sprintf(execl_command, "%s %s %s", base_exec_command, stop_command, name);
+        execv_args[0] = base_exec_command;
+        execv_args[1] = stop_command;
+        execv_args[2] = name;
+        execv_args[3] = NULL;
     } else if (command_is_matched(argv[1], attach_command)) {
-        sprintf(execl_command, "%s %s %s", base_exec_command, attach_command, name);
+        execv_args[0] = base_exec_command;
+        execv_args[1] = attach_command;
+        execv_args[2] = name;
+        execv_args[3] = NULL;
     } else if (command_is_matched(argv[1], rm_command)) {
-        sprintf(execl_command, "%s %s %s", base_exec_command, rm_command, name);
+        execv_args[0] = base_exec_command;
+        execv_args[1] = rm_command;
+        execv_args[2] = name;
+        execv_args[3] = NULL;
     } else if (command_is_matched(argv[1], cp_command)) {
         if (argc<3) {
             printf("cp command requires file path\n");
             return 0;
         }
-        sprintf(execl_command, "%s %s %s %s:%s", base_exec_command, cp_command, argv[2], name, cp_file_path);
+        execv_args[0] = base_exec_command;
+        execv_args[1] = cp_command;
+        execv_args[2] = argv[2];
+
+        char file_path[128];
+        strcat(file_path, name);
+        strcat(file_path, ":");
+        strcat(file_path, cp_file_path);
+        execv_args[3] = file_path;
+        execv_args[4] = NULL;
     } else {
         usage();
         return 0;
     }
-    printf(execl_command);
-    printf("\n");
-    free(execl_command);
+
+    int i;
+    for (i = 0; execv_args[i] != NULL; i++) {
+        printf("%s", execv_args[i]);
+        printf(execv_args[i+1] != NULL ? " " : "\n");
+    }
+    execv(base_exec_command, execv_args);
     return 0;
 }
 
--- a/ie-docker.h	Thu Nov 26 17:52:30 2015 +0900
+++ b/ie-docker.h	Thu Nov 26 18:16:25 2015 +0900
@@ -2,7 +2,7 @@
 #ifndef IE_DOCKER
 
 #define base_exec_command "/usr/bin/docker"
-#define ps_exec_command   "/usr/bin/docker ps -a"
+#define ps_exec_command   "/usr/bin/docker ps"
 
 #define run_command "run"
 #define build_command ""