aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/find.h4
-rw-r--r--include/fs.h2
-rw-r--r--include/str.h1
-rw-r--r--lib/find.c146
-rw-r--r--lib/fs.c72
5 files changed, 73 insertions, 152 deletions
diff --git a/include/find.h b/include/find.h
index 34c471c..839766d 100644
--- a/include/find.h
+++ b/include/find.h
@@ -4,9 +4,5 @@
#ifndef SPM_FIND_H
#define SPM_FIND_H
-char *find_executable(const char *program);
-char *find_file(const char *root, const char *filename);
-char *find_package(const char *filename);
-int errglob(const char *epath, int eerrno);
#endif //SPM_FIND_H
diff --git a/include/fs.h b/include/fs.h
index 0698871..9f706b2 100644
--- a/include/fs.h
+++ b/include/fs.h
@@ -47,4 +47,6 @@ char *expandpath(const char *_path);
char *spm_mkdtemp(const char *base, const char *name, const char *extended_path);
int touch(const char *path);
char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn *readerFn);
+char *find_executable(const char *program);
+int find_in_file(const char *filename, const char *pattern);
#endif //SPM_FSTREE_H
diff --git a/include/str.h b/include/str.h
index ae54997..5a94bd0 100644
--- a/include/str.h
+++ b/include/str.h
@@ -22,7 +22,6 @@ char *join(char **arr, const char *separator);
char *join_ex(char *separator, ...);
char *substring_between(char *sptr, const char *delims);
void strsort(char **arr, unsigned int sort_mode);
-int find_in_file(const char *filename, const char *pattern);
int isrelational(char ch);
void print_banner(const char *s, int len);
char *strstr_array(char **arr, const char *str);
diff --git a/lib/find.c b/lib/find.c
index 348a8f7..39a52df 100644
--- a/lib/find.c
+++ b/lib/find.c
@@ -3,149 +3,3 @@
*/
#include "spm.h"
-/**
- * glob callback function
- * @param epath path to file that generated the error condition
- * @param eerrno the error condition
- * @return the error condition
- */
-int errglob(const char *epath, int eerrno) {
- fprintf(stderr, "glob matching error: %s (%d)", epath, eerrno);
- return eerrno;
-}
-
-/**
- * Scan a directory for a file by name, or by wildcard
- *
- * @param root directory path to scan
- * @param filename file to find (wildcards accepted)
- * @return success=path to file, failure=NULL
- */
-char *find_file(const char *root, const char *filename) {
- glob_t results;
- int glob_flags = 0;
- int match = 0;
- char path[PATH_MAX];
- memset(path, '\0', PATH_MAX);
-
- // GUARD
- if (!root || !filename || strstr(filename, "..") || strstr(filename, "./")) {
- return NULL;
- }
-
- if (realpath(root, path) == NULL) {
- perror("Cannot determine realpath()");
- fprintf(SYSERROR);
- return NULL;
- }
-
- strcat(path, "/");
- strcat(path, filename);
-
- // Save a little time if the file exists
- if (access(path, F_OK) != -1) {
- return strdup(path);
- }
-
- // Inject wildcard
- strcat(path, "*");
- // Search for the file
- match = glob(path, glob_flags, errglob, &results);
-
- if (match != 0) {
- // report critical errors except GLOB_NOMATCH
- if (match == GLOB_NOSPACE || match == GLOB_ABORTED) {
- fprintf(SYSERROR);
- }
- globfree(&results);
- return NULL;
- }
-
- // Replace path string with wanted path string
- strcpy(path, results.gl_pathv[0]);
-
- globfree(&results);
- return strdup(path);
-}
-
-/**
- * Scan the package directory for a package by name
- * @param filename file to find
- * @return success=path to file, failure=NULL
- */
-char *find_package(const char *filename) {
- char *repo = join((char *[]) {SPM_GLOBAL.package_dir, SPM_GLOBAL.repo_target, NULL}, DIRSEPS);
- char *match = find_file(repo, filename);
- free(repo);
- return match;
-}
-
-/**
- * Determine whether `pattern` is present within a file
- * @param filename
- * @param pattern
- * @return 0=found, 1=not found, -1=OS error
- */
-int find_in_file(const char *filename, const char *pattern) {
- int result = 1; // default "not found"
-
- FILE *fp = fopen(filename, "rb");
- if (!fp) {
- return -1;
- }
-
- long int file_len = get_file_size(filename);
- if (file_len == -1) {
- fclose(fp);
- return -1;
- }
- char *buffer = (char *)calloc((size_t) file_len, sizeof(char));
- if (!buffer) {
- fclose(fp);
- return -1;
- }
- size_t pattern_len = strlen(pattern);
-
- fread(buffer, (size_t) file_len, sizeof(char), fp);
- fclose(fp);
-
- for (size_t i = 0; i < (size_t) file_len; i++) {
- if (memcmp(&buffer[i], pattern, pattern_len) == 0) {
- result = 0; // found
- break;
- }
- }
- free(buffer);
- return result;
-}
-
-/**
- * Get the full path of a shell command
- * @param program
- * @return success=absolute path to program, failure=NULL
- */
-char *find_executable(const char *program) {
- int found = 0;
- char *result = NULL;
- char *env_path = NULL;
- env_path = getenv("PATH");
- if (!env_path) {
- return NULL;
- }
- char **search_paths = split(env_path, ":");
-
- char buf[PATH_MAX];
- for (int i = 0; search_paths[i] != NULL; i++) {
- sprintf(buf, "%s%c%s", search_paths[i], DIRSEP, program);
- if (access(buf, F_OK | X_OK) == 0) {
- found = 1;
- break;
- }
- memset(buf, '\0', sizeof(buf));
- }
- if (found) {
- result = strdup(buf);
- }
- split_free(search_paths);
- return result;
-}
diff --git a/lib/fs.c b/lib/fs.c
index 0cdda41..98a18e6 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -764,4 +764,74 @@ char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn
fclose(fp);
}
return result;
-} \ No newline at end of file
+}
+
+/**
+ * Determine whether `pattern` is present within a file
+ * @param filename
+ * @param pattern
+ * @return 0=found, 1=not found, -1=OS error
+ */
+int find_in_file(const char *filename, const char *pattern) {
+ int result = 1; // default "not found"
+
+ FILE *fp = fopen(filename, "rb");
+ if (!fp) {
+ return -1;
+ }
+
+ long int file_len = get_file_size(filename);
+ if (file_len == -1) {
+ fclose(fp);
+ return -1;
+ }
+ char *buffer = (char *)calloc((size_t) file_len, sizeof(char));
+ if (!buffer) {
+ fclose(fp);
+ return -1;
+ }
+ size_t pattern_len = strlen(pattern);
+
+ fread(buffer, (size_t) file_len, sizeof(char), fp);
+ fclose(fp);
+
+ for (size_t i = 0; i < (size_t) file_len; i++) {
+ if (memcmp(&buffer[i], pattern, pattern_len) == 0) {
+ result = 0; // found
+ break;
+ }
+ }
+ free(buffer);
+ return result;
+}
+
+/**
+ * Get the full path of a shell command
+ * @param program
+ * @return success=absolute path to program, failure=NULL
+ */
+char *find_executable(const char *program) {
+ int found = 0;
+ char *result = NULL;
+ char *env_path = NULL;
+ env_path = getenv("PATH");
+ if (!env_path) {
+ return NULL;
+ }
+ char **search_paths = split(env_path, ":");
+
+ char buf[PATH_MAX];
+ for (int i = 0; search_paths[i] != NULL; i++) {
+ sprintf(buf, "%s%c%s", search_paths[i], DIRSEP, program);
+ if (access(buf, F_OK | X_OK) == 0) {
+ found = 1;
+ break;
+ }
+ memset(buf, '\0', sizeof(buf));
+ }
+ if (found) {
+ result = strdup(buf);
+ }
+ split_free(search_paths);
+ return result;
+}