config::attribute_value &config::attribute_value::operator=(const std::string &v) { // Handle some special strings. if (v.empty()) { value_ = v; return *this; } if ( v == s_yes ) { value_ = yes_no(true); return *this; } if ( v == s_no ) { value_ = yes_no(false); return *this; } if ( v == s_true ) { value_ = true_false(true); return *this; } if ( v == s_false ) { value_ = true_false(false); return *this; } // Attempt to convert to a number. char *eptr; double d = strtod(v.c_str(), &eptr); if ( *eptr == '\0' ) { // Possibly a number. See what type it should be stored in. // (All conversions will be from the string since the largest integer // type could have more precision than a double.) if ( d > 0.0 ) { // The largest type for positive integers is unsigned long long. unsigned long long ull = 0; if ( from_string_verify<unsigned long long>(v, ull) ) return *this = ull; } else { // The largest (variant) type for negative integers is int. int i = 0; if ( from_string_verify<int>(v, i) ) return *this = i; } // This does not look like an integer, so it should be a double. // However, make sure it can convert back to the same string (in // case this is a string that just looks like a numeric value). std::ostringstream tester; tester << d; if ( tester.str() == v ) { value_ = d; return *this; } } // No conversion possible. Store the string. value_ = v; return *this; }
static void emit_configuration (MateRRConfig *config, GString *string) { int j; g_string_append_printf (string, " <configuration>\n"); g_string_append_printf (string, " <clone>%s</clone>\n", yes_no (config->clone)); for (j = 0; config->outputs[j] != NULL; ++j) { MateOutputInfo *output = config->outputs[j]; g_string_append_printf ( string, " <output name=\"%s\">\n", output->name); if (output->connected && *output->vendor != '\0') { g_string_append_printf ( string, " <vendor>%s</vendor>\n", output->vendor); g_string_append_printf ( string, " <product>0x%04x</product>\n", output->product); g_string_append_printf ( string, " <serial>0x%08x</serial>\n", output->serial); } /* An unconnected output which is on does not make sense */ if (output->connected && output->on) { g_string_append_printf ( string, " <width>%d</width>\n", output->width); g_string_append_printf ( string, " <height>%d</height>\n", output->height); g_string_append_printf ( string, " <rate>%d</rate>\n", output->rate); g_string_append_printf ( string, " <x>%d</x>\n", output->x); g_string_append_printf ( string, " <y>%d</y>\n", output->y); g_string_append_printf ( string, " <rotation>%s</rotation>\n", get_rotation_name (output->rotation)); g_string_append_printf ( string, " <reflect_x>%s</reflect_x>\n", get_reflect_x (output->rotation)); g_string_append_printf ( string, " <reflect_y>%s</reflect_y>\n", get_reflect_y (output->rotation)); g_string_append_printf ( string, " <primary>%s</primary>\n", yes_no (output->primary)); } g_string_append_printf (string, " </output>\n"); } g_string_append_printf (string, " </configuration>\n"); }
int main() { char question[80]; char suspect[20]; node *start_node = create("Does suspect have a mustache"); start_node->no = create("Loretta Barnsworth"); start_node->yes = create("Vinny the Spoon"); node *current; do { current = start_node; while (1) { if (yes_no(current->question)) { if (current->yes) { current = current->yes; } else { printf("SUSPECT IDENTIFIED\n"); break; } } else if (current->no) { current = current->no; } else { /* Make the yes-node the new suspect name */ printf("Who's the suspect? "); fgets(suspect, 20, stdin); node *yes_node = create(suspect); current->yes = yes_node; /* Make the no-node a copy of this question */ node *no_node = create(current->question); current->no = no_node; /* Then replace this question with the new question */ printf("Give me a question that is TRUE for %s but not for %s? ", suspect, current->question); fgets(question, 80, stdin); free(current->question); current->question = strdup(question); break; } } }while(yes_no("Run again")); release(start_node); return 0; }
void kill_context_dump(KillContext *c, FILE *f, const char *prefix) { assert(c); prefix = strempty(prefix); fprintf(f, "%sKillMode: %s\n" "%sKillSignal: SIG%s\n" "%sSendSIGKILL: %s\n" "%sSendSIGHUP: %s\n", prefix, kill_mode_to_string(c->kill_mode), prefix, signal_to_string(c->kill_signal), prefix, yes_no(c->send_sigkill), prefix, yes_no(c->send_sighup)); }
int main(int argc, char *argv[]) { int r; bool run_ambient; test_last_cap_file(); test_last_cap_probe(); log_parse_environment(); log_open(); log_info("have ambient caps: %s", yes_no(ambient_capabilities_supported())); if (getuid() != 0) return EXIT_TEST_SKIP; r = setup_tests(&run_ambient); if (r < 0) return -r; show_capabilities(); test_drop_privileges(); test_update_inherited_set(); fork_test(test_have_effective_cap); if (run_ambient) fork_test(test_set_ambient_caps); return 0; }
void CommandGetParents::show_arguments() { show_single_input_arguments(m_vout); show_output_arguments(m_vout); m_vout << " other options:\n"; m_vout << " add self: " << yes_no(m_add_self); m_vout << " default object type: " << osmium::item_type_to_name(m_default_item_type) << "\n"; if (m_verbose_ids) { m_vout << " looking for these ids:\n"; m_vout << " nodes:"; for (osmium::object_id_type id : m_ids(osmium::item_type::node)) { m_vout << " " << id; } m_vout << "\n"; m_vout << " ways:"; for (osmium::object_id_type id : m_ids(osmium::item_type::way)) { m_vout << " " << id; } m_vout << "\n"; m_vout << " relations:"; for (osmium::object_id_type id : m_ids(osmium::item_type::relation)) { m_vout << " " << id; } m_vout << "\n"; } else { m_vout << " looking for " << m_ids(osmium::item_type::node).size() << " node ID(s), " << m_ids(osmium::item_type::way).size() << " way ID(s), and " << m_ids(osmium::item_type::relation).size() << " relation ID(s)\n"; } }
int main(int argc, char *argv[]) { uid_t shift, range; int r; log_set_max_level(LOG_DEBUG); log_parse_environment(); log_open(); if (argc != 4) { log_error("Expected PATH SHIFT RANGE parameters."); return EXIT_FAILURE; } r = parse_uid(argv[2], &shift); if (r < 0) { log_error_errno(r, "Failed to parse UID shift %s.", argv[2]); return EXIT_FAILURE; } r = parse_gid(argv[3], &range); if (r < 0) { log_error_errno(r, "Failed to parse UID range %s.", argv[3]); return EXIT_FAILURE; } r = path_patch_uid(argv[1], shift, range); if (r < 0) { log_error_errno(r, "Failed to patch directory tree: %m"); return EXIT_FAILURE; } log_info("Changed: %s", yes_no(r)); return EXIT_SUCCESS; }
static void print_source(uint64_t flags, usec_t rtt) { char rtt_str[FORMAT_TIMESTAMP_MAX]; if (!arg_legend) return; if (flags == 0) return; fputs("\n-- Information acquired via", stdout); if (flags != 0) printf(" protocol%s%s%s%s%s", flags & SD_RESOLVED_DNS ? " DNS" :"", flags & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "", flags & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "", flags & SD_RESOLVED_MDNS_IPV4 ? "mDNS/IPv4" : "", flags & SD_RESOLVED_MDNS_IPV6 ? "mDNS/IPv6" : ""); assert_se(format_timespan(rtt_str, sizeof(rtt_str), rtt, 100)); printf(" in %s", rtt_str); fputc('.', stdout); fputc('\n', stdout); printf("-- Data is authenticated: %s\n", yes_no(flags & SD_RESOLVED_AUTHENTICATED)); }
int main() { char question[80]; char suspect[20]; node *start_node = create("suspect have bear?"); start_node->no = create("loletta.barnsworse"); start_node->yes = create("benny.the.spoon"); node *current; do{ current = start_node; while(1){ if(yes_no(current->question)) { if(current->yes){ current = current->yes; }else{ printf("found!\n"); break; } }else if(current->no){ current = current->no; }else{ /*make yes->node name by suspect*/ printf("who is criminal?"); fgets(suspect,20,stdin); node *yes_node = create(suspect); current->yes = yes_node; /*cpoy no_node this question*/ node *no_node = create(current->question); current->no = no_node; /*change this question new question*/ printf("This is true in %s, that question does not apply to the %s?\n" ,suspect,current->question); fgets(question,80,stdin); current->question = strdup(question); break; } } }while(yes_no("one more?")); release(start_node); return 0; }
int main() { char question[80]; char suspect[20]; node *start_node = create("容疑者はひげを生やしているか"); start_node->no = create("ロレッタ・バーンスワース"); start_node->yes = create("ベニー・ザ・スプーン"); node *current; do{ current = start_node; while(1){ if(yes_no(current->question)) { if(current->yes){ current = current->yes; }else{ printf("容疑者判明\n"); break; } }else if(current->no){ current = current->no; }else{ /* yes ノードを新しい容疑者名にする */ printf("容疑者は誰?"); fgets(suspect,20,stdin); node *yes_node = create(suspect); current->yes = yes_node; /* no ノードをこの質問のコピーにする */ node *no_node = create(current->question); current->no = no_node; /* この質問を新しい質問に置き換える */ printf("%sに当てはまり、%sに当てはまらない質問は?",suspect, current->question); fgets(question,80,stdin); free(current->question); current->question = strdup(question); break; } } }while(yes_no("再実行しますか")); release(start_node); return 0; }
static void test_is_wanted_print(bool header) { _cleanup_free_ char *cmdline = NULL; log_info("-- %s --", __func__); assert_se(proc_cmdline(&cmdline) >= 0); log_info("cmdline: %s", cmdline); if (header) { log_info(_CGROUP_HIEARCHY_); (void) system("findmnt -n /sys/fs/cgroup"); } log_info("is_unified_wanted() → %s", yes_no(cg_is_unified_wanted())); log_info("is_hybrid_wanted() → %s", yes_no(cg_is_hybrid_wanted())); log_info("is_legacy_wanted() → %s", yes_no(cg_is_legacy_wanted())); log_info(" "); }
int main(int argc, char const *argv[]) { char question[80]; char suspect[40]; node *start_node = create("Носит ли подозреваемый усы?"); start_node->no = create("Лоррета Барншворц"); start_node->yes = create("Иван Ложкин"); node *current; do { current = start_node; while (1) { if (yes_no(current->question)) { if (current->yes){ current = current->yes; } else { printf("Подозреваемый опознан!\n"); break; } } else if (current->no) { current = current->no; } else { printf("Кто подозреваемый? "); fgets(suspect, 40, stdin); node *yes_node = create(suspect); current->yes = yes_node; node *no_node = create(current->question); current->no = no_node; printf("Задайте мне вопрос, который подходит для %s, но не для %s ", suspect, current->question); fgets(question, 80, stdin); free(current->question); current->question = strdup(question); break; } } } while (yes_no("Выполнить еще раз")); release(start_node); return 0; }
FILE * tbl_open(int tbl, char *mode) { char prompt[256]; char fullpath[256]; FILE *f; struct stat fstats; int retcode; if (*tdefs[tbl].name == PATH_SEP) strcpy(fullpath, tdefs[tbl].name); else sprintf(fullpath, "%s%c%s", env_config(PATH_TAG, PATH_DFLT), PATH_SEP, tdefs[tbl].name); retcode = stat(fullpath, &fstats); if (retcode && (errno != ENOENT)) { fprintf(stderr, "stat(%s) failed.\n", fullpath); exit(-1); } if (S_ISREG(fstats.st_mode) && !force && *mode != 'r' ) { sprintf(prompt, "Do you want to overwrite %s ?", fullpath); if (!yes_no(prompt)) exit(0); } if (S_ISFIFO(fstats.st_mode)) { retcode = open(fullpath, ((*mode == 'r')?O_RDONLY:O_WRONLY)|O_CREAT); f = fdopen(retcode, mode); } else{ #ifdef LINUX /* allow large files on Linux */ /*use open to first to get the in fd and apply regular fdopen*/ /*cheng: Betty mentioned about write mode problem here, added 066*/ retcode = open(fullpath, ((*mode == 'r')?O_RDONLY:O_WRONLY)|O_CREAT|O_LARGEFILE,0644); f = fdopen(retcode, mode); #else f = fopen(fullpath, mode); #endif } OPEN_CHECK(f, fullpath); if (header && columnar && tdefs[tbl].header != NULL) tdefs[tbl].header(f); return (f); }
static void test_mask_supported(void) { CGroupMask m; CGroupController c; assert_se(cg_mask_supported(&m) >= 0); for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) printf("'%s' is supported: %s\n", cgroup_controller_to_string(c), yes_no(m & CGROUP_CONTROLLER_TO_MASK(c))); }
static int scope_serialize(Unit *u, FILE *f, FDSet *fds) { Scope *s = SCOPE(u); assert(s); assert(f); assert(fds); unit_serialize_item(u, f, "state", scope_state_to_string(s->state)); unit_serialize_item(u, f, "was-abandoned", yes_no(s->was_abandoned)); return 0; }
static void test_conf_files_list(bool use_root) { char tmp_dir[] = "/tmp/test-conf-files-XXXXXX"; _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL; const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c, *mask; log_debug("/* %s(%s) */", __func__, yes_no(use_root)); setup_test_dir(tmp_dir, "/dir1/a.conf", "/dir2/a.conf", "/dir2/b.conf", "/dir2/c.foo", "/dir2/d.conf", NULL); mask = strjoina(tmp_dir, "/dir1/d.conf"); assert_se(symlink("/dev/null", mask) >= 0); if (use_root) { root_dir = tmp_dir; search_1 = "/dir1"; search_2 = "/dir2"; } else { root_dir = NULL; search_1 = strjoina(tmp_dir, "/dir1"); search_2 = strjoina(tmp_dir, "/dir2"); } expect_a = strjoina(tmp_dir, "/dir1/a.conf"); expect_b = strjoina(tmp_dir, "/dir2/b.conf"); expect_c = strjoina(tmp_dir, "/dir2/c.foo"); log_debug("/* Check when filtered by suffix */"); assert_se(conf_files_list(&found_files, ".conf", root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0); strv_print(found_files); assert_se(found_files); assert_se(streq_ptr(found_files[0], expect_a)); assert_se(streq_ptr(found_files[1], expect_b)); assert_se(!found_files[2]); log_debug("/* Check when unfiltered */"); assert_se(conf_files_list(&found_files2, NULL, root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0); strv_print(found_files2); assert_se(found_files2); assert_se(streq_ptr(found_files2[0], expect_a)); assert_se(streq_ptr(found_files2[1], expect_b)); assert_se(streq_ptr(found_files2[2], expect_c)); assert_se(!found_files2[3]); assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); }
const char *address_family_boolean_to_string(AddressFamilyBoolean b) { if (b == ADDRESS_FAMILY_YES || b == ADDRESS_FAMILY_NO) return yes_no(b == ADDRESS_FAMILY_YES); if (b == ADDRESS_FAMILY_IPV4) return "ipv4"; if (b == ADDRESS_FAMILY_IPV6) return "ipv6"; return NULL; }
static void busname_dump(Unit *u, FILE *f, const char *prefix) { BusName *n = BUSNAME(u); assert(n); assert(f); fprintf(f, "%sBus Name State: %s\n" "%sResult: %s\n" "%sName: %s\n" "%sActivating: %s\n" "%sAccept FD: %s\n", prefix, busname_state_to_string(n->state), prefix, busname_result_to_string(n->result), prefix, n->name, prefix, yes_no(n->activating), prefix, yes_no(n->accept_fd)); if (n->control_pid > 0) fprintf(f, "%sControl PID: "PID_FMT"\n", prefix, n->control_pid); }
void CloseFidonet(int force) { char fin[PATH_MAX], fout[PATH_MAX], temp[10]; FILE *fi, *fo; st_list *fid = NULL, *tmp; snprintf(fin, PATH_MAX, "%s/etc/fidonet.data", getenv("MBSE_ROOT")); snprintf(fout, PATH_MAX, "%s/etc/fidonet.temp", getenv("MBSE_ROOT")); if (FidoUpdated == 1) { if (force || (yes_no((char *)"Database is changed, save changes") == 1)) { working(1, 0, 0); exp_golded = TRUE; fi = fopen(fout, "r"); fo = fopen(fin, "w"); fread(&fidonethdr, fidonethdr.hdrsize, 1, fi); fwrite(&fidonethdr, fidonethdr.hdrsize, 1, fo); while (fread(&fidonet, fidonethdr.recsize, 1, fi) == 1) if (!fidonet.deleted) { snprintf(temp, 10, "%05d", fidonet.zone[0]); fill_stlist(&fid, temp, ftell(fi) - fidonethdr.recsize); } sort_stlist(&fid); for (tmp = fid; tmp; tmp = tmp->next) { fseek(fi, tmp->pos, SEEK_SET); fread(&fidonet, fidonethdr.recsize, 1, fi); fwrite(&fidonet, fidonethdr.recsize, 1, fo); } fclose(fi); fclose(fo); unlink(fout); tidy_stlist(&fid); chmod(fin, 0640); Syslog('+', "Updated \"fidonet.data\""); if (!force) working(6, 0, 0); return; } } chmod(fin, 0640); working(1, 0, 0); unlink(fout); }
FILE * tbl_open(int tbl, char *mode) { char prompt[256]; char fullpath[256]; #ifdef MYRIAD_NO_OUTPUT strcpy(fullpath, "/dev/null"); #endif /* MYRIAD_NO_OUTPUT */ FILE *f; struct stat fstats; int retcode; // DO NOT CREATE FILES FOR THE TABLES #ifndef MYRIAD_NO_OUTPUT if (*tdefs[tbl].name == PATH_SEP) strcpy(fullpath, tdefs[tbl].name); else sprintf(fullpath, "%s%c%s", env_config(PATH_TAG, PATH_DFLT), PATH_SEP, tdefs[tbl].name); #endif /* MYRIAD_NO_OUTPUT */ retcode = stat(fullpath, &fstats); if (retcode) { if (errno != ENOENT) { fprintf(stderr, "stat(%s) failed.\n", fullpath); exit(-1); } else f = fopen(fullpath, mode); // create and open the file } else { /* note this code asumes we are writing but tests if mode == r -jrg */ if (S_ISREG(fstats.st_mode) && !force && *mode != 'r' ) { sprintf(prompt, "Do you want to overwrite %s ?", fullpath); if (!yes_no(prompt)) exit(0); f = fopen(fullpath, mode); } else if (S_ISFIFO(fstats.st_mode)) { retcode = open(fullpath, ((*mode == 'r')?O_RDONLY:O_WRONLY)|O_CREAT, 0664); f = fdopen(retcode, mode); } else f = fopen(fullpath, mode); } OPEN_CHECK(f, fullpath); return (f); }
void CloseMGroup(int force) { char fin[PATH_MAX], fout[PATH_MAX]; FILE *fi, *fo; st_list *mgr = NULL, *tmp; free(gedgrps); snprintf(fin, PATH_MAX, "%s/etc/mgroups.data", getenv("MBSE_ROOT")); snprintf(fout, PATH_MAX, "%s/etc/mgroups.temp", getenv("MBSE_ROOT")); if (MGrpUpdated == 1) { if (force || (yes_no((char *)"Database is changed, save changes") == 1)) { working(1, 0, 0); fi = fopen(fout, "r"); fo = fopen(fin, "w"); fread(&mgrouphdr, mgrouphdr.hdrsize, 1, fi); fwrite(&mgrouphdr, mgrouphdr.hdrsize, 1, fo); while (fread(&mgroup, mgrouphdr.recsize, 1, fi) == 1) if (!mgroup.Deleted) fill_stlist(&mgr, mgroup.Name, ftell(fi) - mgrouphdr.recsize); sort_stlist(&mgr); for (tmp = mgr; tmp; tmp = tmp->next) { fseek(fi, tmp->pos, SEEK_SET); fread(&mgroup, mgrouphdr.recsize, 1, fi); fwrite(&mgroup, mgrouphdr.recsize, 1, fo); } tidy_stlist(&mgr); fclose(fi); fclose(fo); unlink(fout); chmod(fin, 0660); disk_reset(); Syslog('+', "Updated \"mgroups.data\""); if (!force) working(6, 0, 0); exp_golded = TRUE; return; } } chmod(fin, 0660); working(1, 0, 0); unlink(fout); }
static void test_mount_points_list(const char *fname) { _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head); MountPoint *m; log_info("/* %s(\"%s\") */", __func__, fname ?: "/proc/self/mountinfo"); LIST_HEAD_INIT(mp_list_head); assert_se(mount_points_list_get(fname, &mp_list_head) >= 0); LIST_FOREACH(mount_point, m, mp_list_head) log_debug("path=%s o=%s f=0x%lx try-ro=%s dev=%u:%u", m->path, strempty(m->remount_options), m->remount_flags, yes_no(m->try_remount_ro), major(m->devnum), minor(m->devnum)); }
void CloseHatch(int force) { char fin[PATH_MAX], fout[PATH_MAX]; FILE *fi, *fo; st_list *hat = NULL, *tmp; snprintf(fin, PATH_MAX, "%s/etc/hatch.data", getenv("MBSE_ROOT")); snprintf(fout, PATH_MAX, "%s/etc/hatch.temp", getenv("MBSE_ROOT")); if (HatchUpdated == 1) { if (force || (yes_no((char *)"Database is changed, save changes") == 1)) { working(1, 0, 0); fi = fopen(fout, "r"); fo = fopen(fin, "w"); fread(&hatchhdr, hatchhdr.hdrsize, 1, fi); fwrite(&hatchhdr, hatchhdr.hdrsize, 1, fo); while (fread(&hatch, hatchhdr.recsize, 1, fi) == 1) if (!hatch.Deleted) fill_stlist(&hat, hatch.Spec, ftell(fi) - hatchhdr.recsize); sort_stlist(&hat); for (tmp = hat; tmp; tmp = tmp->next) { fseek(fi, tmp->pos, SEEK_SET); fread(&hatch, hatchhdr.recsize, 1, fi); fwrite(&hatch, hatchhdr.recsize, 1, fo); } tidy_stlist(&hat); fclose(fi); fclose(fo); unlink(fout); chmod(fin, 0640); disk_reset(); Syslog('+', "Updated \"hatch.data\""); if (!force) working(6, 0, 0); return; } } chmod(fin, 0640); working(1, 0, 0); unlink(fout); }
void CloseMagics(int force) { char fin[PATH_MAX], fout[PATH_MAX]; FILE *fi, *fo; st_list *mag = NULL, *tmp; snprintf(fin, PATH_MAX, "%s/etc/magic.data", getenv("FTND_ROOT")); snprintf(fout, PATH_MAX, "%s/etc/magic.temp", getenv("FTND_ROOT")); if (MagicUpdated == 1) { if (force || (yes_no((char *)"Database is changed, save changes") == 1)) { working(1, 0, 0); fi = fopen(fout, "r"); fo = fopen(fin, "w"); fread(&magichdr, magichdr.hdrsize, 1, fi); fwrite(&magichdr, magichdr.hdrsize, 1, fo); while (fread(&magic, magichdr.recsize, 1, fi) == 1) if (!magic.Deleted) fill_stlist(&mag, magic.Mask, ftell(fi) - magichdr.recsize); sort_stlist(&mag); for (tmp = mag; tmp; tmp = tmp->next) { fseek(fi, tmp->pos, SEEK_SET); fread(&magic, magichdr.recsize, 1, fi); fwrite(&magic, magichdr.recsize, 1, fo); } fclose(fi); fclose(fo); tidy_stlist(&mag); unlink(fout); chmod(fin, 0640); disk_reset(); Syslog('+', "Updated \"magic.data\""); if (!force) working(6, 0, 0); return; } } chmod(fin, 0640); working(1, 0, 0); unlink(fout); }
FILE * tbl_open(int tbl, char *mode) { char prompt[256]; char fullpath[256]; FILE *f; struct stat fstats; int retcode; if (*tdefs[tbl].name == PATH_SEP) strcpy(fullpath, tdefs[tbl].name); else sprintf(fullpath, "%s%c%s", env_config(PATH_TAG, PATH_DFLT), PATH_SEP, tdefs[tbl].name); retcode = stat(fullpath, &fstats); if (retcode && (errno != ENOENT)) { fprintf(stderr, "stat(%s) failed.\n", fullpath); exit(-1); } if (S_ISREG(fstats.st_mode) && !force && *mode != 'r' ) { sprintf(prompt, "Do you want to overwrite %s ?", fullpath); if (!yes_no(prompt)) exit(0); } if (S_ISFIFO(fstats.st_mode)) { retcode = open(fullpath, ((*mode == 'r')?O_RDONLY:O_WRONLY)|O_CREAT); f = fdopen(retcode, mode); } else f = fopen(fullpath, mode); OPEN_CHECK(f, fullpath); if (header && columnar && tdefs[tbl].header != NULL) tdefs[tbl].header(f); return (f); }
void CloseIBC(int force) { char fin[PATH_MAX], fout[PATH_MAX]; FILE *fi, *fo; st_list *vir = NULL, *tmp; snprintf(fin, PATH_MAX, "%s/etc/ibcsrv.data", getenv("FTND_ROOT")); snprintf(fout, PATH_MAX, "%s/etc/ibcsrv.temp", getenv("FTND_ROOT")); if (IBCUpdated == 1) { if (force || (yes_no((char *)"Database is changed, save changes") == 1)) { working(1, 0, 0); fi = fopen(fout, "r"); fo = fopen(fin, "w"); fread(&ibcsrvhdr, ibcsrvhdr.hdrsize, 1, fi); fwrite(&ibcsrvhdr, ibcsrvhdr.hdrsize, 1, fo); while (fread(&ibcsrv, ibcsrvhdr.recsize, 1, fi) == 1) if (!ibcsrv.Deleted) fill_stlist(&vir, ibcsrv.comment, ftell(fi) - ibcsrvhdr.recsize); sort_stlist(&vir); for (tmp = vir; tmp; tmp = tmp->next) { fseek(fi, tmp->pos, SEEK_SET); fread(&ibcsrv, ibcsrvhdr.recsize, 1, fi); fwrite(&ibcsrv, ibcsrvhdr.recsize, 1, fo); } tidy_stlist(&vir); fclose(fi); fclose(fo); unlink(fout); chmod(fin, 0640); Syslog('+', "Updated \"ibcsrv.data\""); if (!force) working(6, 0, 0); return; } } chmod(fin, 0640); unlink(fout); }
void CloseOneline(int force) { char fin[PATH_MAX], fout[PATH_MAX]; snprintf(fin, PATH_MAX, "%s/etc/oneline.data", getenv("FTND_ROOT")); snprintf(fout, PATH_MAX, "%s/etc/oneline.temp", getenv("FTND_ROOT")); if (OnelUpdated == 1) { if (force || (yes_no((char *)"Database is changed, save changes") == 1)) { working(1, 0, 0); if ((rename(fout, fin)) == 0) unlink(fout); chmod(fin, 0660); Syslog('+', "Updated \"oneline.data\""); if (!force) working(6, 0, 0); return; } } chmod(fin, 0660); working(1, 0, 0); unlink(fout); }
config::attribute_value &config::attribute_value::operator=(bool v) { value_ = yes_no(v); return *this; }
int main(int argc, char *argv[]) { int r; Hashmap *a = NULL, *b = NULL; unsigned iteration = 0; usec_t last_refresh = 0; bool quit = false, immediate_refresh = false; _cleanup_free_ char *root = NULL; CGroupMask mask; log_parse_environment(); log_open(); r = cg_mask_supported(&mask); if (r < 0) { log_error_errno(r, "Failed to determine supported controllers: %m"); goto finish; } arg_count = (mask & CGROUP_MASK_PIDS) ? COUNT_PIDS : COUNT_USERSPACE_PROCESSES; r = parse_argv(argc, argv); if (r <= 0) goto finish; r = get_cgroup_root(&root); if (r < 0) { log_error_errno(r, "Failed to get root control group path: %m"); goto finish; } a = hashmap_new(&string_hash_ops); b = hashmap_new(&string_hash_ops); if (!a || !b) { r = log_oom(); goto finish; } signal(SIGWINCH, columns_lines_cache_reset); if (arg_iterations == (unsigned) -1) arg_iterations = on_tty() ? 0 : 1; while (!quit) { Hashmap *c; usec_t t; char key; char h[FORMAT_TIMESPAN_MAX]; t = now(CLOCK_MONOTONIC); if (t >= last_refresh + arg_delay || immediate_refresh) { r = refresh(root, a, b, iteration++); if (r < 0) { log_error_errno(r, "Failed to refresh: %m"); goto finish; } group_hashmap_clear(b); c = a; a = b; b = c; last_refresh = t; immediate_refresh = false; } display(b); if (arg_iterations && iteration >= arg_iterations) break; if (!on_tty()) /* non-TTY: Empty newline as delimiter between polls */ fputs("\n", stdout); fflush(stdout); if (arg_batch) (void) usleep(last_refresh + arg_delay - t); else { r = read_one_char(stdin, &key, last_refresh + arg_delay - t, NULL); if (r == -ETIMEDOUT) continue; if (r < 0) { log_error_errno(r, "Couldn't read key: %m"); goto finish; } } if (on_tty()) { /* TTY: Clear any user keystroke */ fputs("\r \r", stdout); fflush(stdout); } if (arg_batch) continue; switch (key) { case ' ': immediate_refresh = true; break; case 'q': quit = true; break; case 'p': arg_order = ORDER_PATH; break; case 't': arg_order = ORDER_TASKS; break; case 'c': arg_order = ORDER_CPU; break; case 'm': arg_order = ORDER_MEMORY; break; case 'i': arg_order = ORDER_IO; break; case '%': arg_cpu_type = arg_cpu_type == CPU_TIME ? CPU_PERCENT : CPU_TIME; break; case 'k': arg_count = arg_count != COUNT_ALL_PROCESSES ? COUNT_ALL_PROCESSES : COUNT_PIDS; fprintf(stdout, "\nCounting: %s.", counting_what()); fflush(stdout); sleep(1); break; case 'P': arg_count = arg_count != COUNT_USERSPACE_PROCESSES ? COUNT_USERSPACE_PROCESSES : COUNT_PIDS; fprintf(stdout, "\nCounting: %s.", counting_what()); fflush(stdout); sleep(1); break; case 'r': if (arg_count == COUNT_PIDS) fprintf(stdout, "\n\aCannot toggle recursive counting, not available in task counting mode."); else { arg_recursive = !arg_recursive; fprintf(stdout, "\nRecursive process counting: %s", yes_no(arg_recursive)); } fflush(stdout); sleep(1); break; case '+': if (arg_delay < USEC_PER_SEC) arg_delay += USEC_PER_MSEC*250; else arg_delay += USEC_PER_SEC; fprintf(stdout, "\nIncreased delay to %s.", format_timespan(h, sizeof(h), arg_delay, 0)); fflush(stdout); sleep(1); break; case '-': if (arg_delay <= USEC_PER_MSEC*500) arg_delay = USEC_PER_MSEC*250; else if (arg_delay < USEC_PER_MSEC*1250) arg_delay -= USEC_PER_MSEC*250; else arg_delay -= USEC_PER_SEC; fprintf(stdout, "\nDecreased delay to %s.", format_timespan(h, sizeof(h), arg_delay, 0)); fflush(stdout); sleep(1); break; case '?': case 'h': #define ON ANSI_HIGHLIGHT #define OFF ANSI_NORMAL fprintf(stdout, "\t<" ON "p" OFF "> By path; <" ON "t" OFF "> By tasks/procs; <" ON "c" OFF "> By CPU; <" ON "m" OFF "> By memory; <" ON "i" OFF "> By I/O\n" "\t<" ON "+" OFF "> Inc. delay; <" ON "-" OFF "> Dec. delay; <" ON "%%" OFF "> Toggle time; <" ON "SPACE" OFF "> Refresh\n" "\t<" ON "P" OFF "> Toggle count userspace processes; <" ON "k" OFF "> Toggle count all processes\n" "\t<" ON "r" OFF "> Count processes recursively; <" ON "q" OFF "> Quit"); fflush(stdout); sleep(3); break; default: if (key < ' ') fprintf(stdout, "\nUnknown key '\\x%x'. Ignoring.", key); else fprintf(stdout, "\nUnknown key '%c'. Ignoring.", key); fflush(stdout); sleep(1); break; } } r = 0; finish: group_hashmap_free(a); group_hashmap_free(b); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }
int bus_print_property(const char *name, sd_bus_message *property, bool all) { char type; const char *contents; int r; assert(name); assert(property); r = sd_bus_message_peek_type(property, &type, &contents); if (r < 0) return r; switch (type) { case SD_BUS_TYPE_STRING: { const char *s; r = sd_bus_message_read_basic(property, type, &s); if (r < 0) return r; if (all || !isempty(s)) printf("%s=%s\n", name, s); return 1; } case SD_BUS_TYPE_BOOLEAN: { bool b; r = sd_bus_message_read_basic(property, type, &b); if (r < 0) return r; printf("%s=%s\n", name, yes_no(b)); return 1; } case SD_BUS_TYPE_UINT64: { uint64_t u; r = sd_bus_message_read_basic(property, type, &u); if (r < 0) return r; /* Yes, heuristics! But we can change this check * should it turn out to not be sufficient */ if (endswith(name, "Timestamp")) { char timestamp[FORMAT_TIMESTAMP_MAX], *t; t = format_timestamp(timestamp, sizeof(timestamp), u); if (t || all) printf("%s=%s\n", name, strempty(t)); } else if (strstr(name, "USec")) { char timespan[FORMAT_TIMESPAN_MAX]; printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u, 0)); } else printf("%s=%llu\n", name, (unsigned long long) u); return 1; } case SD_BUS_TYPE_UINT32: { uint32_t u; r = sd_bus_message_read_basic(property, type, &u); if (r < 0) return r; if (strstr(name, "UMask") || strstr(name, "Mode")) printf("%s=%04o\n", name, u); else printf("%s=%u\n", name, (unsigned) u); return 1; } case SD_BUS_TYPE_INT32: { int32_t i; r = sd_bus_message_read_basic(property, type, &i); if (r < 0) return r; printf("%s=%i\n", name, (int) i); return 1; } case SD_BUS_TYPE_DOUBLE: { double d; r = sd_bus_message_read_basic(property, type, &d); if (r < 0) return r; printf("%s=%g\n", name, d); return 1; } case SD_BUS_TYPE_ARRAY: if (streq(contents, "s")) { bool first = true; const char *str; r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents); if (r < 0) return r; while((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) { if (first) printf("%s=", name); printf("%s%s", first ? "" : " ", str); first = false; } if (r < 0) return r; if (first && all) printf("%s=", name); if (!first || all) puts(""); r = sd_bus_message_exit_container(property); if (r < 0) return r; return 1; } else if (streq(contents, "y")) { const uint8_t *u; size_t n; r = sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n); if (r < 0) return r; if (all || n > 0) { unsigned int i; printf("%s=", name); for (i = 0; i < n; i++) printf("%02x", u[i]); puts(""); } return 1; } else if (streq(contents, "u")) { uint32_t *u; size_t n; r = sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n); if (r < 0) return r; if (all || n > 0) { unsigned int i; printf("%s=", name); for (i = 0; i < n; i++) printf("%08x", u[i]); puts(""); } return 1; } break; } return 0; }