ln: Use stdbool, style nits.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	imp, allanjude
Differential Revision:	https://reviews.freebsd.org/D44511
This commit is contained in:
Dag-Erling Smørgrav 2024-03-27 11:03:49 +01:00
parent 74a4aa9b15
commit 437d53daf7

View file

@ -37,24 +37,25 @@
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h> #include <libgen.h>
#include <limits.h> #include <limits.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
static int fflag; /* Unlink existing files. */ static bool fflag; /* Unlink existing files. */
static int Fflag; /* Remove empty directories also. */ static bool Fflag; /* Remove empty directories also. */
static int hflag; /* Check new name for symlink first. */ static bool hflag; /* Check new name for symlink first. */
static int iflag; /* Interactive mode. */ static bool iflag; /* Interactive mode. */
static int Pflag; /* Create hard links to symlinks. */ static bool Pflag; /* Create hard links to symlinks. */
static int sflag; /* Symbolic, not hard, link. */ static bool sflag; /* Symbolic, not hard, link. */
static int vflag; /* Verbose output. */ static bool vflag; /* Verbose output. */
static int wflag; /* Warn if symlink target does not static bool wflag; /* Warn if symlink target does not
* exist, and -f is not enabled. */ * exist, and -f is not enabled. */
static char linkch; static char linkch;
static int linkit(const char *, const char *, int); static int linkit(const char *, const char *, bool);
static void usage(void); static void usage(void) __dead2;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -79,41 +80,41 @@ main(int argc, char *argv[])
argv += optind; argv += optind;
if (argc != 2) if (argc != 2)
usage(); usage();
exit(linkit(argv[0], argv[1], 0)); exit(linkit(argv[0], argv[1], false));
} }
while ((ch = getopt(argc, argv, "FLPfhinsvw")) != -1) while ((ch = getopt(argc, argv, "FLPfhinsvw")) != -1)
switch (ch) { switch (ch) {
case 'F': case 'F':
Fflag = 1; Fflag = true;
break; break;
case 'L': case 'L':
Pflag = 0; Pflag = false;
break; break;
case 'P': case 'P':
Pflag = 1; Pflag = true;
break; break;
case 'f': case 'f':
fflag = 1; fflag = true;
iflag = 0; iflag = false;
wflag = 0; wflag = false;
break; break;
case 'h': case 'h':
case 'n': case 'n':
hflag = 1; hflag = true;
break; break;
case 'i': case 'i':
iflag = 1; iflag = true;
fflag = 0; fflag = false;
break; break;
case 's': case 's':
sflag = 1; sflag = true;
break; break;
case 'v': case 'v':
vflag = 1; vflag = true;
break; break;
case 'w': case 'w':
wflag = 1; wflag = true;
break; break;
case '?': case '?':
default: default:
@ -124,21 +125,21 @@ main(int argc, char *argv[])
argc -= optind; argc -= optind;
linkch = sflag ? '-' : '='; linkch = sflag ? '-' : '=';
if (sflag == 0) if (!sflag)
Fflag = 0; Fflag = false;
if (Fflag == 1 && iflag == 0) { if (Fflag && !iflag) {
fflag = 1; fflag = true;
wflag = 0; /* Implied when fflag != 0 */ wflag = false; /* Implied when fflag is true */
} }
switch(argc) { switch (argc) {
case 0: case 0:
usage(); usage();
/* NOTREACHED */ /* NOTREACHED */
case 1: /* ln source */ case 1: /* ln source */
exit(linkit(argv[0], ".", 1)); exit(linkit(argv[0], ".", true));
case 2: /* ln source target */ case 2: /* ln source target */
exit(linkit(argv[0], argv[1], 0)); exit(linkit(argv[0], argv[1], false));
default: default:
; ;
} }
@ -157,7 +158,7 @@ main(int argc, char *argv[])
if (!S_ISDIR(sb.st_mode)) if (!S_ISDIR(sb.st_mode))
usage(); usage();
for (exitval = 0; *argv != targetdir; ++argv) for (exitval = 0; *argv != targetdir; ++argv)
exitval |= linkit(*argv, targetdir, 1); exitval |= linkit(*argv, targetdir, true);
exit(exitval); exit(exitval);
} }
@ -208,14 +209,15 @@ samedirent(const char *path1, const char *path2)
} }
static int static int
linkit(const char *source, const char *target, int isdir) linkit(const char *source, const char *target, bool isdir)
{ {
struct stat sb;
const char *p;
int ch, exists, first;
char path[PATH_MAX]; char path[PATH_MAX];
char wbuf[PATH_MAX]; char wbuf[PATH_MAX];
char bbuf[PATH_MAX]; char bbuf[PATH_MAX];
struct stat sb;
const char *p;
int ch, first;
bool exists;
if (!sflag) { if (!sflag) {
/* If source doesn't exist, quit now. */ /* If source doesn't exist, quit now. */
@ -278,7 +280,7 @@ linkit(const char *source, const char *target, int isdir)
/* /*
* If the file exists, first check it is not the same directory entry. * If the file exists, first check it is not the same directory entry.
*/ */
exists = !lstat(target, &sb); exists = lstat(target, &sb) == 0;
if (exists) { if (exists) {
if (!sflag && samedirent(source, target)) { if (!sflag && samedirent(source, target)) {
warnx("%s and %s are the same directory entry", warnx("%s and %s are the same directory entry",