⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abs2rel.c

📁 代码检索工具GLOBAL源码。可用来浏览分析LINUX源码。
💻 C
字号:
/* * Copyright (c) 1997, 1999 Tama Communications Corporation * * This file is part of GNU GLOBAL. * * GNU GLOBAL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * GNU GLOBAL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA. */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <errno.h>#ifdef STDC_HEADERS#include <stdlib.h>#endif#ifdef HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include "abs2rel.h"/* * abs2rel: convert an absolute path name into relative. * *	i)	path	absolute path *	i)	base	base directory (must be absolute path) *	o)	result	result buffer *	i)	size	size of result buffer *	r)		!= NULL: relative path *			== NULL: error */char *abs2rel(path, base, result, size)	const char *path;	const char *base;	char *result;	const int size;{	const char *pp, *bp, *branch;	/*	 * endp points the last position which is safe in the result buffer.	 */	const char *endp = result + size - 1;	char *rp;	if (*path != '/') {		if (strlen(path) >= size)			goto erange;		strcpy(result, path);		goto finish;	} else if (*base != '/' || !size) {		errno = EINVAL;		return (NULL);	} else if (size == 1)		goto erange;	/*	 * seek to branched point.	 */	branch = path;	for (pp = path, bp = base; *pp && *bp && *pp == *bp; pp++, bp++)		if (*pp == '/')			branch = pp;	if ((*pp == 0 || (*pp == '/' && *(pp + 1) == 0)) &&	    (*bp == 0 || (*bp == '/' && *(bp + 1) == 0))) {		rp = result;		*rp++ = '.';		if (*pp == '/' || *(pp - 1) == '/')			*rp++ = '/';		if (rp > endp)			goto erange;		*rp = 0;		goto finish;	}	if ((*pp == 0 && *bp == '/') || (*pp == '/' && *bp == 0))		branch = pp;	/*	 * up to root.	 */	rp = result;	for (bp = base + (branch - path); *bp; bp++)		if (*bp == '/' && *(bp + 1) != 0) {			if (rp + 3 > endp)				goto erange;			*rp++ = '.';			*rp++ = '.';			*rp++ = '/';		}	if (rp > endp)		goto erange;	*rp = 0;	/*	 * down to leaf.	 */	if (*branch) {		if (rp + strlen(branch + 1) > endp)			goto erange;		strcpy(rp, branch + 1);	} else		*--rp = 0;finish:	return result;erange:	errno = ERANGE;	return (NULL);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -