From 4ba93e9679394e7f05be4cc7cfa500e8ab941e75 Mon Sep 17 00:00:00 2001 From: Jean-Francois Brousseau Date: Fri, 30 Jul 2004 11:50:34 +0000 Subject: Add cvs_file_find() to find a particular file from its path within a hierarchy --- usr.bin/cvs/file.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 5 deletions(-) (limited to 'usr.bin/cvs/file.c') diff --git a/usr.bin/cvs/file.c b/usr.bin/cvs/file.c index 46229ca33a4..e9df757714a 100644 --- a/usr.bin/cvs/file.c +++ b/usr.bin/cvs/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.12 2004/07/29 17:51:06 jfb Exp $ */ +/* $OpenBSD: file.c,v 1.13 2004/07/30 11:50:33 jfb Exp $ */ /* * Copyright (c) 2004 Jean-Francois Brousseau * All rights reserved. @@ -399,6 +399,59 @@ cvs_file_getspec(char **fspec, int fsn, int flags) } +/* + * cvs_file_find() + * + * Find the pointer to a CVS file entry within the file hierarchy . + * The file's pathname must be relative to the base of . + * Returns the entry on success, or NULL on failure. + */ + +CVSFILE* +cvs_file_find(CVSFILE *hier, const char *path) +{ + char *pp, *sp, pbuf[MAXPATHLEN]; + CVSFILE *sf, *cf; + + strlcpy(pbuf, path, sizeof(pbuf)); + + cf = hier; + pp = pbuf; + do { + sp = strchr(pp, '/'); + if (sp != NULL) + *sp = '\0'; + + /* special case */ + if (*pp == '.') { + if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) { + /* request to go back to parent */ + if (cf->cf_parent == NULL) { + cvs_log(LP_NOTICE, + "path %s goes back too far", path); + return (NULL); + } + cf = cf->cf_parent; + continue; + } + else if (*(pp + 1) == '\0') + continue; + } + + TAILQ_FOREACH(sf, &(cf->cf_ddat->cd_files), cf_list) + if (strcmp(pp, sf->cf_name) == 0) + break; + if (sf == NULL) + return (NULL); + + cf = sf; + pp = sp; + } while (sp != NULL); + + return (NULL); +} + + /* * cvs_file_getdir() * @@ -408,9 +461,9 @@ cvs_file_getspec(char **fspec, int fsn, int flags) static int cvs_file_getdir(struct cvs_file *cf, int flags) { - int nf, ret, fd; + int ret, fd; long base; - void *dp, *ep, *tmp; + void *dp, *ep; char fbuf[2048], pbuf[MAXPATHLEN]; struct dirent *ent; struct cvs_file *cfp; @@ -525,11 +578,13 @@ cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg) TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) { ret = cvs_file_examine(fp, exam, arg); if (ret == -1) - return (-1); + break; } } else - return (*exam)(cf, arg); + ret = (*exam)(cf, arg); + + return (ret); } -- cgit v1.2.3