dbus-marshal-validate: Validate length of arrays of fixed-length items
[platform/upstream/dbus.git] / tools / dbus-uuidgen.c
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 (const char *name, int ecode) _DBUS_GNUC_NORETURN;
31
32 static void
33 usage (const char *name,
34        int ecode)
35 {
36   if (name == NULL)
37     name = "dbus-uuidgen";
38   
39   fprintf (stderr, "Usage: %s [--ensure[=FILENAME]] [--get[=FILENAME]]\n", name);
40   exit (ecode);
41 }
42
43 static void version (void) _DBUS_GNUC_NORETURN;
44
45 static void
46 version (void)
47 {
48   printf ("D-Bus UUID Generator %s\n"
49           "Copyright (C) 2006 Red Hat, Inc.\n"
50           "This is free software; see the source for copying conditions.\n"
51           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
52           VERSION);
53   exit (0);
54 }
55
56 static dbus_bool_t
57 get_arg (const char  *arg,
58          const char  *option,
59          const char **value_p)
60 {
61   const char *fn;
62
63   if (strlen(arg) < strlen(option))
64     return FALSE;
65   
66   fn = arg + strlen(option);
67
68   if (!(*fn == '=' || *fn == ' ' || *fn == '\0'))
69     {
70       usage (NULL, 1);
71     }
72   
73   if (*fn == '=')
74     ++fn;
75   
76   while (*fn == ' ' && *fn != '\0')
77     ++fn;
78   
79   if (*fn != '\0')
80     {
81       *value_p = fn;
82       return TRUE;
83     }
84
85   return FALSE;
86 }
87
88 int
89 main (int argc, char *argv[])
90 {
91   int i;
92   const char *filename;
93   dbus_bool_t ensure_uuid;
94   dbus_bool_t get_uuid;
95   DBusError error;
96
97   ensure_uuid = FALSE;
98   get_uuid = FALSE;
99   
100   filename = NULL;
101
102   for (i = 1; i < argc; i++)
103     {
104       char *arg = argv[i];
105
106       if (strncmp (arg, "--ensure", strlen("--ensure")) == 0)
107         {
108           get_arg (arg, "--ensure", &filename);
109           ensure_uuid = TRUE;
110         }
111       else if (strncmp (arg, "--get", strlen("--get")) == 0)
112         {
113           get_arg (arg, "--get", &filename);
114           get_uuid = TRUE;
115         }
116       else if (strcmp (arg, "--help") == 0)
117         usage (argv[0], 0);
118       else if (strcmp (arg, "--version") == 0)
119         version ();
120       else
121         usage (argv[0], 1);
122     }
123
124   if (get_uuid && ensure_uuid)
125     {
126       fprintf (stderr, "Can't specify both --get and --ensure\n");
127       exit (1);
128     }
129
130   dbus_error_init (&error);
131   
132   if (get_uuid || ensure_uuid)
133     {
134       char *uuid;
135       if (dbus_internal_do_not_use_get_uuid (filename, &uuid, ensure_uuid, &error))
136         {
137           if (get_uuid) /* print nothing on --ensure */
138             printf ("%s\n", uuid);
139           dbus_free (uuid);
140         }
141     }
142   else
143     {
144       char *uuid;
145
146       if (_dbus_create_uuid (&uuid, &error))
147         {
148           printf ("%s\n", uuid);
149           dbus_free (uuid);
150         }
151     }
152
153   if (dbus_error_is_set (&error))
154     {
155       fprintf (stderr, "%s\n", error.message);
156       dbus_error_free (&error);
157       exit (1);
158     }
159   else
160     {
161       exit (0);
162     }
163 }