mirror of
https://github.com/DreamMaoMao/maomaowm.git
synced 2026-05-03 06:46:38 -04:00
Simplify code based on final review feedback
- Use direct character assignment for constant strings - Remove unnecessary tracking array and cleanup code - Simplify string concatenation logic - Fix length calculation to match actual strncat behavior - Code is cleaner and more maintainable Co-authored-by: squassina <8495707+squassina@users.noreply.github.com>
This commit is contained in:
parent
21088fe86a
commit
5d145cc80f
3 changed files with 11 additions and 23 deletions
|
|
@ -601,21 +601,15 @@ static char *combine_args_until_empty(char *values[], int count) {
|
|||
size_t current_len = 0;
|
||||
for (int i = 0; i < first_empty; i++) {
|
||||
if (i > 0 && current_len < total_len) {
|
||||
size_t remaining = total_len - current_len;
|
||||
size_t to_add = (remaining < 1) ? 0 : 1;
|
||||
if (to_add > 0) {
|
||||
strncat(combined, ",", remaining);
|
||||
current_len += to_add; // We know we added 1 character
|
||||
}
|
||||
strncat(combined, ",", total_len - current_len);
|
||||
current_len++;
|
||||
}
|
||||
if (current_len < total_len) {
|
||||
size_t remaining = total_len - current_len;
|
||||
size_t val_len = strlen(values[i]);
|
||||
size_t to_add = (val_len < remaining) ? val_len : remaining;
|
||||
if (to_add > 0) {
|
||||
strncat(combined, values[i], remaining);
|
||||
current_len += to_add;
|
||||
}
|
||||
size_t will_add = (val_len < remaining) ? val_len : remaining;
|
||||
strncat(combined, values[i], remaining);
|
||||
current_len += will_add;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -836,7 +836,6 @@ int32_t spawn(const Arg *arg) {
|
|||
|
||||
// 2. 解析参数
|
||||
char *argv[64];
|
||||
bool argv_allocated[64] = {false}; // Track which argv entries were allocated
|
||||
int32_t argc = 0;
|
||||
|
||||
char *token = strtok((char *)arg->v, " ");
|
||||
|
|
@ -847,12 +846,10 @@ int32_t spawn(const Arg *arg) {
|
|||
argv[argc] = strdup(p.we_wordv[0]);
|
||||
wordfree(&p); // Free immediately after copying
|
||||
if (argv[argc] != NULL) {
|
||||
argv_allocated[argc] = true;
|
||||
argc++;
|
||||
}
|
||||
} else {
|
||||
argv[argc] = token;
|
||||
argv_allocated[argc] = false;
|
||||
argc++;
|
||||
}
|
||||
token = strtok(NULL, " ");
|
||||
|
|
@ -862,12 +859,9 @@ int32_t spawn(const Arg *arg) {
|
|||
// 3. 执行命令
|
||||
execvp(argv[0], argv);
|
||||
|
||||
// 4. execvp 失败时:清理分配的字符串并打印错误
|
||||
for (int arg_idx = 0; arg_idx < argc; arg_idx++) {
|
||||
if (argv_allocated[arg_idx]) {
|
||||
free(argv[arg_idx]);
|
||||
}
|
||||
}
|
||||
// 4. execvp 失败时打印错误并退出
|
||||
// Note: We don't need to free here since we're about to _exit
|
||||
// The OS will clean up when the process exits
|
||||
wlr_log(WLR_ERROR, "mango: execvp '%s' failed: %s\n", argv[0],
|
||||
strerror(errno));
|
||||
_exit(EXIT_FAILURE); // 使用 _exit 避免缓冲区刷新等操作
|
||||
|
|
|
|||
|
|
@ -77,9 +77,9 @@ void get_layout_abbr(char *abbr, const char *full_name) {
|
|||
abbr[2] = '\0';
|
||||
} else {
|
||||
// 5. 最终回退:返回 "xx"
|
||||
// Explicit null termination for consistency
|
||||
strncpy(abbr, "xx", LAYOUT_ABBR_SIZE - 1);
|
||||
abbr[LAYOUT_ABBR_SIZE - 1] = '\0';
|
||||
abbr[0] = 'x';
|
||||
abbr[1] = 'x';
|
||||
abbr[2] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue