]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/write_entry.c
ncurses 5.9 - patch 20131116
[ncurses.git] / ncurses / tinfo / write_entry.c
1 /****************************************************************************
2  * Copyright (c) 1998-2012,2013 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.90 2013/11/16 19:58:14 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     char *term_names = tp->term_names;
277     size_t name_size = strlen(term_names);
278
279     if (name_size == 0) {
280         _nc_syserr_abort("no terminal name found.");
281     } else if (name_size >= sizeof(name_list) - 1) {
282         _nc_syserr_abort("terminal name too long: %s", term_names);
283     }
284
285     _nc_STRCPY(name_list, term_names, sizeof(name_list));
286     DEBUG(7, ("Name list = '%s'", name_list));
287
288     first_name = name_list;
289
290     ptr = &name_list[name_size - 1];
291     other_names = ptr + 1;
292
293     while (ptr > name_list && *ptr != '|')
294         ptr--;
295
296     if (ptr != name_list) {
297         *ptr = '\0';
298
299         for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
300             continue;
301
302         if (*ptr == '\0')
303             other_names = ptr;
304         else {
305             *ptr = '\0';
306             other_names = ptr + 1;
307         }
308     }
309
310     DEBUG(7, ("First name = '%s'", first_name));
311     DEBUG(7, ("Other names = '%s'", other_names));
312
313     _nc_set_type(first_name);
314
315 #if USE_HASHED_DB
316     if (write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
317         DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
318         DBT key, data;
319
320         if (capdb != 0) {
321             buffer[0] = 0;
322
323             memset(&key, 0, sizeof(key));
324             key.data = term_names;
325             key.size = name_size;
326
327             memset(&data, 0, sizeof(data));
328             data.data = buffer;
329             data.size = offset + 1;
330
331             _nc_db_put(capdb, &key, &data);
332
333             buffer[0] = 2;
334
335             key.data = name_list;
336             key.size = strlen(name_list);
337
338             _nc_STRCPY(buffer + 1,
339                        term_names,
340                        sizeof(buffer) - 1);
341             data.size = name_size + 1;
342
343             _nc_db_put(capdb, &key, &data);
344
345             while (*other_names != '\0') {
346                 ptr = other_names++;
347                 assert(ptr < buffer + sizeof(buffer) - 1);
348                 while (*other_names != '|' && *other_names != '\0')
349                     other_names++;
350
351                 if (*other_names != '\0')
352                     *(other_names++) = '\0';
353
354                 key.data = ptr;
355                 key.size = strlen(ptr);
356
357                 _nc_db_put(capdb, &key, &data);
358             }
359         }
360     }
361 #else /* !USE_HASHED_DB */
362     if (call_count++ == 0) {
363         start_time = 0;
364     }
365
366     if (strlen(first_name) >= sizeof(filename) - (2 + LEAF_LEN))
367         _nc_warning("terminal name too long.");
368
369     _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
370                 LEAF_FMT "/%s", first_name[0], first_name);
371
372     /*
373      * Has this primary name been written since the first call to
374      * write_entry()?  If so, the newer write will step on the older,
375      * so warn the user.
376      */
377     if (start_time > 0 &&
378         stat(filename, &statbuf) >= 0
379         && statbuf.st_mtime >= start_time) {
380 #if HAVE_LINK && !USE_SYMLINKS
381         /*
382          * If the file has more than one link, the reason for the previous
383          * write could be that the current primary name used to be an alias for
384          * the previous entry.  In that case, unlink the file so that we will
385          * not modify the previous entry as we write this one.
386          */
387         if (statbuf.st_nlink > 1) {
388             _nc_warning("name redefined.");
389             unlink(filename);
390         } else {
391             _nc_warning("name multiply defined.");
392         }
393 #else
394         _nc_warning("name multiply defined.");
395 #endif
396     }
397
398     check_writeable(first_name[0]);
399     write_file(filename, tp);
400
401     if (start_time == 0) {
402         if (stat(filename, &statbuf) < 0
403             || (start_time = statbuf.st_mtime) == 0) {
404             _nc_syserr_abort("error obtaining time from %s/%s",
405                              _nc_tic_dir(0), filename);
406         }
407     }
408     while (*other_names != '\0') {
409         ptr = other_names++;
410         while (*other_names != '|' && *other_names != '\0')
411             other_names++;
412
413         if (*other_names != '\0')
414             *(other_names++) = '\0';
415
416         if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
417             _nc_warning("terminal alias %s too long.", ptr);
418             continue;
419         }
420         if (strchr(ptr, '/') != 0) {
421             _nc_warning("cannot link alias %s.", ptr);
422             continue;
423         }
424
425         check_writeable(ptr[0]);
426         _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
427                     LEAF_FMT "/%s", ptr[0], ptr);
428
429         if (strcmp(filename, linkname) == 0) {
430             _nc_warning("self-synonym ignored");
431         } else if (stat(linkname, &statbuf) >= 0 &&
432                    statbuf.st_mtime < start_time) {
433             _nc_warning("alias %s multiply defined.", ptr);
434         } else if (_nc_access(linkname, W_OK) == 0)
435 #if HAVE_LINK
436         {
437             int code;
438 #if USE_SYMLINKS
439             if (first_name[0] == linkname[0])
440                 strncpy(symlinkname, first_name, sizeof(symlinkname) - 1);
441             else {
442                 _nc_STRCPY(symlinkname, "../", sizeof(suymlinkname));
443                 strncat(symlinkname, filename, sizeof(symlinkname) - 4);
444             }
445             symlinkname[sizeof(symlinkname) - 1] = '\0';
446 #endif /* USE_SYMLINKS */
447 #if HAVE_REMOVE
448             code = remove(linkname);
449 #else
450             code = unlink(linkname);
451 #endif
452             if (code != 0 && errno == ENOENT)
453                 code = 0;
454 #if USE_SYMLINKS
455             if (symlink(symlinkname, linkname) < 0)
456 #else
457             if (link(filename, linkname) < 0)
458 #endif /* USE_SYMLINKS */
459             {
460                 /*
461                  * If there wasn't anything there, and we cannot
462                  * link to the target because it is the same as the
463                  * target, then the source must be on a filesystem
464                  * that uses caseless filenames, such as Win32, etc.
465                  */
466                 if (code == 0 && errno == EEXIST)
467                     _nc_warning("can't link %s to %s", filename, linkname);
468                 else if (code == 0 && (errno == EPERM || errno == ENOENT))
469                     write_file(linkname, tp);
470                 else {
471 #if MIXEDCASE_FILENAMES
472                     _nc_syserr_abort("can't link %s to %s", filename, linkname);
473 #else
474                     _nc_warning("can't link %s to %s (errno=%d)", filename,
475                                 linkname, errno);
476 #endif
477                 }
478             } else {
479                 DEBUG(1, ("Linked %s", linkname));
480             }
481         }
482 #else /* just make copies */
483             write_file(linkname, tp);
484 #endif /* HAVE_LINK */
485     }
486 #endif /* USE_HASHED_DB */
487 }
488
489 static size_t
490 fake_write(char *dst,
491            unsigned *offset,
492            size_t limit,
493            char *src,
494            size_t want,
495            size_t size)
496 {
497     size_t have = (limit - *offset);
498
499     want *= size;
500     if (have > 0) {
501         if (want > have)
502             want = have;
503         memcpy(dst + *offset, src, want);
504         *offset += (unsigned) want;
505     } else {
506         want = 0;
507     }
508     return (want / size);
509 }
510
511 #define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
512
513 #undef LITTLE_ENDIAN            /* BSD/OS defines this as a feature macro */
514 #define HI(x)                   ((x) / 256)
515 #define LO(x)                   ((x) % 256)
516 #define LITTLE_ENDIAN(p, x)     (p)[0] = (unsigned char)LO(x),  \
517                                 (p)[1] = (unsigned char)HI(x)
518
519 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
520
521 static int
522 compute_offsets(char **Strings, size_t strmax, short *offsets)
523 {
524     int nextfree = 0;
525     size_t i;
526
527     for (i = 0; i < strmax; i++) {
528         if (Strings[i] == ABSENT_STRING) {
529             offsets[i] = -1;
530         } else if (Strings[i] == CANCELLED_STRING) {
531             offsets[i] = -2;
532         } else {
533             offsets[i] = (short) nextfree;
534             nextfree += (int) strlen(Strings[i]) + 1;
535             TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
536                        _nc_visbuf(Strings[i]), (int) nextfree));
537         }
538     }
539     return nextfree;
540 }
541
542 static void
543 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
544 {
545     size_t i;
546     for (i = 0; i < count; i++) {
547         if (Numbers[i] == ABSENT_NUMERIC) {     /* HI/LO won't work */
548             buf[2 * i] = buf[2 * i + 1] = 0377;
549         } else if (Numbers[i] == CANCELLED_NUMERIC) {   /* HI/LO won't work */
550             buf[2 * i] = 0376;
551             buf[2 * i + 1] = 0377;
552         } else {
553             LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
554             TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
555         }
556     }
557 }
558
559 #define even_boundary(value) \
560             ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
561
562 #if NCURSES_XNAMES
563 static unsigned
564 extended_Booleans(TERMTYPE *tp)
565 {
566     unsigned result = 0;
567     unsigned i;
568
569     for (i = 0; i < tp->ext_Booleans; ++i) {
570         if (tp->Booleans[BOOLCOUNT + i] == TRUE)
571             result = (i + 1);
572     }
573     return result;
574 }
575
576 static unsigned
577 extended_Numbers(TERMTYPE *tp)
578 {
579     unsigned result = 0;
580     unsigned i;
581
582     for (i = 0; i < tp->ext_Numbers; ++i) {
583         if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
584             result = (i + 1);
585     }
586     return result;
587 }
588
589 static unsigned
590 extended_Strings(TERMTYPE *tp)
591 {
592     unsigned short result = 0;
593     unsigned short i;
594
595     for (i = 0; i < tp->ext_Strings; ++i) {
596         if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
597             result = (unsigned short) (i + 1);
598     }
599     return result;
600 }
601
602 /*
603  * _nc_align_termtype() will extend entries that are referenced in a use=
604  * clause - discard the unneeded data.
605  */
606 static bool
607 extended_object(TERMTYPE *tp)
608 {
609     bool result = FALSE;
610
611     if (_nc_user_definable) {
612         result = ((extended_Booleans(tp)
613                    + extended_Numbers(tp)
614                    + extended_Strings(tp)) != 0);
615     }
616     return result;
617 }
618 #endif
619
620 static int
621 write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
622 {
623     char *namelist;
624     size_t namelen, boolmax, nummax, strmax;
625     char zero = '\0';
626     size_t i;
627     int nextfree;
628     short offsets[MAX_ENTRY_SIZE / 2];
629     unsigned char buf[MAX_ENTRY_SIZE];
630     unsigned last_bool = BOOLWRITE;
631     unsigned last_num = NUMWRITE;
632     unsigned last_str = STRWRITE;
633
634 #if NCURSES_XNAMES
635     /*
636      * Normally we limit the list of values to exclude the "obsolete"
637      * capabilities.  However, if we are accepting extended names, add
638      * these as well, since they are used for supporting translation
639      * to/from termcap.
640      */
641     if (_nc_user_definable) {
642         last_bool = BOOLCOUNT;
643         last_num = NUMCOUNT;
644         last_str = STRCOUNT;
645     }
646 #endif
647
648     namelist = tp->term_names;
649     namelen = strlen(namelist) + 1;
650
651     boolmax = 0;
652     for (i = 0; i < last_bool; i++) {
653         if (tp->Booleans[i] == TRUE)
654             boolmax = i + 1;
655     }
656
657     nummax = 0;
658     for (i = 0; i < last_num; i++) {
659         if (tp->Numbers[i] != ABSENT_NUMERIC)
660             nummax = i + 1;
661     }
662
663     strmax = 0;
664     for (i = 0; i < last_str; i++) {
665         if (tp->Strings[i] != ABSENT_STRING)
666             strmax = i + 1;
667     }
668
669     nextfree = compute_offsets(tp->Strings, strmax, offsets);
670
671     /* fill in the header */
672     LITTLE_ENDIAN(buf, MAGIC);
673     LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
674     LITTLE_ENDIAN(buf + 4, boolmax);
675     LITTLE_ENDIAN(buf + 6, nummax);
676     LITTLE_ENDIAN(buf + 8, strmax);
677     LITTLE_ENDIAN(buf + 10, nextfree);
678
679     /* write out the header */
680     TRACE_OUT(("Header of %s @%d", namelist, *offset));
681     if (Write(buf, 12, 1) != 1
682         || Write(namelist, sizeof(char), namelen) != namelen)
683           return (ERR);
684
685     for (i = 0; i < boolmax; i++)
686         if (tp->Booleans[i] == TRUE)
687             buf[i] = TRUE;
688         else
689             buf[i] = FALSE;
690     if (Write(buf, sizeof(char), boolmax) != boolmax)
691           return (ERR);
692
693     if (even_boundary(namelen + boolmax))
694         return (ERR);
695
696     TRACE_OUT(("Numerics begin at %04x", *offset));
697
698     /* the numerics */
699     convert_shorts(buf, tp->Numbers, nummax);
700     if (Write(buf, 2, nummax) != nummax)
701         return (ERR);
702
703     TRACE_OUT(("String offsets begin at %04x", *offset));
704
705     /* the string offsets */
706     convert_shorts(buf, offsets, strmax);
707     if (Write(buf, 2, strmax) != strmax)
708         return (ERR);
709
710     TRACE_OUT(("String table begins at %04x", *offset));
711
712     /* the strings */
713     for (i = 0; i < strmax; i++)
714         if (VALID_STRING(tp->Strings[i]))
715             if (!WRITE_STRING(tp->Strings[i]))
716                 return (ERR);
717
718 #if NCURSES_XNAMES
719     if (extended_object(tp)) {
720         unsigned extcnt = (unsigned) NUM_EXT_NAMES(tp);
721
722         if (even_boundary(nextfree))
723             return (ERR);
724
725         nextfree = compute_offsets(tp->Strings + STRCOUNT,
726                                    (size_t) tp->ext_Strings,
727                                    offsets);
728         TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
729
730         if (tp->ext_Strings >= SIZEOF(offsets))
731             return (ERR);
732
733         nextfree += compute_offsets(tp->ext_Names,
734                                     (size_t) extcnt,
735                                     offsets + tp->ext_Strings);
736         TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
737         strmax = tp->ext_Strings + extcnt;
738
739         /*
740          * Write the extended header
741          */
742         LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
743         LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
744         LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
745         LITTLE_ENDIAN(buf + 6, strmax);
746         LITTLE_ENDIAN(buf + 8, nextfree);
747         TRACE_OUT(("WRITE extended-header @%d", *offset));
748         if (Write(buf, 10, 1) != 1)
749             return (ERR);
750
751         TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
752         if (tp->ext_Booleans
753             && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
754                      tp->ext_Booleans) != tp->ext_Booleans)
755               return (ERR);
756
757         if (even_boundary(tp->ext_Booleans))
758             return (ERR);
759
760         TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
761         if (tp->ext_Numbers) {
762             convert_shorts(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
763             if (Write(buf, 2, tp->ext_Numbers) != tp->ext_Numbers)
764                 return (ERR);
765         }
766
767         /*
768          * Convert the offsets for the ext_Strings and ext_Names tables,
769          * in that order.
770          */
771         convert_shorts(buf, offsets, strmax);
772         TRACE_OUT(("WRITE offsets @%d", *offset));
773         if (Write(buf, 2, strmax) != strmax)
774             return (ERR);
775
776         /*
777          * Write the string table after the offset tables so we do not
778          * have to do anything about alignment.
779          */
780         for (i = 0; i < tp->ext_Strings; i++) {
781             if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
782                 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
783                            _nc_visbuf(tp->Strings[i + STRCOUNT])));
784                 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
785                     return (ERR);
786             }
787         }
788
789         /*
790          * Write the extended names
791          */
792         for (i = 0; i < extcnt; i++) {
793             TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
794             if (!WRITE_STRING(tp->ext_Names[i]))
795                 return (ERR);
796         }
797
798     }
799 #endif /* NCURSES_XNAMES */
800
801     total_written++;
802     return (OK);
803 }
804
805 /*
806  * Returns the total number of entries written by this process
807  */
808 NCURSES_EXPORT(int)
809 _nc_tic_written(void)
810 {
811     return total_written;
812 }