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;
133 len_file = strlen(file);
135 len_suffix = strlen(suffix);
137 len_total = len_suffix + len_file;
143 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
145 ods_log_crit(
"[%s] build path failed: malloc failed", file_str);
149 strncpy(openf, file, len_file);
150 openf[len_file] =
'\0';
153 for (i=0; i<len_file; i++) {
167 strncat(openf, suffix, len_suffix);
170 strncat(openf,
"/", 1);
172 openf[len_total] =
'\0';
185 ods_fopen(
const char* file,
const char* dir,
const char* mode)
190 size_t len_total = 0;
195 (dir?
"dir=":
""), (dir?dir:
""), (file?file:
"(null)"),
199 len_dir= strlen(dir);
202 len_file= strlen(file);
204 len_total = len_dir + len_file;
206 openf = (
char*) malloc(
sizeof(
char)*(len_total + 1));
208 ods_log_error(
"[%s] unable to open file %s%s%s for %s: malloc() "
209 "failed", file_str, (dir?dir:
""), (dir?
"/":
""),
214 strncpy(openf, dir, len_dir);
215 openf[len_dir] =
'\0';
217 strncat(openf, file, len_file);
220 strncpy(openf, file, len_file);
222 openf[len_total] =
'\0';
225 fd = fopen(openf, mode);
228 file_str, openf?openf:
"(null)",
232 ods_log_debug(
"[%s] openfile %s count %u", file_str, openf?openf:
"(null)", file_count);
269 if ((nwritten = write(fd, ptr, nleft)) <= 0) {
270 if (nwritten < 0 && errno == EINTR) {
294 if ((fd =
ods_fopen(file, NULL,
"r")) != NULL) {
295 ret = stat(file, &buf);
298 file, strerror(errno));
303 ods_log_debug(
"[%s] unable to stat file %s: ods_fopen() failed",
323 }
else if (strlen(s1) != strlen(s2)) {
324 if (strncmp(s1, s2, strlen(s1)) == 0) {
325 return strlen(s1) - strlen(s2);
328 return strncmp(s1, s2, strlen(s1));
339 while (str1 && str2 && *str1 !=
'\0' && *str2 !=
'\0') {
340 if (tolower((
int)*str1) != tolower((
int)*str2)) {
341 if (tolower((
int)*str1) < tolower((
int)*str2)) {
350 if (*str1 == *str2) {
352 }
else if (*str1 ==
'\0') {
355 }
else if (!str1 && !str2) {
357 }
else if (!str1 && str2) {
369 ods_replace(
const char *str,
const char *oldstr,
const char *newstr)
373 size_t part1_len = 0;
374 size_t part2_len = 0;
375 size_t part3_len = 0;
380 if (!oldstr || !newstr) {
384 if (!(ch = strstr(str, oldstr))) {
385 buffer = strdup(str);
390 part2_len = strlen(newstr);
391 part3_len = strlen(ch+strlen(oldstr));
392 buffer = calloc(part1_len+part2_len+part3_len+1,
sizeof(
char));
398 strncpy(buffer, str, part1_len);
399 buffer[part1_len] =
'\0';
402 strncat(buffer, str, part2_len);
403 buffer[part1_len+part2_len] =
'\0';
406 strncpy(buffer, newstr, part2_len);
407 buffer[part2_len] =
'\0';
411 strncat(buffer, ch+strlen(oldstr), part3_len);
412 buffer[part1_len+part2_len+part3_len] =
'\0';
415 buffer[ch-str] =
'\0';
416 snprintf(buffer+(ch-str),
SYSTEM_MAXLEN,
"%s%s", newstr, ch+strlen(oldstr));
426 ods_file_copy(
const char* file1,
const char* file2,
long startpos,
int append)
432 if (!file1 || !file2) {
435 if ((fin = open(file1, O_RDONLY|O_NONBLOCK)) < 0) {
439 fout = open(file2, O_WRONLY|O_APPEND|O_CREAT, 0666);
441 fout = open(file2, O_WRONLY|O_TRUNC|O_CREAT, 0666);
447 ods_log_debug(
"[%s] lseek file %s pos %ld", file_str, file1, startpos);
448 if (lseek(fin, startpos, SEEK_SET) < 0) {
452 read_size = read(fin, buf,
sizeof(buf));
453 if (read_size == 0) {
463 if (write(fout, buf, (
unsigned int) read_size) < 0) {
464 ods_log_error(
"[%s] write file %s error %s", file_str, file1,
483 int l = strlen(file);
489 while (l>0 && strncmp(file + (l-1),
"/", 1) != 0) {
494 while (l>0 && strncmp(file + (l-1),
"/", 1) == 0) {
499 dir = (
char*) calloc(l+1,
sizeof(
char));
501 dir = strncpy(dir, file, l);
513 ods_chown(
const char* file, uid_t uid, gid_t gid,
int getdir)
523 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
524 file_str, file, (
signed long) uid, (
signed long) gid);
525 if (chown(file, uid, gid) != 0) {
530 ods_log_debug(
"[%s] create and chown %s with user=%ld group=%ld",
531 file_str, dir, (
signed long) uid, (
signed long) gid);
532 if (chown(dir, uid, gid) != 0) {
534 dir, strerror(errno));
551 int i = strlen(str), nl = 0;
556 if (str[i] ==
'\n') {
559 if (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\n') {
571 while (str[i] ==
' ' || str[i] ==
'\t') {
574 while (*(str+i) !=
'\0') {
594 for (count=0; (*list)[count]; ++count) {
599 *list = (
char**) calloc(
sizeof(
char*), count+2);
605 memcpy(*list, old, count *
sizeof(
char*));
608 (*list)[count] = str;
609 (*list)[count+1] = NULL;
612 *list = calloc(
sizeof(
char*), 2);