ls.c (6027b8601527b83fd174dc857073dfd37e9a3de8) ls.c (fab57d1b740f6df905ebda23c9345ef88a7a046a)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE

--- 9 unchanged lines hidden (view full) ---

18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
25 * Copyright 2015 Gary Mills
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE

--- 9 unchanged lines hidden (view full) ---

18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
25 * Copyright 2015 Gary Mills
26 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
26 * Copyright 2020 Peter Tribble
27 */
28
29/*
30 * Copyright 2017 Jason King. All rights reserved.
31 * Use is subject to license terms.
32 */
33

--- 194 unchanged lines hidden (view full) ---

228static int maxn = 0; /* number of flist entries with lbufs asigned */
229static int quantn = 64; /* allocation growth quantum */
230
231static struct lbuf *nxtlbf; /* ptr to next lbuf to be assigned */
232static struct lbuf **flist; /* ptr to list of lbuf pointers */
233static struct lbuf *gstat(char *, int, struct ditem *);
234static char *getname(uid_t);
235static char *getgroup(gid_t);
27 * Copyright 2020 Peter Tribble
28 */
29
30/*
31 * Copyright 2017 Jason King. All rights reserved.
32 * Use is subject to license terms.
33 */
34

--- 194 unchanged lines hidden (view full) ---

229static int maxn = 0; /* number of flist entries with lbufs asigned */
230static int quantn = 64; /* allocation growth quantum */
231
232static struct lbuf *nxtlbf; /* ptr to next lbuf to be assigned */
233static struct lbuf **flist; /* ptr to list of lbuf pointers */
234static struct lbuf *gstat(char *, int, struct ditem *);
235static char *getname(uid_t);
236static char *getgroup(gid_t);
237static char *getusid(uid_t);
238static char *getgsid(gid_t);
236static char *makename(char *, char *);
237static void pentry(struct lbuf *);
238static void column(void);
239static void pmode(mode_t aflag);
240static void selection(int *);
241static void new_line(void);
242static void rddir(char *, struct ditem *);
243static int strcol(unsigned char *);

--- 1030 unchanged lines hidden (view full) ---

1274 (void) putchar(p->ltype);
1275 curcol++;
1276 pmode(p->lflags);
1277
1278 /* ACL: additional access mode flag */
1279 (void) putchar(p->acl);
1280 curcol++;
1281
239static char *makename(char *, char *);
240static void pentry(struct lbuf *);
241static void column(void);
242static void pmode(mode_t aflag);
243static void selection(int *);
244static void new_line(void);
245static void rddir(char *, struct ditem *);
246static int strcol(unsigned char *);

--- 1030 unchanged lines hidden (view full) ---

