-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
155 lines (124 loc) · 3.19 KB
/
utils.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
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
/*
* Copyright (c) 2011-2018 Josef 'Jeff' Sipek <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "utils.h"
#define HDD_START 0
#define HDD_1DOT 1
#define HDD_2DOT 2
#define HDD_OK 3
int hasdotdot(const char *path)
{
int state = HDD_START;
while(*path) {
switch(*path) {
case '.':
if (state == HDD_START)
state = HDD_1DOT;
else if (state == HDD_1DOT)
state = HDD_2DOT;
else
state = HDD_OK;
break;
case '/':
if (state == HDD_2DOT)
return 1; /* '/../' found */
state = HDD_START;
break;
default:
state = HDD_OK;
break;
}
path++;
}
return (state == HDD_2DOT);
}
#define NARGS 5
char *concat5(char *a, char *b, char *c, char *d, char *e)
{
char *src[NARGS] = { a, b, c, d, e, };
size_t len[NARGS]; /* source lengths */
size_t totallen; /* total output string len */
unsigned tofree; /* what to free bitmap */
char *ret, *out;
int first, last; /* the first and last non-NULL index */
int nsrc; /* number of non-NULL sources */
int i, j;
nsrc = 0;
totallen = 0;
first = -1;
last = -1;
tofree = 0x1f;
for (i = 0; i < 5; i++) {
len[i] = 0;
if (src[i]) {
len[i] = strlen(src[i]);
last = i;
if (first == -1)
first = i;
nsrc++;
for (j = i + 1; j < 5; j++)
if (src[i] == src[j])
tofree &= ~(1 << i);
}
totallen += len[i];
}
if (nsrc == 1)
return src[first];
if (!totallen) {
for (i = first; i < last; i++)
if (tofree & (1 << i))
free(src[i]);
return xstrdup("");
}
ret = malloc(totallen + 1);
ASSERT(ret);
out = ret;
for (i = first; i <= last; i++) {
if (!src[i])
continue;
/* copy the source */
strcpy(out, src[i]);
/* advance the output */
out += len[i];
if (tofree & (1 << i))
free(src[i]);
}
/*
* We're guaranteed that the above loop executed at least once, and
* therefore the output is null-terminated
*/
return ret;
}
time_t parse_time_cstr(const char *str)
{
struct tm tm;
if (!str)
return 0;
strptime(str, "%Y-%m-%d %H:%M", &tm);
return mktime(&tm);
}