Fix minor bugs (RhBug:788194)

- clear temp files
- handle commands from stdin properly
- run command even if ran as "scl enable SCL command" from already
  enabled SCL
This commit is contained in:
Jindrich Novy 2012-02-09 08:46:14 +01:00
parent afb7b54720
commit f56475e20c
2 changed files with 27 additions and 8 deletions

View File

@ -1,6 +1,6 @@
Summary: Utilities for alternative packaging Summary: Utilities for alternative packaging
Name: scl-utils Name: scl-utils
Version: 20120125 Version: 20120209
Release: 1%{?dist} Release: 1%{?dist}
License: GPLv2+ License: GPLv2+
Group: Applications/File Group: Applications/File
@ -47,6 +47,13 @@ rm -rf %buildroot
%{_sysconfdir}/rpm/macros.scl %{_sysconfdir}/rpm/macros.scl
%changelog %changelog
* Thu Feb 09 2012 Jindrich Novy <jnovy@redhat.com> 20120209-1
- fix minor bugs (#788194)
- clear temp files
- handle commands from stdin properly
- run command even if ran as "scl enable SCL command" from already
enabled SCL
* Wed Jan 25 2012 Jindrich Novy <jnovy@redhat.com> 20120125-1 * Wed Jan 25 2012 Jindrich Novy <jnovy@redhat.com> 20120125-1
- remove dsc macros - remove dsc macros
- trigger scl-utils-build BR inclusion while using scl macros - trigger scl-utils-build BR inclusion while using scl macros

26
scl.c
View File

@ -58,7 +58,13 @@ int main(int argc, char **argv) {
int i, tfd, ffd, stdin_read = 0; int i, tfd, ffd, stdin_read = 0;
if (!strcmp(argv[argc-1], "-")) { /* reading command from stdin */ if (!strcmp(argv[argc-1], "-")) { /* reading command from stdin */
size_t r = 0; 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); cmd = malloc(BUFSIZ);
if (!cmd) { if (!cmd) {
@ -66,9 +72,9 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (;(r += fread(cmd+r, 1, BUFSIZ, stdin));) { for (r=0; (r += fread(cmd+r, 1, BUFSIZ, stdin));) {
if (feof(stdin)) break; if (feof(stdin)) break;
cmd = realloc(cmd, r); cmd = realloc(cmd, r+BUFSIZ);
if (!cmd) { if (!cmd) {
fprintf(stderr, "Can't reallocate memory.\n"); fprintf(stderr, "Can't reallocate memory.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -92,10 +98,10 @@ int main(int argc, char **argv) {
tfd = mkstemp(tmp); tfd = mkstemp(tmp);
check_asprintf(&enabled, "scl_enabled %s\n[ $? == 0 ] && exit 1\n" check_asprintf(&enabled, "scl_enabled %s\nif [ $? != 0 ]; then\n"
"eval \"SCLS=( ${x_scls[*]} )\"\n" " eval \"SCLS=( ${x_scls[*]} )\"\n"
"SCLS+=(%s)\n" " SCLS+=(%s)\n"
"export X_SCLS=$(printf '%%q ' \"${SCLS[@]}\")\n", argv[2], argv[2]); " export X_SCLS=$(printf '%%q ' \"${SCLS[@]}\")\nfi\n", argv[2], argv[2]);
write_script(tfd, enabled); write_script(tfd, enabled);
free(enabled); free(enabled);
@ -107,11 +113,13 @@ int main(int argc, char **argv) {
check_asprintf(&path, "/etc/scl/prefixes/%s", argv[i]); check_asprintf(&path, "/etc/scl/prefixes/%s", argv[i]);
if (!(f=fopen(path,"r"))) { if (!(f=fopen(path,"r"))) {
fprintf(stderr, "Unable to open %s!\n", path); fprintf(stderr, "Unable to open %s!\n", path);
unlink(tmp);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
r = fread(scl_dir, 1, BUFSIZ, f); r = fread(scl_dir, 1, BUFSIZ, f);
if (!r) { if (!r) {
fprintf(stderr, "Unable to read or file empty %s!\n", path); fprintf(stderr, "Unable to read or file empty %s!\n", path);
unlink(tmp);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
scl_dir[r-1] = '\0'; scl_dir[r-1] = '\0';
@ -124,10 +132,12 @@ int main(int argc, char **argv) {
check_asprintf(&path, "%s", scl_dir); check_asprintf(&path, "%s", scl_dir);
if (lstat(path, &st)) { if (lstat(path, &st)) {
fprintf(stderr, "%s doesn't exist\n", path); fprintf(stderr, "%s doesn't exist\n", path);
unlink(tmp);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!S_ISDIR(st.st_mode)) { if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s is not a directory\n", path); fprintf(stderr, "%s is not a directory\n", path);
unlink(tmp);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
check_asprintf(&enablepath, "%s/%s", path, argv[1]); check_asprintf(&enablepath, "%s/%s", path, argv[1]);
@ -139,6 +149,7 @@ int main(int argc, char **argv) {
write_script(tfd, echo); write_script(tfd, echo);
} else { } else {
fprintf(stderr, "WARNING: %s scriptlet does not exist!\n", enablepath); fprintf(stderr, "WARNING: %s scriptlet does not exist!\n", enablepath);
unlink(tmp);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -155,6 +166,7 @@ int main(int argc, char **argv) {
check_asprintf(&bash_cmd, "/bin/bash %s", tmp); check_asprintf(&bash_cmd, "/bin/bash %s", tmp);
i = system(bash_cmd); i = system(bash_cmd);
free(bash_cmd); free(bash_cmd);
unlink(tmp);
return WEXITSTATUS(i); return WEXITSTATUS(i);
} }