From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21340 invoked by alias); 22 Sep 2012 16:07:08 -0000 Received: (qmail 21324 invoked by uid 22791); 22 Sep 2012 16:07:06 -0000 X-SWARE-Spam-Status: No, hits=-3.6 required=5.0 tests=AWL,BAYES_00,KHOP_THREADED,RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from server-nat-6.cs.umd.edu (HELO bacon.cs.umd.edu) (128.8.127.149) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 22 Sep 2012 16:06:53 +0000 Received: from [192.168.2.10] (pool-72-83-72-24.washdc.east.verizon.net [72.83.72.24]) (Authenticated sender: khooyp) by bacon.cs.umd.edu (Postfix) with ESMTPSA id 2195CB407AC; Sat, 22 Sep 2012 12:06:50 -0400 (EDT) Subject: Re: [PATCH 1/2]: Refactor relocate_path to also check if the relocated file/directory exists Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: multipart/mixed; boundary=Apple-Mail-35-655616514 From: Khoo Yit Phang In-Reply-To: Date: Sat, 22 Sep 2012 16:07:00 -0000 Cc: Khoo Yit Phang , Jan Kratochvil , Eli Zaretskii , gdb-patches@sourceware.org Message-Id: References: <21ACC598-F6B4-4117-BA7B-B316414DE9E3@cs.umd.edu> <20120919130040.GA20442@host2.jankratochvil.net> <20120921182637.GA8747@host2.jankratochvil.net> <83y5k3w7es.fsf@gnu.org> <20120921184623.GA12696@host2.jankratochvil.net> To: Andreas Schwab X-CSD-MailScanner-ID: 2195CB407AC.ACCEF X-CSD-MailScanner: Found to be clean X-CSD-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-50, required 5, autolearn=not spam, ALL_TRUSTED -50.00) X-CSD-MailScanner-From: khooyp@cs.umd.edu X-CSD-MailScanner-Watermark: 1348934810.67851@zVt4/kjQA+DlMuWRalyKVQ Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-09/txt/msg00472.txt.bz2 --Apple-Mail-35-655616514 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Content-length: 436 Hi, On Sep 21, 2012, at 3:09 PM, Andreas Schwab wrote: > Jan Kratochvil writes: >=20 >> It would have to be written as: >> if (stat (path, &s) !=3D 0 || !isdir !=3D !S_ISDIR (s.st_mode)) >=20 > A common idiom is to use !! when the other side is already boolean. I like this idiom, it does seem to be used in GDB, and isdir should be a bo= olean. Here's a patch that uses it. Yit September 22, 2012 --Apple-Mail-35-655616514 Content-Disposition: attachment; filename=refactor-relocate-path.txt Content-Type: text/plain; name="refactor-relocate-path.txt" Content-Transfer-Encoding: 7bit Content-length: 2819 gdb/ChangeLog 2012-09-21 Khoo Yit Phang Refactor relocate_path to also check if the relocated file/directory exists. * main.c (relocate_path): Add an isdir argument, and check that the relocated file/directory exists. (relocate_gdb_directory): Remove the directory check that is subsumed by relocate_path. (get_init_files): Remove the file check that is subsumed by relocate_path. diff --git a/gdb/main.c b/gdb/main.c --- a/gdb/main.c +++ b/gdb/main.c @@ -91,17 +91,34 @@ static void print_gdb_help (struct ui_file *); -/* Relocate a file or directory. PROGNAME is the name by which gdb - was invoked (i.e., argv[0]). INITIAL is the default value for the - file or directory. FLAG is true if the value is relocatable, false - otherwise. Returns a newly allocated string; this may return NULL - under the same conditions as make_relative_prefix. */ +/* Relocate a file or directory, checking if it exists. PROGNAME is the + name by which gdb was invoked (i.e., argv[0]). INITIAL is the default + value for the file or directory. ISDIR is true if INITIAL is a + directory. FLAG is true if the value is relocatable, false otherwise. + Returns a newly allocated string; this may return NULL under the same + conditions as make_relative_prefix, or if the relocated path does not + exist. */ static char * -relocate_path (const char *progname, const char *initial, int flag) +relocate_path (const char *progname, const char *initial, int isdir, + int flag) { if (flag) - return make_relative_prefix (progname, BINDIR, initial); + { + char *path; + path = make_relative_prefix (progname, BINDIR, initial); + if (path) + { + struct stat s; + + if (stat (path, &s) != 0 || (isdir != !!S_ISDIR (s.st_mode))) + { + xfree (path); + path = NULL; + } + } + return path; + } return xstrdup (initial); } @@ -116,17 +133,7 @@ { char *dir; - dir = relocate_path (gdb_program_name, initial, flag); - if (dir) - { - struct stat s; - - if (stat (dir, &s) != 0 || !S_ISDIR (s.st_mode)) - { - xfree (dir); - dir = NULL; - } - } + dir = relocate_path (gdb_program_name, initial, 1, flag); if (!dir) dir = xstrdup (initial); @@ -162,15 +169,15 @@ if (!initialized) { - struct stat homebuf, cwdbuf, s; + struct stat homebuf, cwdbuf; char *homedir, *relocated_sysgdbinit; if (SYSTEM_GDBINIT[0]) { relocated_sysgdbinit = relocate_path (gdb_program_name, - SYSTEM_GDBINIT, + SYSTEM_GDBINIT, 0, SYSTEM_GDBINIT_RELOCATABLE); - if (relocated_sysgdbinit && stat (relocated_sysgdbinit, &s) == 0) + if (relocated_sysgdbinit) sysgdbinit = relocated_sysgdbinit; else xfree (relocated_sysgdbinit); --Apple-Mail-35-655616514--