/* Function to execute requested program at child */ void child_code(char *command){ char *parameters[BUF_SIZE/2+2]; //the parameters could consist of single characters seperated by a space. Add 1 for rounding and 1 for the null character to the array const char delimiter[2] = " "; str_to_array(command, delimiter, parameters); execvp(parameters[0], parameters); perror("exec"); /*will not be reached in case of a success, as exec replaces the complete program*/ exit(-1); }
static int oem_partition_gpt_handler(FILE *fp) { int argc = 0; int ret = 0; int i; char buffer[K_MAX_ARG_LEN]; char **argv = NULL; char value[PROPERTY_VALUE_MAX] = {'\0'}; property_get("sys.partitioning", value, NULL); if (strcmp(value, "1")) { error("Partitioning is not started\n"); return -1; } uuid_generator = uuid_generate; while (fgets(buffer, sizeof(buffer), fp)) { if (buffer[strlen(buffer)-1] == '\n') buffer[strlen(buffer)-1]='\0'; argv = str_to_array(buffer, &argc); if(argv != NULL) { ret = oem_partition_gpt_sub_command(argc, argv); for(i = 0; i < argc ; i++) { if (argv[i]) { free(argv[i]); argv[i]=NULL; } } free(argv); argv=NULL; if (ret) { error("GPT command failed\n"); return -1; } } else { error("GPT str_to_array error. Malformed string ?\n"); return -1; } } if (indirected_cmd_reload == cmd_reload) partlink_populate(); return 0; }
long get_stock_from_yahoo (char* code, char* from_mm, char* from_dd, char* from_yy, char* to_mm, char* to_dd, char* to_yy, GPtrArray* data) { STOCK record [10000]; int fd[2]; int curse; char* temp = "http://ichart.finance.yahoo.com/table.csv?s="; char url [100]; memset (url, 0x00, sizeof (url)); strcpy (url, temp); curse = (int) strlen (url); strcpy (url + curse, code); curse = (int) strlen (url); strcpy (url + curse, "&a="); curse = (int) strlen (url); strcpy (url + curse, from_mm); curse = (int) strlen (url); strcpy (url + curse, "&b="); curse = (int) strlen (url); strcpy (url + curse, from_dd); curse = (int) strlen (url); strcpy (url + curse, "&c="); curse = (int) strlen (url); strcpy (url + curse, from_yy); curse = (int) strlen (url); strcpy (url + curse, "&d="); curse = (int) strlen (url); strcpy (url + curse, to_mm); curse = (int) strlen (url); strcpy (url + curse, "&e="); curse = (int) strlen (url); strcpy (url + curse, to_dd); curse = (int) strlen (url); strcpy (url + curse, "&f="); curse = (int) strlen (url); strcpy (url + curse, to_yy); curse = (int) strlen (url); strcpy (url + curse, "&g=d&ignore=.csv"); printf ("%s\n", url); printf ("%s\n", "http://ichart.finance.yahoo.com/table.csv?s=068400.KS&a=07&b=1&c=2011&d=07&e=10&f=2011&g=d&ignore=.csv"); /* int output = open ("output2.csv", O_WRONLY | O_CREAT | O_TRUNC, 0755); if (output == -1) return 1; */ BufferStruct buffer; buffer.buffer = NULL; buffer.size = 0; CURL* curlHandle = curl_easy_init (); curl_easy_setopt (curlHandle, CURLOPT_URL, url); curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt (curlHandle, CURLOPT_WRITEDATA, (void*) &buffer); curl_easy_perform (curlHandle); int i = 0; char* line = strtok (buffer.buffer, ",\n"); for (i=0;i<6;i++) { strtok (NULL, ",\n"); } int t = 1; while (str_to_array (line, data)) { } curl_easy_cleanup (curlHandle); /* close (output); */ }
Value *FlashImageAtPartition(const char *name, State * state, int argc, Expr * argv[]) { Value *funret = NULL; char *osname, *filename, *parttable; void *data; off_t offset = 0; int fd; FILE *fp; char buffer[K_MAX_ARG_LEN]; char partition_type[K_MAX_ARG_LEN]; char **gpt_argv = NULL; int i, gpt_argc = 0; Value *ret = NULL; int fret; if (argc != 3) { ErrorAbort(state, "%s: Invalid parameters.", name); goto exit; } if (ReadArgs(state, argv, 3, &osname, &filename, &parttable) < 0) { ErrorAbort(state, "%s: ReadArgs failed.", name); goto exit; } /* Open partition file */ fp = fopen(parttable, "r"); if (!fp) { ErrorAbort(state, "%s: Can't open %s partition file.", name, parttable); ret = StringValue(strdup("")); goto free; } /* Check first line of partition file */ memset(buffer, 0, sizeof(buffer)); if (!fgets(buffer, sizeof(buffer), fp)) { ErrorAbort(state, "partition file is empty"); ret = StringValue(strdup("")); goto free; } buffer[strlen(buffer) - 1] = '\0'; if (sscanf(buffer, "%*[^=]=%255s", partition_type) != 1) { ErrorAbort(state, "partition file is invalid"); ret = StringValue(strdup("")); goto free; } bool found = false; /* parse partitition files line to catch osname */ while (fgets(buffer, sizeof(buffer), fp)) { if (buffer[strlen(buffer) - 1] == '\n') buffer[strlen(buffer) - 1] = '\0'; gpt_argv = str_to_array(buffer, &gpt_argc); if (gpt_argv == NULL) continue; char *command = gpt_argv[0]; char* e; /* catch add commands */ if (0 == strncmp("add", command, strlen(command))) { /* catch partitions to update */ for (i=0; i < gpt_argc; i++) { if (0 == strncmp(osname, gpt_argv[i], strlen(gpt_argv[i]))) { found = true; printf("found %s ", osname); } } /* get offset of osname */ if (found) { for (i=0; i < gpt_argc; i++) { if (0 == strncmp("-b", gpt_argv[i], strlen(gpt_argv[i]))) { offset = strtoull(gpt_argv[i+1], &e, 0) * 512; if (e && *e) { ErrorAbort(state, "Unable to get LBA of partition %s", buffer); ret = StringValue(strdup("")); goto free; } printf("at LBA %s offset % ld \n", gpt_argv[i+1], offset); } } } } /* free gpt_argv */ for (i = 0; i < gpt_argc; i++) { free(gpt_argv[i]); gpt_argv[i] = NULL; } free(gpt_argv); gpt_argv = NULL; if (found) break; } fclose(fp); if (!found) { ErrorAbort(state, "partition %s not found in %s", name, parttable); ret = StringValue(strdup("")); goto free; } else { printf("%s: found partition %s at offset %ld\n", name, osname, offset); } /* Flash image at offset */ int length = file_size(filename); if (length == -1) goto free; data = file_mmap(filename, length, false); if (data == MAP_FAILED) { goto free; } fd = open(MMC_DEV_POS, O_WRONLY); if (fd == -1) { ErrorAbort(state, "%s: Failed to open %s device block, %s.", name, MMC_DEV_POS, strerror(errno)); goto unmmap_file; } fret = lseek(fd, offset, SEEK_SET); if (fret == -1) { ErrorAbort(state, "%s: Failed to seek into %s device block, %s.", name, MMC_DEV_POS, strerror(errno)); ret = StringValue(strdup("")); goto close; } char *ptr = (char *)data; fret = 0; for (; length; length -= fret, ptr += fret) { fret = write(fd, ptr, length); if (fret == -1) { ErrorAbort(state, "%s: Failed to write into %s device block, %s.", name, MMC_DEV_POS, strerror(errno)); ret = StringValue(strdup("")); goto close; } } fret = fsync(fd); if (fret == -1) { ErrorAbort(state, "%s: Failed to sync %s, %s.", name, MMC_DEV_POS, strerror(errno)); ret = StringValue(strdup("")); goto close; } ret = StringValue(strdup("t")); close: close(fd); unmmap_file: munmap(data, length); free: free(osname); free(filename); free(parttable); exit: return ret; }
Value *FlashOsipToGPTPartition(const char *name, State * state, int argc, Expr * argv[]) { FILE *fp, *fp_update; char *filename; char buffer[K_MAX_ARG_LEN]; char partition_type[K_MAX_ARG_LEN]; char **gpt_argv = NULL; unsigned int j; int i, gpt_argc = 0; Value *ret = NULL; struct OSIP_header osip; /* Do not reload partition table during OTA since some partition * are still mounted, reload would failed. */ oem_partition_disable_cmd_reload(); if (argc != 1) { ErrorAbort(state, "%s: Invalid parameters.", name); return StringValue(strdup("")); } if (ReadArgs(state, argv, argc, &filename) < 0) { ErrorAbort(state, "%s: ReadArgs failed.", name); return StringValue(strdup("")); } /* Dump OSIP to ease debug and get LBA of OSIP entries */ if (read_OSIP(&osip)) { fprintf(stderr, "read_OSIP fails\n"); } dump_osip_header(&osip); /* Open partition and partition update files */ fp = fopen(filename, "r"); if (!fp) { ErrorAbort(state, "%s: Can't open %s partition file.", name, filename); free(filename); ret = StringValue(strdup("")); goto exit; } fp_update = fopen(PARTITION_UPDATE, "w+"); if (!fp_update) { ErrorAbort(state, "%s: Can't create %s partition file\n", name, PARTITION_UPDATE); fclose(fp); free(filename); ret = StringValue(strdup("")); goto exit; } /* Check first line of partition file */ memset(buffer, 0, sizeof(buffer)); if (!fgets(buffer, sizeof(buffer), fp)) { ErrorAbort(state, "partition file is empty"); ret = StringValue(strdup("")); goto error; } buffer[strlen(buffer) - 1] = '\0'; if (sscanf(buffer, "%*[^=]=%255s", partition_type) != 1) { ErrorAbort(state, "partition file is invalid"); ret = StringValue(strdup("")); goto error; } /* Write first line in update file */ fprintf(fp_update, "%s\n", buffer); /* parse partitition files line to catch update_partitions */ while (fgets(buffer, sizeof(buffer), fp)) { if (buffer[strlen(buffer) - 1] == '\n') buffer[strlen(buffer) - 1] = '\0'; gpt_argv = str_to_array(buffer, &gpt_argc); if (gpt_argv == NULL) continue; char *command = gpt_argv[0]; uint64_t size, lba_start; int64_t osii_lba; bool update_need = false; bool update_write = true; char *e; const char* update_partitions[] = {ANDROID_OS_NAME, RECOVERY_OS_NAME, FASTBOOT_OS_NAME}; /* catch add commands */ if (0 == strncmp("add", command, strlen(command))) { /* catch partitions to update */ for (i = 0; i < gpt_argc; i++) { /* Do not write back "reserved" */ if (0 == strncmp("reserved", gpt_argv[i], strlen(gpt_argv[i]))) update_write = false; for (j = 0; j < ARRAY_SIZE(update_partitions); j++) { if (0 == strncmp(update_partitions[j], gpt_argv[i], strlen(gpt_argv[i]))) { if ((osii_lba = get_named_osii_logical_start_block(gpt_argv[i])) == -1) { ErrorAbort(state, "Unable to get LBA of %s partition", gpt_argv[i]); ret = StringValue(strdup("")); goto error; } printf("Found %s partition at osii_lba %"PRId64"\n", gpt_argv[i], osii_lba); update_need = true; break; } } if (update_need || !update_write) break; } /* update size and LBA */ if (update_need) { for (i = 0; i < gpt_argc; i++) { if (0 == strncmp("-s", gpt_argv[i], strlen(gpt_argv[i]))) { size = strtoull(gpt_argv[i+1], &e, 0); if (e && *e) { ErrorAbort(state, "Unable to get size of partition %s", buffer); ret = StringValue(strdup("")); goto error; } printf("Size was %"PRIu64" new is %u \n", size, OS_MAX_LBA); sprintf(gpt_argv[i+1],"%d", OS_MAX_LBA); } if (0 == strncmp("-b", gpt_argv[i], strlen(gpt_argv[i]))) { lba_start = strtoull(gpt_argv[i+1], &e, 0); if (e && *e) { ErrorAbort(state, "Unable to get LBA of partition %s", buffer); ret = StringValue(strdup("")); goto error; } printf("LBA was %"PRIu64" new is %"PRId64" \n", lba_start, osii_lba); sprintf(gpt_argv[i+1],"%"PRId64"", osii_lba); } } } } /* write lines in partition update */ for (i = 0; i < gpt_argc; i++) { if (update_write) fprintf(fp_update,"%s ",gpt_argv[i]); free(gpt_argv[i]); gpt_argv[i] = NULL; } if (update_write) fprintf(fp_update,"\n"); free(gpt_argv); gpt_argv = NULL; } /* Ensure to close fp_update as it will be re-open by oem_partition_cmd_handler */ free(filename); fclose(fp); fclose(fp_update); /* partition with new partition file */ property_set("sys.partitioning", "1"); char *path[] = {0,PARTITION_UPDATE}; int partret = -1; if ((partret = oem_partition_cmd_handler(2,(char **)path)) == -1) { ErrorAbort(state, "%s: re-partitionning fails with error %d", name, partret); ret = StringValue(strdup("")); } else ret = StringValue(strdup("t")); property_set("sys.partitioning", "0"); goto exit; error: free(filename); fclose(fp); fclose(fp_update); exit: return ret; }