From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?UTF-8?B?UmVuw6kgU2NoYXJmZQ==?= Subject: [PATCH 1/3] strbuf: add strbuf_add_cwd() Date: Sun, 20 Jul 2014 13:21:14 +0200 Message-ID: <53CBA62A.5010800@web.de> References: <53CBA59C.8050901@web.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Cc: Karsten Blees , =?UTF-8?B?Tmd1eeG7hW4gVGjDoWk=?= =?UTF-8?B?IE5n4buNYyBEdXk=?= , Junio C Hamano To: Git Mailing List X-From: git-owner@vger.kernel.org Sun Jul 20 13:21:42 2014 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1X8pBV-000286-Gm for gcvg-git-2@plane.gmane.org; Sun, 20 Jul 2014 13:21:41 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752124AbaGTLVh (ORCPT ); Sun, 20 Jul 2014 07:21:37 -0400 Received: from mout.web.de ([212.227.15.4]:54302 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751916AbaGTLVh (ORCPT ); Sun, 20 Jul 2014 07:21:37 -0400 Received: from [192.168.178.27] ([79.253.172.97]) by smtp.web.de (mrweb004) with ESMTPSA (Nemesis) id 0Lir9R-1WY7W70qqb-00cwCG; Sun, 20 Jul 2014 13:21:34 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <53CBA59C.8050901@web.de> X-Provags-ID: V03:K0:mwhlFoXrS0dIu9EC/HuuwHbRGgTlL1NnibP9PvYHZtXtTByjxjR NIx2NbuLl8CWDWrej7i2k6Bw0QkOLLYlhRyuXd/tLR3DTxxLrfvMgUTR4yalFgKFOXhwyrj qZteC6gcJg3ThUzFNYj14OsqIc0vGXkpbTGSQcCjHxGLj/Ohq9GW4hj6ZvLvbvVcm1VuGai cniZFNN8C7yjjajGMONCg== Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: Add strbuf_add_cwd(), which adds the current working directory to a strbuf. Because it doesn't use a fixed-size buffer it supports arbitrarily long paths, as long as the platform's getcwd() does as well. At least on Linux and FreeBSD it handles paths longer than PATH_MAX just fine. Suggested-by: Karsten Blees Signed-off-by: Rene Scharfe --- Documentation/technical/api-strbuf.txt | 4 ++++ strbuf.c | 22 ++++++++++++++++++++++ strbuf.h | 1 + 3 files changed, 27 insertions(+) diff --git a/Documentation/technical/api-strbuf.txt b/Documentation/technical/api-strbuf.txt index f9c06a7..b96b78c 100644 --- a/Documentation/technical/api-strbuf.txt +++ b/Documentation/technical/api-strbuf.txt @@ -257,6 +257,10 @@ which can be used by the programmer of the callback as she sees fit. Add a formatted string to the buffer. +`strbuf_add_cwd`:: + + Add the current working directory to the buffer. + `strbuf_commented_addf`:: Add a formatted string prepended by a comment character and a diff --git a/strbuf.c b/strbuf.c index 33018d8..4e44773 100644 --- a/strbuf.c +++ b/strbuf.c @@ -406,6 +406,28 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) return -1; } +int strbuf_add_cwd(struct strbuf *sb) +{ + size_t oldalloc = sb->alloc; + size_t guessed_len = 32; + + for (;; guessed_len *= 2) { + char *cwd; + + strbuf_grow(sb, guessed_len); + cwd = getcwd(sb->buf + sb->len, sb->alloc - sb->len); + if (cwd) { + strbuf_setlen(sb, sb->len + strlen(cwd)); + return 0; + } + if (errno != ERANGE) + break; + } + if (oldalloc == 0) + strbuf_release(sb); + return -1; +} + int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term) { int ch; diff --git a/strbuf.h b/strbuf.h index a7c0192..ba95cd6 100644 --- a/strbuf.h +++ b/strbuf.h @@ -174,6 +174,7 @@ extern size_t strbuf_fread(struct strbuf *, size_t, FILE *); extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint); extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint); extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint); +extern int strbuf_add_cwd(struct strbuf *sb); extern int strbuf_getwholeline(struct strbuf *, FILE *, int); extern int strbuf_getline(struct strbuf *, FILE *, int); -- 2.0.2