static void set_hostname(void) { FILE* fp = fopen("/etc/hostname", "r"); if ( !fp && errno == ENOENT ) return; if ( !fp ) return warning("unable to set hostname: /etc/hostname: %m"); char* hostname = read_single_line(fp); fclose(fp); if ( !hostname ) return warning("unable to set hostname: /etc/hostname: %m"); int ret = sethostname(hostname, strlen(hostname) + 1); if ( ret < 0 ) warning("unable to set hostname: `%s': %m", hostname); free(hostname); }
static void set_videomode(void) { FILE* fp = fopen("/etc/videomode", "r"); if ( !fp && errno == ENOENT ) return; if ( !fp ) return warning("unable to set video mode: /etc/videomode: %m"); char* videomode = read_single_line(fp); fclose(fp); if ( !videomode ) return warning("unable to set video mode: /etc/videomode: %m"); unsigned int xres = 0; unsigned int yres = 0; unsigned int bpp = 0; if ( sscanf(videomode, "%ux%ux%u", &xres, &yres, &bpp) != 3 ) { warning("/etc/videomode: Invalid video mode `%s'", videomode); free(videomode); return; } free(videomode); struct dispmsg_set_crtc_mode set_mode; memset(&set_mode, 0, sizeof(set_mode)); set_mode.msgid = DISPMSG_SET_CRTC_MODE; set_mode.device = 0; set_mode.connector = 0; set_mode.mode.driver_index = 0; set_mode.mode.magic = 0; set_mode.mode.control = DISPMSG_CONTROL_VALID; set_mode.mode.fb_format = bpp; set_mode.mode.view_xres = xres; set_mode.mode.view_yres = yres; set_mode.mode.fb_location = 0; set_mode.mode.pitch = xres * (bpp / 8); set_mode.mode.surf_off_x = 0; set_mode.mode.surf_off_y = 0; set_mode.mode.start_x = 0; set_mode.mode.start_y = 0; set_mode.mode.end_x = 0; set_mode.mode.end_y = 0; set_mode.mode.desktop_height = yres; if ( dispmsg_issue(&set_mode, sizeof(set_mode)) < 0 ) warning("/etc/videomode: Failed to set video mode `%ux%ux%u': %m", xres, yres, bpp); }
void CSVFileParser::GetNextRow(CSVFileRow &row) { char *buffer = NULL; unsigned int buffer_length = 0; row.clear(); if(!bhas_more_line_ && pfile_) return; //skip rows that start with ';' do { read_single_line(&buffer, &buffer_length); parse_single_line(buffer, buffer_length, row); ++ireaded_line_count_; free(buffer); } while (bhas_more_line_ && row.size() > 0 && row[0].size() > 0 && row[0][0] == ';'); }
bool CSVFileParser::InitWithFileName(const char *my_fileName) { CCLog("Load csv file %s", my_fileName); if(my_fileName == NULL) return false; std::string file_name = CSV_DATA_PATH; file_name += my_fileName; std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(file_name.c_str()); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) unsigned long nSize = 0; unsigned char* pBuffer = cocos2d::CCFileUtils::sharedFileUtils()-> getFileData(fullPath.c_str(), "rb", &nSize); pfile_ = fmemopen(pBuffer, nSize, "rb"); #else pfile_ = fopen(fullPath.c_str(), "rb"); #endif if(pfile_ == NULL) { CCLog("Error: unable open file %s\n", fullPath.c_str()); return false; } bhas_more_line_ = !(fgetc(pfile_) == EOF); rewind(pfile_); ireaded_line_count_ = 0; //skip line if(iskip_line_count_ > 0) { char *buffer = NULL; unsigned int buffer_length = 0; while (bhas_more_line_ && ireaded_line_count_ < iskip_line_count_) { read_single_line(&buffer, &buffer_length); ++ireaded_line_count_; } } return true; }
int main(int argc, const char *argv[]) { //Echoes everything written via stdin to stdout. //A blank line ends exits the program. for(;;) { char* line; bool done; line = read_single_line(); done = (strncmp(line, "\n") == 0); if (!done) printf("%s", line); free(line); if (done) return 0; } return 0; }
static void set_kblayout(void) { FILE* fp = fopen("/etc/kblayout", "r"); if ( !fp && errno == ENOENT ) return; if ( !fp ) return warning("unable to set keyboard layout: /etc/kblayout: %m"); char* kblayout = read_single_line(fp); fclose(fp); if ( !kblayout ) return warning("unable to set keyboard layout: /etc/kblayout: %m"); pid_t child_pid = fork(); if ( child_pid < 0 ) return warning("unable to set keyboard layout: fork: %m"); if ( !child_pid ) { execlp("chkblayout", "chkblayout", "--", kblayout, (char*) NULL); warning("setting keyboard layout: chkblayout: %m"); _exit(127); } int status; waitpid(child_pid, &status, 0); free(kblayout); }
void *client_handler(void *void_args) { user_session_t *session = (user_session_t *) void_args; status_t error; //Finish session initialization session->directory = realpath(".", NULL); if (session->directory == NULL) { error = REALPATH_ERROR; goto exit0; } session->data_sock = -1; //End session initialization error = send_response(session->command_sock, SERVICE_READY, "Ready. Please send USER.", session->server->log, 0); if (error) { goto exit1; } string_t command; string_initialize(&command); uint8_t done = 0; do { char_vector_clear(&command); error = read_single_line(session->command_sock, &command); if (!error) { error = write_received_message_to_log(session->server->log, &command); if (!error) { //Remove the CRLF from the command char_vector_pop_back(&command); char_vector_pop_back(&command); //Split the string up by spaces size_t len; string_t *split = string_split_skip_consecutive(&command, ' ', &len, 1); char *c_str = string_c_str(split + 0); //Determine which command has been sent if (bool_strcmp(c_str, "USER")) { error = handle_user_command(session, split, len); } else if (bool_strcmp(c_str, "PASS")) { error = handle_pass_command(session, split, len); } else if (bool_strcmp(c_str, "CWD")) { error = handle_cwd_command(session, split, len); } else if (bool_strcmp(c_str, "CDUP")) { error = handle_cdup_command(session, split, len); } else if (bool_strcmp(c_str, "QUIT")) { done = 1; error = handle_quit_command(session, split, len); } else if (bool_strcmp(c_str, "PASV")) { error = handle_pasv_command(session, split, len); } else if (bool_strcmp(c_str, "EPSV")) { error = handle_epsv_command(session, split, len); } else if (bool_strcmp(c_str, "PORT")) { error = handle_port_command(session, split, len); } else if (bool_strcmp(c_str, "EPRT")) { error = handle_eprt_command(session, split, len); } else if (bool_strcmp(c_str, "RETR")) { error = handle_retr_command(session, split, len); } else if (bool_strcmp(c_str, "PWD")) { error = handle_pwd_command(session, split, len); } else if (bool_strcmp(c_str, "LIST")) { error = handle_list_command(session, split, len); } else if (bool_strcmp(c_str, "HELP")) { error = handle_help_command(session, split, len); } else { error = handle_unrecognized_command(session, split, len); } size_t i; for (i = 0; i < len; i++) { string_uninitialize(split + i); } free(split); } } if (error) { char message[] = "Error encountered while processing: "; string_t error_message; string_initialize(&error_message); char *error_string = get_error_message(error); string_assign_from_char_array(&error_message, error_string); prepend_and_write_to_log(session->server->log, &error_message, message, sizeof message); string_uninitialize(&error_message); } } while (!error && !done); char quitting_message[] = "Client quitting.\n"; exit2: string_uninitialize(&command); exit1: free(session->directory); exit0: write_log(session->server->log, quitting_message, sizeof quitting_message); printf("%s", quitting_message); free(session); pthread_exit(NULL); }
int main(int argc, char* argv[]) { main_pid = getpid(); setlocale(LC_ALL, ""); const char* target = NULL; const char* argv0 = argv[0]; for ( int i = 1; i < argc; i++ ) { const char* arg = argv[i]; if ( arg[0] != '-' || !arg[1] ) continue; argv[i] = NULL; if ( !strcmp(arg, "--") ) break; if ( arg[1] != '-' ) { char c; while ( (c = *++arg) ) switch ( c ) { default: fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c); help(stderr, argv0); exit(1); } } else if ( !strncmp(arg, "--target=", strlen("--target=")) ) target = arg + strlen("--target="); else if ( !strcmp(arg, "--target") ) { if ( i + 1 == argc ) { error(0, 0, "option '--target' requires an argument"); fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); exit(125); } target = argv[i+1]; argv[++i] = NULL; } else if ( !strcmp(arg, "--help") ) help(stdout, argv0), exit(0); else if ( !strcmp(arg, "--version") ) version(stdout, argv0), exit(0); else { fprintf(stderr, "%s: unknown option: %s\n", argv0, arg); help(stderr, argv0); exit(1); } } compact_arguments(&argc, &argv); char* target_string = NULL; if ( !target ) { const char* target_path = "/etc/init/target"; if ( access(target_path, F_OK) == 0 ) { FILE* target_fp = fopen(target_path, "r"); if ( !target_fp ) fatal("%s: %m", target_path); target_string = read_single_line(target_fp); if ( !target_string ) fatal("read: %s: %m", target_path); target = target_string; fclose(target_fp); } else fatal("Refusing to initialize because %s doesn't exist", target_path); } if ( getenv("INIT_PID") ) fatal("System is already managed by an init process"); if ( !strcmp(target, "single-user") || !strcmp(target, "multi-user") || !strcmp(target, "sysinstall") || !strcmp(target, "sysupgrade") ) return init(target); if ( !strcmp(target, "chain") || !strcmp(target, "chain-merge") ) return init_chain(target); fatal("Unknown initialization target `%s'", target); }