consistent error check

This commit is contained in:
Connor Lane Smith 2011-06-04 12:20:41 +01:00
parent de221bc6f5
commit d90ced2047
13 changed files with 22 additions and 22 deletions

View File

@ -53,7 +53,7 @@ main(int argc, char *argv[])
void
chmodr(const char *path)
{
if(chmod(path, mode) != 0)
if(chmod(path, mode) == -1)
eprintf("chmod %s:", path);
if(rflag)
recurse(path, chmodr);

View File

@ -37,7 +37,7 @@ main(int argc, char *argv[])
if(owner && *owner) {
errno = 0;
pw = getpwnam(owner);
if(errno != 0)
if(errno == -1)
eprintf("getpwnam %s:", owner);
else if(!pw)
eprintf("getpwnam %s: no such user\n", owner);
@ -45,7 +45,7 @@ main(int argc, char *argv[])
if(group && *group) {
errno = 0;
gr = getgrnam(group);
if(errno != 0)
if(errno == -1)
eprintf("getgrnam %s:", group);
else if(!gr)
eprintf("getgrnam %s: no such group\n", group);
@ -58,7 +58,7 @@ main(int argc, char *argv[])
void
chownpwgr(const char *path)
{
if(chown(path, pw ? pw->pw_uid : -1, gr ? gr->gr_gid : -1) != 0)
if(chown(path, pw ? pw->pw_uid : -1, gr ? gr->gr_gid : -1) == -1)
eprintf("chown %s:", path);
if(rflag)
recurse(path, chownpwgr);

2
ln.c
View File

@ -34,7 +34,7 @@ main(int argc, char *argv[])
int
ln(const char *s1, const char *s2)
{
if(fflag && remove(s2) != 0 && errno != ENOENT)
if(fflag && remove(s2) == -1 && errno != ENOENT)
eprintf("remove %s:", s2);
return (sflag ? symlink : link)(s1, s2);
}

6
ls.c
View File

@ -99,7 +99,7 @@ lsdir(const char *path)
cwd = agetcwd();
if(!(dp = opendir(path)))
eprintf("opendir %s:", path);
if(chdir(path) != 0)
if(chdir(path) == -1)
eprintf("chdir %s:", path);
while((d = readdir(dp))) {
@ -125,7 +125,7 @@ lsdir(const char *path)
output(&ents[i]);
free(ents[i].name);
}
if(chdir(cwd) != 0)
if(chdir(cwd) == -1)
eprintf("chdir %s:", cwd);
free(ents);
free(cwd);
@ -136,7 +136,7 @@ mkent(Entry *ent, char *path)
{
struct stat st;
if(lstat(path, &st) != 0)
if(lstat(path, &st) == -1)
eprintf("lstat %s:", path);
ent->name = path;
ent->mode = st.st_mode;

View File

@ -27,7 +27,7 @@ main(int argc, char *argv[])
for(; optind < argc; optind++)
if(pflag)
mkdirp(argv[optind]);
else if(mkdir(argv[optind], S_IRWXU|S_IRWXG|S_IRWXO) != 0)
else if(mkdir(argv[optind], S_IRWXU|S_IRWXG|S_IRWXO) == -1)
eprintf("mkdir %s:", argv[optind]);
return EXIT_SUCCESS;
}
@ -40,7 +40,7 @@ mkdirp(char *path)
do {
if((p = strchr(&p[1], '/')))
*p = '\0';
if(mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO) != 0 && errno != EEXIST)
if(mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO) == -1 && errno != EEXIST)
eprintf("mkdir %s:", path);
if(p)
*p = '/';

View File

@ -11,7 +11,7 @@ main(int argc, char *argv[])
while(getopt(argc, argv, "") != -1)
exit(EXIT_FAILURE);
for(; optind < argc; optind++)
if(mkfifo(argv[optind], S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) != 0)
if(mkfifo(argv[optind], S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) == -1)
eprintf("mkfifo %s:", argv[optind]);
return EXIT_SUCCESS;
}

4
pwd.c
View File

@ -33,9 +33,9 @@ getpwd(const char *cwd)
const char *pwd;
struct stat cst, pst;
if(!(pwd = getenv("PWD")) || pwd[0] != '/' || stat(pwd, &pst) != 0)
if(!(pwd = getenv("PWD")) || pwd[0] != '/' || stat(pwd, &pst) == -1)
return cwd;
if(stat(cwd, &cst) != 0)
if(stat(cwd, &cst) == -1)
eprintf("stat %s:", cwd);
if(pst.st_dev == cst.st_dev && pst.st_ino == cst.st_ino)
return pwd;

2
rm.c
View File

@ -36,6 +36,6 @@ rm(const char *path)
{
if(rflag)
recurse(path, rm);
if(remove(path) != 0 && !fflag)
if(remove(path) == -1 && !fflag)
eprintf("remove %s:", path);
}

2
sort.c
View File

@ -44,7 +44,7 @@ main(int argc, char *argv[])
qsort(lines, nlines, sizeof *lines, (int (*)(const void *, const void *))linecmp);
for(i = 0; i < nlines; i++)
if(!uflag || i == 0 || strcmp(lines[i], lines[i-1]) != 0)
if(!uflag || i == 0 || strcmp(lines[i], lines[i-1]) == -1)
fputs(lines[i], stdout);
return EXIT_SUCCESS;
}

View File

@ -45,7 +45,7 @@ touch(const char *str)
struct stat st;
struct utimbuf ut;
if(stat(str, &st) != 0) {
if(stat(str, &st) == -1) {
if(errno != ENOENT)
eprintf("stat %s:", str);
if(cflag)
@ -56,6 +56,6 @@ touch(const char *str)
}
ut.actime = st.st_atime;
ut.modtime = t;
if(utime(str, &ut) != 0)
if(utime(str, &ut) == -1)
eprintf("utime %s:", str);
}

View File

@ -40,7 +40,7 @@ main(int argc, char *argv[])
default:
exit(EXIT_FAILURE);
}
if(uname(&u) != 0)
if(uname(&u) == -1)
eprintf("uname:");
if(sflag || !(nflag || rflag || vflag || mflag))

View File

@ -16,7 +16,7 @@ enmasse(int argc, char **argv, int (*fn)(const char *, const char *))
struct stat st;
if(argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) {
if(fn(argv[0], argv[1]) != 0)
if(fn(argv[0], argv[1]) == -1)
eprintf("%s:", argv[1]);
return;
}
@ -31,7 +31,7 @@ enmasse(int argc, char **argv, int (*fn)(const char *, const char *))
eprintf("malloc:");
for(i = 0; i < argc; i++) {
snprintf(buf, size, "%s/%s", dir, basename(argv[i]));
if(fn(argv[i], buf) != 0)
if(fn(argv[i], buf) == -1)
eprintf("%s:", buf);
}
free(buf);

View File

@ -20,14 +20,14 @@ recurse(const char *path, void (*fn)(const char *))
eprintf("opendir %s:", path);
}
cwd = agetcwd();
if(chdir(path) != 0)
if(chdir(path) == -1)
eprintf("chdir %s:", path);
while((d = readdir(dp)))
if(strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
fn(d->d_name);
closedir(dp);
if(chdir(cwd) != 0)
if(chdir(cwd) == -1)
eprintf("chdir %s:", cwd);
free(cwd);
}