?? write_entry.c
字號:
if (strchr(ptr, '/') != 0) { _nc_warning("cannot link alias %s.", ptr); continue; } check_writeable(ptr[0]); sprintf(linkname, "%c/%s", ptr[0], ptr); if (strcmp(filename, linkname) == 0) { _nc_warning("self-synonym ignored"); } else if (stat(linkname, &statbuf) >= 0 && statbuf.st_mtime < start_time) { _nc_warning("alias %s multiply defined.", ptr); } else if (_nc_access(linkname, W_OK) == 0)#if HAVE_LINK { int code;#if USE_SYMLINKS strcpy(symlinkname, "../"); strncat(symlinkname, filename, sizeof(symlinkname) - 4); symlinkname[sizeof(symlinkname) - 1] = '\0';#endif /* USE_SYMLINKS */#if HAVE_REMOVE code = remove(linkname);#else code = unlink(linkname);#endif if (code != 0 && errno == ENOENT) code = 0;#if USE_SYMLINKS if (symlink(symlinkname, linkname) < 0)#else if (link(filename, linkname) < 0)#endif /* USE_SYMLINKS */ { /* * If there wasn't anything there, and we cannot * link to the target because it is the same as the * target, then the source must be on a filesystem * that uses caseless filenames, such as Win32, etc. */ if (code == 0 && errno == EEXIST) _nc_warning("can't link %s to %s", filename, linkname); else if (code == 0 && (errno == EPERM || errno == ENOENT)) write_file(linkname, tp); else {#if MIXEDCASE_FILENAMES _nc_syserr_abort("can't link %s to %s", filename, linkname);#else _nc_warning("can't link %s to %s (errno=%d)", filename, linkname, errno);#endif } } else { DEBUG(1, ("Linked %s", linkname)); } }#else /* just make copies */ write_file(linkname, tp);#endif /* HAVE_LINK */ }}#undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */#define HI(x) ((x) / 256)#define LO(x) ((x) % 256)#define LITTLE_ENDIAN(p, x) (p)[0] = LO(x), (p)[1] = HI(x)#define WRITE_STRING(str) (fwrite(str, sizeof(char), strlen(str) + 1, fp) == strlen(str) + 1)static intcompute_offsets(char **Strings, unsigned strmax, short *offsets){ size_t nextfree = 0; unsigned i; for (i = 0; i < strmax; i++) { if (Strings[i] == ABSENT_STRING) { offsets[i] = -1; } else if (Strings[i] == CANCELLED_STRING) { offsets[i] = -2; } else { offsets[i] = nextfree; nextfree += strlen(Strings[i]) + 1; TRACE_OUT(("put Strings[%d]=%s(%d)", i, _nc_visbuf(Strings[i]), nextfree)); } } return nextfree;}static voidconvert_shorts(unsigned char *buf, short *Numbers, unsigned count){ unsigned i; for (i = 0; i < count; i++) { if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */ buf[2 * i] = buf[2 * i + 1] = 0377; } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */ buf[2 * i] = 0376; buf[2 * i + 1] = 0377; } else { LITTLE_ENDIAN(buf + 2 * i, Numbers[i]); TRACE_OUT(("put Numbers[%d]=%d", i, Numbers[i])); } }}#define even_boundary(value) \ ((value) % 2 != 0 && fwrite(&zero, sizeof(char), 1, fp) != 1)static intwrite_object(FILE * fp, TERMTYPE * tp){ char *namelist; size_t namelen, boolmax, nummax, strmax; char zero = '\0'; size_t i; short nextfree; short offsets[MAX_ENTRY_SIZE / 2]; unsigned char buf[MAX_ENTRY_SIZE]; unsigned last_bool = BOOLWRITE; unsigned last_num = NUMWRITE; unsigned last_str = STRWRITE;#if NCURSES_XNAMES /* * Normally we limit the list of values to exclude the "obsolete" * capabilities. However, if we are accepting extended names, add * these as well, since they are used for supporting translation * to/from termcap. */ if (_nc_user_definable) { last_bool = BOOLCOUNT; last_num = NUMCOUNT; last_str = STRCOUNT; }#endif namelist = tp->term_names; namelen = strlen(namelist) + 1; boolmax = 0; for (i = 0; i < last_bool; i++) { if (tp->Booleans[i] == TRUE) boolmax = i + 1; } nummax = 0; for (i = 0; i < last_num; i++) { if (tp->Numbers[i] != ABSENT_NUMERIC) nummax = i + 1; } strmax = 0; for (i = 0; i < last_str; i++) { if (tp->Strings[i] != ABSENT_STRING) strmax = i + 1; } nextfree = compute_offsets(tp->Strings, strmax, offsets); /* fill in the header */ LITTLE_ENDIAN(buf, MAGIC); LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1)); LITTLE_ENDIAN(buf + 4, boolmax); LITTLE_ENDIAN(buf + 6, nummax); LITTLE_ENDIAN(buf + 8, strmax); LITTLE_ENDIAN(buf + 10, nextfree); /* write out the header */ TRACE_OUT(("Header of %s @%ld", namelist, ftell(fp))); if (fwrite(buf, 12, 1, fp) != 1 || fwrite(namelist, sizeof(char), namelen, fp) != namelen) return (ERR); for (i = 0; i < boolmax; i++) if (tp->Booleans[i] == TRUE) buf[i] = TRUE; else buf[i] = FALSE; if (fwrite(buf, sizeof(char), boolmax, fp) != boolmax) return (ERR); if (even_boundary(namelen + boolmax)) return (ERR); TRACE_OUT(("Numerics begin at %04lx", ftell(fp))); /* the numerics */ convert_shorts(buf, tp->Numbers, nummax); if (fwrite(buf, 2, nummax, fp) != nummax) return (ERR); TRACE_OUT(("String offsets begin at %04lx", ftell(fp))); /* the string offsets */ convert_shorts(buf, offsets, strmax); if (fwrite(buf, 2, strmax, fp) != strmax) return (ERR); TRACE_OUT(("String table begins at %04lx", ftell(fp))); /* the strings */ for (i = 0; i < strmax; i++) if (VALID_STRING(tp->Strings[i])) if (!WRITE_STRING(tp->Strings[i])) return (ERR);#if NCURSES_XNAMES if (NUM_EXT_NAMES(tp)) { unsigned extcnt = NUM_EXT_NAMES(tp); if (even_boundary(nextfree)) return (ERR); nextfree = compute_offsets(tp->Strings + STRCOUNT, tp->ext_Strings, offsets); TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree)); nextfree += compute_offsets(tp->ext_Names, extcnt, offsets + tp->ext_Strings); TRACE_OUT(("after extended capnames, nextfree=%d", nextfree)); strmax = tp->ext_Strings + extcnt; /* * Write the extended header */ LITTLE_ENDIAN(buf + 0, tp->ext_Booleans); LITTLE_ENDIAN(buf + 2, tp->ext_Numbers); LITTLE_ENDIAN(buf + 4, tp->ext_Strings); LITTLE_ENDIAN(buf + 6, strmax); LITTLE_ENDIAN(buf + 8, nextfree); TRACE_OUT(("WRITE extended-header @%ld", ftell(fp))); if (fwrite(buf, 10, 1, fp) != 1) return (ERR); TRACE_OUT(("WRITE %d booleans @%ld", tp->ext_Booleans, ftell(fp))); if (tp->ext_Booleans && fwrite(tp->Booleans + BOOLCOUNT, sizeof(char), tp->ext_Booleans, fp) != tp->ext_Booleans) return (ERR); if (even_boundary(tp->ext_Booleans)) return (ERR); TRACE_OUT(("WRITE %d numbers @%ld", tp->ext_Numbers, ftell(fp))); if (tp->ext_Numbers) { convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers); if (fwrite(buf, 2, tp->ext_Numbers, fp) != tp->ext_Numbers) return (ERR); } /* * Convert the offsets for the ext_Strings and ext_Names tables, * in that order. */ convert_shorts(buf, offsets, strmax); TRACE_OUT(("WRITE offsets @%ld", ftell(fp))); if (fwrite(buf, 2, strmax, fp) != strmax) return (ERR); /* * Write the string table after the offset tables so we do not * have to do anything about alignment. */ for (i = 0; i < tp->ext_Strings; i++) { if (VALID_STRING(tp->Strings[i + STRCOUNT])) { TRACE_OUT(("WRITE ext_Strings[%d]=%s", i, _nc_visbuf(tp->Strings[i + STRCOUNT]))); if (!WRITE_STRING(tp->Strings[i + STRCOUNT])) return (ERR); } } /* * Write the extended names */ for (i = 0; i < extcnt; i++) { TRACE_OUT(("WRITE ext_Names[%d]=%s", i, tp->ext_Names[i])); if (!WRITE_STRING(tp->ext_Names[i])) return (ERR); } }#endif /* NCURSES_XNAMES */ total_written++; return (OK);}/* * Returns the total number of entries written by this process */NCURSES_EXPORT(int)_nc_tic_written(void){ return total_written;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -