[Libguestfs] [PATCH libguestfs 2/5] guestfish: write --help to stdout, use gnulib's progname module
Jim Meyering
jim at meyering.net
Mon Aug 24 12:21:51 UTC 2009
Richard W.M. Jones wrote:
> On Mon, Aug 24, 2009 at 01:51:18PM +0200, Jim Meyering wrote:
>> @@ -205,6 +202,11 @@ main (int argc, char *argv[])
>> (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
>> guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
>>
>> + /* getopt_long uses argv[0], so give it the sanitized name, too.
>> + But only temporarily. */
>> + char *argv0 = argv[0];
>> + argv[0] = bad_cast (program_name);
>> +
>> for (;;) {
>> c = getopt_long (argc, argv, options, long_options, &option_index);
>> if (c == -1) break;
>
>> @@ -325,6 +327,9 @@ main (int argc, char *argv[])
>> }
>> }
>>
>> + /* Restore original value. */
>> + argv[0] = argv0;
>> +
>> /* Inspector mode invalidates most of the other arguments. */
>> if (inspector) {
>> char cmd[1024];
>
> I'm maybe missing something, but why restore it?
>
> How about instead something like:
>
> char *real_argv0 = argv[0];
> argv[0] = bad_cast (program_name);
>
> //...
>
> /* change the path-munging code to use real_argv0 */
>
> //...
>
> /* don't bother restoring argv[0] */
Sure. That works, too.
Here's the incremental:
diff --git a/fish/fish.c b/fish/fish.c
index baa1b6e..e799f86 100644
--- a/fish/fish.c
+++ b/fish/fish.c
@@ -203,7 +203,7 @@ main (int argc, char *argv[])
/* getopt_long uses argv[0], so give it the sanitized name, too.
But only temporarily. */
- char *argv0 = argv[0];
+ char *real_argv0 = argv[0];
argv[0] = bad_cast (program_name);
for (;;) {
@@ -328,9 +328,6 @@ main (int argc, char *argv[])
}
}
- /* Restore original value. */
- argv[0] = argv0;
-
/* Inspector mode invalidates most of the other arguments. */
if (inspector) {
char cmd[1024];
@@ -352,7 +349,7 @@ main (int argc, char *argv[])
strcpy (cmd, "a=`virt-inspector");
while (optind < argc) {
- if (strlen (cmd) + strlen (argv[optind]) + strlen (argv[0]) + 60
+ if (strlen (cmd) + strlen (argv[optind]) + strlen (real_argv0) + 60
>= sizeof cmd) {
fprintf (stderr,
_("%s: virt-inspector command too long for fixed-size buffer\n"),
@@ -370,7 +367,7 @@ main (int argc, char *argv[])
else
strcat (cmd, " --fish");
- sprintf (&cmd[strlen(cmd)], "` && %s $a", argv[0]);
+ sprintf (&cmd[strlen(cmd)], "` && %s $a", real_argv0);
if (guestfs_get_verbose (g))
strcat (cmd, " -v");
More information about the Libguestfs
mailing list