1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-uuidgen.c  Utility program to create UUIDs
3  *
4  * Copyright (C) 2006 Red Hat, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21 
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include <dbus/dbus-uuidgen.h>
28 #include <dbus/dbus.h>
29 
30 static void
usage(char * name,int ecode)31 usage (char *name, int ecode)
32 {
33   if (name == NULL)
34     name = "dbus-uuidgen";
35 
36   fprintf (stderr, "Usage: %s [--ensure[=FILENAME]] [--get[=FILENAME]]\n", name);
37   exit (ecode);
38 }
39 
40 static void
version(void)41 version (void)
42 {
43   printf ("D-Bus UUID Generator %s\n"
44           "Copyright (C) 2006 Red Hat, Inc.\n"
45           "This is free software; see the source for copying conditions.\n"
46           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
47           VERSION);
48   exit (0);
49 }
50 
51 static dbus_bool_t
get_arg(const char * arg,const char * option,const char ** value_p)52 get_arg (const char  *arg,
53          const char  *option,
54          const char **value_p)
55 {
56   const char *fn;
57 
58   if (strlen(arg) < strlen(option))
59     return FALSE;
60 
61   fn = arg + strlen(option);
62 
63   if (!(*fn == '=' || *fn == ' ' || *fn == '\0'))
64     {
65       usage (NULL, 1);
66     }
67 
68   if (*fn == '=')
69     ++fn;
70 
71   while (*fn == ' ' && *fn != '\0')
72     ++fn;
73 
74   if (*fn != '\0')
75     {
76       *value_p = fn;
77       return TRUE;
78     }
79 
80   return FALSE;
81 }
82 
83 int
main(int argc,char * argv[])84 main (int argc, char *argv[])
85 {
86   int i;
87   const char *filename;
88   dbus_bool_t ensure_uuid;
89   dbus_bool_t get_uuid;
90   DBusError error;
91 
92   ensure_uuid = FALSE;
93   get_uuid = FALSE;
94 
95   filename = NULL;
96 
97   for (i = 1; i < argc; i++)
98     {
99       char *arg = argv[i];
100 
101       if (strncmp (arg, "--ensure", strlen("--ensure")) == 0)
102         {
103           get_arg (arg, "--ensure", &filename);
104           ensure_uuid = TRUE;
105         }
106       else if (strncmp (arg, "--get", strlen("--get")) == 0)
107         {
108           get_arg (arg, "--get", &filename);
109           get_uuid = TRUE;
110         }
111       else if (strcmp (arg, "--help") == 0)
112 	usage (argv[0], 0);
113       else if (strcmp (arg, "--version") == 0)
114         version ();
115       else
116         usage (argv[0], 1);
117     }
118 
119   if (get_uuid && ensure_uuid)
120     {
121       fprintf (stderr, "Can't specify both --get and --ensure\n");
122       exit (1);
123     }
124 
125   dbus_error_init (&error);
126 
127   if (get_uuid || ensure_uuid)
128     {
129       char *uuid;
130       if (dbus_internal_do_not_use_get_uuid (filename, &uuid, ensure_uuid, &error))
131         {
132           if (get_uuid) /* print nothing on --ensure */
133             printf ("%s\n", uuid);
134           dbus_free (uuid);
135         }
136     }
137   else
138     {
139       char *uuid;
140       if (dbus_internal_do_not_use_create_uuid (&uuid))
141         {
142           printf ("%s\n", uuid);
143           dbus_free (uuid);
144         }
145       else
146         {
147           dbus_set_error (&error, DBUS_ERROR_NO_MEMORY, "No memory");
148         }
149     }
150 
151   if (dbus_error_is_set (&error))
152     {
153       fprintf (stderr, "%s\n", error.message);
154       dbus_error_free (&error);
155       exit (1);
156     }
157   else
158     {
159       exit (0);
160     }
161 }
162