aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjozan <jozan@noemail.net>2020-11-15 18:22:15 +0000
committerjozan <jozan@noemail.net>2020-11-15 18:22:15 +0000
commitec63698ce2acba9c5e8040c32a0317ca1037b124 (patch)
tree127e0122010b71acbc526741cd645a62ce08b1de
parentCreate new branch named "no-chdir" (diff)
downloadunixize-no-chdir.tar.gz
unixize-no-chdir.tar.bz2
unixize-no-chdir.tar.xz
unixize-no-chdir.tar.zst
unixize-no-chdir.zip
no chdir, yeyeno-chdir
FossilOrigin-Name: fb7c8f30bd334f8b102126291f7a115647566f90c0a5e8212beac3504599f307
-rw-r--r--src/c_lfiles.c8
-rw-r--r--src/c_lfiles.h2
-rw-r--r--src/c_unixize.c74
-rw-r--r--src/u_utils.c22
-rw-r--r--src/u_utils.h4
5 files changed, 52 insertions, 58 deletions
diff --git a/src/c_lfiles.c b/src/c_lfiles.c
index b420373..f22fed0 100644
--- a/src/c_lfiles.c
+++ b/src/c_lfiles.c
@@ -116,7 +116,7 @@ c_lfiles_new
}
struct lfiles_s*
-c_lfiles_gather(void)
+c_lfiles_gather(const char dir[])
{
DIR* dirp;
struct dirent* dp;
@@ -125,7 +125,7 @@ c_lfiles_gather(void)
head = NULL;
link = NULL;
- dirp = opendir(".");
+ dirp = opendir(dir);
if (dirp == NULL) {
u_dump_errno();
return (NULL);
@@ -134,12 +134,12 @@ c_lfiles_gather(void)
if (
strncmp(dp->d_name, ".", 2 * sizeof(char)) == 0 ||
strncmp(dp->d_name, "..", 3 * sizeof(char)) == 0
- ) {
+ ) {
continue;
}
link = c_lfiles_new(dp->d_name, dp->d_type);
if (link == NULL) {
- u_dump_errno();
+ u_dump_errno_path(dir);
c_lfiles_clear(&head);
return (NULL);
}
diff --git a/src/c_lfiles.h b/src/c_lfiles.h
index 3dec049..5259833 100644
--- a/src/c_lfiles.h
+++ b/src/c_lfiles.h
@@ -51,6 +51,6 @@
void c_lfiles_add_back(struct lfiles_s**, struct lfiles_s*);
void c_lfiles_clear(struct lfiles_s**);
struct lfiles_s* c_lfiles_new(const char[], unsigned char);
-struct lfiles_s* c_lfiles_gather(void);
+struct lfiles_s* c_lfiles_gather(const char[]);
#endif /* end of include guard: __C_LFILES_H__ */
diff --git a/src/c_unixize.c b/src/c_unixize.c
index 84c4371..c96baea 100644
--- a/src/c_unixize.c
+++ b/src/c_unixize.c
@@ -72,27 +72,24 @@ main
struct lfiles_s* og_files_head;
struct opts_s opts;
int nargc;
+ int tmp;
char** nargv;
+ char og_file[MAXPATHLEN];
+ char new_file[MAXPATHLEN];
static int ret = 0;
- static char subpath[MAXPATHLEN] = "";
+ static char path[MAXPATHLEN] = "";
if (c_get_opts(&opts, argc, argv) == FALSE) {
return (0);
}
- if (chdir((const char*)opts.dir) == -1) {
- u_dump_errno_path(opts.dir);
- return (1);
- }
if (
- argv[0][0] != 'r' &&
- strlen(opts.dir) > 1 &&
- strncmp(opts.dir, ".", 2 * sizeof(char)) != 0
+ argv[0][0] != 'r'
) {
- strlcpy(subpath, opts.dir, MAXPATHLEN - 1);
- subpath[strlen(subpath) + 1] = 0x00;
- subpath[strlen(subpath)] = '/';
+ strlcpy(path, opts.dir, MAXPATHLEN - 1);
+ path[strlen(path) + 1] = 0x00;
+ path[strlen(path)] = '/';
}
- og_files = c_lfiles_gather();
+ og_files = c_lfiles_gather(path);
if (og_files == NULL) {
return (0);
}
@@ -115,23 +112,18 @@ main
continue;
}
if (opts.recursive == TRUE && og_files->filetype == DT_DIR) {
- if (chdir((const char*)og_files->filename) == -1) {
- u_dump_errno_path(og_files->filename);
- }
- else {
- u_increase_subpath(subpath, og_files->filename);
- nargv = u_get_nargv(&opts);
- if (nargv != NULL) {
- nargc = 0;
- while (argv[nargc + 1] != NULL) {
- nargc++;
- }
- ret = main(nargc, (const char**)nargv);
- u_del_nargv(nargv);
+ u_inc_path(path, og_files->filename);
+ nargv = u_get_nargv(&opts);
+ if (nargv != NULL) {
+ nargc = 0;
+ while (argv[nargc + 1] != NULL) {
+ nargc++;
}
- chdir("../");
- u_decrease_subpath(subpath);
+ tmp = main(nargc, (const char**)nargv);
+ ret = (tmp != 0) ? (tmp) : (ret);
+ u_del_nargv(nargv);
}
+ u_dec_path(path);
}
if (
strncmp(
@@ -145,30 +137,32 @@ main
dprintf(
STDOUT_FILENO,
"'%s%s' -> '%s%s'\n",
- subpath,
+ path,
og_files->filename,
- subpath,
+ path,
new_files->filename
);
}
if (opts.pretend == FALSE) {
- /* if (rename(og_files->filename, new_files->filename) == -1) { */
- /* dprintf( */
- /* STDERR_FILENO, */
- /* "unixize: rename %s to %s: %s\n", */
- /* og_files->filename, */
- /* new_files->filename, */
- /* strerror(errno) */
- /* ); */
- /* ret = 2; */
- /* } */
+ sprintf(og_file, "%s%s", path, og_files->filename);
+ sprintf(new_file, "%s%s", path, new_files->filename);
+ if (rename(og_file, new_file) == -1) {
+ dprintf(
+ STDERR_FILENO,
+ "unixize: rename %s to %s: %s\n",
+ og_files->filename,
+ new_files->filename,
+ strerror(errno)
+ );
+ ret = 2;
+ }
}
}
else if (opts.rverbose == TRUE) {
dprintf(
STDOUT_FILENO,
"Untouched: '%s%s'\n",
- subpath,
+ path,
og_files->filename
);
}
diff --git a/src/u_utils.c b/src/u_utils.c
index 8555710..54b7916 100644
--- a/src/u_utils.c
+++ b/src/u_utils.c
@@ -210,37 +210,37 @@ u_get_nargv(struct opts_s* opts)
}
void
-u_increase_subpath
-(char subp[],
+u_inc_path
+(char path[],
const char newp[])
{
strlcpy(
- subp + strlen(subp),
+ path + strlen(path),
newp,
- MAXPATHLEN - strlen(subp) - 1
+ MAXPATHLEN - strlen(path) - 1
);
- subp[strlen(subp) + 1] = 0x00;
- subp[strlen(subp)] = '/';
+ path[strlen(path) + 1] = 0x00;
+ path[strlen(path)] = '/';
}
void
-u_decrease_subpath(char subp[])
+u_dec_path(char path[])
{
char* p;
- p = subp;
- p += strlen(subp);
+ p = path;
+ p += strlen(path);
if (p == 0) {
return;
}
p -= 2;
- while (subp - p != 0 && *p != '/') {
+ while (path - p != 0 && *p != '/') {
p--;
}
if (*p == '/') {
*(p + 1) = 0x00;
}
- else if (subp - p == 0) {
+ else if (path - p == 0) {
*p = 0x00;
}
}
diff --git a/src/u_utils.h b/src/u_utils.h
index c966cf2..da29e9c 100644
--- a/src/u_utils.h
+++ b/src/u_utils.h
@@ -52,7 +52,7 @@ void u_dump_errno(void);
void u_dump_errno_path(const char[]);
void u_del_nargv(char** nargv);
char** u_get_nargv(struct opts_s*);
-void u_increase_subpath(char[], const char[]);
-void u_decrease_subpath(char[]);
+void u_inc_path(char[], const char[]);
+void u_dec_path(char[]);
bool_t u_ischarset(const int, const char[]);
bool_t u_isucharset(const unsigned char, const char[]);