2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1997-03-11 13:08:12 +00:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Case Larsen.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1997-03-11 13:08:12 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-08-21 06:51:10 +00:00
|
|
|
static const char copyright[] =
|
1997-03-11 13:08:12 +00:00
|
|
|
"@(#) Copyright (c) 1989, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-08-21 06:51:10 +00:00
|
|
|
#if 0
|
1997-03-11 13:08:12 +00:00
|
|
|
static char sccsid[] = "@(#)uniq.c 8.3 (Berkeley) 5/4/95";
|
1997-08-21 06:51:10 +00:00
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
1999-08-28 01:08:13 +00:00
|
|
|
"$FreeBSD$";
|
1997-03-11 13:08:12 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2014-03-16 11:04:44 +00:00
|
|
|
#include <sys/capsicum.h>
|
2013-07-18 22:11:27 +00:00
|
|
|
|
2018-06-19 23:43:14 +00:00
|
|
|
#include <capsicum_helpers.h>
|
1997-03-11 13:08:12 +00:00
|
|
|
#include <ctype.h>
|
1997-08-21 06:51:10 +00:00
|
|
|
#include <err.h>
|
2013-07-18 22:11:27 +00:00
|
|
|
#include <errno.h>
|
2018-05-02 01:17:08 +00:00
|
|
|
#include <getopt.h>
|
1999-10-24 04:08:15 +00:00
|
|
|
#include <limits.h>
|
1998-03-08 20:56:43 +00:00
|
|
|
#include <locale.h>
|
2013-07-18 22:11:27 +00:00
|
|
|
#include <nl_types.h>
|
2009-12-17 17:03:47 +00:00
|
|
|
#include <stdint.h>
|
1997-08-21 06:51:10 +00:00
|
|
|
#include <stdio.h>
|
1997-03-11 13:08:12 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-07-18 22:11:27 +00:00
|
|
|
#include <termios.h>
|
1997-03-11 13:08:12 +00:00
|
|
|
#include <unistd.h>
|
2004-07-02 23:43:05 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
1997-03-11 13:08:12 +00:00
|
|
|
|
2011-11-06 08:18:11 +00:00
|
|
|
static int cflag, dflag, uflag, iflag;
|
|
|
|
static int numchars, numfields, repeats;
|
|
|
|
|
2018-05-02 01:17:08 +00:00
|
|
|
static const struct option long_opts[] =
|
|
|
|
{
|
|
|
|
{"count", no_argument, NULL, 'c'},
|
|
|
|
{"repeated", no_argument, NULL, 'd'},
|
|
|
|
{"skip-fields", required_argument, NULL, 'f'},
|
|
|
|
{"ignore-case", no_argument, NULL, 'i'},
|
|
|
|
{"skip-chars", required_argument, NULL, 's'},
|
|
|
|
{"unique", no_argument, NULL, 'u'},
|
|
|
|
{NULL, no_argument, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2011-11-06 08:18:11 +00:00
|
|
|
static FILE *file(const char *, const char *);
|
|
|
|
static wchar_t *convert(const char *);
|
|
|
|
static int inlcmp(const char *, const char *);
|
|
|
|
static void show(FILE *, const char *);
|
|
|
|
static wchar_t *skip(wchar_t *);
|
|
|
|
static void obsolete(char *[]);
|
2002-03-22 01:42:45 +00:00
|
|
|
static void usage(void);
|
1999-10-24 04:08:15 +00:00
|
|
|
|
2013-07-18 22:11:27 +00:00
|
|
|
static void
|
|
|
|
strerror_init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cache NLS data before entering capability mode.
|
|
|
|
* XXXPJD: There should be strerror_init() and strsignal_init() in libc.
|
|
|
|
*/
|
|
|
|
(void)catopen("libc", NL_CAT_LOCALE);
|
|
|
|
}
|
|
|
|
|
1997-03-11 13:08:12 +00:00
|
|
|
int
|
2002-09-04 23:29:10 +00:00
|
|
|
main (int argc, char *argv[])
|
1997-03-11 13:08:12 +00:00
|
|
|
{
|
2010-03-08 19:40:22 +00:00
|
|
|
wchar_t *tprev, *tthis;
|
1997-03-11 13:08:12 +00:00
|
|
|
FILE *ifp, *ofp;
|
2010-03-06 19:21:57 +00:00
|
|
|
int ch, comp;
|
2010-03-08 19:40:22 +00:00
|
|
|
size_t prevbuflen, thisbuflen, b1;
|
2010-03-06 19:21:57 +00:00
|
|
|
char *prevline, *thisline, *p;
|
2004-07-02 23:43:05 +00:00
|
|
|
const char *ifn;
|
2013-07-18 22:11:27 +00:00
|
|
|
cap_rights_t rights;
|
1997-03-11 13:08:12 +00:00
|
|
|
|
2002-04-19 08:16:01 +00:00
|
|
|
(void) setlocale(LC_ALL, "");
|
1998-03-08 20:56:43 +00:00
|
|
|
|
1997-03-11 13:08:12 +00:00
|
|
|
obsolete(argv);
|
2018-05-02 01:17:08 +00:00
|
|
|
while ((ch = getopt_long(argc, argv, "+cdif:s:u", long_opts,
|
|
|
|
NULL)) != -1)
|
1997-03-11 13:08:12 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'c':
|
|
|
|
cflag = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
1997-09-07 15:09:22 +00:00
|
|
|
case 'i':
|
|
|
|
iflag = 1;
|
|
|
|
break;
|
1997-03-11 13:08:12 +00:00
|
|
|
case 'f':
|
|
|
|
numfields = strtol(optarg, &p, 10);
|
|
|
|
if (numfields < 0 || *p)
|
1997-08-21 06:51:10 +00:00
|
|
|
errx(1, "illegal field skip value: %s", optarg);
|
1997-03-11 13:08:12 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
numchars = strtol(optarg, &p, 10);
|
|
|
|
if (numchars < 0 || *p)
|
1997-08-21 06:51:10 +00:00
|
|
|
errx(1, "illegal character skip value: %s", optarg);
|
1997-03-11 13:08:12 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
2007-05-17 00:11:58 +00:00
|
|
|
}
|
1997-03-11 13:08:12 +00:00
|
|
|
|
2002-05-29 23:52:55 +00:00
|
|
|
argc -= optind;
|
2007-05-17 00:18:01 +00:00
|
|
|
argv += optind;
|
1997-03-11 13:08:12 +00:00
|
|
|
|
2002-05-30 00:07:14 +00:00
|
|
|
if (argc > 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
ifp = stdin;
|
2004-07-02 23:43:05 +00:00
|
|
|
ifn = "stdin";
|
2002-05-30 00:07:14 +00:00
|
|
|
ofp = stdout;
|
|
|
|
if (argc > 0 && strcmp(argv[0], "-") != 0)
|
2004-07-02 23:43:05 +00:00
|
|
|
ifp = file(ifn = argv[0], "r");
|
Change the cap_rights_t type from uint64_t to a structure that we can extend
in the future in a backward compatible (API and ABI) way.
The cap_rights_t represents capability rights. We used to use one bit to
represent one right, but we are running out of spare bits. Currently the new
structure provides place for 114 rights (so 50 more than the previous
cap_rights_t), but it is possible to grow the structure to hold at least 285
rights, although we can make it even larger if 285 rights won't be enough.
The structure definition looks like this:
struct cap_rights {
uint64_t cr_rights[CAP_RIGHTS_VERSION + 2];
};
The initial CAP_RIGHTS_VERSION is 0.
The top two bits in the first element of the cr_rights[] array contain total
number of elements in the array - 2. This means if those two bits are equal to
0, we have 2 array elements.
The top two bits in all remaining array elements should be 0.
The next five bits in all array elements contain array index. Only one bit is
used and bit position in this five-bits range defines array index. This means
there can be at most five array elements in the future.
To define new right the CAPRIGHT() macro must be used. The macro takes two
arguments - an array index and a bit to set, eg.
#define CAP_PDKILL CAPRIGHT(1, 0x0000000000000800ULL)
We still support aliases that combine few rights, but the rights have to belong
to the same array element, eg:
#define CAP_LOOKUP CAPRIGHT(0, 0x0000000000000400ULL)
#define CAP_FCHMOD CAPRIGHT(0, 0x0000000000002000ULL)
#define CAP_FCHMODAT (CAP_FCHMOD | CAP_LOOKUP)
There is new API to manage the new cap_rights_t structure:
cap_rights_t *cap_rights_init(cap_rights_t *rights, ...);
void cap_rights_set(cap_rights_t *rights, ...);
void cap_rights_clear(cap_rights_t *rights, ...);
bool cap_rights_is_set(const cap_rights_t *rights, ...);
bool cap_rights_is_valid(const cap_rights_t *rights);
void cap_rights_merge(cap_rights_t *dst, const cap_rights_t *src);
void cap_rights_remove(cap_rights_t *dst, const cap_rights_t *src);
bool cap_rights_contains(const cap_rights_t *big, const cap_rights_t *little);
Capability rights to the cap_rights_init(), cap_rights_set(),
cap_rights_clear() and cap_rights_is_set() functions are provided by
separating them with commas, eg:
cap_rights_t rights;
cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_FSTAT);
There is no need to terminate the list of rights, as those functions are
actually macros that take care of the termination, eg:
#define cap_rights_set(rights, ...) \
__cap_rights_set((rights), __VA_ARGS__, 0ULL)
void __cap_rights_set(cap_rights_t *rights, ...);
Thanks to using one bit as an array index we can assert in those functions that
there are no two rights belonging to different array elements provided
together. For example this is illegal and will be detected, because CAP_LOOKUP
belongs to element 0 and CAP_PDKILL to element 1:
cap_rights_init(&rights, CAP_LOOKUP | CAP_PDKILL);
Providing several rights that belongs to the same array's element this way is
correct, but is not advised. It should only be used for aliases definition.
This commit also breaks compatibility with some existing Capsicum system calls,
but I see no other way to do that. This should be fine as Capsicum is still
experimental and this change is not going to 9.x.
Sponsored by: The FreeBSD Foundation
2013-09-05 00:09:56 +00:00
|
|
|
cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
|
|
|
|
if (cap_rights_limit(fileno(ifp), &rights) < 0 && errno != ENOSYS)
|
2013-07-18 22:11:27 +00:00
|
|
|
err(1, "unable to limit rights for %s", ifn);
|
Change the cap_rights_t type from uint64_t to a structure that we can extend
in the future in a backward compatible (API and ABI) way.
The cap_rights_t represents capability rights. We used to use one bit to
represent one right, but we are running out of spare bits. Currently the new
structure provides place for 114 rights (so 50 more than the previous
cap_rights_t), but it is possible to grow the structure to hold at least 285
rights, although we can make it even larger if 285 rights won't be enough.
The structure definition looks like this:
struct cap_rights {
uint64_t cr_rights[CAP_RIGHTS_VERSION + 2];
};
The initial CAP_RIGHTS_VERSION is 0.
The top two bits in the first element of the cr_rights[] array contain total
number of elements in the array - 2. This means if those two bits are equal to
0, we have 2 array elements.
The top two bits in all remaining array elements should be 0.
The next five bits in all array elements contain array index. Only one bit is
used and bit position in this five-bits range defines array index. This means
there can be at most five array elements in the future.
To define new right the CAPRIGHT() macro must be used. The macro takes two
arguments - an array index and a bit to set, eg.
#define CAP_PDKILL CAPRIGHT(1, 0x0000000000000800ULL)
We still support aliases that combine few rights, but the rights have to belong
to the same array element, eg:
#define CAP_LOOKUP CAPRIGHT(0, 0x0000000000000400ULL)
#define CAP_FCHMOD CAPRIGHT(0, 0x0000000000002000ULL)
#define CAP_FCHMODAT (CAP_FCHMOD | CAP_LOOKUP)
There is new API to manage the new cap_rights_t structure:
cap_rights_t *cap_rights_init(cap_rights_t *rights, ...);
void cap_rights_set(cap_rights_t *rights, ...);
void cap_rights_clear(cap_rights_t *rights, ...);
bool cap_rights_is_set(const cap_rights_t *rights, ...);
bool cap_rights_is_valid(const cap_rights_t *rights);
void cap_rights_merge(cap_rights_t *dst, const cap_rights_t *src);
void cap_rights_remove(cap_rights_t *dst, const cap_rights_t *src);
bool cap_rights_contains(const cap_rights_t *big, const cap_rights_t *little);
Capability rights to the cap_rights_init(), cap_rights_set(),
cap_rights_clear() and cap_rights_is_set() functions are provided by
separating them with commas, eg:
cap_rights_t rights;
cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_FSTAT);
There is no need to terminate the list of rights, as those functions are
actually macros that take care of the termination, eg:
#define cap_rights_set(rights, ...) \
__cap_rights_set((rights), __VA_ARGS__, 0ULL)
void __cap_rights_set(cap_rights_t *rights, ...);
Thanks to using one bit as an array index we can assert in those functions that
there are no two rights belonging to different array elements provided
together. For example this is illegal and will be detected, because CAP_LOOKUP
belongs to element 0 and CAP_PDKILL to element 1:
cap_rights_init(&rights, CAP_LOOKUP | CAP_PDKILL);
Providing several rights that belongs to the same array's element this way is
correct, but is not advised. It should only be used for aliases definition.
This commit also breaks compatibility with some existing Capsicum system calls,
but I see no other way to do that. This should be fine as Capsicum is still
experimental and this change is not going to 9.x.
Sponsored by: The FreeBSD Foundation
2013-09-05 00:09:56 +00:00
|
|
|
cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
|
2002-05-30 00:07:14 +00:00
|
|
|
if (argc > 1)
|
1997-03-11 13:08:12 +00:00
|
|
|
ofp = file(argv[1], "w");
|
2013-07-18 22:11:27 +00:00
|
|
|
else
|
Change the cap_rights_t type from uint64_t to a structure that we can extend
in the future in a backward compatible (API and ABI) way.
The cap_rights_t represents capability rights. We used to use one bit to
represent one right, but we are running out of spare bits. Currently the new
structure provides place for 114 rights (so 50 more than the previous
cap_rights_t), but it is possible to grow the structure to hold at least 285
rights, although we can make it even larger if 285 rights won't be enough.
The structure definition looks like this:
struct cap_rights {
uint64_t cr_rights[CAP_RIGHTS_VERSION + 2];
};
The initial CAP_RIGHTS_VERSION is 0.
The top two bits in the first element of the cr_rights[] array contain total
number of elements in the array - 2. This means if those two bits are equal to
0, we have 2 array elements.
The top two bits in all remaining array elements should be 0.
The next five bits in all array elements contain array index. Only one bit is
used and bit position in this five-bits range defines array index. This means
there can be at most five array elements in the future.
To define new right the CAPRIGHT() macro must be used. The macro takes two
arguments - an array index and a bit to set, eg.
#define CAP_PDKILL CAPRIGHT(1, 0x0000000000000800ULL)
We still support aliases that combine few rights, but the rights have to belong
to the same array element, eg:
#define CAP_LOOKUP CAPRIGHT(0, 0x0000000000000400ULL)
#define CAP_FCHMOD CAPRIGHT(0, 0x0000000000002000ULL)
#define CAP_FCHMODAT (CAP_FCHMOD | CAP_LOOKUP)
There is new API to manage the new cap_rights_t structure:
cap_rights_t *cap_rights_init(cap_rights_t *rights, ...);
void cap_rights_set(cap_rights_t *rights, ...);
void cap_rights_clear(cap_rights_t *rights, ...);
bool cap_rights_is_set(const cap_rights_t *rights, ...);
bool cap_rights_is_valid(const cap_rights_t *rights);
void cap_rights_merge(cap_rights_t *dst, const cap_rights_t *src);
void cap_rights_remove(cap_rights_t *dst, const cap_rights_t *src);
bool cap_rights_contains(const cap_rights_t *big, const cap_rights_t *little);
Capability rights to the cap_rights_init(), cap_rights_set(),
cap_rights_clear() and cap_rights_is_set() functions are provided by
separating them with commas, eg:
cap_rights_t rights;
cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_FSTAT);
There is no need to terminate the list of rights, as those functions are
actually macros that take care of the termination, eg:
#define cap_rights_set(rights, ...) \
__cap_rights_set((rights), __VA_ARGS__, 0ULL)
void __cap_rights_set(cap_rights_t *rights, ...);
Thanks to using one bit as an array index we can assert in those functions that
there are no two rights belonging to different array elements provided
together. For example this is illegal and will be detected, because CAP_LOOKUP
belongs to element 0 and CAP_PDKILL to element 1:
cap_rights_init(&rights, CAP_LOOKUP | CAP_PDKILL);
Providing several rights that belongs to the same array's element this way is
correct, but is not advised. It should only be used for aliases definition.
This commit also breaks compatibility with some existing Capsicum system calls,
but I see no other way to do that. This should be fine as Capsicum is still
experimental and this change is not going to 9.x.
Sponsored by: The FreeBSD Foundation
2013-09-05 00:09:56 +00:00
|
|
|
cap_rights_set(&rights, CAP_IOCTL);
|
|
|
|
if (cap_rights_limit(fileno(ofp), &rights) < 0 && errno != ENOSYS) {
|
2013-07-18 22:11:27 +00:00
|
|
|
err(1, "unable to limit rights for %s",
|
|
|
|
argc > 1 ? argv[1] : "stdout");
|
|
|
|
}
|
Change the cap_rights_t type from uint64_t to a structure that we can extend
in the future in a backward compatible (API and ABI) way.
The cap_rights_t represents capability rights. We used to use one bit to
represent one right, but we are running out of spare bits. Currently the new
structure provides place for 114 rights (so 50 more than the previous
cap_rights_t), but it is possible to grow the structure to hold at least 285
rights, although we can make it even larger if 285 rights won't be enough.
The structure definition looks like this:
struct cap_rights {
uint64_t cr_rights[CAP_RIGHTS_VERSION + 2];
};
The initial CAP_RIGHTS_VERSION is 0.
The top two bits in the first element of the cr_rights[] array contain total
number of elements in the array - 2. This means if those two bits are equal to
0, we have 2 array elements.
The top two bits in all remaining array elements should be 0.
The next five bits in all array elements contain array index. Only one bit is
used and bit position in this five-bits range defines array index. This means
there can be at most five array elements in the future.
To define new right the CAPRIGHT() macro must be used. The macro takes two
arguments - an array index and a bit to set, eg.
#define CAP_PDKILL CAPRIGHT(1, 0x0000000000000800ULL)
We still support aliases that combine few rights, but the rights have to belong
to the same array element, eg:
#define CAP_LOOKUP CAPRIGHT(0, 0x0000000000000400ULL)
#define CAP_FCHMOD CAPRIGHT(0, 0x0000000000002000ULL)
#define CAP_FCHMODAT (CAP_FCHMOD | CAP_LOOKUP)
There is new API to manage the new cap_rights_t structure:
cap_rights_t *cap_rights_init(cap_rights_t *rights, ...);
void cap_rights_set(cap_rights_t *rights, ...);
void cap_rights_clear(cap_rights_t *rights, ...);
bool cap_rights_is_set(const cap_rights_t *rights, ...);
bool cap_rights_is_valid(const cap_rights_t *rights);
void cap_rights_merge(cap_rights_t *dst, const cap_rights_t *src);
void cap_rights_remove(cap_rights_t *dst, const cap_rights_t *src);
bool cap_rights_contains(const cap_rights_t *big, const cap_rights_t *little);
Capability rights to the cap_rights_init(), cap_rights_set(),
cap_rights_clear() and cap_rights_is_set() functions are provided by
separating them with commas, eg:
cap_rights_t rights;
cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_FSTAT);
There is no need to terminate the list of rights, as those functions are
actually macros that take care of the termination, eg:
#define cap_rights_set(rights, ...) \
__cap_rights_set((rights), __VA_ARGS__, 0ULL)
void __cap_rights_set(cap_rights_t *rights, ...);
Thanks to using one bit as an array index we can assert in those functions that
there are no two rights belonging to different array elements provided
together. For example this is illegal and will be detected, because CAP_LOOKUP
belongs to element 0 and CAP_PDKILL to element 1:
cap_rights_init(&rights, CAP_LOOKUP | CAP_PDKILL);
Providing several rights that belongs to the same array's element this way is
correct, but is not advised. It should only be used for aliases definition.
This commit also breaks compatibility with some existing Capsicum system calls,
but I see no other way to do that. This should be fine as Capsicum is still
experimental and this change is not going to 9.x.
Sponsored by: The FreeBSD Foundation
2013-09-05 00:09:56 +00:00
|
|
|
if (cap_rights_is_set(&rights, CAP_IOCTL)) {
|
2013-07-18 22:11:27 +00:00
|
|
|
unsigned long cmd;
|
|
|
|
|
|
|
|
cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
|
|
|
|
|
|
|
|
if (cap_ioctls_limit(fileno(ofp), &cmd, 1) < 0 &&
|
|
|
|
errno != ENOSYS) {
|
|
|
|
err(1, "unable to limit ioctls for %s",
|
|
|
|
argc > 1 ? argv[1] : "stdout");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strerror_init();
|
2018-06-19 23:43:14 +00:00
|
|
|
if (caph_enter() < 0)
|
2013-07-18 22:11:27 +00:00
|
|
|
err(1, "unable to enter capability mode");
|
1997-03-11 13:08:12 +00:00
|
|
|
|
2010-03-08 19:40:22 +00:00
|
|
|
prevbuflen = thisbuflen = 0;
|
|
|
|
prevline = thisline = NULL;
|
1997-03-11 13:08:12 +00:00
|
|
|
|
2010-03-08 19:40:22 +00:00
|
|
|
if (getline(&prevline, &prevbuflen, ifp) < 0) {
|
2007-05-17 00:19:56 +00:00
|
|
|
if (ferror(ifp))
|
2007-05-17 00:18:01 +00:00
|
|
|
err(1, "%s", ifn);
|
1997-03-11 13:08:12 +00:00
|
|
|
exit(0);
|
2004-07-02 23:43:05 +00:00
|
|
|
}
|
2010-03-08 19:40:22 +00:00
|
|
|
tprev = convert(prevline);
|
2010-03-06 19:21:57 +00:00
|
|
|
|
2010-03-08 19:40:22 +00:00
|
|
|
tthis = NULL;
|
|
|
|
while (getline(&thisline, &thisbuflen, ifp) >= 0) {
|
|
|
|
if (tthis != NULL)
|
|
|
|
free(tthis);
|
|
|
|
tthis = convert(thisline);
|
1997-03-11 13:08:12 +00:00
|
|
|
|
2010-03-06 19:21:57 +00:00
|
|
|
if (tthis == NULL && tprev == NULL)
|
2010-03-08 19:40:22 +00:00
|
|
|
comp = inlcmp(thisline, prevline);
|
2010-03-06 19:21:57 +00:00
|
|
|
else if (tthis == NULL || tprev == NULL)
|
|
|
|
comp = 1;
|
1997-09-07 15:09:22 +00:00
|
|
|
else
|
2010-03-06 19:21:57 +00:00
|
|
|
comp = wcscoll(tthis, tprev);
|
1997-09-07 15:09:22 +00:00
|
|
|
|
|
|
|
if (comp) {
|
2010-03-06 19:21:57 +00:00
|
|
|
/* If different, print; set previous to new value. */
|
2017-05-15 20:18:14 +00:00
|
|
|
show(ofp, prevline);
|
2010-03-06 19:21:57 +00:00
|
|
|
p = prevline;
|
2010-03-08 19:40:22 +00:00
|
|
|
b1 = prevbuflen;
|
1997-03-11 13:08:12 +00:00
|
|
|
prevline = thisline;
|
2010-03-08 19:40:22 +00:00
|
|
|
prevbuflen = thisbuflen;
|
|
|
|
if (tprev != NULL)
|
|
|
|
free(tprev);
|
2010-03-06 19:21:57 +00:00
|
|
|
tprev = tthis;
|
|
|
|
thisline = p;
|
2010-03-08 19:40:22 +00:00
|
|
|
thisbuflen = b1;
|
|
|
|
tthis = NULL;
|
1997-03-11 13:08:12 +00:00
|
|
|
repeats = 0;
|
|
|
|
} else
|
|
|
|
++repeats;
|
|
|
|
}
|
2004-07-02 23:43:05 +00:00
|
|
|
if (ferror(ifp))
|
2007-05-17 00:18:01 +00:00
|
|
|
err(1, "%s", ifn);
|
2017-05-15 20:18:14 +00:00
|
|
|
show(ofp, prevline);
|
1997-03-11 13:08:12 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:18:11 +00:00
|
|
|
static wchar_t *
|
2010-03-08 19:40:22 +00:00
|
|
|
convert(const char *str)
|
2002-06-21 07:08:34 +00:00
|
|
|
{
|
2010-03-08 19:40:22 +00:00
|
|
|
size_t n;
|
|
|
|
wchar_t *buf, *ret, *p;
|
|
|
|
|
|
|
|
if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
|
|
|
|
return (NULL);
|
2010-03-09 21:06:01 +00:00
|
|
|
if (SIZE_MAX / sizeof(*buf) < n + 1)
|
|
|
|
errx(1, "conversion buffer length overflow");
|
2010-03-08 19:40:22 +00:00
|
|
|
if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
if (mbstowcs(buf, str, n + 1) != n)
|
|
|
|
errx(1, "internal mbstowcs() error");
|
|
|
|
/* The last line may not end with \n. */
|
|
|
|
if (n > 0 && buf[n - 1] == L'\n')
|
|
|
|
buf[n - 1] = L'\0';
|
|
|
|
|
|
|
|
/* If requested get the chosen fields + character offsets. */
|
|
|
|
if (numfields || numchars) {
|
|
|
|
if ((ret = wcsdup(skip(buf))) == NULL)
|
|
|
|
err(1, "wcsdup");
|
|
|
|
free(buf);
|
|
|
|
} else
|
|
|
|
ret = buf;
|
|
|
|
|
|
|
|
if (iflag) {
|
|
|
|
for (p = ret; *p != L'\0'; p++)
|
|
|
|
*p = towlower(*p);
|
2008-02-08 23:04:13 +00:00
|
|
|
}
|
2002-06-21 07:08:34 +00:00
|
|
|
|
2010-03-08 19:40:22 +00:00
|
|
|
return (ret);
|
2002-06-21 07:08:34 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 08:18:11 +00:00
|
|
|
static int
|
2010-03-08 19:40:22 +00:00
|
|
|
inlcmp(const char *s1, const char *s2)
|
2010-03-06 19:21:57 +00:00
|
|
|
{
|
2010-03-08 19:40:22 +00:00
|
|
|
int c1, c2;
|
2010-03-06 19:21:57 +00:00
|
|
|
|
2010-03-08 19:40:22 +00:00
|
|
|
while (*s1 == *s2++)
|
|
|
|
if (*s1++ == '\0')
|
|
|
|
return (0);
|
|
|
|
c1 = (unsigned char)*s1;
|
|
|
|
c2 = (unsigned char)*(s2 - 1);
|
|
|
|
/* The last line may not end with \n. */
|
|
|
|
if (c1 == '\n')
|
|
|
|
c1 = '\0';
|
|
|
|
if (c2 == '\n')
|
|
|
|
c2 = '\0';
|
|
|
|
return (c1 - c2);
|
2010-03-06 19:21:57 +00:00
|
|
|
}
|
|
|
|
|
1997-03-11 13:08:12 +00:00
|
|
|
/*
|
|
|
|
* show --
|
|
|
|
* Output a line depending on the flags and number of repetitions
|
|
|
|
* of the line.
|
|
|
|
*/
|
2011-11-06 08:18:11 +00:00
|
|
|
static void
|
2010-03-06 19:21:57 +00:00
|
|
|
show(FILE *ofp, const char *str)
|
1997-03-11 13:08:12 +00:00
|
|
|
{
|
|
|
|
|
2017-05-15 20:18:14 +00:00
|
|
|
if ((dflag && repeats == 0) || (uflag && repeats > 0))
|
|
|
|
return;
|
2004-09-14 12:01:18 +00:00
|
|
|
if (cflag)
|
2010-03-08 19:40:22 +00:00
|
|
|
(void)fprintf(ofp, "%4d %s", repeats + 1, str);
|
2017-05-15 20:18:14 +00:00
|
|
|
else
|
2010-03-08 19:40:22 +00:00
|
|
|
(void)fprintf(ofp, "%s", str);
|
1997-03-11 13:08:12 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 08:18:11 +00:00
|
|
|
static wchar_t *
|
2004-07-02 23:43:05 +00:00
|
|
|
skip(wchar_t *str)
|
1997-03-11 13:08:12 +00:00
|
|
|
{
|
2002-09-04 23:29:10 +00:00
|
|
|
int nchars, nfields;
|
2002-06-21 07:59:31 +00:00
|
|
|
|
2010-03-06 19:21:57 +00:00
|
|
|
for (nfields = 0; *str != L'\0' && nfields++ != numfields; ) {
|
2004-07-02 23:43:05 +00:00
|
|
|
while (iswblank(*str))
|
2002-06-21 07:59:31 +00:00
|
|
|
str++;
|
2010-03-06 19:21:57 +00:00
|
|
|
while (*str != L'\0' && !iswblank(*str))
|
2002-06-21 07:59:31 +00:00
|
|
|
str++;
|
|
|
|
}
|
2010-03-06 19:21:57 +00:00
|
|
|
for (nchars = numchars; nchars-- && *str != L'\0'; ++str)
|
|
|
|
;
|
1997-03-11 13:08:12 +00:00
|
|
|
return(str);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:18:11 +00:00
|
|
|
static FILE *
|
2002-09-04 23:29:10 +00:00
|
|
|
file(const char *name, const char *mode)
|
1997-03-11 13:08:12 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if ((fp = fopen(name, mode)) == NULL)
|
1997-08-21 06:51:10 +00:00
|
|
|
err(1, "%s", name);
|
1997-03-11 13:08:12 +00:00
|
|
|
return(fp);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:18:11 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
obsolete(char *argv[])
|
1997-03-11 13:08:12 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char *ap, *p, *start;
|
|
|
|
|
1997-08-21 06:51:10 +00:00
|
|
|
while ((ap = *++argv)) {
|
1997-03-11 13:08:12 +00:00
|
|
|
/* Return if "--" or not an option of any form. */
|
|
|
|
if (ap[0] != '-') {
|
|
|
|
if (ap[0] != '+')
|
|
|
|
return;
|
|
|
|
} else if (ap[1] == '-')
|
|
|
|
return;
|
1998-03-08 20:56:43 +00:00
|
|
|
if (!isdigit((unsigned char)ap[1]))
|
1997-03-11 13:08:12 +00:00
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* Digit signifies an old-style option. Malloc space for dash,
|
|
|
|
* new option and argument.
|
|
|
|
*/
|
|
|
|
len = strlen(ap);
|
|
|
|
if ((start = p = malloc(len + 3)) == NULL)
|
2002-07-05 09:28:13 +00:00
|
|
|
err(1, "malloc");
|
1997-03-11 13:08:12 +00:00
|
|
|
*p++ = '-';
|
|
|
|
*p++ = ap[0] == '+' ? 's' : 'f';
|
|
|
|
(void)strcpy(p, ap + 1);
|
|
|
|
*argv = start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-08-21 06:51:10 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
usage(void)
|
1997-03-11 13:08:12 +00:00
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
2017-05-15 20:18:14 +00:00
|
|
|
"usage: uniq [-c] [-d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
|
1997-03-11 13:08:12 +00:00
|
|
|
exit(1);
|
|
|
|
}
|