45 #include <sys/types.h>
48 #define BUFFER_SIZE (16 * 1024)
50 static const char* file_str =
"file";
71 return "unknown mode";
91 if (c == EOF && errno != 0) {
93 file_str, strerror(errno));
111 while ((c=
ods_fgetc(fd, line_nr)) != EOF) {
112 if (c ==
' ' || c ==
'\t' || c ==
'\r') {
129 size_t len_suffix = 0;
130 size_t len_total = 0;
134 len_file = strlen(file);
136 len_suffix = strlen(suffix);
138 len_total = len_suffix + len_file;
144 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
146 ods_log_crit(
"[%s] build path failed: malloc failed", file_str);
150 strncpy(openf, file, len_file);
151 openf[len_file] =
'\0';
154 for (i=0; i<len_file; i++) {
168 strncat(openf, suffix, len_suffix);
171 strncat(openf,
"/", 1);
173 openf[len_total] =
'\0';
186 ods_fopen(
const char* file,
const char* dir,
const char* mode)
191 size_t len_total = 0;
196 (dir?
"dir=":
""), (dir?dir:
""), (file?file:
"(null)"),
200 len_dir= strlen(dir);
203 len_file= strlen(file);
205 len_total = len_dir + len_file;
207 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
209 ods_log_error(
"[%s] unable to open file %s%s%s for %s: malloc() "
210 "failed", file_str, (dir?dir:
""), (dir?
"/":
""),
215 strncpy(openf, dir, len_dir);
216 openf[len_dir] =
'\0';
218 strncat(openf, file, len_file);
221 strncpy(openf, file, len_file);
223 openf[len_total] =
'\0';
226 fd = fopen(openf, mode);
229 file_str, openf?openf:
"(null)",
266 if ((nwritten = write(fd, ptr, nleft)) <= 0) {
267 if (nwritten < 0 && errno == EINTR) {
291 if ((fd =
ods_fopen(file, NULL,
"r")) != NULL) {
292 ret = stat(file, &buf);
295 file, strerror(errno));
300 ods_log_error(
"[%s] unable to stat file %s: ods_fopen() failed",
320 }
else if (strlen(s1) != strlen(s2)) {
321 if (strncmp(s1, s2, strlen(s1)) == 0) {
322 return strlen(s1) - strlen(s2);
325 return strncmp(s1, s2, strlen(s1));
336 while (str1 && str2 && *str1 !=
'\0' && *str2 !=
'\0') {
337 if (tolower((
int)*str1) != tolower((
int)*str2)) {
338 if (tolower((
int)*str1) < tolower((
int)*str2)) {
347 if (*str1 == *str2) {
349 }
else if (*str1 ==
'\0') {
352 }
else if (!str1 && !str2) {
354 }
else if (!str1 && str2) {
366 ods_replace(
const char *str,
const char *oldstr,
const char *newstr)
370 size_t part1_len = 0;
371 size_t part2_len = 0;
372 size_t part3_len = 0;
377 if (!oldstr || !newstr) {
381 if (!(ch = strstr(str, oldstr))) {
382 buffer = strdup(str);
387 part2_len = strlen(newstr);
388 part3_len = strlen(ch+strlen(oldstr));
389 buffer = calloc(part1_len+part2_len+part3_len+1,
sizeof(
char));
395 strncpy(buffer, str, part1_len);
396 buffer[part1_len] =
'\0';
399 strncat(buffer, str, part2_len);
400 buffer[part1_len+part2_len] =
'\0';
403 strncpy(buffer, newstr, part2_len);
404 buffer[part2_len] =
'\0';
408 strncat(buffer, ch+strlen(oldstr), part3_len);
409 buffer[part1_len+part2_len+part3_len] =
'\0';
412 buffer[ch-str] =
'\0';
413 snprintf(buffer+(ch-str),
SYSTEM_MAXLEN,
"%s%s", newstr, ch+strlen(oldstr));
429 if (!file1 || !file2) {
432 if ((fin = open(file1, O_RDONLY|O_NONBLOCK)) < 0) {
435 if ((fout = open(file2, O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0) {
440 read_size = read(fin, buf,
sizeof(buf));
441 if (read_size == 0) {
449 if (write(fout, buf, (
unsigned int) read_size) < 0) {
467 int l = strlen(file);
473 while (l>0 && strncmp(file + (l-1),
"/", 1) != 0) {
478 while (l>0 && strncmp(file + (l-1),
"/", 1) == 0) {
483 dir = (
char*) calloc(l+1,
sizeof(
char));
485 dir = strncpy(dir, file, l);
497 ods_chown(
const char* file, uid_t uid, gid_t gid,
int getdir)
507 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
508 file_str, file, (
signed long) uid, (
signed long) gid);
509 if (chown(file, uid, gid) != 0) {
514 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
515 file_str, dir, (
signed long) uid, (
signed long) gid);
516 if (chown(dir, uid, gid) != 0) {
518 dir, strerror(errno));
535 int i = strlen(str), nl = 0;
540 if (str[i] ==
'\n') {
543 if (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\n') {
555 while (str[i] ==
' ' || str[i] ==
'\t') {
558 while (*(str+i) !=
'\0') {
578 for (count=0; (*list)[count]; ++count) {
583 *list = (
char**) calloc(
sizeof(
char*), count+2);
589 memcpy(*list, old, count *
sizeof(
char*));
592 (*list)[count] = str;
593 (*list)[count+1] = NULL;
596 *list = calloc(
sizeof(
char*), 2);
void ods_log_debug(const char *format,...)
char * ods_dir_name(const char *file)
void ods_fatal_exit(const char *format,...)
int ods_skip_whitespace(FILE *fd, unsigned int *line_nr)
enum ods_enum_status ods_status
time_t ods_file_lastmodified(const char *file)
void ods_log_error(const char *format,...)
int ods_strcmp(const char *s1, const char *s2)
void ods_str_list_add(char ***list, char *str)
int ods_fgetc(FILE *fd, unsigned int *line_nr)
FILE * ods_fopen(const char *file, const char *dir, const char *mode)
void ods_log_crit(const char *format,...)
void ods_str_trim(char *str)
ssize_t ods_writen(int fd, const void *vptr, size_t n)
char * ods_build_path(const char *file, const char *suffix, int dir, int no_slash)
void ods_chown(const char *file, uid_t uid, gid_t gid, int getdir)
void ods_fclose(FILE *fd)
void ods_log_deeebug(const char *format,...)
int ods_strlowercmp(const char *str1, const char *str2)
#define ods_log_assert(x)
const char * ods_replace(const char *str, const char *oldstr, const char *newstr)
void ods_log_warning(const char *format,...)
ods_status ods_file_copy(const char *file1, const char *file2)
const char * ods_file_mode2str(const char *mode)