github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/snapd-env-generator/main.c (about)

     1  /*
     2   * Copyright (C) 2018 Canonical Ltd
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License version 3 as
     6   * published by the Free Software Foundation.
     7   *
     8   * This program is distributed in the hope that it will be useful,
     9   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11   * GNU General Public License for more details.
    12   *
    13   * You should have received a copy of the GNU General Public License
    14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15   *
    16   */
    17  
    18  #include<stdlib.h>
    19  #include<string.h>
    20  #include<stdio.h>
    21  #include<linux/limits.h>
    22  
    23  #include "libsnap-confine-private/string-utils.h"
    24  
    25  #include "config.h"
    26  
    27  // Systemd environment generators work since version 233 which ships
    28  // in Ubuntu 17.10+
    29  int main(int argc, char **argv)
    30  {
    31  	const char *snap_bin_dir = SNAP_MOUNT_DIR "/bin";
    32  
    33  	char *path = getenv("PATH");
    34  	if (path == NULL || sc_streq(path, "")) {
    35  		// do nothing, until systemd is fixed, see LP#1791691
    36  		return 0;
    37  	}
    38  	char buf[PATH_MAX + 1] = { 0 };
    39  	strncpy(buf, path, sizeof(buf) - 1);
    40  	char *s = buf;
    41  
    42  	char *tok = strsep(&s, ":");
    43  	while (tok != NULL) {
    44  		if (sc_streq(tok, snap_bin_dir))
    45  			return 0;
    46  		tok = strsep(&s, ":");
    47  	}
    48  
    49  	printf("PATH=%s:%s\n", path, snap_bin_dir);
    50  	return 0;
    51  }