diff options
| author | Joursoir <chat@joursoir.net> | 2025-02-10 21:34:08 +0300 | 
|---|---|---|
| committer | Joursoir <chat@joursoir.net> | 2025-02-10 21:34:28 +0300 | 
| commit | e9a715195fbf82c9ea7d4b2d6699351468fab222 (patch) | |
| tree | 101b362b2a0da53bc855382d837b2528b02c8b19 | |
| parent | 520b9fa310b3e649420a0b6aa040e42af4928dee (diff) | |
| download | lock-password-e9a715195fbf82c9ea7d4b2d6699351468fab222.tar.gz lock-password-e9a715195fbf82c9ea7d4b2d6699351468fab222.tar.bz2 lock-password-e9a715195fbf82c9ea7d4b2d6699351468fab222.zip | |
make xstrcat() concats a variable number of strings
| -rw-r--r-- | src/lpass.c | 2 | ||||
| -rw-r--r-- | src/tree.c | 2 | ||||
| -rw-r--r-- | src/xstd.c | 57 | ||||
| -rw-r--r-- | src/xstd.h | 6 | 
4 files changed, 49 insertions, 18 deletions
| diff --git a/src/lpass.c b/src/lpass.c index 9a58093..229f3b6 100644 --- a/src/lpass.c +++ b/src/lpass.c @@ -61,7 +61,7 @@ static struct cmd_struct *get_cmd(const char *name)  static int goto_maindir()  { -	char *rootdir = xstrcat(getenv("HOME"), LOCKPASS_DIR, "/"); +	char *rootdir = xstrcat(getenv("HOME"), "/", LOCKPASS_DIR, NULL);  	int retval = chdir(rootdir);  	if(retval)  	{ @@ -90,7 +90,7 @@ int tree(const char *path, const char *prefix, int use_color)  			prefix_depth = "│   ";  		} -		full_path = xstrcat(path, entries[i], "/"); +		full_path = xstrcat(path, "/", entries[i], NULL);  		printf("%s%s", prefix, pointer);  		if(file_exist(full_path) == F_ISDIR) {  			printf("%s%s%s\n",  @@ -1,6 +1,6 @@  /***  	This file is part of LockPassword -	Copyright (C) 2020-2021 Aleksandr D. Goncharov (Joursoir) <chat@joursoir.net> +	Copyright (C) 2020-2025 Aleksandr D. Goncharov (Joursoir) <chat@joursoir.net>  	This program is free software: you can redistribute it and/or modify  	it under the terms of the GNU General Public License as published by @@ -16,22 +16,53 @@  	along with this program.  If not, see <https://www.gnu.org/licenses/>.  ***/ +#include <stdarg.h>  #include <stdlib.h>  #include <string.h>  #include "xstd.h" -char *xstrcat(const char *first, const char *second, -	const char *delimiter) +char *xstrcat(const char *first, ...)  { -	size_t size = strlen(first) + strlen(second) + 1; -	if(delimiter) -		size += strlen(delimiter); -	char *res = malloc(size * sizeof(char)); -	strcpy(res, first); -	if(delimiter) -		strcat(res, delimiter); -	strcat(res, second); -	return res; -} +    va_list args; +    const char *s; +    size_t total_len = 0; +    size_t len; + +    // 1. Calculate the total length required +    if (first != NULL) { +        total_len += strlen(first); +    } + +    va_start(args, first); +    while ((s = va_arg(args, const char *)) != NULL) { +        total_len += strlen(s); +    } +    va_end(args); + +    // 2. Allocate memory for the concatenated string +    char *result = malloc(total_len + 1); +    if (result == NULL) { +        return NULL; +    } +    char *ptr = result; + +    // 3. Copy the first string and then the rest of the strings +    if (first != NULL) { +        len = strlen(first); +        memcpy(ptr, first, len); +        ptr += len; +    } + +    va_start(args, first); +    while ((s = va_arg(args, const char *)) != NULL) { +        len = strlen(s); +        memcpy(ptr, s, len); +        ptr += len; +    } +    va_end(args); + +    *ptr = '\0'; +    return result; +} @@ -3,7 +3,7 @@  /***  	This file is part of LockPassword -	Copyright (C) 2020-2021 Aleksandr D. Goncharov (Joursoir) <chat@joursoir.net> +	Copyright (C) 2020-2025 Aleksandr D. Goncharov (Joursoir) <chat@joursoir.net>  	This program is free software: you can redistribute it and/or modify  	it under the terms of the GNU General Public License as published by @@ -19,7 +19,7 @@  	along with this program.  If not, see <https://www.gnu.org/licenses/>.  ***/ -char *xstrcat(const char *first, const char *second, -	const char *delimiter); +// A `null` pointer indicates the end of the arguments +char *xstrcat(const char *first, ...);  #endif /* LPASS_XSTD_H */ | 
