Add a flag to overwrite existing entries

This commit is contained in:
Pierre Pronchery 2015-06-18 00:20:45 -04:00
parent 3277f106b9
commit 21b31f6e84

View File

@ -46,8 +46,8 @@
/* bookmark */ /* bookmark */
/* private */ /* private */
/* prototypes */ /* prototypes */
static int _bookmark(char const * title, char const * url, char const * icon, static int _bookmark(int force, char const * title, char const * url,
char const * comment); char const * icon, char const * comment);
static int _error(char const * message, int ret); static int _error(char const * message, int ret);
static int _usage(void); static int _usage(void);
@ -55,21 +55,21 @@ static int _usage(void);
/* functions */ /* functions */
/* bookmark */ /* bookmark */
static int _bookmark_do(char const * title, char const * url, char const * icon, static int _bookmark_do(int force, char const * title, char const * url,
char const * comment); char const * icon, char const * comment);
static int _bookmark(char const * title, char const * url, char const * icon, static int _bookmark(int force, char const * title, char const * url,
char const * comment) char const * icon, char const * comment)
{ {
int ret; int ret;
if((ret = _bookmark_do(title, url, icon, comment)) != 0) if((ret = _bookmark_do(force, title, url, icon, comment)) != 0)
error_print(PROGNAME); error_print(PROGNAME);
return ret; return ret;
} }
static int _bookmark_do(char const * title, char const * url, char const * icon, static int _bookmark_do(int force, char const * title, char const * url,
char const * comment) char const * icon, char const * comment)
{ {
int ret = 0; int ret = 0;
const char section[] = "Desktop Entry"; const char section[] = "Desktop Entry";
@ -112,7 +112,7 @@ static int _bookmark_do(char const * title, char const * url, char const * icon,
string_delete(datahome); string_delete(datahome);
return -1; return -1;
} }
if(stat(pathname, &st) == 0) if(force == 0 && stat(pathname, &st) == 0)
ret = -error_set_code(1, "%s: %s", title, ret = -error_set_code(1, "%s: %s", title,
"Bookmark already set"); "Bookmark already set");
else if((ret = config_set(config, section, "Type", "URL")) != 0 else if((ret = config_set(config, section, "Type", "URL")) != 0
@ -143,7 +143,8 @@ static int _error(char const * message, int ret)
/* usage */ /* usage */
static int _usage(void) static int _usage(void)
{ {
fprintf(stderr, _("Usage: %s -u [-t title][-i icon][-C comment] URL\n"), fprintf(stderr, _("Usage: %s -u [-f][-t title][-i icon][-C comment]"
" URL\n"),
PROGNAME); PROGNAME);
return 1; return 1;
} }
@ -155,6 +156,7 @@ int main(int argc, char * argv[])
{ {
int o; int o;
int url = 0; int url = 0;
int force = 0;
char const * title = NULL; char const * title = NULL;
char const * icon = "stock_internet"; char const * icon = "stock_internet";
char const * comment = NULL; char const * comment = NULL;
@ -163,12 +165,15 @@ int main(int argc, char * argv[])
_error("setlocale", 1); _error("setlocale", 1);
bindtextdomain(PACKAGE, LOCALEDIR); bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE); textdomain(PACKAGE);
while((o = getopt(argc, argv, "uC:i:t:")) != -1) while((o = getopt(argc, argv, "uC:fi:t:")) != -1)
switch(o) switch(o)
{ {
case 'C': case 'C':
comment = optarg; comment = optarg;
break; break;
case 'f':
force = 1;
break;
case 'i': case 'i':
icon = optarg; icon = optarg;
break; break;
@ -183,5 +188,6 @@ int main(int argc, char * argv[])
} }
if((optind + 1) != argc || url == 0) if((optind + 1) != argc || url == 0)
return _usage(); return _usage();
return (_bookmark(title, argv[optind], icon, comment) == 0) ? 0 : 2; return (_bookmark(force, title, argv[optind], icon, comment) == 0)
? 0 : 2;
} }