?? write_entry.c
字號:
/**************************************************************************** * Copyright (c) 1998-2000,2002 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, distribute with modifications, sublicense, and/or sell * * copies of the Software, and to permit persons to whom the Software is * * furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included * * in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * * * Except as contained in this notice, the name(s) of the above copyright * * holders shall not be used in advertising or otherwise to promote the * * sale, use or other dealings in this Software without prior written * * authorization. * ****************************************************************************//**************************************************************************** * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * * and: Eric S. Raymond <esr@snark.thyrsus.com> * ****************************************************************************//* * write_entry.c -- write a terminfo structure onto the file system */#include <curses.priv.h>#include <sys/stat.h>#include <tic.h>#include <term_entry.h>#ifndef S_ISDIR#define S_ISDIR(mode) ((mode & S_IFMT) == S_IFDIR)#endif#if 0#define TRACE_OUT(p) DEBUG(2, p)#else#define TRACE_OUT(p) /*nothing */#endifMODULE_ID("$Id: write_entry.c,v 1.58 2002/04/21 20:35:08 tom Exp $")static int total_written;static int write_object(FILE *, TERMTYPE *);static voidwrite_file(char *filename, TERMTYPE * tp){ FILE *fp = (_nc_access(filename, W_OK) == 0) ? fopen(filename, "wb") : 0; if (fp == 0) { perror(filename); _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename); } DEBUG(1, ("Created %s", filename)); if (write_object(fp, tp) == ERR) { _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename); } fclose(fp);}/* * make_directory(char *path) * * Make a directory if it doesn't exist. */static intmake_directory(const char *path){ int rc; struct stat statbuf; char fullpath[PATH_MAX]; const char *destination = _nc_tic_dir(0); if (path == destination || *path == '/') { if (strlen(path) + 1 > sizeof(fullpath)) return (-1); (void) strcpy(fullpath, path); } else { if (strlen(destination) + strlen(path) + 2 > sizeof(fullpath)) return (-1); (void) sprintf(fullpath, "%s/%s", destination, path); } if ((rc = stat(path, &statbuf)) < 0) { rc = mkdir(path, 0777); } else { if (_nc_access(path, R_OK | W_OK | X_OK) < 0) { rc = -1; /* permission denied */ } else if (!(S_ISDIR(statbuf.st_mode))) { rc = -1; /* not a directory */ } } return rc;}NCURSES_EXPORT(void)_nc_set_writedir(char *dir)/* set the write directory for compiled entries */{ const char *destination; char actual[PATH_MAX]; if (dir == 0 && use_terminfo_vars()) dir = getenv("TERMINFO"); if (dir != 0) (void) _nc_tic_dir(dir); destination = _nc_tic_dir(0); if (make_directory(destination) < 0) { char *home = _nc_home_terminfo(); if (home != 0) { destination = home; if (make_directory(destination) < 0) _nc_err_abort("%s: permission denied (errno %d)", destination, errno); } } /* * Note: because of this code, this logic should be exercised * *once only* per run. */ if (chdir(_nc_tic_dir(destination)) < 0 || getcwd(actual, sizeof(actual)) == 0) _nc_err_abort("%s: not a directory", destination); _nc_keep_tic_dir(strdup(actual));}/* * check_writeable(char code) * * Miscellaneous initialisations * * Check for access rights to destination directories * Create any directories which don't exist. * Note: there's no reason to return the result of make_directory(), since * this function is called only in instances where that has to succeed. * */static voidcheck_writeable(int code){ static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; static bool verified[sizeof(dirnames)]; char dir[2]; char *s = 0; if (code == 0 || (s = strchr(dirnames, code)) == 0) _nc_err_abort("Illegal terminfo subdirectory \"%c\"", code); if (verified[s - dirnames]) return; dir[0] = code; dir[1] = '\0'; if (make_directory(dir) < 0) { _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir); } verified[s - dirnames] = TRUE;}/* * _nc_write_entry() * * Save the compiled version of a description in the filesystem. * * make a copy of the name-list * break it up into first-name and all-but-last-name * creat(first-name) * write object information to first-name * close(first-name) * for each name in all-but-last-name * link to first-name * * Using 'time()' to obtain a reference for file timestamps is unreliable, * e.g., with NFS, because the filesystem may have a different time * reference. We check for pre-existence of links by latching the first * timestamp from a file that we create. * * The _nc_warning() calls will report a correct line number only if * _nc_curr_line is properly set before the write_entry() call. */void_nc_write_entry(TERMTYPE * const tp){ struct stat statbuf; char name_list[MAX_TERMINFO_LENGTH]; char *first_name, *other_names; char *ptr; char filename[PATH_MAX]; char linkname[PATH_MAX];#if USE_SYMLINKS char symlinkname[PATH_MAX];#if !HAVE_LINK#undef HAVE_LINK#define HAVE_LINK 1#endif#endif /* USE_SYMLINKS */ static int call_count; static time_t start_time; /* time at start of writes */ if (call_count++ == 0) { start_time = 0; } (void) strcpy(name_list, tp->term_names); DEBUG(7, ("Name list = '%s'", name_list)); first_name = name_list; ptr = &name_list[strlen(name_list) - 1]; other_names = ptr + 1; while (ptr > name_list && *ptr != '|') ptr--; if (ptr != name_list) { *ptr = '\0'; for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++) continue; if (*ptr == '\0') other_names = ptr; else { *ptr = '\0'; other_names = ptr + 1; } } DEBUG(7, ("First name = '%s'", first_name)); DEBUG(7, ("Other names = '%s'", other_names)); _nc_set_type(first_name); if (strlen(first_name) > sizeof(filename) - 3) _nc_warning("terminal name too long."); sprintf(filename, "%c/%s", first_name[0], first_name); /* * Has this primary name been written since the first call to * write_entry()? If so, the newer write will step on the older, * so warn the user. */ if (start_time > 0 && stat(filename, &statbuf) >= 0 && statbuf.st_mtime >= start_time) { _nc_warning("name multiply defined."); } check_writeable(first_name[0]); write_file(filename, tp); if (start_time == 0) { if (stat(filename, &statbuf) < 0 || (start_time = statbuf.st_mtime) == 0) { _nc_syserr_abort("error obtaining time from %s/%s", _nc_tic_dir(0), filename); } } while (*other_names != '\0') { ptr = other_names++; while (*other_names != '|' && *other_names != '\0') other_names++; if (*other_names != '\0') *(other_names++) = '\0'; if (strlen(ptr) > sizeof(linkname) - 3) { _nc_warning("terminal alias %s too long.", ptr); continue; }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -