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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
#if defined(__APPLE__) || defined(__OpenBSD__)
#include <sys/syslimits.h>
#include <sys/stat.h>
#endif
#include <sys/types.h>
#include <sys/ioctl.h>
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <dirent.h>
#include <unistd.h>
#include <libintl.h>
#ifdef CYGWIN
#include <limits.h>
#endif
#include <alpm.h>
#include <alpm_list.h>
#include "util.h"
#include "conf.h"
#include "log.h"
extern config_t *config;
unsigned int getcols()
{
if(!isatty(1)) {
return 80;
} else {
#ifdef TIOCGSIZE
struct ttysize win;
if(ioctl(1, TIOCGSIZE, &win) == 0) {
return win.ts_cols;
}
#elif defined(TIOCGWINSZ)
struct winsize win;
if(ioctl(1, TIOCGWINSZ, &win) == 0) {
return win.ws_col;
}
#endif
TODO
return 80;
}
}
int makepath(char *path)
{
char *orig, *str, *ptr;
char full[PATH_MAX] = "";
mode_t oldmask;
oldmask = umask(0000);
orig = strdup(path);
str = orig;
while((ptr = strsep(&str, "/"))) {
if(strlen(ptr)) {
struct stat buf;
strcat(full, "/");
strcat(full, ptr);
if(stat(full, &buf)) {
if(mkdir(full, 0755)) {
free(orig);
umask(oldmask);
return(1);
}
}
}
}
free(orig);
umask(oldmask);
return(0);
}
int rmrf(char *path)
{
int errflag = 0;
struct dirent *dp;
DIR *dirp;
if(!unlink(path)) {
return(0);
} else {
if(errno == ENOENT) {
return(0);
} else if(errno == EPERM) {
} else if(errno == EISDIR) {
} else if(errno == ENOTDIR) {
return(1);
} else {
return(1);
}
if((dirp = opendir(path)) == (DIR *)-1) {
return(1);
}
for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
if(dp->d_ino) {
char name[PATH_MAX];
sprintf(name, "%s/%s", path, dp->d_name);
if(strcmp(dp->d_name, "..") && strcmp(dp->d_name, ".")) {
errflag += rmrf(name);
}
}
}
closedir(dirp);
if(rmdir(path)) {
errflag++;
}
return(errflag);
}
}
void indentprint(const char *str, unsigned int indent)
{
const char *p = str;
unsigned int cidx = indent;
while(*p) {
if(*p == ' ') {
const char *next = NULL;
unsigned int len;
p++;
if(p == NULL || *p == ' ') continue;
next = strchr(p, ' ');
if(next == NULL) {
next = p + strlen(p);
}
len = next - p;
if(len > (getcols()-cidx-1)) {
unsigned int i;
fprintf(stdout, "\n");
for(i = 0; i < indent; i++) {
fprintf(stdout, " ");
}
cidx = indent;
} else {
printf(" ");
cidx++;
}
}
fprintf(stdout, "%c", *p);
p++;
cidx++;
}
}
char *buildstring(alpm_list_t *strlist)
{
char *str;
size_t size = 1;
alpm_list_t *i;
for(i = strlist; i; i = alpm_list_next(i)) {
size += strlen(alpm_list_getdata(i)) + 1;
}
str = (char *)malloc(size);
if(str == NULL) {
ERR(NL, _("failed to allocate %d bytes\n"), size);
}
str[0] = '\0';
for(i = strlist; i; i = alpm_list_next(i)) {
strcat(str, alpm_list_getdata(i));
strcat(str, " ");
}
str[strlen(str)-1] = '\0';
return(str);
}
char *strtoupper(char *str)
{
char *ptr = str;
while(*ptr) {
(*ptr) = toupper(*ptr);
ptr++;
}
return str;
}
char *strtrim(char *str)
{
char *pch = str;
while(isspace(*pch)) {
pch++;
}
if(pch != str) {
memmove(str, pch, (strlen(pch) + 1));
}
pch = (char *)(str + (strlen(str) - 1));
while(isspace(*pch)) {
pch--;
}
*++pch = '\0';
return str;
}
void list_display(const char *title, alpm_list_t *list)
{
alpm_list_t *i;
int cols, len;
len = strlen(title);
printf("%s ", title);
if(list) {
for(i = list, cols = len; i; i = alpm_list_next(i)) {
char *str = alpm_list_getdata(i);
int s = strlen(str)+1;
unsigned int maxcols = getcols();
if(s + cols >= maxcols) {
int i;
cols = len;
printf("\n");
for (i = 0; i < len+1; ++i) {
printf(" ");
}
}
printf("%s ", str);
cols += s;
}
printf("\n");
} else {
printf(_("None\n"));
}
}
|