* cygcheck.cc (dump_sysinfo_services): Properly null-terminate 'buf'.

Avoid extraneous cygrunsrv invocation if 'verbose' is true.
This commit is contained in:
Brian Dessent 2005-08-17 00:52:43 +00:00
parent e448b01f6b
commit 9a99dcd39c
2 changed files with 49 additions and 32 deletions

View File

@ -1,3 +1,8 @@
2005-08-16 Brian Dessent <brian@dessent.net>
* cygcheck.cc (dump_sysinfo_services): Properly null-terminate 'buf'.
Avoid extraneous cygrunsrv invocation if 'verbose' is true.
2005-08-03 Corinna Vinschen <corinna@vinschen.de> 2005-08-03 Corinna Vinschen <corinna@vinschen.de>
* mount.cc (longopts): Fix typo which disallows --options option. * mount.cc (longopts): Fix typo which disallows --options option.

View File

@ -888,6 +888,7 @@ dump_sysinfo_services ()
char buf[1024]; char buf[1024];
char buf2[1024]; char buf2[1024];
FILE *f; FILE *f;
bool no_services = false;
if (givehelp) if (givehelp)
printf ("\nChecking for any Cygwin services... %s\n\n", printf ("\nChecking for any Cygwin services... %s\n\n",
@ -922,49 +923,60 @@ dump_sysinfo_services ()
} }
fclose (f); fclose (f);
/* run cygrunsrv --list */ /* For verbose mode, just run cygrunsrv --list --verbose and copy output
snprintf (buf, sizeof (buf), "%s --list", cygrunsrv); verbatim; otherwise run cygrunsrv --list and then cygrunsrv --query for
each service. */
snprintf (buf, sizeof (buf), (verbose ? "%s --list --verbose" : "%s --list"),
cygrunsrv);
if ((f = popen (buf, "rt")) == NULL) if ((f = popen (buf, "rt")) == NULL)
{ {
printf ("Failed to execute '%s', skipping services check.\n", buf); printf ("Failed to execute '%s', skipping services check.\n", buf);
return; return;
} }
size_t nchars = fread ((void *) buf, 1, sizeof (buf), f);
pclose (f);
/* were any services found? */ if (verbose)
if (nchars < 1)
{ {
puts ("No Cygwin services found.\n");
return;
}
/* In verbose mode, just run 'cygrunsrv --list --verbose' and copy the
entire output. Otherwise run 'cygrunsrv --query' for each service. */
for (char *srv = strtok (buf, "\n"); srv; srv = strtok (NULL, "\n"))
{
if (verbose)
snprintf (buf2, sizeof (buf2), "%s --list --verbose", cygrunsrv);
else
snprintf (buf2, sizeof (buf2), "%s --query %s", cygrunsrv, srv);
if ((f = popen (buf2, "rt")) == NULL)
{
printf ("Failed to execute '%s', skipping services check.\n", buf2);
return;
}
/* copy output to stdout */ /* copy output to stdout */
do size_t nchars = 0;
{ while (!feof (f) && !ferror (f))
nchars = fread ((void *)buf2, 1, sizeof (buf2), f); nchars += fwrite ((void *) buf, 1,
fwrite ((void *)buf2, 1, nchars, stdout); fread ((void *) buf, 1, sizeof (buf), f), stdout);
}
while (!feof (f) && !ferror (f)); /* cygrunsrv outputs nothing if there are no cygwin services found */
if (nchars < 1)
no_services = true;
pclose (f);
}
else
{
/* read the output of --list, and then run --query for each service */
size_t nchars = fread ((void *) buf, 1, sizeof (buf) - 1, f);
buf[nchars] = 0;
pclose (f); pclose (f);
if (verbose) if (nchars > 0)
break; for (char *srv = strtok (buf, "\n"); srv; srv = strtok (NULL, "\n"))
{
snprintf (buf2, sizeof (buf2), "%s --query %s", cygrunsrv, srv);
if ((f = popen (buf2, "rt")) == NULL)
{
printf ("Failed to execute '%s', skipping services check.\n", buf2);
return;
}
/* copy output to stdout */
while (!feof (f) && !ferror (f))
fwrite ((void *) buf2, 1,
fread ((void *) buf2, 1, sizeof (buf2), f), stdout);
pclose (f);
}
else
no_services = true;
} }
/* inform the user if nothing found */
if (no_services)
puts ("No Cygwin services found.\n");
} }
static void static void