usage.con commit rerere: make rr-cache fanout directory honor umask (fd95633)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "git-compat-util.h"
   7#include "cache.h"
   8
   9void vreportf(const char *prefix, const char *err, va_list params)
  10{
  11        char msg[4096];
  12        vsnprintf(msg, sizeof(msg), err, params);
  13        fprintf(stderr, "%s%s\n", prefix, msg);
  14}
  15
  16void vwritef(int fd, const char *prefix, const char *err, va_list params)
  17{
  18        char msg[4096];
  19        int len = vsnprintf(msg, sizeof(msg), err, params);
  20        if (len > sizeof(msg))
  21                len = sizeof(msg);
  22
  23        write_in_full(fd, prefix, strlen(prefix));
  24        write_in_full(fd, msg, len);
  25        write_in_full(fd, "\n", 1);
  26}
  27
  28static NORETURN void usage_builtin(const char *err, va_list params)
  29{
  30        vreportf("usage: ", err, params);
  31        exit(129);
  32}
  33
  34static NORETURN void die_builtin(const char *err, va_list params)
  35{
  36        vreportf("fatal: ", err, params);
  37        exit(128);
  38}
  39
  40static void error_builtin(const char *err, va_list params)
  41{
  42        vreportf("error: ", err, params);
  43}
  44
  45static void warn_builtin(const char *warn, va_list params)
  46{
  47        vreportf("warning: ", warn, params);
  48}
  49
  50/* If we are in a dlopen()ed .so write to a global variable would segfault
  51 * (ugh), so keep things static. */
  52static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
  53static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
  54static void (*error_routine)(const char *err, va_list params) = error_builtin;
  55static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
  56
  57void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
  58{
  59        die_routine = routine;
  60}
  61
  62void set_error_routine(void (*routine)(const char *err, va_list params))
  63{
  64        error_routine = routine;
  65}
  66
  67void NORETURN usagef(const char *err, ...)
  68{
  69        va_list params;
  70
  71        va_start(params, err);
  72        usage_routine(err, params);
  73        va_end(params);
  74}
  75
  76void NORETURN usage(const char *err)
  77{
  78        usagef("%s", err);
  79}
  80
  81void NORETURN die(const char *err, ...)
  82{
  83        va_list params;
  84
  85        va_start(params, err);
  86        die_routine(err, params);
  87        va_end(params);
  88}
  89
  90void NORETURN die_errno(const char *fmt, ...)
  91{
  92        va_list params;
  93        char fmt_with_err[1024];
  94        char str_error[256], *err;
  95        int i, j;
  96
  97        err = strerror(errno);
  98        for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
  99                if ((str_error[j++] = err[i++]) != '%')
 100                        continue;
 101                if (j < sizeof(str_error) - 1) {
 102                        str_error[j++] = '%';
 103                } else {
 104                        /* No room to double the '%', so we overwrite it with
 105                         * '\0' below */
 106                        j--;
 107                        break;
 108                }
 109        }
 110        str_error[j] = 0;
 111        snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
 112
 113        va_start(params, fmt);
 114        die_routine(fmt_with_err, params);
 115        va_end(params);
 116}
 117
 118int error(const char *err, ...)
 119{
 120        va_list params;
 121
 122        va_start(params, err);
 123        error_routine(err, params);
 124        va_end(params);
 125        return -1;
 126}
 127
 128void warning(const char *warn, ...)
 129{
 130        va_list params;
 131
 132        va_start(params, warn);
 133        warn_routine(warn, params);
 134        va_end(params);
 135}