Now defaults to the current volume as the R5 version of that command does.

Unlike that one, the usage comment is now correct, too.
Some style changes, renamed variables.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5258 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2003-11-05 11:34:44 +00:00
parent 89cd09b305
commit be98595d42
+45 -35
View File
@@ -8,6 +8,7 @@
// Modified by Jerome Duval on November 03, 2003 // Modified by Jerome Duval on November 03, 2003
// //
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@@ -19,7 +20,9 @@
#include <support/SupportDefs.h> #include <support/SupportDefs.h>
#include <support/String.h> #include <support/String.h>
extern "C" { extern "C" {
// ToDo: include the correct header
int32 getopt(int32, const char **, const char *); int32 getopt(int32, const char **, const char *);
extern char *optarg; extern char *optarg;
extern int32 optind; extern int32 optind;
@@ -29,24 +32,28 @@ extern "C" {
bool o_all_volumes = false; // Query all volumes? bool o_all_volumes = false; // Query all volumes?
bool o_escaping = true; // Escape metacharacters? bool o_escaping = true; // Escape metacharacters?
void usage(void)
void
usage(void)
{ {
printf("usage: query [ -e ] [ -a || -v volume ] expression\n"); printf("usage: query [ -e ] [ -a || -v volume ] expression\n");
printf(" -e don't escape meta-characters\n"); printf(" -e don't escape meta-characters\n");
printf(" -a perform the query on all volumes\n"); printf(" -a perform the query on all volumes\n");
printf(" -v <file> perform the query on just one volume;\n"); printf(" -v <file> perform the query on just one volume;\n");
printf(" <file> can be any file on that volume.\n"); printf(" <file> can be any file on that volume.\n");
printf(" defaults to the boot volume.\n"); printf(" defaults to the current volume.\n");
printf(" hint: query 'name=foo' will find a file named \"foo\"\n"); printf(" hint: query 'name=foo' will find a file named \"foo\"\n");
exit(0); exit(0);
} }
void perform_query(BVolume *v, const char *predicate)
void
perform_query(BVolume &volume, const char *predicate)
{ {
BQuery query; BQuery query;
// Set up the volume and predicate for the query. // Set up the volume and predicate for the query.
query.SetVolume(v); query.SetVolume(&volume);
query.SetPredicate(predicate); query.SetPredicate(predicate);
if (query.Fetch() != B_OK) { if (query.Fetch() != B_OK) {
@@ -54,27 +61,32 @@ void perform_query(BVolume *v, const char *predicate)
return; return;
} }
BEntry e; BEntry entry;
BPath p; BPath path;
while (query.GetNextEntry(&e) == B_OK) { while (query.GetNextEntry(&entry) == B_OK) {
e.GetPath(&p); if (entry.GetPath(&path) < B_OK) {
printf("%s\n", o_escaping ? fprintf(stderr, "could not get path for entry\n");
BString().CharacterEscape(p.Path(), " ()?*&\"'[]^\\~|;!<>*$", '\\').String() : continue;
p.Path());
} }
return; printf("%s\n", o_escaping ?
BString().CharacterEscape(path.Path(), " ()?*&\"'[]^\\~|;!<>*$", '\\').String()
: path.Path());
}
} }
int main(int32 argc, const char **argv)
int
main(int32 argc, const char **argv)
{ {
// Make sure we have the minimum number of arguments. // Make sure we have the minimum number of arguments.
if (argc < 2) usage(); if (argc < 2)
usage();
// Which volume do we make the query on? // Which volume do we make the query on?
char volume_path[B_FILE_NAME_LENGTH]; // Default to the current volume.
// Default to the boot volume. char volumePath[B_FILE_NAME_LENGTH];
strcpy(volume_path, "/boot"); strcpy(volumePath, ".");
// Parse command-line arguments. // Parse command-line arguments.
int32 opt; int32 opt;
@@ -89,7 +101,7 @@ int main(int32 argc, const char **argv)
break; break;
case 'v': case 'v':
strncpy(volume_path, optarg, B_FILE_NAME_LENGTH); strncpy(volumePath, optarg, B_FILE_NAME_LENGTH);
break; break;
default: default:
@@ -98,35 +110,33 @@ int main(int32 argc, const char **argv)
} }
} }
BVolume query_volume; BVolume volume;
if (!o_all_volumes) { if (!o_all_volumes) {
// Find the volume that the query should be performed on, // Find the volume that the query should be performed on,
// and set the query to it. // and set the query to it.
BEntry e(volume_path); BEntry entry(volumePath);
if (e.InitCheck() != B_OK) { if (entry.InitCheck() != B_OK) {
printf("query: %s is not a valid file\n", volume_path); printf("query: %s is not a valid file\n", volumePath);
exit(0); exit(0);
} }
e.GetVolume(&query_volume); entry.GetVolume(&volume);
if (!query_volume.KnowsQuery()) if (!volume.KnowsQuery())
printf("query: volume containing %s is not query-enabled\n", volume_path); printf("query: volume containing %s is not query-enabled\n", volumePath);
else else
perform_query(&query_volume, argv[optind]); perform_query(volume, argv[optind]);
} else {
exit(0);
}
// Okay, we want to query all the disks -- so iterate over // Okay, we want to query all the disks -- so iterate over
// them, one by one, running the query. // them, one by one, running the query.
BVolumeRoster volume_roster; BVolumeRoster volumeRoster;
while (volume_roster.GetNextVolume(&query_volume) == B_OK) { while (volumeRoster.GetNextVolume(&volume) == B_OK) {
// We don't print errors here -- this will catch /pipe and // We don't print errors here -- this will catch /pipe and
// other filesystems we don't care about. // other filesystems we don't care about.
if (query_volume.KnowsQuery()) if (volume.KnowsQuery())
perform_query(&query_volume, argv[optind]); perform_query(volume, argv[optind]);
}
} }
exit(0); return 0;
} }