]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/write_entry.c
b76a109d67a664472dad6845fd5d8084741c5e7c
[ncurses.git] / ncurses / tinfo / write_entry.c
1 /****************************************************************************
2  * Copyright (c) 1998-2014,2015 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  ****************************************************************************/
34
35 /*
36  *      write_entry.c -- write a terminfo structure onto the file system
37  */
38
39 #include <curses.priv.h>
40 #include <hashed_db.h>
41
42 #include <tic.h>
43
44 #if 1
45 #define TRACE_OUT(p) DEBUG(2, p)
46 #else
47 #define TRACE_OUT(p)            /*nothing */
48 #endif
49
50 MODULE_ID("$Id: write_entry.c,v 1.95 2016/09/10 20:22:42 tom Exp $")
51
52 static int total_written;
53
54 static int make_db_root(const char *);
55
56 #if !USE_HASHED_DB
57 static void
58 write_file(char *filename, TERMTYPE *tp)
59 {
60     char buffer[MAX_ENTRY_SIZE];
61     unsigned limit = sizeof(buffer);
62     unsigned offset = 0;
63
64     FILE *fp = (_nc_access(filename, W_OK) == 0) ? fopen(filename, "wb") : 0;
65     if (fp == 0) {
66         perror(filename);
67         _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename);
68     }
69     DEBUG(1, ("Created %s", filename));
70
71     if (_nc_write_object(tp, buffer, &offset, limit) == ERR
72         || fwrite(buffer, sizeof(char), (size_t) offset, fp) != offset) {
73         _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
74     }
75
76     fclose(fp);
77 }
78
79 /*
80  * Check for access rights to destination directories
81  * Create any directories which don't exist.
82  *
83  * Note:  there's no reason to return the result of make_db_root(), since
84  * this function is called only in instances where that has to succeed.
85  */
86 static void
87 check_writeable(int code)
88 {
89     static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
90     static bool verified[sizeof(dirnames)];
91
92     char dir[sizeof(LEAF_FMT)];
93     char *s = 0;
94
95     if (code == 0 || (s = (strchr) (dirnames, code)) == 0)
96         _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
97
98     if (verified[s - dirnames])
99         return;
100
101     _nc_SPRINTF(dir, _nc_SLIMIT(sizeof(dir)) LEAF_FMT, code);
102     if (make_db_root(dir) < 0) {
103         _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
104     }
105
106     verified[s - dirnames] = TRUE;
107 }
108 #endif /* !USE_HASHED_DB */
109
110 static int
111 make_db_path(char *dst, const char *src, size_t limit)
112 {
113     int rc = -1;
114     const char *top = _nc_tic_dir(0);
115
116     if (src == top || _nc_is_abs_path(src)) {
117         if (strlen(src) + 1 <= limit) {
118             _nc_STRCPY(dst, src, limit);
119             rc = 0;
120         }
121     } else {
122         if (strlen(top) + strlen(src) + 2 <= limit) {
123             _nc_SPRINTF(dst, _nc_SLIMIT(limit) "%s/%s", top, src);
124             rc = 0;
125         }
126     }
127 #if USE_HASHED_DB
128     if (rc == 0) {
129         static const char suffix[] = DBM_SUFFIX;
130         size_t have = strlen(dst);
131         size_t need = strlen(suffix);
132         if (have > need && strcmp(dst + (int) (have - need), suffix)) {
133             if (have + need <= limit) {
134                 _nc_STRCAT(dst, suffix, limit);
135             } else {
136                 rc = -1;
137             }
138         } else if (_nc_is_dir_path(dst)) {
139             rc = -1;
140         }
141     }
142 #endif
143     return rc;
144 }
145
146 /*
147  * Make a database-root if it doesn't exist.
148  */
149 static int
150 make_db_root(const char *path)
151 {
152     int rc;
153     char fullpath[PATH_MAX];
154
155     if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
156 #if USE_HASHED_DB
157         DB *capdbp;
158
159         if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL) {
160             rc = -1;
161         } else if (_nc_db_close(capdbp) < 0) {
162             rc = -1;
163         }
164 #else
165         struct stat statbuf;
166
167         if ((rc = stat(path, &statbuf)) < 0) {
168             rc = mkdir(path
169 #if !defined(__MINGW32__)
170                        ,0777
171 #endif
172                 );
173         } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
174             rc = -1;            /* permission denied */
175         } else if (!(S_ISDIR(statbuf.st_mode))) {
176             rc = -1;            /* not a directory */
177         }
178 #endif
179     }
180     return rc;
181 }
182
183 /*
184  * Set the write directory for compiled entries.
185  */
186 NCURSES_EXPORT(void)
187 _nc_set_writedir(const char *dir)
188 {
189     const char *destination;
190     char actual[PATH_MAX];
191
192     if (dir == 0
193 #ifndef USE_ROOT_ENVIRON
194         && use_terminfo_vars()
195 #endif
196         )
197         dir = getenv("TERMINFO");
198
199     if (dir != 0)
200         (void) _nc_tic_dir(dir);
201
202     destination = _nc_tic_dir(0);
203     if (make_db_root(destination) < 0) {
204         char *home = _nc_home_terminfo();
205
206         if (home != 0) {
207             destination = home;
208             if (make_db_root(destination) < 0)
209                 _nc_err_abort("%s: permission denied (errno %d)",
210                               destination, errno);
211         }
212     }
213
214     /*
215      * Note: because of this code, this logic should be exercised
216      * *once only* per run.
217      */
218 #if USE_HASHED_DB
219     make_db_path(actual, destination, sizeof(actual));
220 #else
221     if (chdir(_nc_tic_dir(destination)) < 0
222         || getcwd(actual, sizeof(actual)) == 0)
223         _nc_err_abort("%s: not a directory", destination);
224 #endif
225     _nc_keep_tic_dir(strdup(actual));
226 }
227
228 /*
229  *      Save the compiled version of a description in the filesystem.
230  *
231  *      make a copy of the name-list
232  *      break it up into first-name and all-but-last-name
233  *      creat(first-name)
234  *      write object information to first-name
235  *      close(first-name)
236  *      for each name in all-but-last-name
237  *          link to first-name
238  *
239  *      Using 'time()' to obtain a reference for file timestamps is unreliable,
240  *      e.g., with NFS, because the filesystem may have a different time
241  *      reference.  We check for pre-existence of links by latching the first
242  *      timestamp from a file that we create.
243  *
244  *      The _nc_warning() calls will report a correct line number only if
245  *      _nc_curr_line is properly set before the write_entry() call.
246  */
247
248 NCURSES_EXPORT(void)
249 _nc_write_entry(TERMTYPE *const tp)
250 {
251 #if USE_HASHED_DB
252
253     char buffer[MAX_ENTRY_SIZE + 1];
254     unsigned limit = sizeof(buffer);
255     unsigned offset = 0;
256
257 #else /* !USE_HASHED_DB */
258
259     struct stat statbuf;
260     char filename[PATH_MAX];
261     char linkname[PATH_MAX];
262 #if USE_SYMLINKS
263     char symlinkname[PATH_MAX];
264 #if !HAVE_LINK
265 #undef HAVE_LINK
266 #define HAVE_LINK 1
267 #endif
268 #endif /* USE_SYMLINKS */
269
270     static int call_count;
271     static time_t start_time;   /* time at start of writes */
272
273 #endif /* USE_HASHED_DB */
274
275     char name_list[MAX_TERMINFO_LENGTH];
276     char *first_name, *other_names;
277     char *ptr;
278     char *term_names = tp->term_names;
279     size_t name_size = strlen(term_names);
280
281     if (name_size == 0) {
282         _nc_syserr_abort("no terminal name found.");
283     } else if (name_size >= sizeof(name_list) - 1) {
284         _nc_syserr_abort("terminal name too long: %s", term_names);
285     }
286
287     _nc_STRCPY(name_list, term_names, sizeof(name_list));
288     DEBUG(7, ("Name list = '%s'", name_list));
289
290     first_name = name_list;
291
292     ptr = &name_list[name_size - 1];
293     other_names = ptr + 1;
294
295     while (ptr > name_list && *ptr != '|')
296         ptr--;
297
298     if (ptr != name_list) {
299         *ptr = '\0';
300
301         for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
302             continue;
303
304         if (*ptr == '\0')
305             other_names = ptr;
306         else {
307             *ptr = '\0';
308             other_names = ptr + 1;
309         }
310     }
311
312     DEBUG(7, ("First name = '%s'", first_name));
313     DEBUG(7, ("Other names = '%s'", other_names));
314
315     _nc_set_type(first_name);
316
317 #if USE_HASHED_DB
318     if (_nc_write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
319         DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
320         DBT key, data;
321
322         if (capdb != 0) {
323             buffer[0] = 0;
324
325             memset(&key, 0, sizeof(key));
326             key.data = term_names;
327             key.size = name_size;
328
329             memset(&data, 0, sizeof(data));
330             data.data = buffer;
331             data.size = offset + 1;
332
333             _nc_db_put(capdb, &key, &data);
334
335             buffer[0] = 2;
336
337             key.data = name_list;
338             key.size = strlen(name_list);
339
340             _nc_STRCPY(buffer + 1,
341                        term_names,
342                        sizeof(buffer) - 1);
343             data.size = name_size + 1;
344
345             _nc_db_put(capdb, &key, &data);
346
347             while (*other_names != '\0') {
348                 ptr = other_names++;
349                 assert(ptr < buffer + sizeof(buffer) - 1);
350                 while (*other_names != '|' && *other_names != '\0')
351                     other_names++;
352
353                 if (*other_names != '\0')
354                     *(other_names++) = '\0';
355
356                 key.data = ptr;
357                 key.size = strlen(ptr);
358
359                 _nc_db_put(capdb, &key, &data);
360             }
361         }
362     }
363 #else /* !USE_HASHED_DB */
364     if (call_count++ == 0) {
365         start_time = 0;
366     }
367
368     if (strlen(first_name) >= sizeof(filename) - (2 + LEAF_LEN))
369         _nc_warning("terminal name too long.");
370
371     _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
372                 LEAF_FMT "/%s", first_name[0], first_name);
373
374     /*
375      * Has this primary name been written since the first call to
376      * write_entry()?  If so, the newer write will step on the older,
377      * so warn the user.
378      */
379     if (start_time > 0 &&
380         stat(filename, &statbuf) >= 0
381         && statbuf.st_mtime >= start_time) {
382 #if HAVE_LINK && !USE_SYMLINKS
383         /*
384          * If the file has more than one link, the reason for the previous
385          * write could be that the current primary name used to be an alias for
386          * the previous entry.  In that case, unlink the file so that we will
387          * not modify the previous entry as we write this one.
388          */
389         if (statbuf.st_nlink > 1) {
390             _nc_warning("name redefined.");
391             unlink(filename);
392         } else {
393             _nc_warning("name multiply defined.");
394         }
395 #else
396         _nc_warning("name multiply defined.");
397 #endif
398     }
399
400     check_writeable(first_name[0]);
401     write_file(filename, tp);
402
403     if (start_time == 0) {
404         if (stat(filename, &statbuf) < 0
405             || (start_time = statbuf.st_mtime) == 0) {
406             _nc_syserr_abort("error obtaining time from %s/%s",
407                              _nc_tic_dir(0), filename);
408         }
409     }
410     while (*other_names != '\0') {
411         ptr = other_names++;
412         while (*other_names != '|' && *other_names != '\0')
413             other_names++;
414
415         if (*other_names != '\0')
416             *(other_names++) = '\0';
417
418         if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
419             _nc_warning("terminal alias %s too long.", ptr);
420             continue;
421         }
422         if (strchr(ptr, '/') != 0) {
423             _nc_warning("cannot link alias %s.", ptr);
424             continue;
425         }
426
427         check_writeable(ptr[0]);
428         _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
429                     LEAF_FMT "/%s", ptr[0], ptr);
430
431         if (strcmp(filename, linkname) == 0) {
432             _nc_warning("self-synonym ignored");
433         } else if (stat(linkname, &statbuf) >= 0 &&
434                    statbuf.st_mtime < start_time) {
435             _nc_warning("alias %s multiply defined.", ptr);
436         } else if (_nc_access(linkname, W_OK) == 0)
437 #if HAVE_LINK
438         {
439             int code;
440 #if USE_SYMLINKS
441 #define MY_SIZE sizeof(symlinkname) - 1
442             if (first_name[0] == linkname[0]) {
443                 _nc_STRNCPY(symlinkname, first_name, MY_SIZE);
444             } else {
445                 _nc_STRCPY(symlinkname, "../", sizeof(symlinkname));
446                 _nc_STRNCPY(symlinkname + 3, first_name, MY_SIZE - 3);
447             }
448             symlinkname[MY_SIZE] = '\0';
449 #endif /* USE_SYMLINKS */
450 #if HAVE_REMOVE
451             code = remove(linkname);
452 #else
453             code = unlink(linkname);
454 #endif
455             if (code != 0 && errno == ENOENT)
456                 code = 0;
457 #if USE_SYMLINKS
458             if (symlink(symlinkname, linkname) < 0)
459 #else
460             if (link(filename, linkname) < 0)
461 #endif /* USE_SYMLINKS */
462             {
463                 /*
464                  * If there wasn't anything there, and we cannot
465                  * link to the target because it is the same as the
466                  * target, then the source must be on a filesystem
467                  * that uses caseless filenames, such as Win32, etc.
468                  */
469                 if (code == 0 && errno == EEXIST)
470                     _nc_warning("can't link %s to %s", filename, linkname);
471                 else if (code == 0 && (errno == EPERM || errno == ENOENT))
472                     write_file(linkname, tp);
473                 else {
474 #if MIXEDCASE_FILENAMES
475                     _nc_syserr_abort("can't link %s to %s", filename, linkname);
476 #else
477                     _nc_warning("can't link %s to %s (errno=%d)", filename,
478                                 linkname, errno);
479 #endif
480                 }
481             } else {
482                 DEBUG(1, ("Linked %s", linkname));
483             }
484         }
485 #else /* just make copies */
486             write_file(linkname, tp);
487 #endif /* HAVE_LINK */
488     }
489 #endif /* USE_HASHED_DB */
490 }
491
492 static size_t
493 fake_write(char *dst,
494            unsigned *offset,
495            size_t limit,
496            char *src,
497            size_t want,
498            size_t size)
499 {
500     size_t have = (limit - *offset);
501
502     want *= size;
503     if (have > 0) {
504         if (want > have)
505             want = have;
506         memcpy(dst + *offset, src, want);
507         *offset += (unsigned) want;
508     } else {
509         want = 0;
510     }
511     return (want / size);
512 }
513
514 #define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
515
516 #undef LITTLE_ENDIAN            /* BSD/OS defines this as a feature macro */
517 #define HI(x)                   ((x) / 256)
518 #define LO(x)                   ((x) % 256)
519 #define LITTLE_ENDIAN(p, x)     (p)[0] = (unsigned char)LO(x),  \
520                                 (p)[1] = (unsigned char)HI(x)
521
522 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
523
524 static int
525 compute_offsets(char **Strings, size_t strmax, short *offsets)
526 {
527     int nextfree = 0;
528     size_t i;
529
530     for (i = 0; i < strmax; i++) {
531         if (Strings[i] == ABSENT_STRING) {
532             offsets[i] = -1;
533         } else if (Strings[i] == CANCELLED_STRING) {
534             offsets[i] = -2;
535         } else {
536             offsets[i] = (short) nextfree;
537             nextfree += (int) strlen(Strings[i]) + 1;
538             TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
539                        _nc_visbuf(Strings[i]), (int) nextfree));
540         }
541     }
542     return nextfree;
543 }
544
545 static void
546 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
547 {
548     size_t i;
549     for (i = 0; i < count; i++) {
550         if (Numbers[i] == ABSENT_NUMERIC) {     /* HI/LO won't work */
551             buf[2 * i] = buf[2 * i + 1] = 0377;
552         } else if (Numbers[i] == CANCELLED_NUMERIC) {   /* HI/LO won't work */
553             buf[2 * i] = 0376;
554             buf[2 * i + 1] = 0377;
555         } else {
556             LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
557             TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
558         }
559     }
560 }
561
562 #define even_boundary(value) \
563             ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
564
565 #if NCURSES_XNAMES
566 static unsigned
567 extended_Booleans(TERMTYPE *tp)
568 {
569     unsigned result = 0;
570     unsigned i;
571
572     for (i = 0; i < tp->ext_Booleans; ++i) {
573         if (tp->Booleans[BOOLCOUNT + i] == TRUE)
574             result = (i + 1);
575     }
576     return result;
577 }
578
579 static unsigned
580 extended_Numbers(TERMTYPE *tp)
581 {
582     unsigned result = 0;
583     unsigned i;
584
585     for (i = 0; i < tp->ext_Numbers; ++i) {
586         if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
587             result = (i + 1);
588     }
589     return result;
590 }
591
592 static unsigned
593 extended_Strings(TERMTYPE *tp)
594 {
595     unsigned short result = 0;
596     unsigned short i;
597
598     for (i = 0; i < tp->ext_Strings; ++i) {
599         if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
600             result = (unsigned short) (i + 1);
601     }
602     return result;
603 }
604
605 /*
606  * _nc_align_termtype() will extend entries that are referenced in a use=
607  * clause - discard the unneeded data.
608  */
609 static bool
610 extended_object(TERMTYPE *tp)
611 {
612     bool result = FALSE;
613
614     if (_nc_user_definable) {
615         result = ((extended_Booleans(tp)
616                    + extended_Numbers(tp)
617                    + extended_Strings(tp)) != 0);
618     }
619     return result;
620 }
621 #endif
622
623 NCURSES_EXPORT(int)
624 _nc_write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
625 {
626     char *namelist;
627     size_t namelen, boolmax, nummax, strmax;
628     char zero = '\0';
629     size_t i;
630     int nextfree;
631     short offsets[MAX_ENTRY_SIZE / 2];
632     unsigned char buf[MAX_ENTRY_SIZE];
633     unsigned last_bool = BOOLWRITE;
634     unsigned last_num = NUMWRITE;
635     unsigned last_str = STRWRITE;
636
637 #if NCURSES_XNAMES
638     /*
639      * Normally we limit the list of values to exclude the "obsolete"
640      * capabilities.  However, if we are accepting extended names, add
641      * these as well, since they are used for supporting translation
642      * to/from termcap.
643      */
644     if (_nc_user_definable) {
645         last_bool = BOOLCOUNT;
646         last_num = NUMCOUNT;
647         last_str = STRCOUNT;
648     }
649 #endif
650
651     namelist = tp->term_names;
652     namelen = strlen(namelist) + 1;
653
654     boolmax = 0;
655     for (i = 0; i < last_bool; i++) {
656         if (tp->Booleans[i] == TRUE)
657             boolmax = i + 1;
658     }
659
660     nummax = 0;
661     for (i = 0; i < last_num; i++) {
662         if (tp->Numbers[i] != ABSENT_NUMERIC)
663             nummax = i + 1;
664     }
665
666     strmax = 0;
667     for (i = 0; i < last_str; i++) {
668         if (tp->Strings[i] != ABSENT_STRING)
669             strmax = i + 1;
670     }
671
672     nextfree = compute_offsets(tp->Strings, strmax, offsets);
673
674     /* fill in the header */
675     LITTLE_ENDIAN(buf, MAGIC);
676     LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
677     LITTLE_ENDIAN(buf + 4, boolmax);
678     LITTLE_ENDIAN(buf + 6, nummax);
679     LITTLE_ENDIAN(buf + 8, strmax);
680     LITTLE_ENDIAN(buf + 10, nextfree);
681
682     /* write out the header */
683     TRACE_OUT(("Header of %s @%d", namelist, *offset));
684     if (Write(buf, 12, 1) != 1
685         || Write(namelist, sizeof(char), namelen) != namelen)
686           return (ERR);
687
688     for (i = 0; i < boolmax; i++)
689         if (tp->Booleans[i] == TRUE)
690             buf[i] = TRUE;
691         else
692             buf[i] = FALSE;
693     if (Write(buf, sizeof(char), boolmax) != boolmax)
694           return (ERR);
695
696     if (even_boundary(namelen + boolmax))
697         return (ERR);
698
699     TRACE_OUT(("Numerics begin at %04x", *offset));
700
701     /* the numerics */
702     convert_shorts(buf, tp->Numbers, nummax);
703     if (Write(buf, 2, nummax) != nummax)
704         return (ERR);
705
706     TRACE_OUT(("String offsets begin at %04x", *offset));
707
708     /* the string offsets */
709     convert_shorts(buf, offsets, strmax);
710     if (Write(buf, 2, strmax) != strmax)
711         return (ERR);
712
713     TRACE_OUT(("String table begins at %04x", *offset));
714
715     /* the strings */
716     for (i = 0; i < strmax; i++)
717         if (VALID_STRING(tp->Strings[i]))
718             if (!WRITE_STRING(tp->Strings[i]))
719                 return (ERR);
720
721 #if NCURSES_XNAMES
722     if (extended_object(tp)) {
723         unsigned extcnt = (unsigned) NUM_EXT_NAMES(tp);
724
725         if (even_boundary(nextfree))
726             return (ERR);
727
728         nextfree = compute_offsets(tp->Strings + STRCOUNT,
729                                    (size_t) tp->ext_Strings,
730                                    offsets);
731         TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
732
733         if (tp->ext_Strings >= SIZEOF(offsets))
734             return (ERR);
735
736         nextfree += compute_offsets(tp->ext_Names,
737                                     (size_t) extcnt,
738                                     offsets + tp->ext_Strings);
739         TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
740         strmax = tp->ext_Strings + extcnt;
741
742         /*
743          * Write the extended header
744          */
745         LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
746         LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
747         LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
748         LITTLE_ENDIAN(buf + 6, strmax);
749         LITTLE_ENDIAN(buf + 8, nextfree);
750         TRACE_OUT(("WRITE extended-header @%d", *offset));
751         if (Write(buf, 10, 1) != 1)
752             return (ERR);
753
754         TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
755         if (tp->ext_Booleans
756             && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
757                      tp->ext_Booleans) != tp->ext_Booleans)
758               return (ERR);
759
760         if (even_boundary(tp->ext_Booleans))
761             return (ERR);
762
763         TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
764         if (tp->ext_Numbers) {
765             convert_shorts(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
766             if (Write(buf, 2, tp->ext_Numbers) != tp->ext_Numbers)
767                 return (ERR);
768         }
769
770         /*
771          * Convert the offsets for the ext_Strings and ext_Names tables,
772          * in that order.
773          */
774         convert_shorts(buf, offsets, strmax);
775         TRACE_OUT(("WRITE offsets @%d", *offset));
776         if (Write(buf, 2, strmax) != strmax)
777             return (ERR);
778
779         /*
780          * Write the string table after the offset tables so we do not
781          * have to do anything about alignment.
782          */
783         for (i = 0; i < tp->ext_Strings; i++) {
784             if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
785                 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
786                            _nc_visbuf(tp->Strings[i + STRCOUNT])));
787                 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
788                     return (ERR);
789             }
790         }
791
792         /*
793          * Write the extended names
794          */
795         for (i = 0; i < extcnt; i++) {
796             TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
797             if (!WRITE_STRING(tp->ext_Names[i]))
798                 return (ERR);
799         }
800
801     }
802 #endif /* NCURSES_XNAMES */
803
804     total_written++;
805     return (OK);
806 }
807
808 /*
809  * Returns the total number of entries written by this process
810  */
811 NCURSES_EXPORT(int)
812 _nc_tic_written(void)
813 {
814     return total_written;
815 }