static void test_string_erase(void) {
        char *x;

        x = strdupa("");
        assert_se(streq(string_erase(x), ""));

        x = strdupa("1");
        assert_se(streq(string_erase(x), "x"));

        x = strdupa("12");
        assert_se(streq(string_erase(x), "xx"));

        x = strdupa("123");
        assert_se(streq(string_erase(x), "xxx"));

        x = strdupa("1234");
        assert_se(streq(string_erase(x), "xxxx"));

        x = strdupa("12345");
        assert_se(streq(string_erase(x), "xxxxx"));

        x = strdupa("123456");
        assert_se(streq(string_erase(x), "xxxxxx"));

        x = strdupa("1234567");
        assert_se(streq(string_erase(x), "xxxxxxx"));

        x = strdupa("12345678");
        assert_se(streq(string_erase(x), "xxxxxxxx"));

        x = strdupa("123456789");
        assert_se(streq(string_erase(x), "xxxxxxxxx"));
}
Beispiel #2
0
int main(int argc, char *argv[]) {
        bool enabled;
        int r;

        r = parse_argv(argc, argv);
        if (r <= 0)
                goto finish;

        log_set_target(LOG_TARGET_AUTO);
        log_parse_environment();
        log_open();

        umask(0022);

        r = proc_cmdline_get_bool("systemd.firstboot", &enabled);
        if (r < 0) {
                log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
                goto finish;
        }
        if (r > 0 && !enabled) {
                r = 0; /* disabled */
                goto finish;
        }

        r = process_locale();
        if (r < 0)
                goto finish;

        r = process_keymap();
        if (r < 0)
                goto finish;

        r = process_timezone();
        if (r < 0)
                goto finish;

        r = process_hostname();
        if (r < 0)
                goto finish;

        r = process_machine_id();
        if (r < 0)
                goto finish;

        r = process_root_password();
        if (r < 0)
                goto finish;

finish:
        free(arg_root);
        free(arg_locale);
        free(arg_locale_messages);
        free(arg_keymap);
        free(arg_timezone);
        free(arg_hostname);
        string_erase(arg_root_password);
        free(arg_root_password);

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Beispiel #3
0
int string_replace(string *s, size_t pos, size_t len, char *data)
{
  int e;

  e = string_erase(s, pos, len);
  if (e == -1)
    return -1;

  return string_insert(s, pos, data);
}
Beispiel #4
0
static void test_string_erase(void) {
        char *x;

        x = strdupa("");
        assert_se(streq(string_erase(x), ""));

        x = strdupa("1");
        assert_se(streq(string_erase(x), ""));

        x = strdupa("123456789");
        assert_se(streq(string_erase(x), ""));

        assert_se(x[1] == '\0');
        assert_se(x[2] == '\0');
        assert_se(x[3] == '\0');
        assert_se(x[4] == '\0');
        assert_se(x[5] == '\0');
        assert_se(x[6] == '\0');
        assert_se(x[7] == '\0');
        assert_se(x[8] == '\0');
        assert_se(x[9] == '\0');
}
Beispiel #5
0
int main(int argc, char *argv[]) {
        int r;

        r = parse_argv(argc, argv);
        if (r <= 0)
                goto finish;

        log_set_target(LOG_TARGET_AUTO);
        log_parse_environment();
        log_open();

        umask(0022);

        r = process_locale();
        if (r < 0)
                goto finish;

        r = process_timezone();
        if (r < 0)
                goto finish;

        r = process_hostname();
        if (r < 0)
                goto finish;

        r = process_machine_id();
        if (r < 0)
                goto finish;

        r = process_root_password();
        if (r < 0)
                goto finish;

finish:
        free(arg_root);
        free(arg_locale);
        free(arg_locale_messages);
        free(arg_timezone);
        free(arg_hostname);
        string_erase(arg_root_password);
        free(arg_root_password);

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Beispiel #6
0
int input_box(char* msg, char* result, int result_size, char* def_input, int def_cursor)
{
    gInputBoxMsg = msg;
    
    int result2 = 0;
    gInputBoxCursor = def_cursor;

    string_put(gInputBoxInput, def_input);
    
    gView = input_box_view;
    
    while(1) {
        xclear();
        view();
        //input_box_view();
        refresh();

        /// input ///
        int meta;
        int key = xgetch(&meta);
        
        if(key == 10 || key == 13) {
            result2 = 0;
            break;
        }
        else if(key == 6 || key == KEY_RIGHT) {
            input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, 1);
        }
        else if(key == 2 || key == KEY_LEFT) {
            input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, -1);
        }
        else if(key == 8 || key == KEY_BACKSPACE) {    // CTRL-H
            if(gInputBoxCursor > 0) {
                char* str2 = string_c_str(gInputBoxInput);

                int utfpos = str_pointer2kanjipos(gKanjiCode, str2, str2 + gInputBoxCursor);
                char* before_point = str_kanjipos2pointer(gKanjiCode, str2, utfpos-1);
                int new_cursor = before_point-str2;

                string_erase(gInputBoxInput, before_point - str2, (str2 + gInputBoxCursor) - before_point);
                gInputBoxCursor = new_cursor;
            }
        }
        else if(key == 4 || key == KEY_DC) {    // CTRL-D DELETE
            char* str2 = string_c_str(gInputBoxInput);
            if(string_length(gInputBoxInput) > 0) {
                if(gInputBoxCursor < string_length(gInputBoxInput)) {
                    int utfpos = str_pointer2kanjipos(gKanjiCode, str2, str2 + gInputBoxCursor);
                    char* next_point = str_kanjipos2pointer(gKanjiCode, str2, utfpos+1);

                    string_erase(gInputBoxInput, gInputBoxCursor, next_point - (str2 + gInputBoxCursor));
                }
            }
        }
        else if(key == 1 || key == KEY_HOME) {    // CTRL-A
            input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, -999);
        }
        else if(key == 5 || key == KEY_END) {    // CTRL-E
            input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, 999);
        }
        else if(key == 11) {    // CTRL-K
            string_erase(gInputBoxInput, gInputBoxCursor, string_length(gInputBoxInput)-gInputBoxCursor);
        }
        
        else if(key == 21) {    // CTRL-U
            string_put(gInputBoxInput, "");

            gInputBoxCursor = 0;
        }
        else if(key == 23) {     // CTRL-W
            if(gInputBoxCursor > 0) {
                const char* s = string_c_str(gInputBoxInput);
                int pos = gInputBoxCursor-1;
                if(s[pos]==' ' || s[pos]=='/' || s[pos]=='\'' || s[pos]=='"') {
                    while(pos>=0 && (s[pos]==' ' || s[pos]=='/' || s[pos]=='\'' || s[pos]=='"'))
                    {
                        pos--;
                    }
                }
                while(pos>=0 && s[pos]!=' ' && s[pos]!='/' && s[pos]!='\'' && s[pos]!='"')
                {
                    pos--;
                }

                string_erase(gInputBoxInput, pos+1, gInputBoxCursor-pos-1);

                gInputBoxCursor = pos+1;
            }
        }
        else if(meta==1 && key == 'd') {     // Meta-d
            const char* s = string_c_str(gInputBoxInput);

            if(s[gInputBoxCursor] != 0) {
                int pos = gInputBoxCursor;
                pos++;
                while(s[pos]!=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) {
                    pos++;
                }
                while(s[pos]!=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') {
                    pos++;
                }

                string_erase(gInputBoxInput, gInputBoxCursor, pos-gInputBoxCursor);
            }
        }
        else if(meta==1 && key == 'b') {     // META-b
            if(gInputBoxCursor > 0) {
                const char* s = string_c_str(gInputBoxInput);
                int pos = gInputBoxCursor;
                pos--;
                while(pos>=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) {
                    pos--;
                }
                while(pos>=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') {
                    pos--;
                }

                gInputBoxCursor = pos+1;
            }
        }
        else if(meta==1 && key == 'f') {     // META-f
            const char* s = string_c_str(gInputBoxInput);

            if(s[gInputBoxCursor] != 0) {
                int pos = gInputBoxCursor;
                pos++;
                while(s[pos]!=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) {
                    pos++;
                }
                while(s[pos]!=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') {
                    pos++;
                }

                gInputBoxCursor = pos;
            }
        }
        else if(key == 3 || key == 7 || key == 27) { // CTRL-C -G Escape
            result2 = 1;
            break;
        }
        else if(key == 12) {            // CTRL-L
            xclear_immediately();
        }
        else {
            if(meta == 0 && !(key >= 0 && key <= 27)) {
                char tmp[128];

                snprintf(tmp, 128, "%c", key);
                string_insert(gInputBoxInput, gInputBoxCursor, tmp);
                gInputBoxCursor++;
            }
        }
    }
    
    gView = NULL;
    
    int maxx = mgetmaxx();
    int maxy = mgetmaxy();
    
    xstrncpy(result, string_c_str(gInputBoxInput), result_size);

    mmove_immediately(maxy -2, 0);

#if defined(__CYGWIN__)
    xclear_immediately();       // 画面の再描写
    view();
    refresh();
#endif

    return result2;
}