int read_loop(const int fd) { char *line = NULL; size_t linecap = 0; ssize_t linelen; int line_size; char *response = NULL; printf("\n>>> "); while ((linelen = getline(&line, &linecap, stdin)) > 0) { line[linelen-1] = '\0'; if (line[0] == 'l') { local_parse(line, linelen); printf("\n>>> "); continue; } if (strnstr(line, "put", 3) != 0) { int name_size = strlen(&line[4]); char name[name_size]; strncpy(name, &line[4], name_size); assert(name[name_size-1] == 0); printf("Name '%s', %i:\n", name, name_size); if (access(name, R_OK) != -1) { int in_fd = open(name, O_RDONLY, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); int file_size = get_fd_size(in_fd); write(fd, &file_size, sizeof(int)); char *file_line = malloc(file_size); read(in_fd, file_line, file_size); write(fd, file_line, file_size); free(file_line); free(line); continue; } else { puts("File couldn't be read"); printf("\n>>> "); continue; } } line_size = linelen; write(fd, &line_size, sizeof(int)); write(fd, line, line_size); if (read(fd, &line_size, sizeof(int)) == -1) { perror("reading size"); exit(-1); } response = malloc(line_size); if (read(fd, response, line_size) == -1) { perror("reading message"); exit(-1); } printf("%s\n", response); if (strcmp(response, "exit") == 0) { break; } free(response); printf("\n>>> "); } close(fd); return 0; }
int local_rdbi_sql( rdbi_context_def *context, int sqlid, rdbi_string_def* sql, int defer, int type ) { rdbi_cursor_def *cursor = (rdbi_cursor_def *) NULL; /* for ease of reference */ rdbi_cursor_def *cursor_coc = (rdbi_cursor_def *) NULL; /* for ease of reference */ int trace_line_num = 0; int sqlid_coc = -1; char stats[128]; #ifdef _DEBUG if (context->dispatch.capabilities.supports_unicode == 1){ debug_on4("rdbi_sql2", "\tContext: %d Db: %s#%d\n\tSQL: %.120ls", sqlid, context->rdbi_cnct->db_name, context->rdbi_cnct->connect_id, sql->cwString); }else{ debug_on4("rdbi_sql2", "\tContext: %d Db: %s#%d\n\tSQL: %.120s", sqlid, context->rdbi_cnct->db_name, context->rdbi_cnct->connect_id, sql->ccString); } #endif cursor = context->rdbi_cursor_ptrs[sqlid]; if (context->rdbi_cnct->autocommit_on && cursor->tran_begun) { char tran_id[50]; sprintf(tran_id, "auto-exec-%s %d", cursor->verb, cursor->trace_line); rdbi_tran_end(context, tran_id); cursor->tran_begun = FALSE; } sprintf(stats, "Open Cursor: %d", sqlid); debug_trace(stats, (wchar_t *)NULL, NULL); if (context->dispatch.capabilities.supports_unicode == 1) debug_trace(NULL, sql->wString, &trace_line_num); else debug_trace(sql->cString, NULL, &trace_line_num); cursor->sql_parsed = FALSE; /* set default cursor conditions */ cursor->bound_vars = FALSE; cursor->defined_vars = FALSE; cursor->sel_for_update = FALSE; cursor->n_executions = 0; cursor->tran_begun = FALSE; cursor->trace_line = trace_line_num; #ifdef _DEBUG /* free any previously-stored SQL statement */ ut_vm_free("rdbi_sql", cursor->sql); cursor->sql = (char *)NULL; if( sql->cwString ) { if (context->dispatch.capabilities.supports_unicode == 1) { cursor->sqlW = (wchar_t*)ut_vm_malloc("rdbi_sql", (wcslen(sql->cwString) + 1)*sizeof(wchar_t)); if (cursor->sqlW == (wchar_t *)NULL) { cursor->status = RDBI_MALLOC_FAILED; goto the_exit; } wcscpy(cursor->sqlW, sql->cwString); } else { cursor->sql = (char*)ut_vm_malloc("rdbi_sql", strlen(sql->ccString) + 1); if (cursor->sql == (char *)NULL) { cursor->status = RDBI_MALLOC_FAILED; goto the_exit; } strcpy(cursor->sql, sql->ccString); } } #endif /* instead of parsing the statement for 'current of cursor' we just do this: */ cursor->status = RDBI_SUCCESS; cursor->statement_type = 0; cursor->sel_for_update = 0; /* get the verb */ if (context->dispatch.capabilities.supports_unicode == 1) { local_parseW (sql->cwString, cursor->verb, type); cursor->status = (*(context->dispatch.sqlW))(context->drvr, cursor->vendor_data, sql->cwString, defer, cursor->verb, NULL, cursor_coc == (rdbi_cursor_def *) NULL ? (char *) NULL : cursor_coc->vendor_data); } else { local_parse (sql->ccString, cursor->verb, type); cursor->status = (*(context->dispatch.sql))(context->drvr, cursor->vendor_data, sql->ccString, defer, cursor->verb, NULL, cursor_coc == (rdbi_cursor_def *) NULL ? (char *) NULL : cursor_coc->vendor_data); } if (cursor->status == RDBI_SUCCESS) cursor->sql_parsed = TRUE; #ifdef _DEBUG the_exit: #endif context->rdbi_last_status = cursor->status; debug_return(NULL, cursor->status); }