struct arm_file_desc_table_t *arm_file_desc_table_create(void) { struct arm_file_desc_table_t *table; struct arm_file_desc_t *desc; /* Allocate */ table = calloc(1, sizeof(struct arm_file_desc_table_t)); if (!table) fatal("%s: out of memory", __FUNCTION__); /* Initialize */ table->arm_file_desc_list = list_create(); /* Add stdin */ desc = arm_file_desc_create(arm_file_desc_std, 0, 0, 0, NULL); list_add(table->arm_file_desc_list, desc); /* Add stdout */ desc = arm_file_desc_create(arm_file_desc_std, 1, 1, 0, NULL); list_add(table->arm_file_desc_list, desc); /* Add stderr */ desc = arm_file_desc_create(arm_file_desc_std, 2, 2, 0, NULL); list_add(table->arm_file_desc_list, desc); /* Return */ return table; }
struct arm_file_desc_t *arm_file_desc_table_entry_new_guest_fd(struct arm_file_desc_table_t *table, enum arm_file_desc_kind_t kind, int guest_fd, int host_fd, char *path, int flags) { struct arm_file_desc_t *desc; int i; /* Look for a free entry */ for (i = 0; i < list_count(table->arm_file_desc_list) && guest_fd < 0; i++) if (!list_get(table->arm_file_desc_list, i)) guest_fd = i; /* If no free entry was found, add new entry. */ if (guest_fd < 0) { guest_fd = list_count(table->arm_file_desc_list); list_add(table->arm_file_desc_list, NULL); } /* Specified guest_fd may still be too large */ for (i = list_count(table->arm_file_desc_list); i <= guest_fd; ++i) { list_add(table->arm_file_desc_list, NULL); } /* Create guest arm_file descriptor and return. */ desc = arm_file_desc_create(kind, guest_fd, host_fd, flags, path); list_set(table->arm_file_desc_list, guest_fd, desc); /* Return */ return desc; }
struct arm_file_desc_table_t *arm_file_desc_table_create(void) { struct arm_file_desc_table_t *table; struct arm_file_desc_t *desc; /* Initialize */ table = xcalloc(1, sizeof(struct arm_file_desc_table_t)); table->arm_file_desc_list = list_create(); /* Add stdin */ desc = arm_file_desc_create(arm_file_desc_std, 0, 0, 0, NULL); list_add(table->arm_file_desc_list, desc); /* Add stdout */ desc = arm_file_desc_create(arm_file_desc_std, 1, 1, 0, NULL); list_add(table->arm_file_desc_list, desc); /* Add stderr */ desc = arm_file_desc_create(arm_file_desc_std, 2, 2, 0, NULL); list_add(table->arm_file_desc_list, desc); /* Return */ return table; }