aboutsummaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c83
1 files changed, 79 insertions, 4 deletions
diff --git a/strbuf.c b/strbuf.c
index df2c113f2..c4e91a665 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -442,6 +442,17 @@ int strbuf_getcwd(struct strbuf *sb)
strbuf_setlen(sb, strlen(sb->buf));
return 0;
}
+
+ /*
+ * If getcwd(3) is implemented as a syscall that falls
+ * back to a regular lookup using readdir(3) etc. then
+ * we may be able to avoid EACCES by providing enough
+ * space to the syscall as it's not necessarily bound
+ * to the same restrictions as the fallback.
+ */
+ if (errno == EACCES && guessed_len < PATH_MAX)
+ continue;
+
if (errno != ERANGE)
break;
}
@@ -700,6 +711,17 @@ void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
strbuf_addstr(sb, path);
}
+void strbuf_add_real_path(struct strbuf *sb, const char *path)
+{
+ if (sb->len) {
+ struct strbuf resolved = STRBUF_INIT;
+ strbuf_realpath(&resolved, path, 1);
+ strbuf_addbuf(sb, &resolved);
+ strbuf_release(&resolved);
+ } else
+ strbuf_realpath(sb, path, 1);
+}
+
int printf_ln(const char *fmt, ...)
{
int ret;
@@ -756,14 +778,48 @@ char *xstrfmt(const char *fmt, ...)
return ret;
}
-void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
+void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
+ int tz_offset, const char *tz_name)
{
+ struct strbuf munged_fmt = STRBUF_INIT;
size_t hint = 128;
size_t len;
if (!*fmt)
return;
+ /*
+ * There is no portable way to pass timezone information to
+ * strftime, so we handle %z and %Z here.
+ */
+ for (;;) {
+ const char *percent = strchrnul(fmt, '%');
+ strbuf_add(&munged_fmt, fmt, percent - fmt);
+ if (!*percent)
+ break;
+ fmt = percent + 1;
+ switch (*fmt) {
+ case '%':
+ strbuf_addstr(&munged_fmt, "%%");
+ fmt++;
+ break;
+ case 'z':
+ strbuf_addf(&munged_fmt, "%+05d", tz_offset);
+ fmt++;
+ break;
+ case 'Z':
+ if (tz_name) {
+ strbuf_addstr(&munged_fmt, tz_name);
+ fmt++;
+ break;
+ }
+ /* FALLTHROUGH */
+ default:
+ strbuf_addch(&munged_fmt, '%');
+ }
+ }
+ fmt = munged_fmt.buf;
+
strbuf_grow(sb, hint);
len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);
@@ -775,17 +831,16 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
* output contains at least one character, and then drop the extra
* character before returning.
*/
- struct strbuf munged_fmt = STRBUF_INIT;
- strbuf_addf(&munged_fmt, "%s ", fmt);
+ strbuf_addch(&munged_fmt, ' ');
while (!len) {
hint *= 2;
strbuf_grow(sb, hint);
len = strftime(sb->buf + sb->len, sb->alloc - sb->len,
munged_fmt.buf, tm);
}
- strbuf_release(&munged_fmt);
len--; /* drop munged space */
}
+ strbuf_release(&munged_fmt);
strbuf_setlen(sb, sb->len + len);
}
@@ -863,3 +918,23 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
strbuf_setlen(sb, j);
}
+
+int strbuf_normalize_path(struct strbuf *src)
+{
+ struct strbuf dst = STRBUF_INIT;
+
+ strbuf_grow(&dst, src->len);
+ if (normalize_path_copy(dst.buf, src->buf) < 0) {
+ strbuf_release(&dst);
+ return -1;
+ }
+
+ /*
+ * normalize_path does not tell us the new length, so we have to
+ * compute it by looking for the new NUL it placed
+ */
+ strbuf_setlen(&dst, strlen(dst.buf));
+ strbuf_swap(src, &dst);
+ strbuf_release(&dst);
+ return 0;
+}