static void do_test_libeprom24x(void) { int value; do { print_menu(); printf("Enter choice : "); scanf("%d",&value); switch(value) { case 1: get_prod_info(); break; case 2: get_last_error(); break; case 3: initialize(); break; case 4: finalize(); break; case 5: read_u8(); break; case 6: read_u16(); break; case 7: read_u32(); break; case 8: read_to_buffer(); break; case 9: write_u8(); break; case 10: write_u16(); break; case 11: write_u32(); break; case 12: write_from_buffer(); break; case 13: erase_chip(); break; case 100: /* Exit */ break; default: printf("Illegal choice!\n"); } } while (value != 100); return; }
bool write_ini_file(const char* filename, UniversalContainer& uc) { Buffer* buf = uc_encode_ini(uc); if (!buf) return false; bool result = write_from_buffer(buf,filename); delete buf; return result; }
int write_file(ino iNode, const char *buffer, int offset, int numbytes) { int bytesWritten = 0; char iNodeDataBlock[BLOCK_SIZE]; iNodeEntry *iNodes; if (iNode < 16) { ReadBlock(4, iNodeDataBlock); iNodes = (iNodeEntry *)iNodeDataBlock; bytesWritten = write_from_buffer(iNodes[iNode], buffer, offset, numbytes); } else { ReadBlock(5, iNodeDataBlock); iNodes = (iNodeEntry *)iNodeDataBlock; bytesWritten = write_from_buffer(iNodes[iNode - 16], buffer, offset, numbytes); } return bytesWritten; }
void print(UniversalContainer& uc) { Buffer* buf = uc_encode_ini(uc); write_from_buffer(buf,stdout); delete buf; }
void run_log_forwarder(struct fd_pair *internal, struct fd_pair *input, struct fd_pair *output, struct fd_pair *error, int interactive) { int i; fd_set read_set; fd_set write_set; struct timeval timeout; int stdin_open = 1; int stdout_open = 1; int stderr_open = 1; int internal_open = 1; int input_open = 1; int output_open = 1; int error_open = error ? 1 : 0; int max_fd = internal->write_side; if (input->write_side > max_fd) max_fd = input->write_side; if (output->read_side > max_fd) max_fd = output->read_side; if (error_open && error->read_side > max_fd) max_fd = error->read_side; max_fd++; reserve = NULL; struct list internal_buffer = {NULL, NULL}; struct list input_buffer = {NULL, NULL}; struct list stdout_buffer = {NULL, NULL}; struct list stderr_buffer = {NULL, NULL}; /* non-blocking read/write */ int flags = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); flags = fcntl(STDOUT_FILENO, F_GETFL, 0); fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); flags = fcntl(STDERR_FILENO, F_GETFL, 0); fcntl(STDERR_FILENO, F_SETFL, flags | O_NONBLOCK); flags = fcntl(internal->write_side, F_GETFL, 0); fcntl(internal->write_side, F_SETFL, flags | O_NONBLOCK); flags = fcntl(input->write_side, F_GETFL, 0); fcntl(input->write_side, F_SETFL, flags | O_NONBLOCK); flags = fcntl(output->read_side, F_GETFL, 0); fcntl(output->read_side, F_SETFL, flags | O_NONBLOCK); if (error_open) { flags = fcntl(error->read_side, F_GETFL, 0); fcntl(error->read_side, F_SETFL, flags | O_NONBLOCK); } /* as long as there is a parent process and something to read */ while (getppid() > 1 && (output_open || error_open)) { /* prepare read set */ FD_ZERO(&read_set); if (stdin_open) FD_SET(STDIN_FILENO, &read_set); if (output_open) FD_SET(output->read_side, &read_set); if (error_open) FD_SET(error->read_side, &read_set); /* prepare write set */ FD_ZERO(&write_set); if (internal_buffer.head && internal_open) FD_SET(internal->write_side, &write_set); if (input_buffer.head && input_open) FD_SET(input->write_side, &write_set); if (stdout_buffer.head && stdout_open) FD_SET(STDOUT_FILENO, &write_set); if (stderr_buffer.head && stderr_open) FD_SET(STDERR_FILENO, &write_set); /* timeout to avoid hanging processes if PPID changes to 1 between getppid and select */ timeout.tv_sec = 300; timeout.tv_usec = 0; /* what to do */ int result = select(max_fd, &read_set, &write_set, NULL, &timeout); /* error handling */ if (result < 0) { if (EINTR == errno) continue; perror("select"); break; } /* timeout */ else if (0 == result) { continue; } /* non-blocking write */ if (FD_ISSET(input->write_side, &write_set)) { input_open = write_from_buffer(input->write_side, &input_buffer, 0); } if (FD_ISSET(STDERR_FILENO, &write_set)) { stderr_open = write_from_buffer(STDERR_FILENO, &stderr_buffer, 0); } if (FD_ISSET(STDOUT_FILENO, &write_set)) { stdout_open = write_from_buffer(STDOUT_FILENO, &stdout_buffer, 0); } if (FD_ISSET(internal->write_side, &write_set)) { internal_open = write_from_buffer(internal->write_side, &internal_buffer, 1); } /* non-blocking read */ if (FD_ISSET(STDIN_FILENO, &read_set)) { stdin_open = read_to_buffer(STDIN_FILENO, &input_buffer, (internal_open && !interactive) ? &internal_buffer : NULL); } if (error_open && FD_ISSET(error->read_side, &read_set)) { error_open = read_to_buffer(error->read_side, &stderr_buffer, internal_open ? &internal_buffer : NULL); } if (FD_ISSET(output->read_side, &read_set)) { output_open = read_to_buffer(output->read_side, &stdout_buffer, internal_open ? &internal_buffer : NULL); } /* propagate closed input */ if (!stdin_open && !input_buffer.head && input_open) { close(input->write_side); input_open = 0; } } /* do we have something left out for writing? (reset to blocking mode before writing) */ flags = fcntl(STDERR_FILENO, F_GETFL, 0); fcntl(STDERR_FILENO, F_SETFL, flags & (~O_NONBLOCK)); flags = fcntl(STDOUT_FILENO, F_GETFL, 0); fcntl(STDOUT_FILENO, F_SETFL, flags & (~O_NONBLOCK)); flags = fcntl(internal->write_side, F_GETFL, 0); fcntl(internal->write_side, F_SETFL, flags & (~O_NONBLOCK)); while (stderr_buffer.head) { write_from_buffer(STDERR_FILENO, &stderr_buffer, 0); } while (stdout_buffer.head) { write_from_buffer(STDOUT_FILENO, &stdout_buffer, 0); } while (internal_open && internal_buffer.head) { internal_open = write_from_buffer(internal->write_side, &internal_buffer, 1); } /* still some input? Write it out immediately 1:1*/ if (internal_open) { while (error_open && read_to_buffer(error->read_side, &stderr_buffer, &internal_buffer)) { write_from_buffer(STDERR_FILENO, &stderr_buffer, 0); write_from_buffer(internal->write_side, &internal_buffer, 1); } while (read_to_buffer(output->read_side, &stdout_buffer, &internal_buffer)) { write_from_buffer(STDOUT_FILENO, &stdout_buffer, 0); write_from_buffer(internal->write_side, &internal_buffer, 1); } } else { while (error_open && read_to_buffer(error->read_side, &stderr_buffer, NULL)) { write_from_buffer(STDERR_FILENO, &stderr_buffer, 0); } while (read_to_buffer(output->read_side, &stdout_buffer, NULL)) { write_from_buffer(STDOUT_FILENO, &stdout_buffer, 0); } } /* final cleanup */ if (NULL != reserve) { free(reserve); } }