Re: [PATCH 3/5] git-cherry-pick: Add ignore-if-made-empty option [v2]
- From
Johannes Sixt <j6t@kdbg.org>
- Date
- Apr 6, 2012, 18:30 UTC
- Message-ID
- <4F7F363B.3090007@kdbg.org>
- In-Reply-To
- <1333654745-7898-4-git-send-email-nhorman@tuxdriver.com>
Am 05.04.2012 21:39, schrieb Neil Horman:
Show 12 quoted lines
> + if (pipe2(pipefd, 0) < 0) > + return 0; > + output = xfdopen(pipefd[0], "r"); > + memset(&cp, 0, sizeof(struct child_process)); > + ... setup cp ... > + if (start_command(&cp) < 0) > + goto out; > + if (fscanf(output, "%s\n", ptree)< 1) > + goto out; > + finish_command(&cp); > + fclose(output); > + close(pipefd[0]);
Instead of this sequence (I quoted only the relevant pieces), use the following:
memset(&cp, 0, sizeof(struct child_process)); cp.out = -1; ... set other pieces in cp ... if (start_command(&cp) < 0) goto out; read_in_full(cp.out, ptree, sizeof(ptree)); /* add suitable error reporting above */ close(cp.out); if (!finish_command(&cp)) goto out;
i.e.,
1. let start_command create the pipe for you by setting cp.out = -1, 2. avoid fscanf() if read_in_full() is equally simple to use, 3. close the pipe before finish_command(), 4. check the return code of finish_command().
-- Hannes