/* Copyright (C) 2011 Red Hat, Inc. Written by Jindrich Novy . This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include static void check_asprintf( char **strp, const char *fmt, ... ) { va_list args; va_start(args, fmt); if (vasprintf(strp, fmt, args) == -1 || !*strp) { fprintf(stderr, "Allocation failed.\n"); exit(EXIT_FAILURE); } va_end(args); } static void write_script( int tfd, char *s ) { if (write(tfd, s, strlen(s)) == -1) { fprintf(stderr, "Error writing to temporary file\n"); exit(EXIT_FAILURE); } } static void list_collections() { struct stat sb; struct dirent **nl; int n, i; const char prefix[] = "/etc/scl/prefixes/"; if (stat(prefix, &sb) == -1) { fprintf(stderr, "%s does not exist\n", prefix); exit(EXIT_FAILURE); } if (!S_ISDIR(sb.st_mode)) { fprintf(stderr, "%s is not a directory\n", prefix); exit(EXIT_FAILURE); } if ((n = scandir(prefix, &nl, 0, alphasort)) < 0) { perror("scandir"); exit(EXIT_FAILURE); } for (i=0; id_name != '.') { printf("%s\n", nl[i]->d_name); } } free(nl); } int main(int argc, char **argv) { struct stat st; char *path, *enablepath; char tmp[] = "/var/tmp/sclXXXXXX"; char *cmd = NULL, *bash_cmd, *echo, *enabled; int i, tfd, ffd, stdin_read = 0; if (argc == 2 && (!strcmp(argv[1],"--list") || !strcmp(argv[1],"-l"))) { list_collections(); return 0; } if (!strcmp(argv[argc-1], "-")) { /* reading command from stdin */ size_t r; if (argc < 4) { fprintf(stderr, "Need at least 3 arguments.\nRun %s without arguments to get help.\n", argv[0]); exit(EXIT_FAILURE); } cmd = malloc(BUFSIZ); if (!cmd) { fprintf(stderr, "Can't allocate memory.\n"); exit(EXIT_FAILURE); } for (r=0; (r += fread(cmd+r, 1, BUFSIZ, stdin));) { if (feof(stdin)) break; cmd = realloc(cmd, r+BUFSIZ); if (!cmd) { fprintf(stderr, "Can't reallocate memory.\n"); exit(EXIT_FAILURE); } } if (!r) { fprintf(stderr, "Error reading command from stdin.\n"); exit(EXIT_FAILURE); } stdin_read = 1; } if (!stdin_read) { if (argc < 4) { fprintf(stderr, "Usage: %s [, ...] \n" "If is '-' then the command will be read from standard input.\n", argv[0]); exit(EXIT_FAILURE); } cmd = strdup(argv[argc-1]); } tfd = mkstemp(tmp); check_asprintf(&enabled, "scl_enabled %s\nif [ $? != 0 ]; then\n" " eval \"SCLS=( ${x_scls[*]} )\"\n" " SCLS+=(%s)\n" " export X_SCLS=$(printf '%%q ' \"${SCLS[@]}\")\nfi\n", argv[2], argv[2]); write_script(tfd, enabled); free(enabled); for (i=2; i