#include "sha256.h"
#endif
#include "sha256.h"
#endif
-int file_exists(const char *file_name)
+int
+file_exists(const char *file_name)
- int err;
- struct stat stat_buf;
+ struct stat st;
+
+ if (stat(file_name, &st) == -1)
+ return 0;
- err = stat(file_name, &stat_buf);
- if (err == 0) {
- } else {
- return 0;
- }
-int file_is_dir(const char *file_name)
+int
+file_is_dir(const char *file_name)
- int err;
- struct stat stat_buf;
- err = stat(file_name, &stat_buf);
- if (err) {
- return 0;
- }
+ if (stat(file_name, &st) == -1)
+ return 0;
- return S_ISDIR(stat_buf.st_mode);
+ return S_ISDIR(st.st_mode);
}
/* read a single line from a file, stopping at a newline or EOF.
}
/* read a single line from a file, stopping at a newline or EOF.
Return value is NULL if the file is at EOF when called.
*/
Return value is NULL if the file is at EOF when called.
*/
-#define FILE_READ_LINE_BUF_SIZE 1024
-char *file_read_line_alloc(FILE *file)
+char *
+file_read_line_alloc(FILE *fp)
- char buf[FILE_READ_LINE_BUF_SIZE];
- int buf_len;
- char *line = NULL;
- int line_size = 0;
-
- memset(buf, 0, FILE_READ_LINE_BUF_SIZE);
- while (fgets(buf, FILE_READ_LINE_BUF_SIZE, file)) {
- buf_len = strlen(buf);
- if (line) {
- line_size += buf_len;
- line = xrealloc(line, line_size);
- strcat(line, buf);
- } else {
- line_size = buf_len + 1;
- line = xstrdup(buf);
- }
- if (buf[buf_len - 1] == '\n') {
- break;
+ char buf[BUFSIZ];
+ int buf_len;
+ char *line = NULL;
+ int line_size = 0;
+
+ buf[0] = '\0';
+
+ while (fgets(buf, BUFSIZ, fp)) {
+ buf_len = strlen(buf);
+ if (line) {
+ line_size += buf_len;
+ line = xrealloc(line, line_size+1);
+ strncat(line, buf, line_size);
+ } else {
+ line_size = buf_len + 1;
+ line = xstrdup(buf);
+ }
+ if (buf[buf_len - 1] == '\n') {
+ buf[buf_len -1] = '\0';
+ break;
+ }
-int file_move(const char *src, const char *dest)
+int
+file_move(const char *src, const char *dest)
- int err;
-
- err = rename(src, dest);
-
- if (err && errno == EXDEV) {
- err = file_copy(src, dest);
- unlink(src);
- } else if (err) {
- fprintf(stderr, "%s: ERROR: failed to rename %s to %s: %s\n",
- __FUNCTION__, src, dest, strerror(errno));
- }
+ int err;
+
+ err = rename(src, dest);
+ if (err == -1) {
+ if (errno == EXDEV) {
+ /* src & dest live on different file systems */
+ err = file_copy(src, dest);
+ if (err == 0)
+ unlink(src);
+ } else {
+ fprintf(stderr, "%s: rename(%s, %s): %s\n",
+ __FUNCTION__, src, dest, strerror(errno));
+ }
+ }
-/* I put these here to keep libbb dependencies from creeping all over
- the opkg code */
-int file_copy(const char *src, const char *dest)
+int
+file_copy(const char *src, const char *dest)
- err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
- if (err) {
- fprintf(stderr, "%s: ERROR: failed to copy %s to %s\n",
- __FUNCTION__, src, dest);
- }
+ err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
+ if (err)
+ fprintf(stderr, "%s: copy_file(%s, %s)\n",
+ __FUNCTION__, src, dest);
-int file_mkdir_hier(const char *path, long mode)
+int
+file_mkdir_hier(const char *path, long mode)
- return make_directory(path, mode, FILEUTILS_RECUR);
+ return make_directory(path, mode, FILEUTILS_RECUR);
}
char *file_md5sum_alloc(const char *file_name)
}
char *file_md5sum_alloc(const char *file_name)
if (regexec(&comment_re, line, 0, 0, 0) == 0) {
goto NEXT_LINE;
}
if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
if (regexec(&comment_re, line, 0, 0, 0) == 0) {
goto NEXT_LINE;
}
if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
filename, line_num, line);
goto NEXT_LINE;
fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
filename, line_num, line);
goto NEXT_LINE;
if (cf_name == NULL) {
break;
}
if (cf_name == NULL) {
break;
}
if (cf_name[0] == '\0') {
continue;
}
if (cf_name[0] == '\0') {
continue;
}
if (line == NULL) {
break;
}
if (line == NULL) {
break;
}
file_name = line;
if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
file_name = line;
if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
return (strcmp(str + str_len - suffix_len, suffix) == 0);
}
return (strcmp(str + str_len - suffix_len, suffix) == 0);
}
-int str_chomp(char *str)
-{
- if (str[strlen(str) - 1] == '\n') {
- str[strlen(str) - 1] = '\0';
- return 1;
- }
- return 0;
-}
-
int str_tolower(char *str)
{
while (*str) {
int str_tolower(char *str)
{
while (*str) {
int str_starts_with(const char *str, const char *prefix);
int str_ends_with(const char *str, const char *suffix);
int str_starts_with(const char *str, const char *prefix);
int str_ends_with(const char *str, const char *suffix);
-int str_chomp(char *str);
int str_tolower(char *str);
int str_toupper(char *str);
int str_tolower(char *str);
int str_toupper(char *str);
if (response == NULL)
return NULL;
if (response == NULL)
return NULL;
str_tolower(response);
return response;
str_tolower(response);
return response;