getprogname: Work around program name truncation when possible.
[gnulib.git] / lib / backup-find.c
blobc39993975be1b47f08205ed23a8970000b57ce5d
1 /* backupfile.c -- make Emacs style backup file names
3 Copyright 2017-2018 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include "backup-internal.h"
22 #include "argmatch.h"
23 #include "xalloc.h"
25 #include <stdlib.h>
27 /* Return the name of a backup file for the existing file FILE,
28 allocated with malloc. Report an error and exit if out of memory.
29 Do not call this function if backup_type == no_backups. */
31 char *
32 find_backup_file_name (char const *file, enum backup_type backup_type)
34 char *result = backupfile_internal (file, backup_type, false);
35 if (!result)
36 xalloc_die ();
37 return result;
40 static char const *const backup_args[] =
42 /* In a series of synonyms, present the most meaningful first, so
43 that argmatch_valid be more readable. */
44 "none", "off",
45 "simple", "never",
46 "existing", "nil",
47 "numbered", "t",
48 NULL
51 static const enum backup_type backup_types[] =
53 no_backups, no_backups,
54 simple_backups, simple_backups,
55 numbered_existing_backups, numbered_existing_backups,
56 numbered_backups, numbered_backups
59 /* Ensure that these two vectors have the same number of elements,
60 not counting the final NULL in the first one. */
61 ARGMATCH_VERIFY (backup_args, backup_types);
63 /* Return the type of backup specified by VERSION.
64 If VERSION is NULL or the empty string, return numbered_existing_backups.
65 If VERSION is invalid or ambiguous, fail with a diagnostic appropriate
66 for the specified CONTEXT. Unambiguous abbreviations are accepted. */
68 enum backup_type
69 get_version (char const *context, char const *version)
71 if (version == 0 || *version == 0)
72 return numbered_existing_backups;
73 else
74 return XARGMATCH (context, version, backup_args, backup_types);
78 /* Return the type of backup specified by VERSION.
79 If VERSION is NULL, use the value of the envvar VERSION_CONTROL.
80 If the specified string is invalid or ambiguous, fail with a diagnostic
81 appropriate for the specified CONTEXT.
82 Unambiguous abbreviations are accepted. */
84 enum backup_type
85 xget_version (char const *context, char const *version)
87 if (version && *version)
88 return get_version (context, version);
89 else
90 return get_version ("$VERSION_CONTROL", getenv ("VERSION_CONTROL"));