introduce "format" date-mode
[gitweb.git] / strbuf.c
index 88cafd4a70b8179a4e911c18704fb4ab0f2a21f5..a7ba0281306e5dc271a446363946b6635a2dff15 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -435,6 +435,47 @@ int strbuf_getcwd(struct strbuf *sb)
        return -1;
 }
 
+#ifdef HAVE_GETDELIM
+int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
+{
+       ssize_t r;
+
+       if (feof(fp))
+               return EOF;
+
+       strbuf_reset(sb);
+
+       /* Translate slopbuf to NULL, as we cannot call realloc on it */
+       if (!sb->alloc)
+               sb->buf = NULL;
+       r = getdelim(&sb->buf, &sb->alloc, term, fp);
+
+       if (r > 0) {
+               sb->len = r;
+               return 0;
+       }
+       assert(r == -1);
+
+       /*
+        * Normally we would have called xrealloc, which will try to free
+        * memory and recover. But we have no way to tell getdelim() to do so.
+        * Worse, we cannot try to recover ENOMEM ourselves, because we have
+        * no idea how many bytes were read by getdelim.
+        *
+        * Dying here is reasonable. It mirrors what xrealloc would do on
+        * catastrophic memory failure. We skip the opportunity to free pack
+        * memory and retry, but that's unlikely to help for a malloc small
+        * enough to hold a single line of input, anyway.
+        */
+       if (errno == ENOMEM)
+               die("Out of memory, getdelim failed");
+
+       /* Restore slopbuf that we moved out of the way before */
+       if (!sb->buf)
+               strbuf_init(sb, 0);
+       return EOF;
+}
+#else
 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 {
        int ch;
@@ -443,18 +484,22 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
                return EOF;
 
        strbuf_reset(sb);
-       while ((ch = fgetc(fp)) != EOF) {
-               strbuf_grow(sb, 1);
+       flockfile(fp);
+       while ((ch = getc_unlocked(fp)) != EOF) {
+               if (!strbuf_avail(sb))
+                       strbuf_grow(sb, 1);
                sb->buf[sb->len++] = ch;
                if (ch == term)
                        break;
        }
+       funlockfile(fp);
        if (ch == EOF && sb->len == 0)
                return EOF;
 
        sb->buf[sb->len] = '\0';
        return 0;
 }
+#endif
 
 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
 {
@@ -664,3 +709,32 @@ char *xstrfmt(const char *fmt, ...)
 
        return ret;
 }
+
+void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
+{
+       size_t len;
+
+       /*
+        * strftime reports "0" if it could not fit the result in the buffer.
+        * Unfortunately, it also reports "0" if the requested time string
+        * takes 0 bytes. So if we were to probe and grow, we have to choose
+        * some arbitrary cap beyond which we guess that the format probably
+        * just results in a 0-length output. Since we have to choose some
+        * reasonable cap anyway, and since it is not that big, we may
+        * as well just grow to their in the first place.
+        */
+       strbuf_grow(sb, 128);
+       len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);
+
+       if (!len) {
+               /*
+                * Either we failed, or the format actually produces a 0-length
+                * output. There's not much we can do, so we leave it blank.
+                * However, the output array is left in an undefined state, so
+                * we must re-assert our NUL terminator.
+                */
+               sb->buf[sb->len] = '\0';
+       } else {
+               sb->len += len;
+       }
+}