void merge_file(char* outfile,char* hirom_name, char* lorom_name) { FILE* out_rom; FILE* hirom; FILE* lorom; unsigned char buff[1]; hirom = fopen(hirom_name,"rb"); lorom = fopen(lorom_name,"rb"); check_open(hirom,hirom_name); check_open(lorom,lorom_name); check_exists(outfile); out_rom = fopen(outfile,"wb"); while(fread(buff,sizeof(buff),1,hirom)!=0) { fwrite(buff,sizeof(buff),1,out_rom); if(fread(buff,sizeof(buff),1,lorom)!=0) { fwrite(buff,sizeof(buff),1,out_rom); } } fclose(hirom); fclose(lorom); fclose(out_rom); printf("Files are merged into: %s\n",outfile); printf("hirom: %s\n",hirom_name); printf("lorom: %s\n",lorom_name); }
FileHandler::FileHandler(const char* dictionary_file_path, const char* input_file_path, const char* log_file_path) { spelling_checker = new SpellingChecker(); dictionary_file.open(dictionary_file_path); input_file.open(input_file_path); log_file.open(log_file_path); check_open(&dictionary_file, dictionary_file_path); check_open(&input_file, input_file_path); }
//============================================================================== void File::read(uint8_t data[], size_t size) { check_open("File::read", file_); if (fread(data, size, 1, file_) != 1 || ferror(file_)) file_io_error("File::read", file_name_); }
//============================================================================== void File::write(const ByteVector &data) { check_open("File::write", file_); if (fwrite(&data[0], data.size(), 1, file_) != 1 || ferror(file_)) file_io_error("File::write", file_name_); }
gboolean g_vfs_afp_connection_close_sync (GVfsAfpConnection *afp_connection, GCancellable *cancellable, GError **error) { GVfsAfpConnectionPrivate *priv = afp_connection->priv; SyncData close_data; /* Take lock */ g_mutex_lock (&priv->mutex); if (!check_open (afp_connection, error)) { g_mutex_unlock (&priv->mutex); return FALSE; } sync_data_init (&close_data, afp_connection, error); priv->pending_closes = g_slist_prepend (priv->pending_closes, &close_data); /* Release lock */ g_mutex_unlock (&priv->mutex); if (g_atomic_int_compare_and_exchange (&priv->atomic_state, STATE_CONNECTED, STATE_PENDING_CLOSE)) g_cancellable_cancel (priv->read_cancellable); sync_data_wait (&close_data); return close_data.res; }
//============================================================================== void File::write(const uint8_t data[], size_t size) { check_open("File::write", file_); if (fwrite(data, size, 1, file_) != 1 || ferror(file_)) file_io_error("File::write", file_name_); }
GVfsAfpReply * g_vfs_afp_connection_send_command_sync (GVfsAfpConnection *afp_connection, GVfsAfpCommand *command, GCancellable *cancellable, GError **error) { SyncData sync_data; GVfsAfpReply *reply; if (!check_open (afp_connection, error)) return FALSE; sync_data_init (&sync_data, afp_connection, NULL); g_vfs_afp_connection_send_command (afp_connection, command, NULL, send_command_sync_cb, cancellable, &sync_data); sync_data_wait (&sync_data); reply = g_vfs_afp_connection_send_command_finish (afp_connection, sync_data.data, error); g_object_unref (sync_data.data); sync_data_clear (&sync_data); return reply; }
int SSH2Channel::getExitStatus(ExceptionSink *xsink) { AutoLocker al(parent->m); if (check_open(xsink)) return -1; return libssh2_channel_get_exit_status(channel); }
bool SSH2Channel::eof(ExceptionSink *xsink) { AutoLocker al(parent->m); if (check_open(xsink)) return true; return (bool)libssh2_channel_eof(channel); }
int check_filecore(char *file) { int i; int j; int fd; char *require; char *file_ext; i = 0; j = 0; require = "cor"; file_ext = (char *)malloc(sizeof(char) * ft_strlen(require)); while (file[i] != '.') i++; i++; while (file[i]) { file_ext[j] = file[i]; i++; j++; } if (ft_strcmp(require, file_ext) != 0) ft_putendl("error : extension file"); if ((fd = check_open(file)) == -1) return (-1); return (fd); }
//============================================================================== void USB_Device::device_decriptor(libusb_device_descriptor &descriptor) { check_open(); int result = libusb_get_device_descriptor(device_, &descriptor); if (result != LIBUSB_SUCCESS) on_error("Failed to get device descriptor", (libusb_error)result); }
int sfs_fclose(int fileID){ if (check_open(fileID)){ fd_table[fileID] = (fd){1,-1,-1,-1}; return 0; } else { return -1; } }
//============================================================================== void File::read(ByteVector &data, size_t size) { check_open("File::read", file_); data.resize(size); if (fread(&data[0], data.size(), 1, file_) != 1 || ferror(file_)) file_io_error("File::read", file_name_); }
//============================================================================== off_t File::seek(off_t offset, uint_t position) { check_open("File::seek", file_); off_t result = fseeko(file_, offset, position); if (result == (off_t) -1) file_io_error("File::seek", file_name_); return result; }
//============================================================================== off_t File::size() { check_open("File::size", file_); struct stat file_stat; if (fstat(fileno(file_), &file_stat) != 0) file_io_error("File::size", file_name_); return file_stat.st_size; }
//============================================================================== bool File::read_n(uint8_t data[], size_t max_size, size_t &read_size) { check_open("File::read_max", file_); ssize_t result = fread(data, 1, max_size, file_); if (ferror(file_)) file_io_error("File::read_max", file_name_); read_size = result; return feof(file_); }
BinaryNode *SSH2Channel::readBinary(qore_size_t size, int stream_id, int timeout_ms, ExceptionSink *xsink) { AutoLocker al(parent->m); if (check_open(xsink)) return 0; SimpleRefHolder<BinaryNode> bin(new BinaryNode); BlockingHelper bh(parent); qore_offset_t rc; // bytes read qore_size_t b_read = 0; // bytes remaining qore_size_t b_remaining = size; while (true) { char buffer[QSSH2_BUFSIZE]; qore_size_t to_read = QSSH2_BUFSIZE < b_remaining ? QSSH2_BUFSIZE : b_remaining; rc = libssh2_channel_read_ex(channel, stream_id, buffer, to_read); //printd(5, "SSH2Channel::read() rc=%lld (EAGAIN=%d)\n", rc, LIBSSH2_ERROR_EAGAIN); if (rc > 0) { bin->append(buffer, rc); b_read += rc; b_remaining -= rc; if (b_read >= size) break; continue; } if (!rc || rc == LIBSSH2_ERROR_EAGAIN) { rc = parent->waitSocketUnlocked(timeout_ms); if (!rc) { xsink->raiseException(SSH2CHANNEL_TIMEOUT, "read timeout after %dms reading %lld byte%s of %lld requested", timeout_ms, b_read, b_read == 1 ? "" : "s", size); return 0; } if (rc < 0) { xsink->raiseException(SSH2CHANNEL_TIMEOUT, strerror(errno)); return 0; } } } if (rc < 0 && rc != LIBSSH2_ERROR_EAGAIN) { parent->doSessionErrUnlocked(xsink); return 0; } return bin.release(); }
//============================================================================== void USB_Device::string_descriptor_ascii(uint8_t index, String &data) { check_open(); uint8_t raw_data[256]; int result = libusb_get_string_descriptor_ascii(handle_, index, raw_data, sizeof(raw_data)); if (result < 0) on_error("Failed to get string descriptor " + number_to_string(index), (libusb_error)result); data.assign(raw_data, raw_data + result); raw_data[result] = '\0'; log_info("usb, get string descriptor %u, data: %s", index, raw_data); }
int sfs_fwseek(int fileID, int loc){ if (check_open(fileID)){ if ((loc<0) || (loc >= inode_table[(fd_table[fileID].inode)].size)) { if ((loc==0) && (inode_table[(fd_table[fileID].inode)].size == 0)){ fd_table[fileID].wptr = loc; return 0; } return -1; //if the pointer is negative or greater than the file size } else { fd_table[fileID].wptr = loc; return 0; } } else { return -1; } }
void g_vfs_afp_connection_send_command (GVfsAfpConnection *afp_connection, GVfsAfpCommand *command, char *reply_buf, GAsyncReadyCallback callback, GCancellable *cancellable, gpointer user_data) { GVfsAfpConnectionPrivate *priv = afp_connection->priv; GError *err = NULL; RequestData *req_data; if (!check_open (afp_connection, &err)) { g_simple_async_report_take_gerror_in_idle (G_OBJECT(afp_connection), callback, user_data, err); return; } req_data = g_slice_new0 (RequestData); req_data->type = REQUEST_TYPE_COMMAND; req_data->command = g_object_ref (command); req_data->reply_buf = reply_buf; req_data->simple = g_simple_async_result_new (G_OBJECT (afp_connection), callback, user_data, g_vfs_afp_connection_send_command); req_data->conn = afp_connection; if (cancellable) req_data->cancellable = g_object_ref (cancellable); /* Take lock */ g_mutex_lock (&priv->mutex); g_queue_push_tail (priv->request_queue, req_data); if (!priv->send_loop_running) { g_main_context_invoke (priv->worker_context, start_send_loop_func, afp_connection); } /* Release lock */ g_mutex_unlock (&priv->mutex); }
int SSH2Channel::extendedDataIgnore(ExceptionSink *xsink, int timeout_ms) { AutoLocker al(parent->m); if (check_open(xsink)) return -1; int rc; while (true) { rc = libssh2_channel_handle_extended_data2(channel, LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE); if (rc == LIBSSH2_ERROR_EAGAIN) { if ((rc = parent->waitSocketUnlocked(xsink, SSH2CHANNEL_TIMEOUT, "SSH2CHANNEL-EXTENDEDDATAIGNORE-ERROR", "SSH2Channel::extendedDataIgnore", timeout_ms))) break; continue; } if (rc < 0) parent->doSessionErrUnlocked(xsink); break; } return rc; }
qore_size_t SSH2Channel::write(ExceptionSink *xsink, const void *buf, qore_size_t buflen, int stream_id, int timeout_ms) { assert(buflen); AutoLocker al(parent->m); if (check_open(xsink)) return -1; BlockingHelper bh(parent); qore_size_t b_sent = 0; while (true) { qore_offset_t rc; while (true) { rc = libssh2_channel_write_ex(channel, stream_id, (char *)buf + b_sent, buflen - b_sent); //printd(5, "SSH2Channel::write(len=%lu) buf=%p buflen=%lu stream_id=%d timeout_ms=%d rc=%ld b_sent=%lu\n", buflen - b_sent, buf, buflen, stream_id, timeout_ms, rc, b_sent); if (rc && rc != LIBSSH2_ERROR_EAGAIN) break; rc = parent->waitSocketUnlocked(timeout_ms); if (!rc) { xsink->raiseException(SSH2CHANNEL_TIMEOUT, "write timeout after %dms writing %lu byte%s of %lu", timeout_ms, b_sent, b_sent == 1 ? "" : "s", buflen); return -1; } if (rc < 0) { xsink->raiseException("SSH2CHANNEL-WRITE-ERROR", strerror(errno)); return -1; } } if (rc < 0) parent->doSessionErrUnlocked(xsink); b_sent += rc; if (b_sent >= buflen) break; } return b_sent; }
int SSH2Channel::shell(ExceptionSink *xsink, int timeout_ms) { AutoLocker al(parent->m); if (check_open(xsink)) return -1; BlockingHelper bh(parent); int rc; while (true) { rc = libssh2_channel_shell(channel); if (rc == LIBSSH2_ERROR_EAGAIN) { if ((rc = parent->waitSocketUnlocked(xsink, SSH2CHANNEL_TIMEOUT, "SSH2CHANNEL-SHELL-ERROR", "SSH2Channel::shell", timeout_ms))) break; continue; } if (rc) parent->doSessionErrUnlocked(xsink); break; } return rc; }
int SSH2Channel::requestPty(ExceptionSink *xsink, const QoreString &term, const QoreString &modes, int width, int height, int width_px, int height_px, int timeout_ms) { AutoLocker al(parent->m); if (check_open(xsink)) return -1; BlockingHelper bh(parent); int rc; while (true) { rc = libssh2_channel_request_pty_ex(channel, term.getBuffer(), term.strlen(), modes.strlen() ? modes.getBuffer() : 0, modes.strlen(), width, height, width_px, height_px); if (rc == LIBSSH2_ERROR_EAGAIN) { if ((rc = parent->waitSocketUnlocked(xsink, SSH2CHANNEL_TIMEOUT, "SSH2CHANNEL-REQUESTPTY-ERROR", "SSH2Channel::requestPty", timeout_ms))) break; continue; } if (rc) parent->doSessionErrUnlocked(xsink); break; } return rc; }
int SSH2Channel::requestX11Forwarding(ExceptionSink *xsink, int screen_number, bool single_connection, const char *auth_proto, const char *auth_cookie, int timeout_ms) { AutoLocker al(parent->m); if (check_open(xsink)) return -1; BlockingHelper bh(parent); //printd(5, "SSH2Channel::requestX11Forwarding() screen_no=%d, single=%s, ap=%s, ac=%s\n", screen_number, single_connection ? "true" : "false", auth_proto ? auth_proto : "n/a", auth_cookie ? auth_cookie : "n/a"); int rc; while (true) { rc = libssh2_channel_x11_req_ex(channel, (int)single_connection, auth_proto, auth_cookie, screen_number); if (rc == LIBSSH2_ERROR_EAGAIN) { if ((rc = parent->waitSocketUnlocked(xsink, SSH2CHANNEL_TIMEOUT, "SSH2CHANNEL-REQUESTX11FORWARDING-ERROR", "SSH2Channel::requestX11Forwarding", timeout_ms))) break; continue; } if (rc < 0) parent->doSessionErrUnlocked(xsink); break; } return rc; }
int SSH2Channel::setenv(const char *name, const char *value, int timeout_ms, ExceptionSink *xsink) { AutoLocker al(parent->m); if (check_open(xsink)) return -1; BlockingHelper bh(parent); int rc; while (true) { rc = libssh2_channel_setenv(channel, (char *)name, value); if (rc == LIBSSH2_ERROR_EAGAIN) { if ((rc = parent->waitSocketUnlocked(xsink, SSH2CHANNEL_TIMEOUT, "SSH2CHANNEL-SETENV-ERROR", "SSH2Channel::setenv", timeout_ms))) break; continue; } if (rc) parent->doSessionErrUnlocked(xsink); break; } return rc; }
void split_files(char* infile,char* hirom_name, char* lorom_name){ FILE* read_rom; FILE* hirom; FILE* lorom; unsigned char buff[1]; int split; read_rom = fopen(infile, "rb"); check_open(read_rom,infile); check_exists(hirom_name); check_exists(lorom_name); hirom = fopen(hirom_name, "wb"); lorom = fopen(lorom_name, "wb"); split = 0; while(fread(buff,sizeof(buff),1,read_rom)!=0){ if(split ==0) { fwrite(buff,sizeof(buff),1,hirom); split = 1; } else { fwrite(buff,sizeof(buff),1,lorom); split = 0; } } fclose(hirom); fclose(lorom); fclose(read_rom); printf("File %s is split into:\n",infile); printf("hirom: %s\n",hirom_name); printf("lorom: %s\n",lorom_name); }
int SSH2Channel::subsystem(const char *command, int timeout_ms, ExceptionSink *xsink) { AutoLocker al(parent->m); if (check_open(xsink)) return -1; BlockingHelper bh(parent); int rc; while (true) { rc = libssh2_channel_subsystem(channel, command); //printd(5, "SSH2Channel::subsystem() cmd=%s rc=%d\n", command, rc); if (rc == LIBSSH2_ERROR_EAGAIN) { if ((rc = parent->waitSocketUnlocked(xsink, SSH2CHANNEL_TIMEOUT, "SSH2CHANNEL-SUBSYSTEM-ERROR", "SSH2Channel::subsystem", timeout_ms))) break; continue; } if (rc) parent->doSessionErrUnlocked(xsink); break; } return rc; }
QoreStringNode *SSH2Channel::read(ExceptionSink *xsink, int stream_id, int timeout_ms) { AutoLocker al(parent->m); if (check_open(xsink)) return 0; QoreStringNodeHolder str(new QoreStringNode(enc)); BlockingHelper bh(parent); qore_offset_t rc; bool first = true; do { loop0: char buffer[QSSH2_BUFSIZE]; rc = libssh2_channel_read_ex(channel, stream_id, buffer, QSSH2_BUFSIZE); //printd(0, "SSH2Channel::read() rc=%ld (EAGAIN=%d)\n", rc, LIBSSH2_ERROR_EAGAIN); if (rc > 0) { str->concat(buffer, rc); } else if (rc == LIBSSH2_ERROR_EAGAIN && !str->strlen() && first) { first = false; if ((rc = parent->waitSocketUnlocked(xsink, SSH2CHANNEL_TIMEOUT, "SSH2CHANNEL-READ-ERROR", "SSH2Channel::read", timeout_ms))) return 0; goto loop0; } } while (rc > 0); if (rc < 0 && rc != LIBSSH2_ERROR_EAGAIN) { parent->doSessionErrUnlocked(xsink); return 0; } return str.release(); }
BinaryNode *SSH2Channel::readBinary(ExceptionSink *xsink, int stream_id, int timeout_ms) { AutoLocker al(parent->m); if (check_open(xsink)) return 0; SimpleRefHolder<BinaryNode> bin(new BinaryNode); BlockingHelper bh(parent); qore_offset_t rc; bool first = true; do { loop0: char buffer[QSSH2_BUFSIZE]; rc = libssh2_channel_read_ex(channel, stream_id, buffer, QSSH2_BUFSIZE); //printd(5, "SSH2Channel::readBinary() rc=%ld (EAGAIN=%d)\n", rc, LIBSSH2_ERROR_EAGAIN); if (rc > 0) { bin->append(buffer, rc); } else if (rc == LIBSSH2_ERROR_EAGAIN && !bin->size() && first) { first = false; if ((rc = parent->waitSocketUnlocked(xsink, SSH2CHANNEL_TIMEOUT, "SSH2CHANNEL-READBINARY-ERROR", "SSH2Channel::readBinary", timeout_ms))) return 0; goto loop0; } } while (rc > 0); if (rc < 0 && rc != LIBSSH2_ERROR_EAGAIN) { parent->doSessionErrUnlocked(xsink); return 0; } return bin.release(); }