* [gentoo-commits] gentoo-projects commit in portage-utils: q.c
@ 2011-02-28 18:15 Mike Frysinger (vapier)
0 siblings, 0 replies; 3+ messages in thread
From: Mike Frysinger (vapier) @ 2011-02-28 18:15 UTC (permalink / raw
To: gentoo-commits
vapier 11/02/28 18:15:33
Modified: q.c
Log:
add a helper for easily running applets from other applets
Revision Changes Path
1.50 portage-utils/q.c
file : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?rev=1.50&view=markup
plain: http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?rev=1.50&content-type=text/plain
diff : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?r1=1.49&r2=1.50
Index: q.c
===================================================================
RCS file: /var/cvsroot/gentoo-projects/portage-utils/q.c,v
retrieving revision 1.49
retrieving revision 1.50
diff -u -r1.49 -r1.50
--- q.c 21 Feb 2011 01:33:47 -0000 1.49
+++ q.c 28 Feb 2011 18:15:32 -0000 1.50
@@ -1,7 +1,7 @@
/*
* Copyright 2005-2010 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/portage-utils/q.c,v 1.49 2011/02/21 01:33:47 vapier Exp $
+ * $Header: /var/cvsroot/gentoo-projects/portage-utils/q.c,v 1.50 2011/02/28 18:15:32 vapier Exp $
*
* Copyright 2005-2010 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2010 Mike Frysinger - <vapier@gentoo.org>
@@ -22,7 +22,7 @@
"Module path",
COMMON_OPTS_HELP
};
-static const char q_rcsid[] = "$Id: q.c,v 1.49 2011/02/21 01:33:47 vapier Exp $";
+static const char q_rcsid[] = "$Id: q.c,v 1.50 2011/02/28 18:15:32 vapier Exp $";
#define q_usage(ret) usage(ret, Q_FLAGS, q_long_opts, q_opts_help, lookup_applet_idx("q"))
static APPLET lookup_applet(const char *applet)
@@ -134,3 +134,43 @@
return (func)(argc - 1, ++argv);
}
+
+static int run_applet_l(const char *arg, ...)
+{
+ int (*applet)(int, char **);
+ va_list ap;
+ int ret, optind_saved, argc;
+ char **argv;
+ const char *argv0_saved;
+
+ optind_saved = optind;
+ argv0_saved = argv0;
+
+ applet = lookup_applet(arg);
+ if (!applet)
+ return -1;
+
+ /* This doesn't NULL terminate argv, but you should be using argc */
+ va_start(ap, arg);
+ argc = 0;
+ argv = NULL;
+ while (arg) {
+ argv = xrealloc(argv, sizeof(*argv) * ++argc);
+ argv[argc - 1] = xstrdup(arg);
+ arg = va_arg(ap, const char *);
+ }
+ va_end(ap);
+
+ optind = 0;
+ argv0 = argv[0];
+ ret = applet(argc, argv);
+
+ while (argc--)
+ free(argv[argc]);
+ free(argv);
+
+ optind = optind_saved;
+ argv0 = argv0_saved;
+
+ return ret;
+}
^ permalink raw reply [flat|nested] 3+ messages in thread
* [gentoo-commits] gentoo-projects commit in portage-utils: q.c
@ 2011-03-01 4:20 Mike Frysinger (vapier)
0 siblings, 0 replies; 3+ messages in thread
From: Mike Frysinger (vapier) @ 2011-03-01 4:20 UTC (permalink / raw
To: gentoo-commits
vapier 11/03/01 04:20:19
Modified: q.c
Log:
rewrite `q -i` to delay its execution after parsing all opts, respect --quiet, and avoid using chdir() to allow execution from other applets
Revision Changes Path
1.51 portage-utils/q.c
file : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?rev=1.51&view=markup
plain: http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?rev=1.51&content-type=text/plain
diff : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?r1=1.50&r2=1.51
Index: q.c
===================================================================
RCS file: /var/cvsroot/gentoo-projects/portage-utils/q.c,v
retrieving revision 1.50
retrieving revision 1.51
diff -u -r1.50 -r1.51
--- q.c 28 Feb 2011 18:15:32 -0000 1.50
+++ q.c 1 Mar 2011 04:20:19 -0000 1.51
@@ -1,7 +1,7 @@
/*
* Copyright 2005-2010 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/portage-utils/q.c,v 1.50 2011/02/28 18:15:32 vapier Exp $
+ * $Header: /var/cvsroot/gentoo-projects/portage-utils/q.c,v 1.51 2011/03/01 04:20:19 vapier Exp $
*
* Copyright 2005-2010 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2010 Mike Frysinger - <vapier@gentoo.org>
@@ -22,7 +22,7 @@
"Module path",
COMMON_OPTS_HELP
};
-static const char q_rcsid[] = "$Id: q.c,v 1.50 2011/02/28 18:15:32 vapier Exp $";
+static const char q_rcsid[] = "$Id: q.c,v 1.51 2011/03/01 04:20:19 vapier Exp $";
#define q_usage(ret) usage(ret, Q_FLAGS, q_long_opts, q_opts_help, lookup_applet_idx("q"))
static APPLET lookup_applet(const char *applet)
@@ -66,7 +66,7 @@
int q_main(int argc, char **argv)
{
- int i;
+ int i, install;
const char *p;
APPLET func;
@@ -83,38 +83,58 @@
if (argc == 1)
q_usage(EXIT_FAILURE);
+ install = 0;
+
while ((i = GETOPT_LONG(Q, q, "+")) != -1) {
switch (i) {
COMMON_GETOPTS_CASES(q)
case 'M': modpath = optarg; break;
case 'm': reinitialize_metacache = 1; break;
case 'r': reinitialize = 1; break;
- case 'i': {
- char buf[_Q_PATH_MAX];
- /* always bzero a buffer before using readlink() */
- memset(buf, 0x00, sizeof(buf));
+ case 'i': install = 1; break;
+ }
+ }
+
+ if (install) {
+ char buf[_Q_PATH_MAX];
+ ssize_t rret;
+ int fd, ret;
+
+ if (!quiet)
printf("Installing symlinks:\n");
- /* solaris: /proc/self/object/a.out bsd: /proc/self/exe or /proc/curproc/file with linuxfs/procfs respectively */
- if (readlink("/proc/self/exe", buf, sizeof(buf) - 1) == -1) {
- char *ptr = which("q");
- if (ptr == NULL) {
- warnfp("haha no symlink love for you");
- return 1;
- }
- strncpy(buf, ptr, sizeof(buf));
- }
- if (chdir(dirname(buf)) != 0) {
- warnfp("could not chdir to '%s'", buf);
+
+ rret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
+ if (rret == -1) {
+ char *ptr = which("q");
+ if (ptr == NULL) {
+ warnfp("haha no symlink love for you");
return 1;
}
- for (i = 1; applets[i].desc != NULL; ++i) {
- printf(" %s ...\t[%s]\n", applets[i].name,
- symlink("q", applets[i].name) ? strerror(errno) : "OK");
- }
- return 0;
+ strncpy(buf, ptr, sizeof(buf));
+ buf[sizeof(buf) - 1] = '\0';
+ } else
+ buf[rret] = '\0';
+
+ fd = open(dirname(buf), O_RDONLY|O_CLOEXEC);
+ if (fd < 0) {
+ warnfp("chdir(%s) failed", buf);
+ return 1;
}
+
+ ret = 0;
+ for (i = 1; applets[i].desc; ++i) {
+ int r = symlinkat(buf, fd, applets[i].name);
+ if (!quiet)
+ printf(" %s ...\t[%s]\n", applets[i].name, r ? strerror(errno) : "OK");
+ if (r && errno != EEXIST)
+ ret = 1;
}
+
+ close(fd);
+
+ return ret;
}
+
if (reinitialize || reinitialize_metacache)
return 0;
if (argc == optind)
^ permalink raw reply [flat|nested] 3+ messages in thread
* [gentoo-commits] gentoo-projects commit in portage-utils: q.c
@ 2011-03-17 3:32 Mike Frysinger (vapier)
0 siblings, 0 replies; 3+ messages in thread
From: Mike Frysinger (vapier) @ 2011-03-17 3:32 UTC (permalink / raw
To: gentoo-commits
vapier 11/03/17 03:32:51
Modified: q.c
Log:
fix `q --install` behavior ... new code was incorrectly linking to the dir due to dirname() modifying the buffer
Revision Changes Path
1.52 portage-utils/q.c
file : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?rev=1.52&view=markup
plain: http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?rev=1.52&content-type=text/plain
diff : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/q.c?r1=1.51&r2=1.52
Index: q.c
===================================================================
RCS file: /var/cvsroot/gentoo-projects/portage-utils/q.c,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -r1.51 -r1.52
--- q.c 1 Mar 2011 04:20:19 -0000 1.51
+++ q.c 17 Mar 2011 03:32:51 -0000 1.52
@@ -1,7 +1,7 @@
/*
* Copyright 2005-2010 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/portage-utils/q.c,v 1.51 2011/03/01 04:20:19 vapier Exp $
+ * $Header: /var/cvsroot/gentoo-projects/portage-utils/q.c,v 1.52 2011/03/17 03:32:51 vapier Exp $
*
* Copyright 2005-2010 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2010 Mike Frysinger - <vapier@gentoo.org>
@@ -22,7 +22,7 @@
"Module path",
COMMON_OPTS_HELP
};
-static const char q_rcsid[] = "$Id: q.c,v 1.51 2011/03/01 04:20:19 vapier Exp $";
+static const char q_rcsid[] = "$Id: q.c,v 1.52 2011/03/17 03:32:51 vapier Exp $";
#define q_usage(ret) usage(ret, Q_FLAGS, q_long_opts, q_opts_help, lookup_applet_idx("q"))
static APPLET lookup_applet(const char *applet)
@@ -97,6 +97,7 @@
if (install) {
char buf[_Q_PATH_MAX];
+ const char *prog;
ssize_t rret;
int fd, ret;
@@ -115,6 +116,7 @@
} else
buf[rret] = '\0';
+ prog = basename(buf);
fd = open(dirname(buf), O_RDONLY|O_CLOEXEC);
if (fd < 0) {
warnfp("chdir(%s) failed", buf);
@@ -123,7 +125,7 @@
ret = 0;
for (i = 1; applets[i].desc; ++i) {
- int r = symlinkat(buf, fd, applets[i].name);
+ int r = symlinkat(prog, fd, applets[i].name);
if (!quiet)
printf(" %s ...\t[%s]\n", applets[i].name, r ? strerror(errno) : "OK");
if (r && errno != EEXIST)
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-03-17 3:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-28 18:15 [gentoo-commits] gentoo-projects commit in portage-utils: q.c Mike Frysinger (vapier)
-- strict thread matches above, loose matches on Subject: below --
2011-03-01 4:20 Mike Frysinger (vapier)
2011-03-17 3:32 Mike Frysinger (vapier)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox