1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/*
* $Id: gfgrep.c 2373 2006-01-12 00:54:22Z soda $
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <regex.h>
#include <gfarm/gfarm.h>
char *program_name = "gfarm_grep";
void
usage()
{
fprintf(stderr, "Usage: %s [option] <regexp> <input_file>\n",
program_name);
fprintf(stderr, "option:\n");
fprintf(stderr, "\t-o <output_gfarm_file>\n");
exit(1);
}
int
main(argc, argv)
int argc;
char **argv;
{
extern char *optarg;
extern int optind;
int c, total_nodes = -1, node_index = -1;
char *regexp = NULL;
char *e, *input, *output = NULL;
regex_t re;
struct gfs_file *igf;
struct gfs_file *ogf;
int eof;
char line[1024];
e = gfarm_initialize(&argc, &argv);
if (e != NULL) {
fprintf(stderr, "%s: %s\n", program_name, e);
exit(1);
}
if (argc >= 1)
program_name = argv[0];
while ((c = getopt(argc, argv, "e:o:")) != -1) {
switch (c) {
case 'e':
regexp = optarg;
break;
case 'o':
output = optarg;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
e = gfs_pio_get_node_rank(&node_index);
if (e != NULL) {
fprintf(stderr, "%s: %s\n", program_name, e);
exit(1);
}
e = gfs_pio_get_node_size(&total_nodes);
if (total_nodes <= 0) {
fprintf(stderr, "%s: %s\n", program_name, e);
exit(1);
}
if (regexp == NULL) {
if (argc == 0) {
if (node_index == 0) {
fprintf(stderr,
"%s: missing regular expression\n",
program_name);
usage();
}
exit(1);
}
regexp = argv[0];
argc--;
argv++;
}
if (output == NULL) {
if (node_index == 0) {
fprintf(stderr,
"%s: \"-o <output gfarm_file>\" is needed\n",
program_name);
}
exit(1);
}
if (argc == 0) {
if (node_index == 0) {
fprintf(stderr, "%s: input file name is mandatory\n",
program_name);
}
exit(1);
}
input = argv[0];
argc--;
argv++;
if (argc != 0) {
if (node_index == 0) {
fprintf(stderr,
"%s: currently, "
"only one input file is supported\n",
program_name);
}
exit(1);
}
if (regcomp(&re, regexp, REG_NOSUB) != 0) {
if (node_index == 0) {
fprintf(stderr, "%s: invalid regular expression\n",
program_name);
}
exit(1);
}
e = gfs_pio_create(output, GFARM_FILE_WRONLY|GFARM_FILE_TRUNC, 0666,
&ogf);
if (e != NULL) {
fprintf(stderr, "%s: node %d, cannot open %s: %s\n",
program_name, node_index, output, e);
exit(1);
}
e = gfs_pio_set_view_local(ogf, GFARM_FILE_SEQUENTIAL);
if (e != NULL) {
fprintf(stderr, "%s: node %d, set_view_local(%s): %s\n",
program_name, node_index, output, e);
exit(1);
}
e = gfs_pio_open(input, GFARM_FILE_RDONLY, &igf);
if (e != NULL) {
fprintf(stderr, "%s: node %d, cannot open %s: %s\n",
program_name, node_index, input, e);
exit(1);
}
e = gfs_pio_set_view_local(igf, GFARM_FILE_SEQUENTIAL);
if (e != NULL) {
fprintf(stderr,
"%s: node %d, set_view_local(%s): %s\n",
program_name, node_index, input, e);
exit(1);
}
for (;;) {
e = gfs_pio_getline(igf,
line, sizeof line, &eof);
if (e != NULL) {
fprintf(stderr, "%s: %s\n", input, e);
break;
}
if (eof)
break;
if (regexec(&re, line, 0, NULL, 0) == 0)
gfs_pio_putline(ogf, line);
}
gfs_pio_close(igf);
gfs_pio_close(ogf);
e = gfarm_terminate();
if (e != NULL) {
fprintf(stderr, "%s: %s\n", program_name, e);
exit(1);
}
regfree(&re);
return (0);
}
|