44 #include <sys/types.h>
47 #define BUFFER_SIZE (16 * 1024)
49 static const char* file_str =
"file";
70 return "unknown mode";
93 if (c == EOF && errno != 0) {
95 file_str, strerror(errno));
113 while ((c=
ods_fgetc(fd, line_nr)) != EOF) {
114 if (c ==
' ' || c ==
'\t' || c ==
'\r') {
131 size_t len_suffix = 0;
132 size_t len_total = 0;
136 len_file = strlen(file);
138 len_suffix = strlen(suffix);
140 len_total = len_suffix + len_file;
146 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
148 ods_log_crit(
"[%s] build path failed: malloc failed", file_str);
152 strncpy(openf, file, len_file);
153 openf[len_file] =
'\0';
156 for (i=0; i<len_file; i++) {
170 strncat(openf, suffix, len_suffix);
173 strncat(openf,
"/", 1);
175 openf[len_total] =
'\0';
188 ods_fopen(
const char* file,
const char* dir,
const char* mode)
193 size_t len_total = 0;
197 ods_log_debug(
"[%s] open file %s%s file=%s mode=%s", file_str,
198 (dir?
"dir=":
""), (dir?dir:
""), (file?file:
"(null)"),
202 len_dir= strlen(dir);
205 len_file= strlen(file);
207 len_total = len_dir + len_file;
209 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
211 ods_log_error(
"[%s] unable to open file %s%s%s for %s: malloc() "
212 "failed", file_str, (dir?dir:
""), (dir?
"/":
""),
217 strncpy(openf, dir, len_dir);
218 openf[len_dir] =
'\0';
220 strncat(openf, file, len_file);
223 strncpy(openf, file, len_file);
225 openf[len_total] =
'\0';
228 fd = fopen(openf, mode);
231 file_str, openf?openf:
"(null)",
268 if ((nwritten = write(fd, ptr, nleft)) <= 0) {
269 if (nwritten < 0 && errno == EINTR) {
295 if ((fd =
ods_fopen(file, NULL,
"r")) != NULL) {
296 ret = stat(file, &buf);
300 file_str, file, strerror(errno));
305 ods_log_error(
"[%s] unable to stat file %s: ods_fopen() failed",
306 file_str, file, strerror(errno));
325 }
else if (strlen(s1) != strlen(s2)) {
326 if (strncmp(s1, s2, strlen(s1)) == 0) {
327 return strlen(s1) - strlen(s2);
330 return strncmp(s1, s2, strlen(s1));
339 ods_replace(
const char *str,
const char *oldstr,
const char *newstr)
343 size_t part1_len = 0;
344 size_t part2_len = 0;
345 size_t part3_len = 0;
350 if (!oldstr || !newstr) {
354 if (!(ch = strstr(str, oldstr))) {
355 buffer = strdup(str);
360 part2_len = strlen(newstr);
361 part3_len = strlen(ch+strlen(oldstr));
362 buffer = calloc(part1_len+part2_len+part3_len+1,
sizeof(
char));
368 strncpy(buffer, str, part1_len);
369 buffer[part1_len] =
'\0';
372 strncat(buffer, str, part2_len);
373 buffer[part1_len+part2_len] =
'\0';
376 strncpy(buffer, newstr, part2_len);
377 buffer[part2_len] =
'\0';
381 strncat(buffer, ch+strlen(oldstr), part3_len);
382 buffer[part1_len+part2_len+part3_len] =
'\0';
385 buffer[ch-str] =
'\0';
386 snprintf(buffer+(ch-str),
SYSTEM_MAXLEN,
"%s%s", newstr, ch+strlen(oldstr));
402 if (!file1 || !file2) {
405 if ((fin = open(file1, O_RDONLY|O_NONBLOCK)) < 0) {
408 if ((fout = open(file2, O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0) {
413 read_size = read(fin, buf,
sizeof(buf));
414 if (read_size == 0) {
422 if (write(fout, buf, (
unsigned int) read_size) < 0) {
440 int l = strlen(file);
446 while (l>0 && strncmp(file + (l-1),
"/", 1) != 0) {
451 while (l>0 && strncmp(file + (l-1),
"/", 1) == 0) {
456 dir = (
char*) calloc(l+1,
sizeof(
char));
458 dir = strncpy(dir, file, l);
470 ods_chown(
const char* file, uid_t uid, gid_t gid,
int getdir)
480 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
481 file_str, file, (
signed long) uid, (
signed long) gid);
482 if (chown(file, uid, gid) != 0) {
487 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
488 file_str, dir, (
signed long) uid, (
signed long) gid);
489 if (chown(dir, uid, gid) != 0) {
491 dir, strerror(errno));
508 int i = strlen(str), nl = 0;
513 if (str[i] ==
'\n') {
516 if (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\n') {
528 while (str[i] ==
' ' || str[i] ==
'\t') {
531 while (*(str+i) !=
'\0') {