45 #include <sys/types.h>
48 #define BUFFER_SIZE (16 * 1024)
50 static const char* file_str =
"file";
51 static unsigned int file_count = 0;
72 return "unknown mode";
92 if (c == EOF && errno != 0) {
94 file_str, strerror(errno));
112 while ((c=
ods_fgetc(fd, line_nr)) != EOF) {
113 if (c ==
' ' || c ==
'\t' || c ==
'\r') {
130 size_t len_suffix = 0;
131 size_t len_total = 0;
135 len_file = strlen(file);
137 len_suffix = strlen(suffix);
139 len_total = len_suffix + len_file;
145 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
147 ods_log_crit(
"[%s] build path failed: malloc failed", file_str);
151 strncpy(openf, file, len_file);
152 openf[len_file] =
'\0';
155 for (i=0; i<len_file; i++) {
169 strncat(openf, suffix, len_suffix);
172 strncat(openf,
"/", 1);
174 openf[len_total] =
'\0';
187 ods_fopen(
const char* file,
const char* dir,
const char* mode)
192 size_t len_total = 0;
197 (dir?
"dir=":
""), (dir?dir:
""), (file?file:
"(null)"),
201 len_dir= strlen(dir);
204 len_file= strlen(file);
206 len_total = len_dir + len_file;
208 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
210 ods_log_error(
"[%s] unable to open file %s%s%s for %s: malloc() "
211 "failed", file_str, (dir?dir:
""), (dir?
"/":
""),
216 strncpy(openf, dir, len_dir);
217 openf[len_dir] =
'\0';
219 strncat(openf, file, len_file);
222 strncpy(openf, file, len_file);
224 openf[len_total] =
'\0';
227 fd = fopen(openf, mode);
230 file_str, openf?openf:
"(null)",
234 ods_log_debug(
"[%s] openfile %s count %u", file_str, openf?openf:
"(null)", file_count);
271 if ((nwritten = write(fd, ptr, nleft)) <= 0) {
272 if (nwritten < 0 && errno == EINTR) {
296 if ((fd =
ods_fopen(file, NULL,
"r")) != NULL) {
297 ret = stat(file, &buf);
300 file, strerror(errno));
305 ods_log_error(
"[%s] unable to stat file %s: ods_fopen() failed",
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));
341 while (str1 && str2 && *str1 !=
'\0' && *str2 !=
'\0') {
342 if (tolower((
int)*str1) != tolower((
int)*str2)) {
343 if (tolower((
int)*str1) < tolower((
int)*str2)) {
352 if (*str1 == *str2) {
354 }
else if (*str1 ==
'\0') {
357 }
else if (!str1 && !str2) {
359 }
else if (!str1 && str2) {
371 ods_replace(
const char *str,
const char *oldstr,
const char *newstr)
375 size_t part1_len = 0;
376 size_t part2_len = 0;
377 size_t part3_len = 0;
382 if (!oldstr || !newstr) {
386 if (!(ch = strstr(str, oldstr))) {
387 buffer = strdup(str);
392 part2_len = strlen(newstr);
393 part3_len = strlen(ch+strlen(oldstr));
394 buffer = calloc(part1_len+part2_len+part3_len+1,
sizeof(
char));
400 strncpy(buffer, str, part1_len);
401 buffer[part1_len] =
'\0';
404 strncat(buffer, str, part2_len);
405 buffer[part1_len+part2_len] =
'\0';
408 strncpy(buffer, newstr, part2_len);
409 buffer[part2_len] =
'\0';
413 strncat(buffer, ch+strlen(oldstr), part3_len);
414 buffer[part1_len+part2_len+part3_len] =
'\0';
417 buffer[ch-str] =
'\0';
418 snprintf(buffer+(ch-str),
SYSTEM_MAXLEN,
"%s%s", newstr, ch+strlen(oldstr));
434 if (!file1 || !file2) {
437 if ((fin = open(file1, O_RDONLY|O_NONBLOCK)) < 0) {
440 if ((fout = open(file2, O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0) {
445 read_size = read(fin, buf,
sizeof(buf));
446 if (read_size == 0) {
454 if (write(fout, buf, (
unsigned int) read_size) < 0) {
472 int l = strlen(file);
478 while (l>0 && strncmp(file + (l-1),
"/", 1) != 0) {
483 while (l>0 && strncmp(file + (l-1),
"/", 1) == 0) {
488 dir = (
char*) calloc(l+1,
sizeof(
char));
490 dir = strncpy(dir, file, l);
502 ods_chown(
const char* file, uid_t uid, gid_t gid,
int getdir)
512 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
513 file_str, file, (
signed long) uid, (
signed long) gid);
514 if (chown(file, uid, gid) != 0) {
519 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
520 file_str, dir, (
signed long) uid, (
signed long) gid);
521 if (chown(dir, uid, gid) != 0) {
523 dir, strerror(errno));
540 int i = strlen(str), nl = 0;
545 if (str[i] ==
'\n') {
548 if (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\n') {
560 while (str[i] ==
' ' || str[i] ==
'\t') {
563 while (*(str+i) !=
'\0') {
583 for (count=0; (*list)[count]; ++count) {
588 *list = (
char**) calloc(
sizeof(
char*), count+2);
594 memcpy(*list, old, count *
sizeof(
char*));
597 (*list)[count] = str;
598 (*list)[count+1] = NULL;
601 *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)