contrib/git-credential-gnome-keyring.c: set Gnome application name
[gitweb.git] / contrib / credential / gnome-keyring / git-credential-gnome-keyring.c
index f2cdefee605aebe6c577129ae2c8cd76a3555a96..43b19dd5a21721b1232b7c7570fdd314480bca3d 100644 (file)
@@ -28,6 +28,7 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <glib.h>
 #include <gnome-keyring.h>
 
 /*
@@ -46,11 +47,6 @@ struct credential
 #define CREDENTIAL_INIT \
   { NULL,NULL,0,NULL,NULL,NULL }
 
-void credential_init(struct credential *c);
-void credential_clear(struct credential *c);
-int  credential_read(struct credential *c);
-void credential_write(const struct credential *c);
-
 typedef int (*credential_op_cb)(struct credential*);
 
 struct credential_operation
@@ -62,14 +58,6 @@ struct credential_operation
 #define CREDENTIAL_OP_END \
   { NULL,NULL }
 
-/*
- * Table with operation callbacks is defined in concrete
- * credential helper implementation and contains entries
- * like { "get", function_to_get_credential } terminated
- * by CREDENTIAL_OP_END.
- */
-struct credential_operation const credential_helper_ops[];
-
 /* ---------------- common helper functions ----------------- */
 
 static inline void free_password(char *password)
@@ -104,16 +92,6 @@ static inline void error(const char *fmt, ...)
        va_end(ap);
 }
 
-static inline void die(const char *fmt, ...)
-{
-       va_list ap;
-
-       va_start(ap,fmt);
-       error(fmt, ap);
-       va_end(ap);
-       exit(EXIT_FAILURE);
-}
-
 static inline void die_errno(int err)
 {
        error("%s", strerror(err));
@@ -140,10 +118,10 @@ static char* keyring_object(struct credential *c)
                return object;
 
        object = (char*) malloc(strlen(c->host)+strlen(c->path)+8);
-       if(!object)
+       if (!object)
                die_errno(errno);
 
-       if(c->port)
+       if (c->port)
                sprintf(object,"%s:%hd/%s",c->host,c->port,c->path);
        else
                sprintf(object,"%s/%s",c->host,c->path);
@@ -151,7 +129,7 @@ static char* keyring_object(struct credential *c)
        return object;
 }
 
-int keyring_get(struct credential *c)
+static int keyring_get(struct credential *c)
 {
        char* object = NULL;
        GList *entries;
@@ -201,7 +179,7 @@ int keyring_get(struct credential *c)
 }
 
 
-int keyring_store(struct credential *c)
+static int keyring_store(struct credential *c)
 {
        guint32 item_id;
        char  *object = NULL;
@@ -235,7 +213,7 @@ int keyring_store(struct credential *c)
        return EXIT_SUCCESS;
 }
 
-int keyring_erase(struct credential *c)
+static int keyring_erase(struct credential *c)
 {
        char  *object = NULL;
        GList *entries;
@@ -300,7 +278,7 @@ int keyring_erase(struct credential *c)
  * Table with helper operation callbacks, used by generic
  * credential helper main function.
  */
-struct credential_operation const credential_helper_ops[] =
+static struct credential_operation const credential_helper_ops[] =
 {
        { "get",   keyring_get   },
        { "store", keyring_store },
@@ -310,12 +288,12 @@ struct credential_operation const credential_helper_ops[] =
 
 /* ------------------ credential functions ------------------ */
 
-void credential_init(struct credential *c)
+static void credential_init(struct credential *c)
 {
        memset(c, 0, sizeof(*c));
 }
 
-void credential_clear(struct credential *c)
+static void credential_clear(struct credential *c)
 {
        free(c->protocol);
        free(c->host);
@@ -326,10 +304,10 @@ void credential_clear(struct credential *c)
        credential_init(c);
 }
 
-int credential_read(struct credential *c)
+static int credential_read(struct credential *c)
 {
        char    buf[1024];
-       ssize_t line_len = 0;
+       size_t line_len;
        char   *key      = buf;
        char   *value;
 
@@ -337,14 +315,14 @@ int credential_read(struct credential *c)
        {
                line_len = strlen(buf);
 
-               if(buf[line_len-1]=='\n')
+               if (line_len && buf[line_len-1] == '\n')
                        buf[--line_len]='\0';
 
-               if(!line_len)
+               if (!line_len)
                        break;
 
                value = strchr(buf,'=');
-               if(!value) {
+               if (!value) {
                        warning("invalid credential line: %s", key);
                        return -1;
                }
@@ -381,14 +359,14 @@ int credential_read(struct credential *c)
        return 0;
 }
 
-void credential_write_item(FILE *fp, const char *key, const char *value)
+static void credential_write_item(FILE *fp, const char *key, const char *value)
 {
        if (!value)
                return;
        fprintf(fp, "%s=%s\n", key, value);
 }
 
-void credential_write(const struct credential *c)
+static void credential_write(const struct credential *c)
 {
        /* only write username/password, if set */
        credential_write_item(stdout, "username", c->username);
@@ -402,9 +380,9 @@ static void usage(const char *name)
 
        basename = (basename) ? basename + 1 : name;
        fprintf(stderr, "usage: %s <", basename);
-       while(try_op->name) {
+       while (try_op->name) {
                fprintf(stderr,"%s",(try_op++)->name);
-               if(try_op->name)
+               if (try_op->name)
                        fprintf(stderr,"%s","|");
        }
        fprintf(stderr,"%s",">\n");
@@ -419,19 +397,21 @@ int main(int argc, char *argv[])
 
        if (!argv[1]) {
                usage(argv[0]);
-               goto out;
+               exit(EXIT_FAILURE);
        }
 
+       g_set_application_name("Git Credential Helper");
+
        /* lookup operation callback */
-       while(try_op->name && strcmp(argv[1], try_op->name))
+       while (try_op->name && strcmp(argv[1], try_op->name))
                try_op++;
 
        /* unsupported operation given -- ignore silently */
-       if(!try_op->name || !try_op->op)
+       if (!try_op->name || !try_op->op)
                goto out;
 
        ret = credential_read(&cred);
-       if(ret)
+       if (ret)
                goto out;
 
        /* perform credential operation */