New function user_list().

This commit is contained in:
default 2022-09-19 23:33:11 +02:00
parent e5167b7b49
commit 5e438f8353
3 changed files with 47 additions and 2 deletions

34
data.c
View File

@ -7,6 +7,9 @@
#include "snac.h" #include "snac.h"
#include <glob.h>
int srv_open(char *basedir) int srv_open(char *basedir)
/* opens a server */ /* opens a server */
{ {
@ -61,7 +64,7 @@ int srv_open(char *basedir)
} }
void snac_free(snac *snac) void user_free(snac *snac)
/* frees a user snac */ /* frees a user snac */
{ {
free(snac->uid); free(snac->uid);
@ -125,9 +128,36 @@ int user_open(snac *snac, char *uid)
srv_log(xs_fmt("invalid user '%s'", uid)); srv_log(xs_fmt("invalid user '%s'", uid));
if (!ret) if (!ret)
snac_free(snac); user_free(snac);
return ret; return ret;
} }
d_char *user_list(void)
/* returns the list of user ids */
{
d_char *list;
xs *spec;
glob_t globbuf;
globbuf.gl_offs = 1;
list = xs_list_new();
spec = xs_fmt("%s/user/*", srv_basedir); /**/
if (glob(spec, 0, NULL, &globbuf) == 0) {
int n;
char *p;
for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) {
if ((p = strrchr(p, '/')) != NULL)
list = xs_list_append(list, p + 1);
}
}
globfree(&globbuf);
return list;
}

14
main.c
View File

@ -16,5 +16,19 @@ int main(int argc, char *argv[])
user_open(&snac, "mike"); user_open(&snac, "mike");
snac_log(&snac, xs_str_new("ok")); snac_log(&snac, xs_str_new("ok"));
{
xs *list = user_list();
char *p, *uid;
p = list;
while (xs_list_iter(&p, &uid)) {
user_open(&snac, uid);
printf("%s (%s)\n", uid, xs_dict_get(snac.config, "name"));
user_free(&snac);
}
}
return 0; return 0;
} }

1
snac.h
View File

@ -28,6 +28,7 @@ typedef struct _snac {
int user_open(snac *snac, char *uid); int user_open(snac *snac, char *uid);
void user_free(snac *snac); void user_free(snac *snac);
d_char *user_list(void);
void snac_debug(snac *snac, int level, d_char *str); void snac_debug(snac *snac, int level, d_char *str);
#define snac_log(snac, str) snac_debug(snac, 0, str) #define snac_log(snac, str) snac_debug(snac, 0, str)