From c1833a2f531cb1f3070d42efdc3e2bf075f8a8f0 Mon Sep 17 00:00:00 2001 From: Charlie Gordon Date: Tue, 3 Dec 2013 17:35:06 +0000 Subject: [PATCH] added make_user_path to compute path relative to user HOME directory --- qe.h | 3 ++- util.c | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/qe.h b/qe.h index 70f4d6a..8dd6f59 100644 --- a/qe.h +++ b/qe.h @@ -2,7 +2,7 @@ * QEmacs, tiny but powerful multimode editor * * Copyright (c) 2000,2001 Fabrice Bellard. - * Copyright (c) 2000-2008 Charlie Gordon. + * Copyright (c) 2000-2013 Charlie Gordon. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -195,6 +195,7 @@ void find_file_close(FindFileState *s); int is_directory(const char *path); void canonicalize_path(char *buf, int buf_size, const char *path); void canonicalize_absolute_path(char *buf, int buf_size, const char *path1); +char *make_user_path(char *buf, int buf_size, const char *path); char *reduce_filename(char *dest, int size, const char *filename); int match_extension(const char *filename, const char *extlist); int remove_slash(char *buf); diff --git a/util.c b/util.c index c40cf47..d7bbd32 100644 --- a/util.c +++ b/util.c @@ -2,7 +2,7 @@ * Utilities for qemacs. * * Copyright (c) 2001 Fabrice Bellard. - * Copyright (c) 2002-2008 Charlie Gordon. + * Copyright (c) 2002-2013 Charlie Gordon. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -280,6 +280,26 @@ void canonicalize_absolute_path(char *buf, int buf_size, const char *path1) canonicalize_path(buf, buf_size, path1); } +/* reduce path relative to homedir */ +char *make_user_path(char *buf, int buf_size, const char *path) +{ + char *homedir; + + homedir = getenv("HOME"); + if (homedir) { + int len = strlen(homedir); + + if (len && homedir[len - 1] == '/') + len--; + + if (!memcmp(path, homedir, len) && path[len] == '/') { + pstrcpy(buf, buf_size, "~"); + return pstrcat(buf, buf_size, path + len); + } + } + return pstrcpy(buf, buf_size, path); +} + char *reduce_filename(char *dest, int size, const char *filename) { const char *base = get_basename(filename);