diff options
| author | 2022-10-03 11:18:49 +0200 | |
|---|---|---|
| committer | 2022-10-03 11:18:49 +0200 | |
| commit | e8c421c51dea128a890d62bd091473aa61f3a357 (patch) | |
| tree | ccb979c924513f39684f7d0c4e0835e6c269cd19 | |
| parent | Use xs_glob() in history_list(). (diff) | |
| download | snac2-e8c421c51dea128a890d62bd091473aa61f3a357.tar.gz snac2-e8c421c51dea128a890d62bd091473aa61f3a357.tar.xz snac2-e8c421c51dea128a890d62bd091473aa61f3a357.zip | |
Backport from xs.
| -rw-r--r-- | Makefile | 7 | ||||
| -rw-r--r-- | data.c | 41 | ||||
| -rw-r--r-- | snac.c | 1 | ||||
| -rw-r--r-- | xs_glob.h | 56 |
4 files changed, 62 insertions, 43 deletions
| @@ -16,14 +16,15 @@ dep: | |||
| 16 | 16 | ||
| 17 | activitypub.o: activitypub.c xs.h xs_encdec.h xs_json.h xs_curl.h \ | 17 | activitypub.o: activitypub.c xs.h xs_encdec.h xs_json.h xs_curl.h \ |
| 18 | xs_mime.h xs_openssl.h xs_regex.h xs_time.h snac.h | 18 | xs_mime.h xs_openssl.h xs_regex.h xs_time.h snac.h |
| 19 | data.o: data.c xs.h xs_io.h xs_json.h xs_openssl.h snac.h | 19 | data.o: data.c xs.h xs_io.h xs_json.h xs_openssl.h xs_glob.h snac.h |
| 20 | html.o: html.c xs.h xs_io.h xs_encdec.h xs_json.h xs_regex.h xs_set.h \ | 20 | html.o: html.c xs.h xs_io.h xs_encdec.h xs_json.h xs_regex.h xs_set.h \ |
| 21 | xs_openssl.h snac.h | 21 | xs_openssl.h xs_time.h snac.h |
| 22 | http.o: http.c xs.h xs_io.h xs_encdec.h xs_openssl.h xs_curl.h xs_time.h \ | 22 | http.o: http.c xs.h xs_io.h xs_encdec.h xs_openssl.h xs_curl.h xs_time.h \ |
| 23 | snac.h | 23 | snac.h |
| 24 | httpd.o: httpd.c xs.h xs_io.h xs_encdec.h xs_json.h xs_socket.h \ | 24 | httpd.o: httpd.c xs.h xs_io.h xs_encdec.h xs_json.h xs_socket.h \ |
| 25 | xs_httpd.h snac.h | 25 | xs_httpd.h snac.h |
| 26 | main.o: main.c xs.h xs_io.h xs_encdec.h xs_json.h snac.h | 26 | main.o: main.c xs.h xs_io.h xs_encdec.h xs_json.h snac.h |
| 27 | snac.o: snac.c xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h xs_openssl.h \ | 27 | snac.o: snac.c xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h xs_openssl.h \ |
| 28 | xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h snac.h | 28 | xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h \ |
| 29 | snac.h | ||
| 29 | webfinger.o: webfinger.c xs.h xs_encdec.h xs_json.h xs_curl.h snac.h | 30 | webfinger.o: webfinger.c xs.h xs_encdec.h xs_json.h xs_curl.h snac.h |
| @@ -5,6 +5,7 @@ | |||
| 5 | #include "xs_io.h" | 5 | #include "xs_io.h" |
| 6 | #include "xs_json.h" | 6 | #include "xs_json.h" |
| 7 | #include "xs_openssl.h" | 7 | #include "xs_openssl.h" |
| 8 | #include "xs_glob.h" | ||
| 8 | 9 | ||
| 9 | #include "snac.h" | 10 | #include "snac.h" |
| 10 | 11 | ||
| @@ -144,46 +145,6 @@ int user_open(snac *snac, char *uid) | |||
| 144 | } | 145 | } |
| 145 | 146 | ||
| 146 | 147 | ||
| 147 | d_char *xs_glob_n(const char *spec, int basename, int reverse, int max) | ||
| 148 | /* does a globbing and returns the found files */ | ||
| 149 | { | ||
| 150 | glob_t globbuf; | ||
| 151 | d_char *list = xs_list_new(); | ||
| 152 | |||
| 153 | if (glob(spec, 0, NULL, &globbuf) == 0) { | ||
| 154 | int n; | ||
| 155 | |||
| 156 | if (max > globbuf.gl_pathc) | ||
| 157 | max = globbuf.gl_pathc; | ||
| 158 | |||
| 159 | for (n = 0; n < max; n++) { | ||
| 160 | char *p; | ||
| 161 | |||
| 162 | if (reverse) | ||
| 163 | p = globbuf.gl_pathv[globbuf.gl_pathc - n - 1]; | ||
| 164 | else | ||
| 165 | p = globbuf.gl_pathv[n]; | ||
| 166 | |||
| 167 | if (p != NULL) { | ||
| 168 | if (basename) { | ||
| 169 | if ((p = strrchr(p, '/')) == NULL) | ||
| 170 | continue; | ||
| 171 | |||
| 172 | p++; | ||
| 173 | } | ||
| 174 | |||
| 175 | list = xs_list_append(list, p); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | globfree(&globbuf); | ||
| 181 | |||
| 182 | return list; | ||
| 183 | } | ||
| 184 | #define xs_glob(spec, basename, reverse) xs_glob_n(spec, basename, reverse, 0xfffffff) | ||
| 185 | |||
| 186 | |||
| 187 | d_char *user_list(void) | 148 | d_char *user_list(void) |
| 188 | /* returns the list of user ids */ | 149 | /* returns the list of user ids */ |
| 189 | { | 150 | { |
| @@ -15,6 +15,7 @@ | |||
| 15 | #include "xs_regex.h" | 15 | #include "xs_regex.h" |
| 16 | #include "xs_set.h" | 16 | #include "xs_set.h" |
| 17 | #include "xs_time.h" | 17 | #include "xs_time.h" |
| 18 | #include "xs_glob.h" | ||
| 18 | 19 | ||
| 19 | #include "snac.h" | 20 | #include "snac.h" |
| 20 | 21 | ||
diff --git a/xs_glob.h b/xs_glob.h new file mode 100644 index 0000000..c5293dc --- /dev/null +++ b/xs_glob.h | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | /* copyright (c) 2022 grunfink - MIT license */ | ||
| 2 | |||
| 3 | #ifndef _XS_GLOB_H | ||
| 4 | |||
| 5 | #define _XS_GLOB_H | ||
| 6 | |||
| 7 | d_char *xs_glob_n(const char *spec, int basename, int reverse, int max); | ||
| 8 | #define xs_glob(spec, basename, reverse) xs_glob_n(spec, basename, reverse, 0xfffffff) | ||
| 9 | |||
| 10 | |||
| 11 | #ifdef XS_IMPLEMENTATION | ||
| 12 | |||
| 13 | #include <glob.h> | ||
| 14 | |||
| 15 | d_char *xs_glob_n(const char *spec, int basename, int reverse, int max) | ||
| 16 | /* does a globbing and returns the found files */ | ||
| 17 | { | ||
| 18 | glob_t globbuf; | ||
| 19 | d_char *list = xs_list_new(); | ||
| 20 | |||
| 21 | if (glob(spec, 0, NULL, &globbuf) == 0) { | ||
| 22 | int n; | ||
| 23 | |||
| 24 | if (max > globbuf.gl_pathc) | ||
| 25 | max = globbuf.gl_pathc; | ||
| 26 | |||
| 27 | for (n = 0; n < max; n++) { | ||
| 28 | char *p; | ||
| 29 | |||
| 30 | if (reverse) | ||
| 31 | p = globbuf.gl_pathv[globbuf.gl_pathc - n - 1]; | ||
| 32 | else | ||
| 33 | p = globbuf.gl_pathv[n]; | ||
| 34 | |||
| 35 | if (p != NULL) { | ||
| 36 | if (basename) { | ||
| 37 | if ((p = strrchr(p, '/')) == NULL) | ||
| 38 | continue; | ||
| 39 | |||
| 40 | p++; | ||
| 41 | } | ||
| 42 | |||
| 43 | list = xs_list_append(list, p); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | globfree(&globbuf); | ||
| 49 | |||
| 50 | return list; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | #endif /* XS_IMPLEMENTATION */ | ||
| 55 | |||
| 56 | #endif /* _XS_GLOB_H */ | ||