From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1PuH4X-0002o7-NH for garchives@archives.gentoo.org; Tue, 01 Mar 2011 04:20:29 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 2FCCD1C00C; Tue, 1 Mar 2011 04:20:22 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id DD28A1C00C for ; Tue, 1 Mar 2011 04:20:21 +0000 (UTC) Received: from flycatcher.gentoo.org (flycatcher.gentoo.org [81.93.255.6]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id E53C91B41D7 for ; Tue, 1 Mar 2011 04:20:20 +0000 (UTC) Received: by flycatcher.gentoo.org (Postfix, from userid 559) id 8986C20054; Tue, 1 Mar 2011 04:20:19 +0000 (UTC) From: "Mike Frysinger (vapier)" To: gentoo-commits@lists.gentoo.org Reply-To: gentoo-dev@lists.gentoo.org, vapier@gentoo.org Subject: [gentoo-commits] gentoo-projects commit in portage-utils: q.c X-VCS-Repository: gentoo-projects X-VCS-Files: q.c X-VCS-Directories: portage-utils X-VCS-Committer: vapier X-VCS-Committer-Name: Mike Frysinger Content-Type: text/plain; charset=utf8 Message-Id: <20110301042019.8986C20054@flycatcher.gentoo.org> Date: Tue, 1 Mar 2011 04:20:19 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 1fe123961f8832165bfdd50fe0027d65 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=3D1.51&view=3Dmarkup plain: http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils= /q.c?rev=3D1.51&content-type=3Dtext/plain diff : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils= /q.c?r1=3D1.50&r2=3D1.51 Index: q.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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/0= 2/28 18:15:32 vapier Exp $ + * $Header: /var/cvsroot/gentoo-projects/portage-utils/q.c,v 1.51 2011/0= 3/01 04:20:19 vapier Exp $ * * Copyright 2005-2010 Ned Ludd - * Copyright 2005-2010 Mike Frysinger - @@ -22,7 +22,7 @@ "Module path", COMMON_OPTS_HELP }; -static const char q_rcsid[] =3D "$Id: q.c,v 1.50 2011/02/28 18:15:32 vap= ier Exp $"; +static const char q_rcsid[] =3D "$Id: q.c,v 1.51 2011/03/01 04:20:19 vap= ier Exp $"; #define q_usage(ret) usage(ret, Q_FLAGS, q_long_opts, q_opts_help, looku= p_applet_idx("q")) =20 static APPLET lookup_applet(const char *applet) @@ -66,7 +66,7 @@ =20 int q_main(int argc, char **argv) { - int i; + int i, install; const char *p; APPLET func; =20 @@ -83,38 +83,58 @@ if (argc =3D=3D 1) q_usage(EXIT_FAILURE); =20 + install =3D 0; + while ((i =3D GETOPT_LONG(Q, q, "+")) !=3D -1) { switch (i) { COMMON_GETOPTS_CASES(q) case 'M': modpath =3D optarg; break; case 'm': reinitialize_metacache =3D 1; break; case 'r': reinitialize =3D 1; break; - case 'i': { - char buf[_Q_PATH_MAX]; - /* always bzero a buffer before using readlink() */ - memset(buf, 0x00, sizeof(buf)); + case 'i': install =3D 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/curp= roc/file with linuxfs/procfs respectively */ - if (readlink("/proc/self/exe", buf, sizeof(buf) - 1) =3D=3D -1) { - char *ptr =3D which("q"); - if (ptr =3D=3D NULL) { - warnfp("haha no symlink love for you"); - return 1; - } - strncpy(buf, ptr, sizeof(buf)); - } - if (chdir(dirname(buf)) !=3D 0) { - warnfp("could not chdir to '%s'", buf); + + rret =3D readlink("/proc/self/exe", buf, sizeof(buf) - 1); + if (rret =3D=3D -1) { + char *ptr =3D which("q"); + if (ptr =3D=3D NULL) { + warnfp("haha no symlink love for you"); return 1; } - for (i =3D 1; applets[i].desc !=3D 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] =3D '\0'; + } else + buf[rret] =3D '\0'; + + fd =3D open(dirname(buf), O_RDONLY|O_CLOEXEC); + if (fd < 0) { + warnfp("chdir(%s) failed", buf); + return 1; } + + ret =3D 0; + for (i =3D 1; applets[i].desc; ++i) { + int r =3D symlinkat(buf, fd, applets[i].name); + if (!quiet) + printf(" %s ...\t[%s]\n", applets[i].name, r ? strerror(errno) : "OK= "); + if (r && errno !=3D EEXIST) + ret =3D 1; } + + close(fd); + + return ret; } + if (reinitialize || reinitialize_metacache) return 0; if (argc =3D=3D optind)