+enum {
+ PRINT_REAL = (1 << 0),
+ NAME_NOT_NUMBER = (1 << 1),
+ JUST_USER = (1 << 2),
+ JUST_GROUP = (1 << 3),
+ JUST_ALL_GROUPS = (1 << 4),
+#if ENABLE_SELINUX
+ JUST_CONTEXT = (1 << 5),
+#endif
+};
+
+static int print_common(unsigned id, const char *name, const char *prefix)
+{
+ if (prefix) {
+ printf("%s", prefix);
+ }
+ if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
+ printf("%u", id);
+ }
+ if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
+ if (name) {
+ printf(option_mask32 ? "%s" : "(%s)", name);
+ } else {
+ /* Don't set error status flag in default mode */
+ if (option_mask32) {
+ if (ENABLE_DESKTOP)
+ bb_error_msg("unknown ID %u", id);
+ return EXIT_FAILURE;
+ }
+ }
+ }
+ return EXIT_SUCCESS;
+}
+
+static int print_group(gid_t id, const char *prefix)
+{
+ return print_common(id, gid2group(id), prefix);
+}
+
+static int print_user(uid_t id, const char *prefix)
+{
+ return print_common(id, uid2uname(id), prefix);
+}
+
+/* On error set *n < 0 and return >= 0
+ * If *n is too small, update it and return < 0
+ * (ok to trash groups[] in both cases)
+ * Otherwise fill in groups[] and return >= 0
+ */
+static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
+{
+ int m;
+
+ if (username) {
+ /* If the user is a member of more than
+ * *n groups, then -1 is returned. Otherwise >= 0.
+ * (and no defined way of detecting errors?!) */
+ m = getgrouplist(username, rgid, groups, n);
+ /* I guess *n < 0 might indicate error. Anyway,
+ * malloc'ing -1 bytes won't be good, so: */
+ if (*n < 0)
+ return 0;
+ return m;