1277 (void) putchar(p->ltype);
1278 curcol++;
1279 pmode(p->lflags);
1280
1281 /* ACL: additional access mode flag */
1282 (void) putchar(p->acl);
1283 curcol++;
1284
1285 /*
1286 * When handling owner/group options (-o -g) note -n:
1287 * With no -n options, getname/getroup converts any
1288 * ephemeral IDs to a winname (if possible) or a SID.
1289 * With just one -n option, convert ephemeral IDs to SIDs
1290 * With two or more -n options, show the ephemeral ID
1291 * (which is a lot less helpful than the SID).
1292 */
1282 curcol += printf("%3lu ", (ulong_t)p->lnl);
1283 if (oflg) {
1293 curcol += printf("%3lu ", (ulong_t)p->lnl);
1294 if (oflg) {
1284 if (!nflg) {
1295 if (nflg == 0) {
1285 cp = getname(p->luid);
1286 curcol += printf("%-8s ", cp);
1296 cp = getname(p->luid);
1297 curcol += printf("%-8s ", cp);
1287 } else
1298 } else if (nflg == 1 && p->luid > MAXUID) {
1299 cp = getusid(p->luid);
1300 curcol += printf("%-8s ", cp);
1301 } else {
1288 curcol += printf("%-8lu ", (ulong_t)p->luid);
1302 curcol += printf("%-8lu ", (ulong_t)p->luid);
1303 }
1289 }
1290 if (gflg) {
1304 }
1305 if (gflg) {
1291 if (!nflg) {
1306 if (nflg == 0) {
1292 cp = getgroup(p->lgid);
1293 curcol += printf("%-8s ", cp);
1307 cp = getgroup(p->lgid);
1308 curcol += printf("%-8s ", cp);
1294 } else
1309 } else if (nflg == 1 && p->lgid > MAXUID) {
1310 cp = getgsid(p->lgid);
1311 curcol += printf("%-8s ", cp);
1312 } else {
1295 curcol += printf("%-8lu ", (ulong_t)p->lgid);
1313 curcol += printf("%-8lu ", (ulong_t)p->lgid);
1314 }
1296 }
1297 if (p->ltype == 'b' || p->ltype == 'c') {
1298 curcol += printf("%3u, %2u",
1299 (uint_t)major((dev_t)p->lsize),
1300 (uint_t)minor((dev_t)p->lsize));
1301 } else if (hflg) {
1302 char numbuf[NN_NUMBUF_SZ];
1303

--- 118 unchanged lines hidden (view full) ---

1422 if (tmflg && alltm) {
1423 new_line();
1424 print_time(p);
1425 new_line();
1426 }
1427 if (vflg) {
1428 new_line();
1429 if (p->aclp) {
1315 }
1316 if (p->ltype == 'b' || p->ltype == 'c') {
1317 curcol += printf("%3u, %2u",
1318 (uint_t)major((dev_t)p->lsize),
1319 (uint_t)minor((dev_t)p->lsize));
1320 } else if (hflg) {
1321 char numbuf[NN_NUMBUF_SZ];
1322

--- 118 unchanged lines hidden (view full) ---

1441 if (tmflg && alltm) {
1442 new_line();
1443 print_time(p);
1444 new_line();
1445 }
1446 if (vflg) {
1447 new_line();
1448 if (p->aclp) {
1430 acl_printacl(p->aclp, num_cols, Vflg);
1449 int pa_flags = 0;
1450
1451 if (Vflg)
1452 pa_flags |= ACL_COMPACT_FMT;
1453 if (nflg)
1454 pa_flags |= ACL_NORESOLVE;
1455 if (nflg < 2)
1456 pa_flags |= ACL_SID_FMT;
1457
1458 acl_printacl2(p->aclp, num_cols, pa_flags);
1431 }
1432 }
1433 /* Free extended system attribute lists */
1434 if (saflg || tmflg)
1435 free_sysattr(p);
1436}
1437
1438/* print various r,w,x permissions */

--- 718 unchanged lines hidden (view full) ---

2157 *dp++ = '/';
2158 fp = file;
2159 while (*fp)
2160 *dp++ = *fp++;
2161 *dp = '\0';
2162 return (dfile);
2163}
2164
1459 }
1460 }
1461 /* Free extended system attribute lists */
1462 if (saflg || tmflg)
1463 free_sysattr(p);
1464}
1465
1466/* print various r,w,x permissions */

--- 718 unchanged lines hidden (view full) ---

