44 #include <sys/types.h>
47 #define BUFFER_SIZE (16 * 1024)
49 static const char* file_str =
"file";
70 return "unknown mode";
109 while ((c=
ods_fgetc(fd, line_nr)) != EOF) {
110 if (c ==
' ' || c ==
'\t' || c ==
'\r') {
127 size_t len_suffix = 0;
128 size_t len_total = 0;
132 len_file = strlen(file);
134 len_suffix = strlen(suffix);
136 len_total = len_suffix + len_file;
142 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
144 ods_log_crit(
"[%s] build path failed: malloc failed", file_str);
148 strncpy(openf, file, len_file);
149 openf[len_file] =
'\0';
152 for (i=0; i<len_file; i++) {
166 strncat(openf, suffix, len_suffix);
169 strncat(openf,
"/", 1);
171 openf[len_total] =
'\0';
184 ods_fopen(
const char* file,
const char* dir,
const char* mode)
189 size_t len_total = 0;
193 ods_log_debug(
"[%s] open file %s%s file=%s mode=%s", file_str,
194 (dir?
"dir=":
""), (dir?dir:
""), (file?file:
"(null)"),
198 len_dir= strlen(dir);
201 len_file= strlen(file);
203 len_total = len_dir + len_file;
205 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
210 strncpy(openf, dir, len_dir);
211 openf[len_dir] =
'\0';
213 strncat(openf, file, len_file);
216 strncpy(openf, file, len_file);
218 openf[len_total] =
'\0';
221 fd = fopen(openf, mode);
224 file_str, openf?openf:
"(null)",
261 if ((nwritten = write(fd, ptr, nleft)) <= 0) {
262 if (nwritten < 0 && errno == EINTR) {
288 if ((fd =
ods_fopen(file, NULL,
"r")) != NULL) {
289 ret = stat(file, &buf);
310 }
else if (strlen(s1) != strlen(s2)) {
311 if (strncmp(s1, s2, strlen(s1)) == 0) {
312 return strlen(s1) - strlen(s2);
315 return strncmp(s1, s2, strlen(s1));
324 ods_replace(
const char *str,
const char *oldstr,
const char *newstr)
328 size_t part1_len = 0;
329 size_t part2_len = 0;
330 size_t part3_len = 0;
335 if (!oldstr || !newstr) {
339 if (!(ch = strstr(str, oldstr))) {
340 buffer = strdup(str);
345 part2_len = strlen(newstr);
346 part3_len = strlen(ch+strlen(oldstr));
347 buffer = calloc(part1_len+part2_len+part3_len+1,
sizeof(
char));
353 strncpy(buffer, str, part1_len);
354 buffer[part1_len] =
'\0';
357 strncat(buffer, str, part2_len);
358 buffer[part1_len+part2_len] =
'\0';
361 strncpy(buffer, newstr, part2_len);
362 buffer[part2_len] =
'\0';
366 strncat(buffer, ch+strlen(oldstr), part3_len);
367 buffer[part1_len+part2_len+part3_len] =
'\0';
370 buffer[ch-str] =
'\0';
371 snprintf(buffer+(ch-str),
SYSTEM_MAXLEN,
"%s%s", newstr, ch+strlen(oldstr));
387 if (!file1 || !file2) {
390 if ((fin = open(file1, O_RDONLY|O_NONBLOCK)) < 0) {
393 if ((fout = open(file2, O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0) {
398 read_size = read(fin, buf,
sizeof(buf));
399 if (read_size == 0) {
407 if (write(fout, buf, (
unsigned int) read_size) < 0) {
425 int l = strlen(file);
431 while (l>0 && strncmp(file + (l-1),
"/", 1) != 0) {
436 while (l>0 && strncmp(file + (l-1),
"/", 1) == 0) {
441 dir = (
char*) calloc(l+1,
sizeof(
char));
443 dir = strncpy(dir, file, l);
455 ods_chown(
const char* file, uid_t uid, gid_t gid,
int getdir)
465 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
466 file_str, file, (
signed long) uid, (
signed long) gid);
467 if (chown(file, uid, gid) != 0) {
472 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
473 file_str, dir, (
signed long) uid, (
signed long) gid);
474 if (chown(dir, uid, gid) != 0) {
476 dir, strerror(errno));
493 int i = strlen(str), nl = 0;
498 if (str[i] ==
'\n') {
501 if (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\n') {
513 while (str[i] ==
' ' || str[i] ==
'\t') {
516 while (*(str+i) !=
'\0') {