summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorKiyoshi Aman <kiyoshi.aman+adelie@gmail.com>2019-05-31 09:03:54 -0500
committerKiyoshi Aman <kiyoshi.aman+adelie@gmail.com>2019-05-31 09:03:54 -0500
commitd8d29173662d45e84f14f75d8cb98f0735f7ae1a (patch)
tree7116fd5f76f05ed0386437144c2b3e7e1b6aba6a /usr.bin
parent96e69a56f752717e5036e8e362584205a6ecfef1 (diff)
downloaduserland-d8d29173662d45e84f14f75d8cb98f0735f7ae1a.tar.gz
userland-d8d29173662d45e84f14f75d8cb98f0735f7ae1a.tar.bz2
userland-d8d29173662d45e84f14f75d8cb98f0735f7ae1a.tar.xz
userland-d8d29173662d45e84f14f75d8cb98f0735f7ae1a.zip
usr.bin/find: make 99% buildable; needs parsedate() or equivalent
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/find/extern.h3
-rw-r--r--usr.bin/find/find.c13
-rw-r--r--usr.bin/find/function.c193
-rw-r--r--usr.bin/find/ls.c35
-rw-r--r--usr.bin/find/main.c16
-rw-r--r--usr.bin/find/misc.c9
-rw-r--r--usr.bin/find/operator.c9
-rw-r--r--usr.bin/find/option.c10
8 files changed, 90 insertions, 198 deletions
diff --git a/usr.bin/find/extern.h b/usr.bin/find/extern.h
index 5c94035..825351e 100644
--- a/usr.bin/find/extern.h
+++ b/usr.bin/find/extern.h
@@ -31,8 +31,6 @@
* from: @(#)extern.h 8.3 (Berkeley) 4/16/94
*/
-#include <sys/cdefs.h>
-
void brace_subst(char *, char **, char *, int *);
PLAN *find_create(char ***);
int find_execute(PLAN *, char **);
@@ -62,7 +60,6 @@ PLAN *c_exec(char ***, int, char *);
PLAN *c_execdir(char ***, int, char *);
PLAN *c_exit(char ***, int, char *);
PLAN *c_false(char ***, int, char *);
-PLAN *c_flags(char ***, int, char *);
PLAN *c_follow(char ***, int, char *);
PLAN *c_fprint(char ***, int, char *);
PLAN *c_fstype(char ***, int, char *);
diff --git a/usr.bin/find/find.c b/usr.bin/find/find.c
index 473244b..7b966bd 100644
--- a/usr.bin/find/find.c
+++ b/usr.bin/find/find.c
@@ -32,15 +32,6 @@
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "from: @(#)find.c 8.5 (Berkeley) 8/5/94";
-#else
-__RCSID("$NetBSD: find.c,v 1.30 2016/06/13 00:04:40 pgoyette Exp $");
-#endif
-#endif /* not lint */
-
#include <sys/types.h>
#include <sys/stat.h>
@@ -165,12 +156,12 @@ sig_init(void)
if (notty)
return;
sigemptyset(&ss);
- sigaddset(&ss, SIGINFO); /* block SIGINFO */
+ sigaddset(&ss, SIGHUP); /* block SIGINFO */
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_RESTART;
sa.sa_handler = show_path;
- (void)sigaction(SIGINFO, &sa, NULL);
+ (void)sigaction(SIGHUP, &sa, NULL);
}
diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c
index 02540f2..be880e9 100644
--- a/usr.bin/find/function.c
+++ b/usr.bin/find/function.c
@@ -32,19 +32,15 @@
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "from: @(#)function.c 8.10 (Berkeley) 5/4/95";
-#else
-__RCSID("$NetBSD: function.c,v 1.77 2018/09/04 15:16:15 kre Exp $");
-#endif
-#endif /* not lint */
-
+#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/stat.h>
+#include <sys/statvfs.h>
#include <sys/wait.h>
#include <sys/mount.h>
+#include <bsd/sys/time.h>
+
+#include <linux/fs.h>
#include <dirent.h>
#include <err.h>
@@ -54,14 +50,14 @@ __RCSID("$NetBSD: function.c,v 1.77 2018/09/04 15:16:15 kre Exp $");
#include <grp.h>
#include <inttypes.h>
#include <limits.h>
+#include <mntent.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <tzfile.h>
#include <unistd.h>
-#include <util.h>
+#include <bsd/unistd.h>
#include "find.h"
@@ -181,7 +177,7 @@ find_parsenum(PLAN *plan, const char *option, const char *vp, char *endch)
* and endchar points to the beginning of the string we know we have
* a syntax error.
*/
- value = strtoq(str, &endchar, 10);
+ value = strtoll(str, &endchar, 10);
if (value == 0 && endchar == str)
errx(1, "%s: %s: illegal numeric value", option, vp);
if (endchar[0] && (endch == NULL || endchar[0] != *endch))
@@ -227,6 +223,7 @@ find_parsedate(PLAN *plan, const char *option, const char *vp)
int
f_amin(PLAN *plan, FTSENT *entry)
{
+#define SECSPERMIN 60
COMPARE((now - entry->fts_statp->st_atime +
SECSPERMIN - 1) / SECSPERMIN, plan->t_data);
}
@@ -312,6 +309,7 @@ c_asince(char ***argvp, int isok, char *opt)
int
f_atime(PLAN *plan, FTSENT *entry)
{
+#define SECSPERDAY 24*60*60
COMPARE((now - entry->fts_statp->st_atime +
SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
}
@@ -450,8 +448,10 @@ c_ctime(char ***argvp, int isok, char *opt)
* Always true. Makes its best shot and continues on regardless.
*/
int
-f_delete(PLAN *plan __unused, FTSENT *entry)
+f_delete(PLAN *plan, FTSENT *entry)
{
+ int flags;
+ FILE *file;
/* ignore these from fts */
if (strcmp(entry->fts_accpath, ".") == 0 ||
strcmp(entry->fts_accpath, "..") == 0)
@@ -468,13 +468,18 @@ f_delete(PLAN *plan __unused, FTSENT *entry)
if (entry->fts_level > 0 && strchr(entry->fts_accpath, '/') != NULL)
errx(1, "-delete: %s: relative path potentially not safe",
entry->fts_accpath);
-
- /* Turn off user immutable bits if running as root */
- if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
- !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
- geteuid() == 0)
- chflags(entry->fts_accpath,
- entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
+
+ file = fopen(entry->fts_name, "r");
+ if (file == NULL) {
+ warn("couldn't check flags for %s", entry->fts_name);
+ } else {
+ ioctl(fileno(file), FS_IOC_GETFLAGS, &flags);
+ /* turn off immutable bit if running as root */
+ if ((geteuid() == 0) && (flags & FS_IMMUTABLE_FL)) {
+ flags = flags & ~FS_IMMUTABLE_FL;
+ ioctl(fileno(file), FS_IOC_SETFLAGS, &flags);
+ }
+ }
/* rmdir directories, unlink everything else */
if (S_ISDIR(entry->fts_statp->st_mode)) {
@@ -490,7 +495,7 @@ f_delete(PLAN *plan __unused, FTSENT *entry)
}
PLAN *
-c_delete(char ***argvp __unused, int isok, char *opt)
+c_delete(char ***argvp, int isok, char *opt)
{
ftsoptions &= ~FTS_NOSTAT; /* no optimize */
@@ -762,7 +767,7 @@ c_exec(char ***argvp, int isok, char *opt)
new->ep_maxargs = ARG_MAX / (sizeof (char *) + 16);
if (new->ep_maxargs > 5000)
new->ep_maxargs = 5000;
- new->e_argv = emalloc((cnt + new->ep_maxargs)
+ new->e_argv = malloc((cnt + new->ep_maxargs)
* sizeof(*new->e_argv));
/* We start stuffing arguments after the user's last one. */
@@ -790,21 +795,21 @@ c_exec(char ***argvp, int isok, char *opt)
* Allocate, and then initialize current, base, and
* end pointers.
*/
- new->ep_p = new->ep_bbp = emalloc(bufsize + 1);
+ new->ep_p = new->ep_bbp = malloc(bufsize + 1);
new->ep_ebp = new->ep_bbp + bufsize - 1;
new->ep_rval = 0;
} else { /* !F_PLUSSET */
cnt = ap - *argvp + 1;
- new->e_argv = emalloc(cnt * sizeof(*new->e_argv));
- new->e_orig = emalloc(cnt * sizeof(*new->e_orig));
- new->e_len = emalloc(cnt * sizeof(*new->e_len));
+ new->e_argv = malloc(cnt * sizeof(*new->e_argv));
+ new->e_orig = malloc(cnt * sizeof(*new->e_orig));
+ new->e_len = malloc(cnt * sizeof(*new->e_len));
for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
new->e_orig[cnt] = *argv;
for (p = *argv; *p; ++p)
if (p[0] == '{' && p[1] == '}') {
new->e_argv[cnt] =
- emalloc(MAXPATHLEN);
+ malloc(MAXPATHLEN);
new->e_len[cnt] = MAXPATHLEN;
break;
}
@@ -893,15 +898,15 @@ c_execdir(char ***argvp, int isok, char *opt)
}
cnt = ap - *argvp + 1;
- new->e_argv = emalloc(cnt * sizeof(*new->e_argv));
- new->e_orig = emalloc(cnt * sizeof(*new->e_orig));
- new->e_len = emalloc(cnt * sizeof(*new->e_len));
+ new->e_argv = malloc(cnt * sizeof(*new->e_argv));
+ new->e_orig = malloc(cnt * sizeof(*new->e_orig));
+ new->e_len = malloc(cnt * sizeof(*new->e_len));
for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
new->e_orig[cnt] = *argv;
for (p = *argv; *p; ++p)
if (p[0] == '{' && p[1] == '}') {
- new->e_argv[cnt] = emalloc(MAXPATHLEN);
+ new->e_argv[cnt] = malloc(MAXPATHLEN);
new->e_len[cnt] = MAXPATHLEN;
break;
}
@@ -953,48 +958,6 @@ c_false(char ***argvp, int isok, char *opt)
return (palloc(N_FALSE, f_false));
}
-
-/*
- * -flags [-]flags functions --
- */
-int
-f_flags(PLAN *plan, FTSENT *entry)
-{
- u_int32_t flags;
-
- flags = entry->fts_statp->st_flags;
- if (plan->flags == F_ATLEAST)
- return ((plan->f_data | flags) == flags);
- else
- return (flags == plan->f_data);
- /* NOTREACHED */
-}
-
-PLAN *
-c_flags(char ***argvp, int isok, char *opt)
-{
- char *flags = **argvp;
- PLAN *new;
- u_long flagset;
-
- (*argvp)++;
- ftsoptions &= ~FTS_NOSTAT;
-
- new = palloc(N_FLAGS, f_flags);
-
- if (*flags == '-') {
- new->flags = F_ATLEAST;
- ++flags;
- }
-
- flagset = 0;
- if ((strcmp(flags, "none") != 0) &&
- (string_to_flags(&flags, &flagset, NULL) != 0))
- errx(1, "%s: %s: illegal flags string", opt, flags);
- new->f_data = flagset;
- return (new);
-}
-
/*
* -follow functions --
*
@@ -1042,7 +1005,7 @@ c_fprint(char ***argvp, int isok, char *opt)
(*argvp)++;
return (new);
}
-
+#define MNT_RDONLY 0x01
/*
* -fstype functions --
*
@@ -1051,57 +1014,33 @@ c_fprint(char ***argvp, int isok, char *opt)
int
f_fstype(PLAN *plan, FTSENT *entry)
{
- static dev_t curdev; /* need a guaranteed illegal dev value */
- static int first = 1;
- struct statvfs sb;
- static short val;
- static char fstype[sizeof(sb.f_fstypename)];
- char *p, save[2];
-
- memset(&save, 0, sizeof save); /* XXX gcc */
-
- /* Only check when we cross mount point. */
- if (first || curdev != entry->fts_statp->st_dev) {
- curdev = entry->fts_statp->st_dev;
+ static char *fstype = NULL;
+ FILE *file;
+ struct mntent *mnt;
+ int flags;
+ char *filename = strdup(entry->fts_name);
+ char *temp = strrchr(filename, '/');
+ temp[0] = '\0';
+
+ file = setmntent("/etc/mtab", "r");
+ if (file == NULL) {
+ err(1, "couldn't read mount table");
+ }
- /*
- * Statfs follows symlinks; find wants the link's file system,
- * not where it points.
- */
- if (entry->fts_info == FTS_SL ||
- entry->fts_info == FTS_SLNONE) {
- if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
- ++p;
- else
- p = entry->fts_accpath;
- save[0] = p[0];
- p[0] = '.';
- save[1] = p[1];
- p[1] = '\0';
-
- } else
- p = NULL;
-
- if (statvfs(entry->fts_accpath, &sb))
- err(1, "%s", entry->fts_accpath);
-
- if (p) {
- p[0] = save[0];
- p[1] = save[1];
+ while ((mnt = getmntent(file)) != NULL) {
+ if (!strncmp(mnt->mnt_dir, filename, strlen(mnt->mnt_dir))) {
+ fstype = strdup(mnt->mnt_type);
+ if (strstr(mnt->mnt_opts, MNTOPT_RO) != NULL) {
+ flags |= MNT_RDONLY;
+ }
+ break;
}
-
- first = 0;
-
- /*
- * Further tests may need both of these values, so
- * always copy both of them.
- */
- val = sb.f_flag;
- strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
}
+ endmntent(file);
+
switch (plan->flags) {
case F_MTFLAG:
- return (val & plan->mt_data);
+ return (flags & plan->mt_data);
case F_MTTYPE:
return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
default:
@@ -1121,13 +1060,6 @@ c_fstype(char ***argvp, int isok, char *opt)
new = palloc(N_FSTYPE, f_fstype);
switch (*arg) {
- case 'l':
- if (!strcmp(arg, "local")) {
- new->flags = F_MTFLAG;
- new->mt_data = MNT_LOCAL;
- return (new);
- }
- break;
case 'r':
if (!strcmp(arg, "rdonly")) {
new->flags = F_MTFLAG;
@@ -1320,6 +1252,7 @@ c_mindepth(char ***argvp, int isok, char *opt)
int
f_mmin(PLAN *plan, FTSENT *entry)
{
+#define SECSPERMIN 60
COMPARE((now - entry->fts_statp->st_mtime + SECSPERMIN - 1) /
SECSPERMIN, plan->t_data);
}
@@ -1456,8 +1389,7 @@ c_newer(char ***argvp, int isok, char *opt)
int
f_nogroup(PLAN *plan, FTSENT *entry)
{
-
- return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
+ return getgrgid(entry->fts_statp->st_gid) != NULL;
}
PLAN *
@@ -1477,8 +1409,7 @@ c_nogroup(char ***argvp, int isok, char *opt)
int
f_nouser(PLAN *plan, FTSENT *entry)
{
-
- return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
+ return getpwuid(entry->fts_statp->st_uid) != NULL;
}
PLAN *
diff --git a/usr.bin/find/ls.c b/usr.bin/find/ls.c
index 23b7cfe..14ce841 100644
--- a/usr.bin/find/ls.c
+++ b/usr.bin/find/ls.c
@@ -29,14 +29,6 @@
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "from: @(#)ls.c 8.1 (Berkeley) 6/6/93";
-#else
-__RCSID("$NetBSD: ls.c,v 1.21 2011/08/31 16:24:57 plunky Exp $");
-#endif
-#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
@@ -49,8 +41,8 @@ __RCSID("$NetBSD: ls.c,v 1.21 2011/08/31 16:24:57 plunky Exp $");
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <bsd/string.h>
#include <time.h>
-#include <tzfile.h>
#include <unistd.h>
#include "find.h"
@@ -66,13 +58,28 @@ printlong(char *name, /* filename to print */
struct stat *sb) /* stat buffer */
{
char modep[15];
+ struct passwd *user = getpwuid(sb->st_uid);
+ struct group *group = getgrgid(sb->st_gid);
+ char *username, *groupname;
+
+ if (user == NULL) {
+ username = malloc(21);
+ snprintf(username, 20, "%d", sb->st_uid);
+ } else {
+ username = user->pw_name;
+ }
+ if (group == NULL) {
+ groupname = malloc(21);
+ snprintf(groupname, 20, "%d", sb->st_gid);
+ } else {
+ groupname = group->gr_name;
+ }
(void)printf("%7lu %6lld ", (u_long)sb->st_ino,
(long long)sb->st_blocks);
(void)strmode(sb->st_mode, modep);
(void)printf("%s %3lu %-*s %-*s ", modep, (unsigned long)sb->st_nlink,
- LOGIN_NAME_MAX, user_from_uid(sb->st_uid, 0), LOGIN_NAME_MAX,
- group_from_gid(sb->st_gid, 0));
+ LOGIN_NAME_MAX, username, LOGIN_NAME_MAX, groupname);
if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
(void)printf("%3llu,%5llu ",
@@ -85,6 +92,9 @@ printlong(char *name, /* filename to print */
if (S_ISLNK(sb->st_mode))
printlink(accpath);
(void)putchar('\n');
+
+ if (user == NULL) free(username);
+ if (group == NULL) free(groupname);
}
static void
@@ -96,7 +106,8 @@ printtime(time_t ftime)
longstring = ctime(&ftime);
for (i = 4; i < 11; ++i)
(void)putchar(longstring[i]);
-
+#define DAYSPERNYEAR 365
+#define SECSPERDAY 24 * 60 * 60
#define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
if (ftime + SIXMONTHS > time(NULL))
for (i = 11; i < 16; ++i)
diff --git a/usr.bin/find/main.c b/usr.bin/find/main.c
index 5aab177..6ff7e3c 100644
--- a/usr.bin/find/main.c
+++ b/usr.bin/find/main.c
@@ -32,17 +32,6 @@
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 5/4/95";
-#else
-__COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\
- The Regents of the University of California. All rights reserved.");
-__RCSID("$NetBSD: main.c,v 1.31 2013/01/24 17:50:08 christos Exp $");
-#endif
-#endif /* not lint */
-
#include <sys/types.h>
#include <sys/stat.h>
@@ -53,6 +42,7 @@ __RCSID("$NetBSD: main.c,v 1.31 2013/01/24 17:50:08 christos Exp $");
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
+#include <bsd/stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
@@ -67,9 +57,9 @@ int isdepth; /* do directories on post-order visit */
int isoutput; /* user specified output operator */
int issort; /* sort directory entries */
int isxargs; /* don't permit xargs delimiting chars */
-int regcomp_flags = REG_BASIC; /* regex compilation flags */
+int regcomp_flags = 0; /* regex compilation flags */
-__dead static void usage(void);
+static void usage(void);
int
main(int argc, char *argv[])
diff --git a/usr.bin/find/misc.c b/usr.bin/find/misc.c
index 8ba84c0..a6401cb 100644
--- a/usr.bin/find/misc.c
+++ b/usr.bin/find/misc.c
@@ -32,15 +32,6 @@
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "from: @(#)misc.c 8.2 (Berkeley) 4/1/94";
-#else
-__RCSID("$NetBSD: misc.c,v 1.14 2006/10/11 19:51:10 apb Exp $");
-#endif
-#endif /* not lint */
-
#include <sys/types.h>
#include <sys/stat.h>
diff --git a/usr.bin/find/operator.c b/usr.bin/find/operator.c
index 63a6aa4..e65677b 100644
--- a/usr.bin/find/operator.c
+++ b/usr.bin/find/operator.c
@@ -32,15 +32,6 @@
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "from: @(#)operator.c 8.1 (Berkeley) 6/6/93";
-#else
-__RCSID("$NetBSD: operator.c,v 1.10 2014/10/18 08:33:30 snj Exp $");
-#endif
-#endif /* not lint */
-
#include <sys/types.h>
#include <err.h>
diff --git a/usr.bin/find/option.c b/usr.bin/find/option.c
index 0af4447..0834c27 100644
--- a/usr.bin/find/option.c
+++ b/usr.bin/find/option.c
@@ -32,15 +32,6 @@
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "from: @(#)option.c 8.2 (Berkeley) 4/16/94";
-#else
-__RCSID("$NetBSD: option.c,v 1.27 2016/06/13 00:04:40 pgoyette Exp $");
-#endif
-#endif /* not lint */
-
#include <sys/types.h>
#include <sys/stat.h>
@@ -77,7 +68,6 @@ static OPTION const options[] = {
{ "-execdir", N_EXECDIR, c_execdir, 1 },
{ "-exit", N_EXIT, c_exit, 0 },
{ "-false", N_FALSE, c_false, 0 },
- { "-flags", N_FLAGS, c_flags, 1 },
{ "-follow", N_FOLLOW, c_follow, 0 },
{ "-fprint", N_FPRINT, c_fprint, 1 },
{ "-fstype", N_FSTYPE, c_fstype, 1 },