2185 *dp++ = '/';
2186 fp = file;
2187 while (*fp)
2188 *dp++ = *fp++;
2189 *dp = '\0';
2190 return (dfile);
2191}
2192
2193#define NMAX 256 /* The maximum size of a SID in string format */
2194#define SCPYN(a, b) (void) strlcpy(a, b, NMAX)
2165
2195
2166#include <pwd.h>
2167#include <grp.h>
2168#include <utmpx.h>
2169
2170struct utmpx utmp;
2171
2172#define NMAX (sizeof (utmp.ut_name))
2173#define SCPYN(a, b) (void) strncpy(a, b, NMAX)
2174
2175
2176struct cachenode { /* this struct must be zeroed before using */
2177 struct cachenode *lesschild; /* subtree whose entries < val */
2178 struct cachenode *grtrchild; /* subtree whose entries > val */
2179 long val; /* the uid or gid of this entry */
2180 int initted; /* name has been filled in */
2196struct cachenode { /* this struct must be zeroed before using */
2197 struct cachenode *lesschild; /* subtree whose entries < val */
2198 struct cachenode *grtrchild; /* subtree whose entries > val */
2199 long val; /* the uid or gid of this entry */
2200 int initted; /* name has been filled in */
2181 char name[NMAX+1]; /* the string that val maps to */
2201 char name[NMAX]; /* the string that val maps to */
2182};
2183static struct cachenode *names, *groups;
2202};
2203static struct cachenode *names, *groups;
2204static struct cachenode *user_sids, *group_sids;
2184
2185static struct cachenode *
2186findincache(struct cachenode **head, long val)
2187{
2188 struct cachenode **parent = head;
2189 struct cachenode *c = *parent;
2190
2191 while (c != NULL) {

--- 18 unchanged lines hidden (view full) ---

2210 *parent = c;
2211 c->val = val;
2212 return (c);
2213}
2214
2215/*
2216 * get name from cache, or passwd file for a given uid;
2217 * lastuid is set to uid.
2205
2206static struct cachenode *
2207findincache(struct cachenode **head, long val)
2208{
2209 struct cachenode **parent = head;
2210 struct cachenode *c = *parent;
2211
2212 while (c != NULL) {

--- 18 unchanged lines hidden (view full) ---

2231 *parent = c;
2232 c->val = val;
2233 return (c);
2234}
2235
2236/*
2237 * get name from cache, or passwd file for a given uid;
2238 * lastuid is set to uid.
2239 *
2240 * If an ephemeral UID (> MAXUID) try to convert to either a
2241 * name or a sid.
2218 */
2219static char *
2220getname(uid_t uid)
2221{
2222 struct passwd *pwent;
2223 struct cachenode *c;
2242 */
2243static char *
2244getname(uid_t uid)
2245{
2246 struct passwd *pwent;
2247 struct cachenode *c;
2248 char *sid;
2224
2225 if ((uid == lastuid) && lastuname)
2226 return (lastuname);
2227
2228 c = findincache(&names, uid);
2229 if (c->initted == 0) {
2249
2250 if ((uid == lastuid) && lastuname)
2251 return (lastuname);
2252
2253 c = findincache(&names, uid);
2254 if (c->initted == 0) {
2230 if ((pwent = getpwuid(uid)) != NULL) {
2255 sid = NULL;
2256 if (uid > MAXUID &&
2257 sid_string_by_id(uid, B_TRUE, &sid, 0) == 0) {
2258 SCPYN(&c->name[0], sid);
2259 free(sid);
2260 } else if ((pwent = getpwuid(uid)) != NULL) {
2231 SCPYN(&c->name[0], pwent->pw_name);
2232 } else {
2233 (void) sprintf(&c->name[0], "%-8u", (int)uid);
2234 }
2235 c->initted = 1;
2236 }
2237 lastuid = uid;
2238 lastuname = &c->name[0];
2239 return (lastuname);
2240}
2241
2242/*
2243 * get name from cache, or group file for a given gid;
2244 * lastgid is set to gid.
2261 SCPYN(&c->name[0], pwent->pw_name);
2262 } else {
2263 (void) sprintf(&c->name[0], "%-8u", (int)uid);
2264 }
2265 c->initted = 1;
2266 }
2267 lastuid = uid;
2268 lastuname = &c->name[0];
2269 return (lastuname);
2270}
2271
2272/*
2273 * get name from cache, or group file for a given gid;
2274 * lastgid is set to gid.
2275 *
2276 * If an ephemeral GID (> MAXUID) try to convert to either a
2277 * name or a sid.
2245 */
2246static char *
2247getgroup(gid_t gid)
2248{
2249 struct group *grent;
2250 struct cachenode *c;
2278 */
2279static char *
2280getgroup(gid_t gid)
2281{
2282 struct group *grent;
2283 struct cachenode *c;
2284 char *sid;
2251
2252 if ((gid == lastgid) && lastgname)
2253 return (lastgname);
2254
2255 c = findincache(&groups, gid);
2256 if (c->initted == 0) {
2285
2286 if ((gid == lastgid) && lastgname)
2287 return (lastgname);
2288
2289 c = findincache(&groups, gid);
2290 if (c->initted == 0) {
2257 if ((grent = getgrgid(gid)) != NULL) {
2291 sid = NULL;
2292 if (gid > MAXUID &&
2293 sid_string_by_id(gid, B_FALSE, &sid, 0) == 0) {
2294 SCPYN(&c->name[0], sid);
2295 free(sid);
2296 } else if ((grent = getgrgid(gid)) != NULL) {
2258 SCPYN(&c->name[0], grent->gr_name);
2259 } else {
2260 (void) sprintf(&c->name[0], "%-8u", (int)gid);
2261 }
2262 c->initted = 1;
2263 }
2264 lastgid = gid;
2265 lastgname = &c->name[0];
2266 return (lastgname);
2267}
2268
2297 SCPYN(&c->name[0], grent->gr_name);
2298 } else {
2299 (void) sprintf(&c->name[0], "%-8u", (int)gid);
2300 }
2301 c->initted = 1;
2302 }
2303 lastgid = gid;
2304 lastgname = &c->name[0];
2305 return (lastgname);
2306}
2307
2308/*
2309 * get SID from cache, or from idmap for a given (ephemeral) uid;
2310 *
2311 * Always an ephemeral UID (> MAXUID) here.
2312 * Just convert to a SID (no winname lookup)
2313 */
2314static char *
2315getusid(uid_t uid)
2316{
2317 struct cachenode *c;
2318 char *sid;
2319
2320 c = findincache(&user_sids, uid);
2321 if (c->initted == 0) {
2322 sid = NULL;
2323 if (sid_string_by_id(uid, B_TRUE, &sid, ACL_NORESOLVE) == 0) {
2324 SCPYN(&c->name[0], sid);
2325 free(sid);
2326 } else {
2327 (void) sprintf(&c->name[0], "%-8u", (int)uid);
2328 }
2329 c->initted = 1;
2330 }
2331
2332 return (&c->name[0]);
2333}
2334
2335/*
2336 * get SID from cache, or from idmap for a given (ephemeral) gid;
2337 *
2338 * If an ephemeral UID (> MAXUID) try to convert to a SID
2339 * (no winname lookup here)
2340 */
2341static char *
2342getgsid(gid_t gid)
2343{
2344 struct cachenode *c;
2345 char *sid;
2346
2347 c = findincache(&group_sids, gid);
2348 if (c->initted == 0) {
2349 sid = NULL;
2350 if (sid_string_by_id(gid, B_FALSE, &sid, ACL_NORESOLVE) == 0) {
2351 SCPYN(&c->name[0], sid);
2352 free(sid);
2353 } else {
2354 (void) sprintf(&c->name[0], "%-8u", (int)gid);
2355 }
2356 c->initted = 1;
2357 }
2358
2359 return (&c->name[0]);
2360}
2361
2269/* return >0 if item pointed by pp2 should appear first */
2270static int
2271compar(struct lbuf **pp1, struct lbuf **pp2)
2272{
2273 struct lbuf *p1, *p2;
2274
2275 p1 = *pp1;
2276 p2 = *pp2;

--- 843 unchanged lines hidden ---
2362/* return >0 if item pointed by pp2 should appear first */
2363static int
2364compar(struct lbuf **pp1, struct lbuf **pp2)
2365{
2366 struct lbuf *p1, *p2;
2367
2368 p1 = *pp1;
2369 p2 = *pp2;

--- 843 unchanged lines hidden ---