author | Lars Hjemli <hjemli@gmail.com> | 2008-02-16 10:53:40 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-02-16 11:07:28 (UTC) |
commit | d14d77fe95c3b6224b40df9b101dded0deea913c (patch) (unidiff) | |
tree | 7e0d9c8f2c0f86b8946aea0bb823085c33b164b3 /shared.c | |
parent | e5ed227ef0da561e2bde8646ec816842392377ee (diff) | |
download | cgit-d14d77fe95c3b6224b40df9b101dded0deea913c.zip cgit-d14d77fe95c3b6224b40df9b101dded0deea913c.tar.gz cgit-d14d77fe95c3b6224b40df9b101dded0deea913c.tar.bz2 |
Introduce struct cgit_context
This struct will hold all the cgit runtime information currently found in
a multitude of global variables.
The first cleanup removes all querystring-related variables.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | shared.c | 43 |
1 files changed, 14 insertions, 29 deletions
@@ -1,133 +1,118 @@ | |||
1 | /* shared.c: global vars + some callback functions | 1 | /* shared.c: global vars + some callback functions |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | 10 | ||
11 | struct repolist cgit_repolist; | 11 | struct repolist cgit_repolist; |
12 | struct repoinfo *cgit_repo; | 12 | struct repoinfo *cgit_repo; |
13 | struct cgit_context ctx; | ||
13 | int cgit_cmd; | 14 | int cgit_cmd; |
14 | 15 | ||
15 | const char *cgit_version = CGIT_VERSION; | 16 | const char *cgit_version = CGIT_VERSION; |
16 | 17 | ||
17 | char *cgit_root_title = "Git repository browser"; | 18 | char *cgit_root_title = "Git repository browser"; |
18 | char *cgit_css = "/cgit.css"; | 19 | char *cgit_css = "/cgit.css"; |
19 | char *cgit_logo = "/git-logo.png"; | 20 | char *cgit_logo = "/git-logo.png"; |
20 | char *cgit_index_header = NULL; | 21 | char *cgit_index_header = NULL; |
21 | char *cgit_index_info = NULL; | 22 | char *cgit_index_info = NULL; |
22 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; | 23 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; |
23 | char *cgit_module_link = "./?repo=%s&page=commit&id=%s"; | 24 | char *cgit_module_link = "./?repo=%s&page=commit&id=%s"; |
24 | char *cgit_agefile = "info/web/last-modified"; | 25 | char *cgit_agefile = "info/web/last-modified"; |
25 | char *cgit_virtual_root = NULL; | 26 | char *cgit_virtual_root = NULL; |
26 | char *cgit_script_name = CGIT_SCRIPT_NAME; | 27 | char *cgit_script_name = CGIT_SCRIPT_NAME; |
27 | char *cgit_cache_root = CGIT_CACHE_ROOT; | 28 | char *cgit_cache_root = CGIT_CACHE_ROOT; |
28 | char *cgit_repo_group = NULL; | 29 | char *cgit_repo_group = NULL; |
29 | char *cgit_robots = "index, nofollow"; | 30 | char *cgit_robots = "index, nofollow"; |
30 | char *cgit_clone_prefix = NULL; | 31 | char *cgit_clone_prefix = NULL; |
31 | 32 | ||
32 | int cgit_nocache = 0; | 33 | int cgit_nocache = 0; |
33 | int cgit_snapshots = 0; | 34 | int cgit_snapshots = 0; |
34 | int cgit_enable_index_links = 0; | 35 | int cgit_enable_index_links = 0; |
35 | int cgit_enable_log_filecount = 0; | 36 | int cgit_enable_log_filecount = 0; |
36 | int cgit_enable_log_linecount = 0; | 37 | int cgit_enable_log_linecount = 0; |
37 | int cgit_max_lock_attempts = 5; | 38 | int cgit_max_lock_attempts = 5; |
38 | int cgit_cache_root_ttl = 5; | 39 | int cgit_cache_root_ttl = 5; |
39 | int cgit_cache_repo_ttl = 5; | 40 | int cgit_cache_repo_ttl = 5; |
40 | int cgit_cache_dynamic_ttl = 5; | 41 | int cgit_cache_dynamic_ttl = 5; |
41 | int cgit_cache_static_ttl = -1; | 42 | int cgit_cache_static_ttl = -1; |
42 | int cgit_cache_max_create_time = 5; | 43 | int cgit_cache_max_create_time = 5; |
43 | int cgit_summary_log = 0; | 44 | int cgit_summary_log = 0; |
44 | int cgit_summary_tags = 0; | 45 | int cgit_summary_tags = 0; |
45 | int cgit_summary_branches = 0; | 46 | int cgit_summary_branches = 0; |
46 | int cgit_renamelimit = -1; | 47 | int cgit_renamelimit = -1; |
47 | 48 | ||
48 | int cgit_max_msg_len = 60; | 49 | int cgit_max_msg_len = 60; |
49 | int cgit_max_repodesc_len = 60; | 50 | int cgit_max_repodesc_len = 60; |
50 | int cgit_max_commit_count = 50; | 51 | int cgit_max_commit_count = 50; |
51 | 52 | ||
52 | int cgit_query_has_symref = 0; | ||
53 | int cgit_query_has_sha1 = 0; | ||
54 | |||
55 | char *cgit_querystring = NULL; | ||
56 | char *cgit_query_repo = NULL; | ||
57 | char *cgit_query_page = NULL; | ||
58 | char *cgit_query_head = NULL; | ||
59 | char *cgit_query_search = NULL; | ||
60 | char *cgit_query_grep = NULL; | ||
61 | char *cgit_query_sha1 = NULL; | ||
62 | char *cgit_query_sha2 = NULL; | ||
63 | char *cgit_query_path = NULL; | ||
64 | char *cgit_query_name = NULL; | ||
65 | int cgit_query_ofs = 0; | ||
66 | |||
67 | int htmlfd = 0; | 53 | int htmlfd = 0; |
68 | 54 | ||
69 | |||
70 | int cgit_get_cmd_index(const char *cmd) | 55 | int cgit_get_cmd_index(const char *cmd) |
71 | { | 56 | { |
72 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", | 57 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", |
73 | "snapshot", "tag", "refs", "patch", NULL}; | 58 | "snapshot", "tag", "refs", "patch", NULL}; |
74 | int i; | 59 | int i; |
75 | 60 | ||
76 | for(i = 0; cmds[i]; i++) | 61 | for(i = 0; cmds[i]; i++) |
77 | if (!strcmp(cmd, cmds[i])) | 62 | if (!strcmp(cmd, cmds[i])) |
78 | return i + 1; | 63 | return i + 1; |
79 | return 0; | 64 | return 0; |
80 | } | 65 | } |
81 | 66 | ||
82 | int chk_zero(int result, char *msg) | 67 | int chk_zero(int result, char *msg) |
83 | { | 68 | { |
84 | if (result != 0) | 69 | if (result != 0) |
85 | die("%s: %s", msg, strerror(errno)); | 70 | die("%s: %s", msg, strerror(errno)); |
86 | return result; | 71 | return result; |
87 | } | 72 | } |
88 | 73 | ||
89 | int chk_positive(int result, char *msg) | 74 | int chk_positive(int result, char *msg) |
90 | { | 75 | { |
91 | if (result <= 0) | 76 | if (result <= 0) |
92 | die("%s: %s", msg, strerror(errno)); | 77 | die("%s: %s", msg, strerror(errno)); |
93 | return result; | 78 | return result; |
94 | } | 79 | } |
95 | 80 | ||
96 | int chk_non_negative(int result, char *msg) | 81 | int chk_non_negative(int result, char *msg) |
97 | { | 82 | { |
98 | if (result < 0) | 83 | if (result < 0) |
99 | die("%s: %s",msg, strerror(errno)); | 84 | die("%s: %s",msg, strerror(errno)); |
100 | return result; | 85 | return result; |
101 | } | 86 | } |
102 | 87 | ||
103 | struct repoinfo *add_repo(const char *url) | 88 | struct repoinfo *add_repo(const char *url) |
104 | { | 89 | { |
105 | struct repoinfo *ret; | 90 | struct repoinfo *ret; |
106 | 91 | ||
107 | if (++cgit_repolist.count > cgit_repolist.length) { | 92 | if (++cgit_repolist.count > cgit_repolist.length) { |
108 | if (cgit_repolist.length == 0) | 93 | if (cgit_repolist.length == 0) |
109 | cgit_repolist.length = 8; | 94 | cgit_repolist.length = 8; |
110 | else | 95 | else |
111 | cgit_repolist.length *= 2; | 96 | cgit_repolist.length *= 2; |
112 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, | 97 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
113 | cgit_repolist.length * | 98 | cgit_repolist.length * |
114 | sizeof(struct repoinfo)); | 99 | sizeof(struct repoinfo)); |
115 | } | 100 | } |
116 | 101 | ||
117 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; | 102 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
118 | ret->url = trim_end(url, '/'); | 103 | ret->url = trim_end(url, '/'); |
119 | ret->name = ret->url; | 104 | ret->name = ret->url; |
120 | ret->path = NULL; | 105 | ret->path = NULL; |
121 | ret->desc = "[no description]"; | 106 | ret->desc = "[no description]"; |
122 | ret->owner = NULL; | 107 | ret->owner = NULL; |
123 | ret->group = cgit_repo_group; | 108 | ret->group = cgit_repo_group; |
124 | ret->defbranch = "master"; | 109 | ret->defbranch = "master"; |
125 | ret->snapshots = cgit_snapshots; | 110 | ret->snapshots = cgit_snapshots; |
126 | ret->enable_log_filecount = cgit_enable_log_filecount; | 111 | ret->enable_log_filecount = cgit_enable_log_filecount; |
127 | ret->enable_log_linecount = cgit_enable_log_linecount; | 112 | ret->enable_log_linecount = cgit_enable_log_linecount; |
128 | ret->module_link = cgit_module_link; | 113 | ret->module_link = cgit_module_link; |
129 | ret->readme = NULL; | 114 | ret->readme = NULL; |
130 | return ret; | 115 | return ret; |
131 | } | 116 | } |
132 | 117 | ||
133 | struct repoinfo *cgit_get_repoinfo(const char *url) | 118 | struct repoinfo *cgit_get_repoinfo(const char *url) |
@@ -178,154 +163,154 @@ void cgit_global_config_cb(const char *name, const char *value) | |||
178 | else if (!strcmp(name, "cache-root-ttl")) | 163 | else if (!strcmp(name, "cache-root-ttl")) |
179 | cgit_cache_root_ttl = atoi(value); | 164 | cgit_cache_root_ttl = atoi(value); |
180 | else if (!strcmp(name, "cache-repo-ttl")) | 165 | else if (!strcmp(name, "cache-repo-ttl")) |
181 | cgit_cache_repo_ttl = atoi(value); | 166 | cgit_cache_repo_ttl = atoi(value); |
182 | else if (!strcmp(name, "cache-static-ttl")) | 167 | else if (!strcmp(name, "cache-static-ttl")) |
183 | cgit_cache_static_ttl = atoi(value); | 168 | cgit_cache_static_ttl = atoi(value); |
184 | else if (!strcmp(name, "cache-dynamic-ttl")) | 169 | else if (!strcmp(name, "cache-dynamic-ttl")) |
185 | cgit_cache_dynamic_ttl = atoi(value); | 170 | cgit_cache_dynamic_ttl = atoi(value); |
186 | else if (!strcmp(name, "max-message-length")) | 171 | else if (!strcmp(name, "max-message-length")) |
187 | cgit_max_msg_len = atoi(value); | 172 | cgit_max_msg_len = atoi(value); |
188 | else if (!strcmp(name, "max-repodesc-length")) | 173 | else if (!strcmp(name, "max-repodesc-length")) |
189 | cgit_max_repodesc_len = atoi(value); | 174 | cgit_max_repodesc_len = atoi(value); |
190 | else if (!strcmp(name, "max-commit-count")) | 175 | else if (!strcmp(name, "max-commit-count")) |
191 | cgit_max_commit_count = atoi(value); | 176 | cgit_max_commit_count = atoi(value); |
192 | else if (!strcmp(name, "summary-log")) | 177 | else if (!strcmp(name, "summary-log")) |
193 | cgit_summary_log = atoi(value); | 178 | cgit_summary_log = atoi(value); |
194 | else if (!strcmp(name, "summary-branches")) | 179 | else if (!strcmp(name, "summary-branches")) |
195 | cgit_summary_branches = atoi(value); | 180 | cgit_summary_branches = atoi(value); |
196 | else if (!strcmp(name, "summary-tags")) | 181 | else if (!strcmp(name, "summary-tags")) |
197 | cgit_summary_tags = atoi(value); | 182 | cgit_summary_tags = atoi(value); |
198 | else if (!strcmp(name, "agefile")) | 183 | else if (!strcmp(name, "agefile")) |
199 | cgit_agefile = xstrdup(value); | 184 | cgit_agefile = xstrdup(value); |
200 | else if (!strcmp(name, "renamelimit")) | 185 | else if (!strcmp(name, "renamelimit")) |
201 | cgit_renamelimit = atoi(value); | 186 | cgit_renamelimit = atoi(value); |
202 | else if (!strcmp(name, "robots")) | 187 | else if (!strcmp(name, "robots")) |
203 | cgit_robots = xstrdup(value); | 188 | cgit_robots = xstrdup(value); |
204 | else if (!strcmp(name, "clone-prefix")) | 189 | else if (!strcmp(name, "clone-prefix")) |
205 | cgit_clone_prefix = xstrdup(value); | 190 | cgit_clone_prefix = xstrdup(value); |
206 | else if (!strcmp(name, "repo.group")) | 191 | else if (!strcmp(name, "repo.group")) |
207 | cgit_repo_group = xstrdup(value); | 192 | cgit_repo_group = xstrdup(value); |
208 | else if (!strcmp(name, "repo.url")) | 193 | else if (!strcmp(name, "repo.url")) |
209 | cgit_repo = add_repo(value); | 194 | cgit_repo = add_repo(value); |
210 | else if (!strcmp(name, "repo.name")) | 195 | else if (!strcmp(name, "repo.name")) |
211 | cgit_repo->name = xstrdup(value); | 196 | cgit_repo->name = xstrdup(value); |
212 | else if (cgit_repo && !strcmp(name, "repo.path")) | 197 | else if (cgit_repo && !strcmp(name, "repo.path")) |
213 | cgit_repo->path = trim_end(value, '/'); | 198 | cgit_repo->path = trim_end(value, '/'); |
214 | else if (cgit_repo && !strcmp(name, "repo.clone-url")) | 199 | else if (cgit_repo && !strcmp(name, "repo.clone-url")) |
215 | cgit_repo->clone_url = xstrdup(value); | 200 | cgit_repo->clone_url = xstrdup(value); |
216 | else if (cgit_repo && !strcmp(name, "repo.desc")) | 201 | else if (cgit_repo && !strcmp(name, "repo.desc")) |
217 | cgit_repo->desc = xstrdup(value); | 202 | cgit_repo->desc = xstrdup(value); |
218 | else if (cgit_repo && !strcmp(name, "repo.owner")) | 203 | else if (cgit_repo && !strcmp(name, "repo.owner")) |
219 | cgit_repo->owner = xstrdup(value); | 204 | cgit_repo->owner = xstrdup(value); |
220 | else if (cgit_repo && !strcmp(name, "repo.defbranch")) | 205 | else if (cgit_repo && !strcmp(name, "repo.defbranch")) |
221 | cgit_repo->defbranch = xstrdup(value); | 206 | cgit_repo->defbranch = xstrdup(value); |
222 | else if (cgit_repo && !strcmp(name, "repo.snapshots")) | 207 | else if (cgit_repo && !strcmp(name, "repo.snapshots")) |
223 | cgit_repo->snapshots = cgit_snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | 208 | cgit_repo->snapshots = cgit_snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
224 | else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) | 209 | else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) |
225 | cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value); | 210 | cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value); |
226 | else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) | 211 | else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) |
227 | cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value); | 212 | cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value); |
228 | else if (cgit_repo && !strcmp(name, "repo.module-link")) | 213 | else if (cgit_repo && !strcmp(name, "repo.module-link")) |
229 | cgit_repo->module_link= xstrdup(value); | 214 | cgit_repo->module_link= xstrdup(value); |
230 | else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) { | 215 | else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) { |
231 | if (*value == '/') | 216 | if (*value == '/') |
232 | cgit_repo->readme = xstrdup(value); | 217 | cgit_repo->readme = xstrdup(value); |
233 | else | 218 | else |
234 | cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value)); | 219 | cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value)); |
235 | } else if (!strcmp(name, "include")) | 220 | } else if (!strcmp(name, "include")) |
236 | cgit_read_config(value, cgit_global_config_cb); | 221 | cgit_read_config(value, cgit_global_config_cb); |
237 | } | 222 | } |
238 | 223 | ||
239 | void cgit_querystring_cb(const char *name, const char *value) | 224 | void cgit_querystring_cb(const char *name, const char *value) |
240 | { | 225 | { |
241 | if (!strcmp(name,"r")) { | 226 | if (!strcmp(name,"r")) { |
242 | cgit_query_repo = xstrdup(value); | 227 | ctx.qry.repo = xstrdup(value); |
243 | cgit_repo = cgit_get_repoinfo(value); | 228 | cgit_repo = cgit_get_repoinfo(value); |
244 | } else if (!strcmp(name, "p")) { | 229 | } else if (!strcmp(name, "p")) { |
245 | cgit_query_page = xstrdup(value); | 230 | ctx.qry.page = xstrdup(value); |
246 | cgit_cmd = cgit_get_cmd_index(value); | 231 | cgit_cmd = cgit_get_cmd_index(value); |
247 | } else if (!strcmp(name, "url")) { | 232 | } else if (!strcmp(name, "url")) { |
248 | cgit_parse_url(value); | 233 | cgit_parse_url(value); |
249 | } else if (!strcmp(name, "qt")) { | 234 | } else if (!strcmp(name, "qt")) { |
250 | cgit_query_grep = xstrdup(value); | 235 | ctx.qry.grep = xstrdup(value); |
251 | } else if (!strcmp(name, "q")) { | 236 | } else if (!strcmp(name, "q")) { |
252 | cgit_query_search = xstrdup(value); | 237 | ctx.qry.search = xstrdup(value); |
253 | } else if (!strcmp(name, "h")) { | 238 | } else if (!strcmp(name, "h")) { |
254 | cgit_query_head = xstrdup(value); | 239 | ctx.qry.head = xstrdup(value); |
255 | cgit_query_has_symref = 1; | 240 | ctx.qry.has_symref = 1; |
256 | } else if (!strcmp(name, "id")) { | 241 | } else if (!strcmp(name, "id")) { |
257 | cgit_query_sha1 = xstrdup(value); | 242 | ctx.qry.sha1 = xstrdup(value); |
258 | cgit_query_has_sha1 = 1; | 243 | ctx.qry.has_sha1 = 1; |
259 | } else if (!strcmp(name, "id2")) { | 244 | } else if (!strcmp(name, "id2")) { |
260 | cgit_query_sha2 = xstrdup(value); | 245 | ctx.qry.sha2 = xstrdup(value); |
261 | cgit_query_has_sha1 = 1; | 246 | ctx.qry.has_sha1 = 1; |
262 | } else if (!strcmp(name, "ofs")) { | 247 | } else if (!strcmp(name, "ofs")) { |
263 | cgit_query_ofs = atoi(value); | 248 | ctx.qry.ofs = atoi(value); |
264 | } else if (!strcmp(name, "path")) { | 249 | } else if (!strcmp(name, "path")) { |
265 | cgit_query_path = trim_end(value, '/'); | 250 | ctx.qry.path = trim_end(value, '/'); |
266 | } else if (!strcmp(name, "name")) { | 251 | } else if (!strcmp(name, "name")) { |
267 | cgit_query_name = xstrdup(value); | 252 | ctx.qry.name = xstrdup(value); |
268 | } | 253 | } |
269 | } | 254 | } |
270 | 255 | ||
271 | void *cgit_free_commitinfo(struct commitinfo *info) | 256 | void *cgit_free_commitinfo(struct commitinfo *info) |
272 | { | 257 | { |
273 | free(info->author); | 258 | free(info->author); |
274 | free(info->author_email); | 259 | free(info->author_email); |
275 | free(info->committer); | 260 | free(info->committer); |
276 | free(info->committer_email); | 261 | free(info->committer_email); |
277 | free(info->subject); | 262 | free(info->subject); |
278 | free(info->msg); | 263 | free(info->msg); |
279 | free(info->msg_encoding); | 264 | free(info->msg_encoding); |
280 | free(info); | 265 | free(info); |
281 | return NULL; | 266 | return NULL; |
282 | } | 267 | } |
283 | 268 | ||
284 | int hextoint(char c) | 269 | int hextoint(char c) |
285 | { | 270 | { |
286 | if (c >= 'a' && c <= 'f') | 271 | if (c >= 'a' && c <= 'f') |
287 | return 10 + c - 'a'; | 272 | return 10 + c - 'a'; |
288 | else if (c >= 'A' && c <= 'F') | 273 | else if (c >= 'A' && c <= 'F') |
289 | return 10 + c - 'A'; | 274 | return 10 + c - 'A'; |
290 | else if (c >= '0' && c <= '9') | 275 | else if (c >= '0' && c <= '9') |
291 | return c - '0'; | 276 | return c - '0'; |
292 | else | 277 | else |
293 | return -1; | 278 | return -1; |
294 | } | 279 | } |
295 | 280 | ||
296 | char *trim_end(const char *str, char c) | 281 | char *trim_end(const char *str, char c) |
297 | { | 282 | { |
298 | int len; | 283 | int len; |
299 | char *s, *t; | 284 | char *s, *t; |
300 | 285 | ||
301 | if (str == NULL) | 286 | if (str == NULL) |
302 | return NULL; | 287 | return NULL; |
303 | t = (char *)str; | 288 | t = (char *)str; |
304 | len = strlen(t); | 289 | len = strlen(t); |
305 | while(len > 0 && t[len - 1] == c) | 290 | while(len > 0 && t[len - 1] == c) |
306 | len--; | 291 | len--; |
307 | 292 | ||
308 | if (len == 0) | 293 | if (len == 0) |
309 | return NULL; | 294 | return NULL; |
310 | 295 | ||
311 | c = t[len]; | 296 | c = t[len]; |
312 | t[len] = '\0'; | 297 | t[len] = '\0'; |
313 | s = xstrdup(t); | 298 | s = xstrdup(t); |
314 | t[len] = c; | 299 | t[len] = c; |
315 | return s; | 300 | return s; |
316 | } | 301 | } |
317 | 302 | ||
318 | char *strlpart(char *txt, int maxlen) | 303 | char *strlpart(char *txt, int maxlen) |
319 | { | 304 | { |
320 | char *result; | 305 | char *result; |
321 | 306 | ||
322 | if (!txt) | 307 | if (!txt) |
323 | return txt; | 308 | return txt; |
324 | 309 | ||
325 | if (strlen(txt) <= maxlen) | 310 | if (strlen(txt) <= maxlen) |
326 | return txt; | 311 | return txt; |
327 | result = xmalloc(maxlen + 1); | 312 | result = xmalloc(maxlen + 1); |
328 | memcpy(result, txt, maxlen - 3); | 313 | memcpy(result, txt, maxlen - 3); |
329 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; | 314 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; |
330 | result[maxlen] = '\0'; | 315 | result[maxlen] = '\0'; |
331 | return result; | 316 | return result; |