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