blob: 8ccca9eca6fca9445996fe10a9bc23d321696d3c [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2023,2024 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30/****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 ****************************************************************************/
35
36/*
37 * write_entry.c -- write a terminfo structure onto the file system
38 */
39
40#include <curses.priv.h>
41#include <hashed_db.h>
42
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053043#include <tic.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053044
micky3879b9f5e72025-07-08 18:04:53 -040045MODULE_ID("$Id: write_entry.c,v 1.132 2024/04/20 17:58:51 tom Exp $")
46
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053047#if 1
48#define TRACE_OUT(p) DEBUG(2, p)
micky3879b9f5e72025-07-08 18:04:53 -040049#define TRACE_NUM(n) if (VALID_NUMERIC(Numbers[n])) { \
50 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) (n), Numbers[n])); }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053051#else
52#define TRACE_OUT(p) /*nothing */
micky3879b9f5e72025-07-08 18:04:53 -040053#define TRACE_NUM(n) /* nothing */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053054#endif
55
micky3879b9f5e72025-07-08 18:04:53 -040056/*
57 * FIXME: special case to work around Cygwin bug in link(), which updates
58 * the target file's timestamp.
59 */
60#if HAVE_LINK && !USE_SYMLINKS && !MIXEDCASE_FILENAMES && defined(__CYGWIN__)
61#define LINK_TOUCHES 1
62#else
63#define LINK_TOUCHES 0
64#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053065
66static int total_written;
micky3879b9f5e72025-07-08 18:04:53 -040067static int total_parts;
68static int total_size;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053069
70static int make_db_root(const char *);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053071
72#if !USE_HASHED_DB
73static void
micky3879b9f5e72025-07-08 18:04:53 -040074write_file(char *filename, TERMTYPE2 *tp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053075{
76 char buffer[MAX_ENTRY_SIZE];
77 unsigned limit = sizeof(buffer);
78 unsigned offset = 0;
79
micky3879b9f5e72025-07-08 18:04:53 -040080 if (_nc_write_object(tp, buffer, &offset, limit) == ERR) {
81 _nc_warning("entry is larger than %u bytes", limit);
82 } else {
83 FILE *fp = ((_nc_access(filename, W_OK) == 0)
84 ? safe_fopen(filename, BIN_W)
85 : 0);
86 size_t actual;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053087
micky3879b9f5e72025-07-08 18:04:53 -040088 if (fp == 0) {
89 perror(filename);
90 _nc_syserr_abort("cannot open %s/%s", _nc_tic_dir(0), filename);
91 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053092
micky3879b9f5e72025-07-08 18:04:53 -040093 actual = fwrite(buffer, sizeof(char), (size_t) offset, fp);
94 if (actual != offset) {
95 int myerr = ferror(fp) ? errno : 0;
96 if (myerr) {
97 _nc_syserr_abort("error writing %s/%s: %s",
98 _nc_tic_dir(NULL),
99 filename,
100 strerror(myerr));
101 } else {
102 _nc_syserr_abort("error writing %s/%s: %u bytes vs actual %lu",
103 _nc_tic_dir(NULL),
104 filename,
105 offset,
106 (unsigned long) actual);
107 }
108 } else {
109 fclose(fp);
110 DEBUG(1, ("Created %s", filename));
111 }
112 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530113}
114
115/*
116 * Check for access rights to destination directories
117 * Create any directories which don't exist.
118 *
119 * Note: there's no reason to return the result of make_db_root(), since
120 * this function is called only in instances where that has to succeed.
121 */
122static void
123check_writeable(int code)
124{
125 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
126 static bool verified[sizeof(dirnames)];
127
128 char dir[sizeof(LEAF_FMT)];
129 char *s = 0;
130
micky3879b9f5e72025-07-08 18:04:53 -0400131 if (code == 0 || (s = (strchr) (dirnames, code)) == 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530132 _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
micky3879b9f5e72025-07-08 18:04:53 -0400133 } else if (!verified[s - dirnames]) {
134 _nc_SPRINTF(dir, _nc_SLIMIT(sizeof(dir)) LEAF_FMT, code);
135 if (make_db_root(dir) < 0) {
136 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(NULL), dir);
137 } else {
138 verified[s - dirnames] = TRUE;
139 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530140 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530141}
142#endif /* !USE_HASHED_DB */
143
144static int
Steve Kondikae271bc2015-11-15 02:50:53 +0100145make_db_path(char *dst, const char *src, size_t limit)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530146{
147 int rc = -1;
micky3879b9f5e72025-07-08 18:04:53 -0400148 const char *top = _nc_tic_dir(NULL);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530149
150 if (src == top || _nc_is_abs_path(src)) {
151 if (strlen(src) + 1 <= limit) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100152 _nc_STRCPY(dst, src, limit);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530153 rc = 0;
154 }
155 } else {
micky3879b9f5e72025-07-08 18:04:53 -0400156 if ((strlen(top) + strlen(src) + 6) <= limit) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100157 _nc_SPRINTF(dst, _nc_SLIMIT(limit) "%s/%s", top, src);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530158 rc = 0;
159 }
160 }
161#if USE_HASHED_DB
162 if (rc == 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100163 static const char suffix[] = DBM_SUFFIX;
164 size_t have = strlen(dst);
165 size_t need = strlen(suffix);
166 if (have > need && strcmp(dst + (int) (have - need), suffix)) {
167 if (have + need <= limit) {
168 _nc_STRCAT(dst, suffix, limit);
169 } else {
170 rc = -1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530171 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100172 } else if (_nc_is_dir_path(dst)) {
173 rc = -1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530174 }
175 }
176#endif
177 return rc;
178}
179
180/*
181 * Make a database-root if it doesn't exist.
182 */
183static int
184make_db_root(const char *path)
185{
186 int rc;
187 char fullpath[PATH_MAX];
188
189 if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
190#if USE_HASHED_DB
191 DB *capdbp;
192
Steve Kondikae271bc2015-11-15 02:50:53 +0100193 if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530194 rc = -1;
Steve Kondikae271bc2015-11-15 02:50:53 +0100195 } else if (_nc_db_close(capdbp) < 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530196 rc = -1;
Steve Kondikae271bc2015-11-15 02:50:53 +0100197 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530198#else
199 struct stat statbuf;
200
micky3879b9f5e72025-07-08 18:04:53 -0400201 if ((rc = stat(path, &statbuf)) == -1) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100202 rc = mkdir(path
micky3879b9f5e72025-07-08 18:04:53 -0400203#ifndef _NC_WINDOWS
Steve Kondikae271bc2015-11-15 02:50:53 +0100204 ,0777
205#endif
206 );
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530207 } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
208 rc = -1; /* permission denied */
209 } else if (!(S_ISDIR(statbuf.st_mode))) {
210 rc = -1; /* not a directory */
211 }
212#endif
213 }
214 return rc;
215}
216
217/*
218 * Set the write directory for compiled entries.
219 */
220NCURSES_EXPORT(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100221_nc_set_writedir(const char *dir)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530222{
223 const char *destination;
224 char actual[PATH_MAX];
micky3879b9f5e72025-07-08 18:04:53 -0400225 bool specific = (dir != NULL);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530226
micky3879b9f5e72025-07-08 18:04:53 -0400227 if (!specific && use_terminfo_vars())
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530228 dir = getenv("TERMINFO");
229
micky3879b9f5e72025-07-08 18:04:53 -0400230 if (dir != NULL)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530231 (void) _nc_tic_dir(dir);
232
micky3879b9f5e72025-07-08 18:04:53 -0400233 destination = _nc_tic_dir(NULL);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530234 if (make_db_root(destination) < 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400235 bool success = FALSE;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530236
micky3879b9f5e72025-07-08 18:04:53 -0400237 if (!specific) {
238 char *home = _nc_home_terminfo();
239
240 if (home != NULL) {
241 destination = home;
242 if (make_db_root(destination) == 0)
243 success = TRUE;
244 }
245 }
246 if (!success) {
247 _nc_err_abort("%s: permission denied (errno %d)",
248 destination, errno);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530249 }
250 }
251
252 /*
253 * Note: because of this code, this logic should be exercised
254 * *once only* per run.
255 */
256#if USE_HASHED_DB
257 make_db_path(actual, destination, sizeof(actual));
258#else
259 if (chdir(_nc_tic_dir(destination)) < 0
micky3879b9f5e72025-07-08 18:04:53 -0400260 || getcwd(actual, sizeof(actual)) == NULL)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530261 _nc_err_abort("%s: not a directory", destination);
262#endif
micky3879b9f5e72025-07-08 18:04:53 -0400263 _nc_keep_tic_dir(actual);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530264}
265
266/*
267 * Save the compiled version of a description in the filesystem.
268 *
269 * make a copy of the name-list
270 * break it up into first-name and all-but-last-name
271 * creat(first-name)
272 * write object information to first-name
273 * close(first-name)
274 * for each name in all-but-last-name
275 * link to first-name
276 *
277 * Using 'time()' to obtain a reference for file timestamps is unreliable,
278 * e.g., with NFS, because the filesystem may have a different time
279 * reference. We check for pre-existence of links by latching the first
280 * timestamp from a file that we create.
281 *
282 * The _nc_warning() calls will report a correct line number only if
283 * _nc_curr_line is properly set before the write_entry() call.
284 */
285
286NCURSES_EXPORT(void)
micky3879b9f5e72025-07-08 18:04:53 -0400287_nc_write_entry(TERMTYPE2 *const tp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530288{
289#if USE_HASHED_DB
290
291 char buffer[MAX_ENTRY_SIZE + 1];
292 unsigned limit = sizeof(buffer);
293 unsigned offset = 0;
294
295#else /* !USE_HASHED_DB */
296
297 struct stat statbuf;
298 char filename[PATH_MAX];
299 char linkname[PATH_MAX];
300#if USE_SYMLINKS
301 char symlinkname[PATH_MAX];
302#if !HAVE_LINK
303#undef HAVE_LINK
304#define HAVE_LINK 1
305#endif
306#endif /* USE_SYMLINKS */
307
micky3879b9f5e72025-07-08 18:04:53 -0400308 unsigned limit2 = sizeof(filename) - (2 + LEAF_LEN);
309 char saved = '\0';
310
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530311 static int call_count;
312 static time_t start_time; /* time at start of writes */
313
314#endif /* USE_HASHED_DB */
315
316 char name_list[MAX_TERMINFO_LENGTH];
317 char *first_name, *other_names;
318 char *ptr;
Steve Kondikae271bc2015-11-15 02:50:53 +0100319 char *term_names = tp->term_names;
320 size_t name_size = strlen(term_names);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530321
Steve Kondikae271bc2015-11-15 02:50:53 +0100322 if (name_size == 0) {
323 _nc_syserr_abort("no terminal name found.");
324 } else if (name_size >= sizeof(name_list) - 1) {
325 _nc_syserr_abort("terminal name too long: %s", term_names);
326 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530327
Steve Kondikae271bc2015-11-15 02:50:53 +0100328 _nc_STRCPY(name_list, term_names, sizeof(name_list));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530329 DEBUG(7, ("Name list = '%s'", name_list));
330
331 first_name = name_list;
332
Steve Kondikae271bc2015-11-15 02:50:53 +0100333 ptr = &name_list[name_size - 1];
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530334 other_names = ptr + 1;
335
336 while (ptr > name_list && *ptr != '|')
337 ptr--;
338
339 if (ptr != name_list) {
340 *ptr = '\0';
341
micky3879b9f5e72025-07-08 18:04:53 -0400342 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++) {
343 /* EMPTY */ ;
344 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530345
346 if (*ptr == '\0')
347 other_names = ptr;
348 else {
349 *ptr = '\0';
350 other_names = ptr + 1;
351 }
352 }
353
354 DEBUG(7, ("First name = '%s'", first_name));
355 DEBUG(7, ("Other names = '%s'", other_names));
356
357 _nc_set_type(first_name);
358
359#if USE_HASHED_DB
micky3879b9f5e72025-07-08 18:04:53 -0400360 if (_nc_write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
361 DB *capdb = _nc_db_open(_nc_tic_dir(NULL), TRUE);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530362 DBT key, data;
363
micky3879b9f5e72025-07-08 18:04:53 -0400364 if (capdb != NULL) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530365 buffer[0] = 0;
366
367 memset(&key, 0, sizeof(key));
Steve Kondikae271bc2015-11-15 02:50:53 +0100368 key.data = term_names;
369 key.size = name_size;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530370
371 memset(&data, 0, sizeof(data));
372 data.data = buffer;
373 data.size = offset + 1;
374
375 _nc_db_put(capdb, &key, &data);
376
377 buffer[0] = 2;
378
379 key.data = name_list;
380 key.size = strlen(name_list);
381
Steve Kondikae271bc2015-11-15 02:50:53 +0100382 _nc_STRCPY(buffer + 1,
383 term_names,
384 sizeof(buffer) - 1);
385 data.size = name_size + 1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530386
micky3879b9f5e72025-07-08 18:04:53 -0400387 total_size += (int) data.size;
388 total_parts++;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530389 _nc_db_put(capdb, &key, &data);
390
391 while (*other_names != '\0') {
392 ptr = other_names++;
Steve Kondikae271bc2015-11-15 02:50:53 +0100393 assert(ptr < buffer + sizeof(buffer) - 1);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530394 while (*other_names != '|' && *other_names != '\0')
395 other_names++;
396
397 if (*other_names != '\0')
398 *(other_names++) = '\0';
399
400 key.data = ptr;
401 key.size = strlen(ptr);
402
micky3879b9f5e72025-07-08 18:04:53 -0400403 total_size += (int) data.size;
404 total_parts++;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530405 _nc_db_put(capdb, &key, &data);
406 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530407 }
408 }
409#else /* !USE_HASHED_DB */
410 if (call_count++ == 0) {
411 start_time = 0;
412 }
413
micky3879b9f5e72025-07-08 18:04:53 -0400414 if (strlen(first_name) >= limit2) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530415 _nc_warning("terminal name too long.");
micky3879b9f5e72025-07-08 18:04:53 -0400416 saved = first_name[limit2];
417 first_name[limit2] = '\0';
418 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530419
Steve Kondikae271bc2015-11-15 02:50:53 +0100420 _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
micky3879b9f5e72025-07-08 18:04:53 -0400421 LEAF_FMT "/%.*s", UChar(first_name[0]),
422 (int) (sizeof(filename) - (LEAF_LEN + 2)),
423 first_name);
424
425 if (saved)
426 first_name[limit2] = saved;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530427
428 /*
429 * Has this primary name been written since the first call to
430 * write_entry()? If so, the newer write will step on the older,
431 * so warn the user.
432 */
433 if (start_time > 0 &&
434 stat(filename, &statbuf) >= 0
435 && statbuf.st_mtime >= start_time) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100436#if HAVE_LINK && !USE_SYMLINKS
437 /*
438 * If the file has more than one link, the reason for the previous
439 * write could be that the current primary name used to be an alias for
440 * the previous entry. In that case, unlink the file so that we will
441 * not modify the previous entry as we write this one.
442 */
443 if (statbuf.st_nlink > 1) {
444 _nc_warning("name redefined.");
445 unlink(filename);
446 } else {
447 _nc_warning("name multiply defined.");
448 }
449#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530450 _nc_warning("name multiply defined.");
Steve Kondikae271bc2015-11-15 02:50:53 +0100451#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530452 }
453
454 check_writeable(first_name[0]);
455 write_file(filename, tp);
456
457 if (start_time == 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400458 if (stat(filename, &statbuf) == -1
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530459 || (start_time = statbuf.st_mtime) == 0) {
460 _nc_syserr_abort("error obtaining time from %s/%s",
micky3879b9f5e72025-07-08 18:04:53 -0400461 _nc_tic_dir(NULL), filename);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530462 }
463 }
464 while (*other_names != '\0') {
465 ptr = other_names++;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530466 while (*other_names != '|' && *other_names != '\0')
467 other_names++;
468
469 if (*other_names != '\0')
470 *(other_names++) = '\0';
471
Steve Kondikae271bc2015-11-15 02:50:53 +0100472 if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530473 _nc_warning("terminal alias %s too long.", ptr);
474 continue;
475 }
micky3879b9f5e72025-07-08 18:04:53 -0400476 if (strchr(ptr, '/') != NULL) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530477 _nc_warning("cannot link alias %s.", ptr);
478 continue;
479 }
480
481 check_writeable(ptr[0]);
Steve Kondikae271bc2015-11-15 02:50:53 +0100482 _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
micky3879b9f5e72025-07-08 18:04:53 -0400483 LEAF_FMT "/%.*s", ptr[0],
484 (int) sizeof(linkname) - (2 + LEAF_LEN), ptr);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530485
486 if (strcmp(filename, linkname) == 0) {
487 _nc_warning("self-synonym ignored");
micky3879b9f5e72025-07-08 18:04:53 -0400488 }
489#if !LINK_TOUCHES
490 else if (stat(linkname, &statbuf) >= 0 &&
491 statbuf.st_mtime < start_time) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530492 _nc_warning("alias %s multiply defined.", ptr);
micky3879b9f5e72025-07-08 18:04:53 -0400493 }
494#endif
495 else if (_nc_access(linkname, W_OK) == 0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530496#if HAVE_LINK
497 {
498 int code;
499#if USE_SYMLINKS
micky3879b9f5e72025-07-08 18:04:53 -0400500#define MY_SIZE sizeof(symlinkname) - 1
501 if (first_name[0] == linkname[0]) {
502 _nc_STRNCPY(symlinkname, first_name, MY_SIZE);
503 } else {
504 _nc_STRCPY(symlinkname, "../", sizeof(symlinkname));
505 _nc_STRNCPY(symlinkname + 3, filename, MY_SIZE - 3);
Steve Kondikae271bc2015-11-15 02:50:53 +0100506 }
micky3879b9f5e72025-07-08 18:04:53 -0400507 symlinkname[MY_SIZE] = '\0';
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530508#endif /* USE_SYMLINKS */
509#if HAVE_REMOVE
510 code = remove(linkname);
511#else
512 code = unlink(linkname);
513#endif
514 if (code != 0 && errno == ENOENT)
515 code = 0;
516#if USE_SYMLINKS
517 if (symlink(symlinkname, linkname) < 0)
518#else
519 if (link(filename, linkname) < 0)
520#endif /* USE_SYMLINKS */
521 {
522 /*
523 * If there wasn't anything there, and we cannot
524 * link to the target because it is the same as the
525 * target, then the source must be on a filesystem
526 * that uses caseless filenames, such as Win32, etc.
527 */
528 if (code == 0 && errno == EEXIST)
529 _nc_warning("can't link %s to %s", filename, linkname);
530 else if (code == 0 && (errno == EPERM || errno == ENOENT))
531 write_file(linkname, tp);
532 else {
533#if MIXEDCASE_FILENAMES
micky3879b9f5e72025-07-08 18:04:53 -0400534 _nc_syserr_abort("cannot link %s to %s", filename, linkname);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530535#else
micky3879b9f5e72025-07-08 18:04:53 -0400536 _nc_warning("cannot link %s to %s (errno=%d)", filename,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530537 linkname, errno);
538#endif
539 }
540 } else {
541 DEBUG(1, ("Linked %s", linkname));
542 }
543 }
544#else /* just make copies */
545 write_file(linkname, tp);
546#endif /* HAVE_LINK */
547 }
548#endif /* USE_HASHED_DB */
549}
550
Steve Kondikae271bc2015-11-15 02:50:53 +0100551static size_t
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530552fake_write(char *dst,
553 unsigned *offset,
Steve Kondikae271bc2015-11-15 02:50:53 +0100554 size_t limit,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530555 char *src,
Steve Kondikae271bc2015-11-15 02:50:53 +0100556 size_t want,
557 size_t size)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530558{
Steve Kondikae271bc2015-11-15 02:50:53 +0100559 size_t have = (limit - *offset);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530560
561 want *= size;
562 if (have > 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100563 if (want > have)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530564 want = have;
565 memcpy(dst + *offset, src, want);
Steve Kondikae271bc2015-11-15 02:50:53 +0100566 *offset += (unsigned) want;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530567 } else {
568 want = 0;
569 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100570 return (want / size);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530571}
572
Steve Kondikae271bc2015-11-15 02:50:53 +0100573#define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530574
575#undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
576#define HI(x) ((x) / 256)
577#define LO(x) ((x) % 256)
Steve Kondikae271bc2015-11-15 02:50:53 +0100578#define LITTLE_ENDIAN(p, x) (p)[0] = (unsigned char)LO(x), \
579 (p)[1] = (unsigned char)HI(x)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530580
581#define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
582
583static int
Steve Kondikae271bc2015-11-15 02:50:53 +0100584compute_offsets(char **Strings, size_t strmax, short *offsets)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530585{
Steve Kondikae271bc2015-11-15 02:50:53 +0100586 int nextfree = 0;
587 size_t i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530588
589 for (i = 0; i < strmax; i++) {
590 if (Strings[i] == ABSENT_STRING) {
591 offsets[i] = -1;
592 } else if (Strings[i] == CANCELLED_STRING) {
593 offsets[i] = -2;
594 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100595 offsets[i] = (short) nextfree;
596 nextfree += (int) strlen(Strings[i]) + 1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530597 TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
598 _nc_visbuf(Strings[i]), (int) nextfree));
599 }
600 }
601 return nextfree;
602}
603
micky3879b9f5e72025-07-08 18:04:53 -0400604static size_t
Steve Kondikae271bc2015-11-15 02:50:53 +0100605convert_shorts(unsigned char *buf, short *Numbers, size_t count)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530606{
Steve Kondikae271bc2015-11-15 02:50:53 +0100607 size_t i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530608 for (i = 0; i < count; i++) {
609 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */
610 buf[2 * i] = buf[2 * i + 1] = 0377;
611 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */
612 buf[2 * i] = 0376;
613 buf[2 * i + 1] = 0377;
614 } else {
615 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
Steve Kondikae271bc2015-11-15 02:50:53 +0100616 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530617 }
618 }
micky3879b9f5e72025-07-08 18:04:53 -0400619 return SIZEOF_SHORT;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530620}
621
micky3879b9f5e72025-07-08 18:04:53 -0400622#if NCURSES_EXT_NUMBERS
623static size_t
624convert_16bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
625{
626 size_t i, j;
627 size_t size = SIZEOF_SHORT;
628 for (i = 0; i < count; i++) {
629 unsigned value = (unsigned) Numbers[i];
630 TRACE_NUM(i);
631 for (j = 0; j < size; ++j) {
632 *buf++ = value & 0xff;
633 value >>= 8;
634 }
635 }
636 return size;
637}
638
639static size_t
640convert_32bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
641{
642 size_t i, j;
643 size_t size = SIZEOF_INT2;
644 for (i = 0; i < count; i++) {
645 unsigned value = (unsigned) Numbers[i];
646 TRACE_NUM(i);
647 for (j = 0; j < size; ++j) {
648 *buf++ = value & 0xff;
649 value >>= 8;
650 }
651 }
652 return size;
653}
654#endif
655
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530656#define even_boundary(value) \
657 ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
658
659#if NCURSES_XNAMES
660static unsigned
micky3879b9f5e72025-07-08 18:04:53 -0400661extended_Booleans(TERMTYPE2 *tp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530662{
Steve Kondikae271bc2015-11-15 02:50:53 +0100663 unsigned result = 0;
664 unsigned i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530665
666 for (i = 0; i < tp->ext_Booleans; ++i) {
667 if (tp->Booleans[BOOLCOUNT + i] == TRUE)
668 result = (i + 1);
669 }
670 return result;
671}
672
673static unsigned
micky3879b9f5e72025-07-08 18:04:53 -0400674extended_Numbers(TERMTYPE2 *tp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530675{
Steve Kondikae271bc2015-11-15 02:50:53 +0100676 unsigned result = 0;
677 unsigned i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530678
679 for (i = 0; i < tp->ext_Numbers; ++i) {
680 if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
681 result = (i + 1);
682 }
683 return result;
684}
685
686static unsigned
micky3879b9f5e72025-07-08 18:04:53 -0400687extended_Strings(TERMTYPE2 *tp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530688{
689 unsigned short result = 0;
690 unsigned short i;
691
692 for (i = 0; i < tp->ext_Strings; ++i) {
693 if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
Steve Kondikae271bc2015-11-15 02:50:53 +0100694 result = (unsigned short) (i + 1);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530695 }
696 return result;
697}
698
699/*
700 * _nc_align_termtype() will extend entries that are referenced in a use=
701 * clause - discard the unneeded data.
702 */
703static bool
micky3879b9f5e72025-07-08 18:04:53 -0400704extended_object(TERMTYPE2 *tp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530705{
706 bool result = FALSE;
707
708 if (_nc_user_definable) {
709 result = ((extended_Booleans(tp)
710 + extended_Numbers(tp)
711 + extended_Strings(tp)) != 0);
712 }
713 return result;
714}
715#endif
716
micky3879b9f5e72025-07-08 18:04:53 -0400717NCURSES_EXPORT(int)
718_nc_write_object(TERMTYPE2 *tp, char *buffer, unsigned *offset, unsigned limit)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530719{
720 char *namelist;
micky3879b9f5e72025-07-08 18:04:53 -0400721 size_t namelen, boolmax, nummax, strmax, numlen;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530722 char zero = '\0';
723 size_t i;
Steve Kondikae271bc2015-11-15 02:50:53 +0100724 int nextfree;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530725 short offsets[MAX_ENTRY_SIZE / 2];
726 unsigned char buf[MAX_ENTRY_SIZE];
727 unsigned last_bool = BOOLWRITE;
728 unsigned last_num = NUMWRITE;
729 unsigned last_str = STRWRITE;
micky3879b9f5e72025-07-08 18:04:53 -0400730#if NCURSES_EXT_NUMBERS
731 bool need_ints = FALSE;
732 size_t (*convert_numbers) (unsigned char *, NCURSES_INT2 *, size_t);
733#else
734#define convert_numbers convert_shorts
735#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530736
737#if NCURSES_XNAMES
738 /*
739 * Normally we limit the list of values to exclude the "obsolete"
740 * capabilities. However, if we are accepting extended names, add
741 * these as well, since they are used for supporting translation
742 * to/from termcap.
743 */
744 if (_nc_user_definable) {
745 last_bool = BOOLCOUNT;
746 last_num = NUMCOUNT;
747 last_str = STRCOUNT;
748 }
749#endif
750
751 namelist = tp->term_names;
752 namelen = strlen(namelist) + 1;
753
754 boolmax = 0;
755 for (i = 0; i < last_bool; i++) {
micky3879b9f5e72025-07-08 18:04:53 -0400756 if (tp->Booleans[i] == TRUE) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530757 boolmax = i + 1;
micky3879b9f5e72025-07-08 18:04:53 -0400758 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530759 }
760
761 nummax = 0;
762 for (i = 0; i < last_num; i++) {
micky3879b9f5e72025-07-08 18:04:53 -0400763 if (tp->Numbers[i] != ABSENT_NUMERIC) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530764 nummax = i + 1;
micky3879b9f5e72025-07-08 18:04:53 -0400765#if NCURSES_EXT_NUMBERS
766 if (tp->Numbers[i] > MAX_OF_TYPE(NCURSES_COLOR_T)) {
767 need_ints = TRUE;
768 }
769#endif
770 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530771 }
772
773 strmax = 0;
774 for (i = 0; i < last_str; i++) {
775 if (tp->Strings[i] != ABSENT_STRING)
776 strmax = i + 1;
777 }
778
779 nextfree = compute_offsets(tp->Strings, strmax, offsets);
780
781 /* fill in the header */
micky3879b9f5e72025-07-08 18:04:53 -0400782#if NCURSES_EXT_NUMBERS
783 if (need_ints) {
784 convert_numbers = convert_32bit;
785 LITTLE_ENDIAN(buf, MAGIC2);
786 } else {
787 convert_numbers = convert_16bit;
788 LITTLE_ENDIAN(buf, MAGIC);
789 }
790#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530791 LITTLE_ENDIAN(buf, MAGIC);
micky3879b9f5e72025-07-08 18:04:53 -0400792#endif
793 namelen = Min(namelen, MAX_NAME_SIZE + 1);
794 LITTLE_ENDIAN(buf + 2, namelen);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530795 LITTLE_ENDIAN(buf + 4, boolmax);
796 LITTLE_ENDIAN(buf + 6, nummax);
797 LITTLE_ENDIAN(buf + 8, strmax);
798 LITTLE_ENDIAN(buf + 10, nextfree);
799
800 /* write out the header */
801 TRACE_OUT(("Header of %s @%d", namelist, *offset));
802 if (Write(buf, 12, 1) != 1
micky3879b9f5e72025-07-08 18:04:53 -0400803 || Write(namelist, sizeof(char), namelen) != namelen) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530804 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400805 }
806
807 for (i = 0; i < boolmax; i++) {
808 if (tp->Booleans[i] == TRUE) {
809 buf[i] = TRUE;
810 } else {
811 buf[i] = FALSE;
812 }
813 }
814 if (Write(buf, sizeof(char), boolmax) != boolmax) {
815 return (ERR);
816 }
817
818 if (even_boundary(namelen + boolmax)) {
819 return (ERR);
820 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530821
822 TRACE_OUT(("Numerics begin at %04x", *offset));
823
824 /* the numerics */
micky3879b9f5e72025-07-08 18:04:53 -0400825 numlen = convert_numbers(buf, tp->Numbers, nummax);
826 if (Write(buf, numlen, nummax) != nummax) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530827 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400828 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530829
830 TRACE_OUT(("String offsets begin at %04x", *offset));
831
832 /* the string offsets */
833 convert_shorts(buf, offsets, strmax);
micky3879b9f5e72025-07-08 18:04:53 -0400834 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530835 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400836 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530837
838 TRACE_OUT(("String table begins at %04x", *offset));
839
840 /* the strings */
micky3879b9f5e72025-07-08 18:04:53 -0400841 for (i = 0; i < strmax; i++) {
842 if (VALID_STRING(tp->Strings[i])) {
843 if (!WRITE_STRING(tp->Strings[i])) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530844 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400845 }
846 }
847 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530848
849#if NCURSES_XNAMES
850 if (extended_object(tp)) {
micky3879b9f5e72025-07-08 18:04:53 -0400851 unsigned ext_total = (unsigned) NUM_EXT_NAMES(tp);
852 unsigned ext_usage = ext_total;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530853
micky3879b9f5e72025-07-08 18:04:53 -0400854 if (even_boundary(nextfree)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530855 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400856 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530857
858 nextfree = compute_offsets(tp->Strings + STRCOUNT,
Steve Kondikae271bc2015-11-15 02:50:53 +0100859 (size_t) tp->ext_Strings,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530860 offsets);
861 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
862
micky3879b9f5e72025-07-08 18:04:53 -0400863 if (tp->ext_Strings >= SIZEOF(offsets)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530864 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400865 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530866
867 nextfree += compute_offsets(tp->ext_Names,
micky3879b9f5e72025-07-08 18:04:53 -0400868 (size_t) ext_total,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530869 offsets + tp->ext_Strings);
870 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
micky3879b9f5e72025-07-08 18:04:53 -0400871 strmax = tp->ext_Strings + ext_total;
872 for (i = 0; i < tp->ext_Strings; ++i) {
873 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
874 ext_usage++;
875 }
876 }
877 TRACE_OUT(("will write %u/%lu strings", ext_usage, (unsigned long) strmax));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530878
879 /*
880 * Write the extended header
881 */
882 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
883 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
884 LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
micky3879b9f5e72025-07-08 18:04:53 -0400885 LITTLE_ENDIAN(buf + 6, ext_usage);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530886 LITTLE_ENDIAN(buf + 8, nextfree);
887 TRACE_OUT(("WRITE extended-header @%d", *offset));
micky3879b9f5e72025-07-08 18:04:53 -0400888 if (Write(buf, 10, 1) != 1) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530889 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400890 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530891
892 TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
893 if (tp->ext_Booleans
894 && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
micky3879b9f5e72025-07-08 18:04:53 -0400895 tp->ext_Booleans) != tp->ext_Booleans) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530896 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400897 }
898
899 if (even_boundary(tp->ext_Booleans)) {
900 return (ERR);
901 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530902
903 TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
904 if (tp->ext_Numbers) {
micky3879b9f5e72025-07-08 18:04:53 -0400905 numlen = convert_numbers(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
906 if (Write(buf, numlen, tp->ext_Numbers) != tp->ext_Numbers) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530907 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400908 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530909 }
910
911 /*
912 * Convert the offsets for the ext_Strings and ext_Names tables,
913 * in that order.
914 */
915 convert_shorts(buf, offsets, strmax);
916 TRACE_OUT(("WRITE offsets @%d", *offset));
micky3879b9f5e72025-07-08 18:04:53 -0400917 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530918 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400919 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530920
921 /*
922 * Write the string table after the offset tables so we do not
923 * have to do anything about alignment.
924 */
925 for (i = 0; i < tp->ext_Strings; i++) {
926 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
927 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
928 _nc_visbuf(tp->Strings[i + STRCOUNT])));
micky3879b9f5e72025-07-08 18:04:53 -0400929 if (!WRITE_STRING(tp->Strings[i + STRCOUNT])) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530930 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400931 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530932 }
933 }
934
935 /*
936 * Write the extended names
937 */
micky3879b9f5e72025-07-08 18:04:53 -0400938 for (i = 0; i < ext_total; i++) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530939 TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
micky3879b9f5e72025-07-08 18:04:53 -0400940 if (!WRITE_STRING(tp->ext_Names[i])) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530941 return (ERR);
micky3879b9f5e72025-07-08 18:04:53 -0400942 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530943 }
944
945 }
946#endif /* NCURSES_XNAMES */
947
948 total_written++;
micky3879b9f5e72025-07-08 18:04:53 -0400949 total_parts++;
950 total_size = total_size + (int) (*offset + 1);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530951 return (OK);
952}
953
954/*
955 * Returns the total number of entries written by this process
956 */
957NCURSES_EXPORT(int)
958_nc_tic_written(void)
959{
micky3879b9f5e72025-07-08 18:04:53 -0400960 TR(TRACE_DATABASE, ("_nc_tic_written %d entries, %d parts, %d size",
961 total_written, total_parts, total_size));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530962 return total_written;
963}