[hackers] [sbase] Convert recurse() callback to return int || sin
commit 10c7b55b04e01a77af31ec052f38fd5d74b4645e
Author: sin <sin_AT_2f30.org>
Date: Sun Dec 21 11:45:29 2014 +0000
Convert recurse() callback to return int
Allows utilities to not treat every error as fatal and
to also be able to report the exit status properly.
diff --git a/chgrp.c b/chgrp.c
index 78ee68b..aca7ea9 100644
--- a/chgrp.c
+++ b/chgrp.c
_AT_@ -11,7 +11,6 @@
#include "util.h"
static int gid;
-static int status;
static int rflag;
static struct stat st;
_AT_@ -21,21 +20,23 @@ usage(void)
eprintf("usage: chgrp [-R] groupname file...\n");
}
-static void
+static int
chgrp(const char *path)
{
if (chown(path, st.st_uid, gid) < 0) {
weprintf("chown %s:", path);
- status = 1;
+ return -1;
}
if (rflag)
recurse(path, chgrp);
+ return 0;
}
int
main(int argc, char *argv[])
{
struct group *gr;
+ int ret = 0;
ARGBEGIN {
case 'R':
_AT_@ -59,10 +60,11 @@ main(int argc, char *argv[])
while (*++argv) {
if (stat(*argv, &st) < 0) {
weprintf("stat %s:", *argv);
- status = 1;
+ ret = 1;
continue;
}
- chgrp(*argv);
+ if (chgrp(*argv) < 0)
+ ret = 1;
}
- return status;
+ return ret;
}
diff --git a/chmod.c b/chmod.c
index 9f3ab9e..c8971db 100644
--- a/chmod.c
+++ b/chmod.c
_AT_@ -6,12 +6,11 @@
#include "util.h"
-static void chmodr(const char *);
+static int chmodr(const char *);
-static int rflag = 0;
+static int rflag;
static char *modestr = "";
-static mode_t mask = 0;
-static int ret = 0;
+static mode_t mask;
static void
usage(void)
_AT_@ -22,7 +21,7 @@ usage(void)
int
main(int argc, char *argv[])
{
- int c;
+ int c, ret;
argv0 = argv[0];
while (--argc > 0 && (*++argv)[0] == '-') {
_AT_@ -55,11 +54,12 @@ done:
usage();
for (; argc > 0; argc--, argv++)
- chmodr(argv[0]);
+ if (chmodr(argv[0]) < 0)
+ ret = 1;
return ret;
}
-void
+int
chmodr(const char *path)
{
struct stat st;
_AT_@ -67,15 +67,15 @@ chmodr(const char *path)
if (stat(path, &st) < 0) {
weprintf("stat %s:", path);
- ret = 1;
- return;
+ return - 1;
}
m = parsemode(modestr, st.st_mode, mask);
if (chmod(path, m) < 0) {
weprintf("chmod %s:", path);
- ret = 1;
+ return -1;
}
if (rflag)
recurse(path, chmodr);
+ return 0;
}
diff --git a/chown.c b/chown.c
index 5b6178b..f3ea1bd 100644
--- a/chown.c
+++ b/chown.c
_AT_@ -8,12 +8,11 @@
#include "util.h"
-static void chownpwgr(const char *);
+static int chownpwgr(const char *);
-static int rflag = 0;
+static int rflag;
static uid_t uid = -1;
static gid_t gid = -1;
-static int ret = 0;
static void
usage(void)
_AT_@ -27,6 +26,7 @@ main(int argc, char *argv[])
char *owner, *group, *end;
struct passwd *pw;
struct group *gr;
+ int ret = 0;
ARGBEGIN {
case 'R':
_AT_@ -73,18 +73,19 @@ main(int argc, char *argv[])
}
}
for (; argc > 0; argc--, argv++)
- chownpwgr(argv[0]);
-
+ if (chownpwgr(argv[0]) < 0)
+ ret = 1;
return ret;
}
-void
+int
chownpwgr(const char *path)
{
if (chown(path, uid, gid) < 0) {
weprintf("chown %s:", path);
- ret = 1;
+ return -1;
}
if (rflag)
recurse(path, chownpwgr);
+ return 0;
}
diff --git a/libutil/recurse.c b/libutil/recurse.c
index f9044e9..2535650 100644
--- a/libutil/recurse.c
+++ b/libutil/recurse.c
_AT_@ -11,7 +11,7 @@
#include "../util.h"
void
-recurse(const char *path, void (*fn)(const char *))
+recurse(const char *path, int (*fn)(const char *))
{
char buf[PATH_MAX];
struct dirent *d;
diff --git a/tar.c b/tar.c
index 65898d5..a72c24c 100644
--- a/tar.c
+++ b/tar.c
_AT_@ -45,7 +45,7 @@ static void putoctal(char *, unsigned, int);
static int archive(const char *);
static int unarchive(char *, int, char[Blksiz]);
static int print(char *, int , char[Blksiz]);
-static void c(const char *);
+static int c(const char *);
static void xt(int (*)(char*, int, char[Blksiz]));
static FILE *tarfile;
_AT_@ -309,11 +309,12 @@ print(char * fname, int l, char b[Blksiz])
return 0;
}
-static void
+static int
c(const char * path)
{
archive(path);
recurse(path, c);
+ return 0;
}
static void
diff --git a/util.h b/util.h
index 5fc2642..1f67a68 100644
--- a/util.h
+++ b/util.h
_AT_@ -52,4 +52,4 @@ mode_t getumask(void);
char *humansize(double);
mode_t parsemode(const char *, mode_t, mode_t);
void putword(const char *);
-void recurse(const char *, void (*)(const char *));
+void recurse(const char *, int (*)(const char *));
Received on Sun Dec 21 2014 - 12:47:20 CET
This archive was generated by hypermail 2.3.0
: Sun Dec 21 2014 - 12:48:10 CET