about summary refs log tree commit diff stats
path: root/rf.c
diff options
context:
space:
mode:
authorRory Bradford <roryrjb@gmail.com>2019-11-25 12:47:10 +0000
committerRory Bradford <roryrjb@gmail.com>2019-11-25 12:47:10 +0000
commit3efea980535615e0409fef637facaceefd32f5f5 (patch)
treec438d304cf4c3abddbd0e5d6663ea48abe7debc3 /rf.c
parent8de2db7fee294d6653507ca294bb5ef0d78dfd2f (diff)
downloadrf-3efea980535615e0409fef637facaceefd32f5f5.tar.gz
Implement limit
Signed-off-by: Rory Bradford <roryrjb@gmail.com>
Diffstat (limited to 'rf.c')
-rw-r--r--rf.c36
1 files changed, 28 insertions, 8 deletions
diff --git a/rf.c b/rf.c
index 0e0d87e..231fe60 100644
--- a/rf.c
+++ b/rf.c
@@ -16,6 +16,8 @@
 struct switches {
   int basename;
   int invert;
+  int limit;
+  int count;
 };
 
 int version(char *error) {
@@ -32,7 +34,10 @@ int usage(char *error) {
   fprintf(stderr, "\nOptions:\n");
   fprintf(stderr, "  --basename, -b   only show basename in results\n");
   fprintf(stderr, "  --invert, -v     invert matching\n");
-  fprintf(stderr, "  --help, -h       show help\n");
+  fprintf(stderr,
+          "  --limit n        limit to [n] results, all if 0 [default=0]\n");
+
+  fprintf(stderr, "\n  --help, -h       show help\n");
   fprintf(stderr, "  --version, -V    show version\n");
   fprintf(stderr, "\n");
 
@@ -199,6 +204,10 @@ void recurse_find(char **patterns, int *pattern_count, char *dirname,
       if ((matched && (switches->invert == 0)) ||
           (matched == 0 && (switches->invert == 1))) {
         print_result(path, switches, entry);
+
+        if (switches->limit > 0 && ++switches->count == switches->limit) {
+          exit(EXIT_SUCCESS);
+        }
       }
     }
 
@@ -207,19 +216,21 @@ void recurse_find(char **patterns, int *pattern_count, char *dirname,
 }
 
 int main(int argc, char **argv) {
-  static struct option options[] = {{"basename", no_argument, 0, 0},
-                                    {"invert", no_argument, 0, 0},
-                                    {"version", no_argument, 0, 0},
-                                    {"help", no_argument, 0, 0},
-                                    {0, 0, 0, 0}};
+  static struct option options[] = {
+      {"basename", no_argument, 0, 0},    {"invert", no_argument, 0, 0},
+      {"limit", required_argument, 0, 0}, {"version", no_argument, 0, 0},
+      {"help", no_argument, 0, 0},        {0, 0, 0, 0}};
 
   int basename = 0;
   int invert = 0;
+  int limit = 0;
 
   int index = 0;
   int res;
 
-  while ((res = getopt_long(argc, argv, "hvb", options, &index)) > -1) {
+  char *remainder;
+
+  while ((res = getopt_long(argc, argv, "hvVb", options, &index)) > -1) {
     switch (res) {
     case 0:
       if (strcmp("version", options[index].name) == 0) {
@@ -230,6 +241,12 @@ int main(int argc, char **argv) {
         basename = 1;
       } else if (strcmp("invert", options[index].name) == 0) {
         invert = 1;
+      } else if (strcmp("limit", options[index].name) == 0) {
+        limit = strtol(optarg, &remainder, 10);
+
+        if (limit < 0) {
+          return usage("Invalid limit.");
+        }
       }
 
       break;
@@ -264,7 +281,10 @@ int main(int argc, char **argv) {
       patterns[i] = argv[i + 1];
     }
 
-    struct switches switches = {.basename = basename, .invert = invert};
+    int count = 0; // used to count how matches we find
+
+    struct switches switches = {
+        .basename = basename, .invert = invert, .limit = limit, .count = count};
 
     recurse_find(patterns, &pattern_count, ".", &switches);
     free(patterns);
le='Blame the previous revision' href='/danisanti/profani-tty/blame/src/xmpp/resource.c?id=95015cec56fa56f2ef97227edf06de17c65000d9'>^
da5104f8 ^
1a6490a5 ^
59788752 ^
da5104f8 ^




da5104f8 ^



a84e4ade ^
a2726b6a ^
a84e4ade ^



























dd11334b ^
a2726b6a ^
da5104f8 ^
59788752 ^
0346fda0 ^

0346fda0 ^

da5104f8 ^
37742d71 ^

a2726b6a ^
37742d71 ^

a2726b6a ^
37742d71 ^








a2726b6a ^










37742d71 ^



a2726b6a ^
37742d71 ^




















a2726b6a ^










37742d71 ^

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