forked from h2oai/jdupes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.c
57 lines (43 loc) · 1.2 KB
/
args.c
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
/* Argument functions
* This file is part of jdupes; see jdupes.c for license information */
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libjodycode.h>
#include "jdupes.h"
char **cloneargs(const int argc, char **argv)
{
static int x;
static char **args;
args = (char **)malloc(sizeof(char *) * (unsigned int)argc);
if (args == NULL) jc_oom("cloneargs() start");
for (x = 0; x < argc; x++) {
args[x] = (char *)malloc(strlen(argv[x]) + 1);
if (args[x] == NULL) jc_oom("cloneargs() loop");
strcpy(args[x], argv[x]);
}
return args;
}
int findarg(const char * const arg, const int start, const int argc, char **argv)
{
int x;
for (x = start; x < argc; x++)
if (jc_streq(argv[x], arg) == 0)
return x;
return x;
}
/* Find the first non-option argument after specified option. */
int nonoptafter(const char *option, const int argc, char **oldargv, char **newargv)
{
int x;
int targetind;
int testind;
int startat = 1;
targetind = findarg(option, 1, argc, oldargv);
for (x = optind; x < argc; x++) {
testind = findarg(newargv[x], startat, argc, oldargv);
if (testind > targetind) return x;
else startat = testind;
}
return x;
}