@@ -35,6 +35,7 @@ const char *extraopts = NULL;
3535const char * usagestring = "" \
3636 "Usage: rc-update [options] add <service> [<runlevel>...]\n" \
3737 " or: rc-update [options] del <service> [<runlevel>...]\n" \
38+ " or: rc-update [options] export <variables>...\n" \
3839 " or: rc-update [options] [show [<runlevel>...]]" ;
3940const char getoptstring [] = "asu" getoptstring_COMMON ;
4041const struct option longopts [] = {
@@ -50,6 +51,8 @@ const char * const longopts_help[] = {
5051 longopts_help_COMMON
5152};
5253
54+ extern const char * * environ ;
55+
5356/* Return the number of changes made:
5457 * -1 = no changes (error)
5558 * 0 = no changes (nothing to do)
@@ -190,11 +193,26 @@ show(RC_STRINGLIST *runlevels, bool verbose)
190193 rc_stringlist_free (services );
191194}
192195
196+ static void
197+ export (char * variable )
198+ {
199+ char * value = variable ;
200+ variable = strsep (& value , "=" );
201+
202+ if (!value && !(value = getenv (variable ))) {
203+ ewarn ("%s: environment variable %s not set, skipping." , applet , variable );
204+ return ;
205+ }
206+
207+ rc_export_variable (variable , value );
208+ }
209+
193210enum update_action {
194211 DO_NONE ,
195212 DO_ADD ,
196213 DO_DELETE ,
197214 DO_SHOW ,
215+ DO_EXPORT
198216};
199217
200218int main (int argc , char * * argv )
@@ -239,6 +257,8 @@ int main(int argc, char **argv)
239257 action = DO_DELETE ;
240258 } else if (strcmp (argv [optind ], "show" ) == 0 ) {
241259 action = DO_SHOW ;
260+ } else if (strcmp (argv [optind ], "export" ) == 0 ) {
261+ action = DO_EXPORT ;
242262 } else {
243263 eerror ("%s: invalid command '%s'" , applet , argv [optind ]);
244264 usage (EXIT_FAILURE );
@@ -249,20 +269,22 @@ int main(int argc, char **argv)
249269 action = DO_SHOW ;
250270
251271 if (optind >= argc && action != DO_SHOW ) {
252- eerror ("%s: no service specified" , applet );
272+ eerror ("%s: no %s specified" , applet , action == DO_EXPORT ? "variable" : "service" );
253273 usage (EXIT_FAILURE );
254274 }
255275
256- service = argv [optind ];
257- optind ++ ;
276+ if (action != DO_EXPORT ) {
277+ service = argv [optind ];
278+ optind ++ ;
258279
259- runlevels = rc_stringlist_new ();
260- while (optind < argc ) {
261- if (rc_runlevel_exists (argv [optind ])) {
262- rc_stringlist_add (runlevels , argv [optind ++ ]);
263- } else {
264- rc_stringlist_free (runlevels );
265- eerrorx ("%s: '%s' is not a valid runlevel" , applet , argv [optind ]);
280+ runlevels = rc_stringlist_new ();
281+ while (optind < argc ) {
282+ if (rc_runlevel_exists (argv [optind ])) {
283+ rc_stringlist_add (runlevels , argv [optind ++ ]);
284+ } else {
285+ rc_stringlist_free (runlevels );
286+ eerrorx ("%s: '%s' is not a valid runlevel" , applet , argv [optind ]);
287+ }
266288 }
267289 }
268290
@@ -283,6 +305,10 @@ int main(int argc, char **argv)
283305 rc_stringlist_sort (& runlevels );
284306 show (runlevels , verbose );
285307 goto exit ;
308+ case DO_EXPORT :
309+ while (optind < argc )
310+ export (argv [optind ++ ]);
311+ return 0 ;
286312 case DO_ADD :
287313 if (all_runlevels ) {
288314 rc_stringlist_free (runlevels );
0 commit comments