gosxappinfo: fix typo in url_escape_hostname
[glib.git] / gio / gio-tool-set.c
blob2b63acab827058a98514425ad163abfebd5da15b
1 /*
2 * Copyright 2015 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Matthias Clasen <mclasen@redhat.com>
20 #include "config.h"
22 #include <gio/gio.h>
23 #include <gi18n.h>
24 #include <stdlib.h>
26 #include "gio-tool.h"
29 static char *attr_type = "string";
30 static gboolean nofollow_symlinks = FALSE;
32 static const GOptionEntry entries[] = {
33 { "type", 't', 0, G_OPTION_ARG_STRING, &attr_type, N_("Type of the attribute"), N_("TYPE") },
34 { "nofollow-symlinks", 'n', 0, G_OPTION_ARG_NONE, &nofollow_symlinks, N_("Don’t follow symbolic links"), NULL },
35 { NULL }
38 static char *
39 hex_unescape (const char *str)
41 int i;
42 char *unescaped_str, *p;
43 unsigned char c;
44 int len;
46 len = strlen (str);
47 unescaped_str = g_malloc (len + 1);
49 p = unescaped_str;
50 for (i = 0; i < len; i++)
52 if (str[i] == '\\' &&
53 str[i+1] == 'x' &&
54 len - i >= 4)
56 c =
57 (g_ascii_xdigit_value (str[i+2]) << 4) |
58 g_ascii_xdigit_value (str[i+3]);
59 *p++ = c;
60 i += 3;
62 else
63 *p++ = str[i];
65 *p++ = 0;
67 return unescaped_str;
70 int
71 handle_set (int argc, char *argv[], gboolean do_help)
73 GOptionContext *context;
74 GError *error = NULL;
75 GFile *file;
76 const char *attribute;
77 GFileAttributeType type;
78 gpointer value;
79 gboolean b;
80 guint32 uint32;
81 gint32 int32;
82 guint64 uint64;
83 gint64 int64;
84 gchar *param;
86 g_set_prgname ("gio set");
88 /* Translators: commandline placeholder */
89 param = g_strdup_printf ("%s %s %s...", _("LOCATION"), _("ATTRIBUTE"), _("VALUE"));
90 context = g_option_context_new (param);
91 g_free (param);
92 g_option_context_set_help_enabled (context, FALSE);
93 g_option_context_set_summary (context, _("Set a file attribute of LOCATION."));
94 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
96 if (do_help)
98 show_help (context, NULL);
99 return 0;
102 if (!g_option_context_parse (context, &argc, &argv, &error))
104 show_help (context, error->message);
105 g_error_free (error);
106 return 1;
109 if (argc < 2)
111 show_help (context, _("Location not specified"));
112 return 1;
115 file = g_file_new_for_commandline_arg (argv[1]);
117 if (argc < 3)
119 show_help (context, _("Attribute not specified"));
120 return 1;
123 attribute = argv[2];
125 type = attribute_type_from_string (attr_type);
126 if ((argc < 4) && (type != G_FILE_ATTRIBUTE_TYPE_INVALID))
128 show_help (context, _("Value not specified"));
129 return 1;
132 if ((argc > 4) && (type != G_FILE_ATTRIBUTE_TYPE_STRINGV))
134 show_help (context, _("Too many arguments"));
135 return 1;
138 g_option_context_free (context);
140 switch (type)
142 case G_FILE_ATTRIBUTE_TYPE_STRING:
143 value = argv[3];
144 break;
145 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
146 value = hex_unescape (argv[3]);
147 break;
148 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
149 b = g_ascii_strcasecmp (argv[3], "true") == 0;
150 value = &b;
151 break;
152 case G_FILE_ATTRIBUTE_TYPE_UINT32:
153 uint32 = atol (argv[3]);
154 value = &uint32;
155 break;
156 case G_FILE_ATTRIBUTE_TYPE_INT32:
157 int32 = atol (argv[3]);
158 value = &int32;
159 break;
160 case G_FILE_ATTRIBUTE_TYPE_UINT64:
161 uint64 = g_ascii_strtoull (argv[3], NULL, 10);
162 value = &uint64;
163 break;
164 case G_FILE_ATTRIBUTE_TYPE_INT64:
165 int64 = g_ascii_strtoll (argv[3], NULL, 10);
166 value = &int64;
167 break;
168 case G_FILE_ATTRIBUTE_TYPE_STRINGV:
169 value = &argv[3];
170 break;
171 case G_FILE_ATTRIBUTE_TYPE_INVALID:
172 value = NULL;
173 break;
174 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
175 default:
176 g_printerr (_("Invalid attribute type %s\n"), attr_type);
177 return 1;
180 if (!g_file_set_attribute (file,
181 attribute,
182 type,
183 value,
184 nofollow_symlinks ?
185 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS :
186 G_FILE_QUERY_INFO_NONE,
187 NULL, &error))
189 g_printerr (_("Error setting attribute: %s\n"), error->message);
190 g_error_free (error);
191 return 1;
194 return 0;