43 #include <sys/types.h>
46 #define BUFFER_SIZE (16 * 1024)
48 static const char* file_str =
"file";
49 static unsigned int file_count = 0;
70 return "unknown mode";
90 if (c == EOF && errno != 0) {
92 file_str, strerror(errno));
110 while ((c=
ods_fgetc(fd, line_nr)) != EOF) {
111 if (c ==
' ' || c ==
'\t' || c ==
'\r') {
128 size_t len_suffix = 0;
129 size_t len_total = 0;
138 len_file = strlen(f);
140 len_suffix = strlen(suffix);
142 len_total = len_suffix + len_file;
148 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
150 ods_log_crit(
"[%s] build path failed: malloc failed", file_str);
154 strncpy(openf, f, len_file);
155 openf[len_file] =
'\0';
158 for (i=0; i<len_file; i++) {
172 strncat(openf, suffix, len_suffix);
175 strncat(openf,
"/", 1);
177 openf[len_total] =
'\0';
190 ods_fopen(
const char* file,
const char* dir,
const char* mode)
195 size_t len_total = 0;
200 (dir?
"dir=":
""), (dir?dir:
""), (file?file:
"(null)"),
204 len_dir= strlen(dir);
207 len_file= strlen(file);
209 len_total = len_dir + len_file;
211 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
213 ods_log_error(
"[%s] unable to open file %s%s%s for %s: malloc() "
214 "failed", file_str, (dir?dir:
""), (dir?
"/":
""),
219 strncpy(openf, dir, len_dir);
220 openf[len_dir] =
'\0';
222 strncat(openf, file, len_file);
225 strncpy(openf, file, len_file);
227 openf[len_total] =
'\0';
230 fd = fopen(openf, mode);
233 file_str, openf?openf:
"(null)",
237 ods_log_debug(
"[%s] openfile %s count %u", file_str, openf?openf:
"(null)", file_count);
274 if ((nwritten = write(fd, ptr, nleft)) <= 0) {
275 if (nwritten < 0 && errno == EINTR) {
299 if ((fd =
ods_fopen(file, NULL,
"r")) != NULL) {
300 ret = stat(file, &buf);
303 file, strerror(errno));
308 ods_log_debug(
"[%s] unable to stat file %s: ods_fopen() failed",
328 }
else if (strlen(s1) != strlen(s2)) {
329 if (strncmp(s1, s2, strlen(s1)) == 0) {
330 return strlen(s1) - strlen(s2);
333 return strncmp(s1, s2, strlen(s1));
344 while (str1 && str2 && *str1 !=
'\0' && *str2 !=
'\0') {
345 if (tolower((
int)*str1) != tolower((
int)*str2)) {
346 if (tolower((
int)*str1) < tolower((
int)*str2)) {
355 if (*str1 == *str2) {
357 }
else if (*str1 ==
'\0') {
360 }
else if (!str1 && !str2) {
362 }
else if (!str1 && str2) {
374 ods_replace(
const char *str,
const char *oldstr,
const char *newstr)
378 size_t part1_len = 0;
379 size_t part2_len = 0;
380 size_t part3_len = 0;
385 if (!oldstr || !newstr) {
389 if (!(ch = strstr(str, oldstr))) {
390 buffer = strdup(str);
395 part2_len = strlen(newstr);
396 part3_len = strlen(ch+strlen(oldstr));
397 buffer = calloc(part1_len+part2_len+part3_len+1,
sizeof(
char));
403 strncpy(buffer, str, part1_len);
404 buffer[part1_len] =
'\0';
407 strncat(buffer, str, part2_len);
408 buffer[part1_len+part2_len] =
'\0';
411 strncpy(buffer, newstr, part2_len);
412 buffer[part2_len] =
'\0';
416 strncat(buffer, ch+strlen(oldstr), part3_len);
417 buffer[part1_len+part2_len+part3_len] =
'\0';
420 buffer[ch-str] =
'\0';
421 snprintf(buffer+(ch-str),
SYSTEM_MAXLEN,
"%s%s", newstr, ch+strlen(oldstr));
431 ods_file_copy(
const char* file1,
const char* file2,
long startpos,
int append)
437 if (!file1 || !file2) {
440 if ((fin = open(file1, O_RDONLY|O_NONBLOCK)) < 0) {
444 fout = open(file2, O_WRONLY|O_APPEND|O_CREAT, 0666);
446 fout = open(file2, O_WRONLY|O_TRUNC|O_CREAT, 0666);
452 ods_log_debug(
"[%s] lseek file %s pos %ld", file_str, file1, startpos);
453 if (lseek(fin, startpos, SEEK_SET) < 0) {
457 read_size = read(fin, buf,
sizeof(buf));
458 if (read_size == 0) {
468 if (write(fout, buf, (
unsigned int) read_size) < 0) {
469 ods_log_error(
"[%s] write file %s error %s", file_str, file1,
488 int l = strlen(file);
494 while (l>0 && strncmp(file + (l-1),
"/", 1) != 0) {
499 while (l>0 && strncmp(file + (l-1),
"/", 1) == 0) {
504 dir = (
char*) calloc(l+1,
sizeof(
char));
506 dir = strncpy(dir, file, l);
518 ods_chown(
const char* file, uid_t uid, gid_t gid,
int getdir)
528 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
529 file_str, file, (
signed long) uid, (
signed long) gid);
530 if (chown(file, uid, gid) != 0) {
535 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
536 file_str, dir, (
signed long) uid, (
signed long) gid);
537 if (chown(dir, uid, gid) != 0) {
539 dir, strerror(errno));
556 int i = strlen(str), nl = 0;
561 if (str[i] ==
'\n') {
564 if (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\n') {
576 while (str[i] ==
' ' || str[i] ==
'\t') {
579 while (*(str+i) !=
'\0') {
599 for (count=0; (*list)[count]; ++count) {
604 *list = (
char**) calloc(
sizeof(
char*), count+2);
610 memcpy(*list, old, count *
sizeof(
char*));
613 (*list)[count] = str;
614 (*list)[count+1] = NULL;
617 *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,...)
const char * ods_file_mode2str(const char *mode)
ods_status ods_file_copy(const char *file1, const char *file2, long startpos, int append)