]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/install-printf.c
Assorted format fixes
[thirdparty/systemd.git] / src / shared / install-printf.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <stdlib.h>
24
25 #include "specifier.h"
26 #include "unit-name.h"
27 #include "util.h"
28 #include "install-printf.h"
29
30 static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
31 InstallInfo *i = userdata;
32 char *n;
33
34 assert(i);
35
36 n = unit_name_to_prefix_and_instance(i->name);
37 if (!n)
38 return -ENOMEM;
39
40 *ret = n;
41 return 0;
42 }
43
44 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
45 InstallInfo *i = userdata;
46 char *n;
47
48 assert(i);
49
50 n = unit_name_to_prefix(i->name);
51 if (!n)
52 return -ENOMEM;
53
54 *ret = n;
55 return 0;
56 }
57
58 static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
59 InstallInfo *i = userdata;
60 char *instance;
61 int r;
62
63 assert(i);
64
65 r = unit_name_to_instance(i->name, &instance);
66 if (r < 0)
67 return r;
68
69 if (!instance) {
70 instance = strdup("");
71 if (!instance)
72 return -ENOMEM;
73 }
74
75 *ret = instance;
76 return 0;
77 }
78
79 static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
80 InstallInfo *i = userdata;
81 const char *username;
82 _cleanup_free_ char *tmp = NULL;
83 char *printed = NULL;
84
85 assert(i);
86
87 if (i->user)
88 username = i->user;
89 else
90 /* get USER env from env or our own uid */
91 username = tmp = getusername_malloc();
92
93 switch (specifier) {
94 case 'u':
95 printed = strdup(username);
96 break;
97 case 'U': {
98 /* fish username from passwd */
99 uid_t uid;
100 int r;
101
102 r = get_user_creds(&username, &uid, NULL, NULL, NULL);
103 if (r < 0)
104 return r;
105
106 if (asprintf(&printed, UID_FMT, uid) < 0)
107 return -ENOMEM;
108 break;
109 }}
110
111
112 *ret = printed;
113 return 0;
114 }
115
116
117 int install_full_printf(InstallInfo *i, const char *format, char **ret) {
118
119 /* This is similar to unit_full_printf() but does not support
120 * anything path-related.
121 *
122 * %n: the full id of the unit (foo@bar.waldo)
123 * %N: the id of the unit without the suffix (foo@bar)
124 * %p: the prefix (foo)
125 * %i: the instance (bar)
126
127 * %U the UID of the configured user or running user
128 * %u the username of the configured user or running user
129 * %m the machine ID of the running system
130 * %H the host name of the running system
131 * %b the boot ID of the running system
132 * %v `uname -r` of the running system
133 */
134
135 const Specifier table[] = {
136 { 'n', specifier_string, i->name },
137 { 'N', specifier_prefix_and_instance, NULL },
138 { 'p', specifier_prefix, NULL },
139 { 'i', specifier_instance, NULL },
140
141 { 'U', specifier_user_name, NULL },
142 { 'u', specifier_user_name, NULL },
143
144 { 'm', specifier_machine_id, NULL },
145 { 'H', specifier_host_name, NULL },
146 { 'b', specifier_boot_id, NULL },
147 { 'v', specifier_kernel_release, NULL },
148 {}
149 };
150
151 assert(i);
152 assert(format);
153 assert(ret);
154
155 return specifier_printf(format, table, i, ret);
156 }