- FILE *passwd;
- smallint error = NAME_NOT_FOUND;
- char *name = (ENABLE_FEATURE_DEL_USER_FROM_GROUP && args[2]) ? args[2] : args[1];
- char *line, *del;
- char *new = xzalloc(1);
-
- passwd = fopen_func(filename, "r");
- if (passwd) {
- while ((line = xmalloc_fgets(passwd))) {
- int len = strlen(name);
-
- if (strncmp(line, name, len) == 0
- && line[len] == ':'
- ) {
- error = STATUS_OK;
- if (ENABLE_FEATURE_DEL_USER_FROM_GROUP) {
- struct group *gr;
- char *p;
- if (args[2]
- /* There were two args on commandline */
- && (gr = getgrnam(name))
- /* The group was not deleted in the meanwhile */
- && (p = strrchr(line, ':'))
- /* We can find a pointer to the last ':' */
- ) {
- error = MEMBER_NOT_FOUND;
- /* Move past ':' (worst case to '\0') and cut the line */
- p[1] = '\0';
- /* Reuse p */
- for (p = xzalloc(1); *gr->gr_mem != NULL; gr->gr_mem++) {
- /* Add all the other group members */
- if (strcmp(args[1], *gr->gr_mem) != 0) {
- del = p;
- p = xasprintf("%s%s%s", p, p[0] ? "," : "", *gr->gr_mem);
- free(del);
- } else
- error = STATUS_OK;
- }
- /* Recompose the line */
- line = xasprintf("%s%s\n", line, p);
- if (ENABLE_FEATURE_CLEAN_UP) free(p);
- } else
- goto skip;
- }
- }
- del = new;
- new = xasprintf("%s%s", new, line);
- free(del);
- skip:
- free(line);
- }
-
- if (ENABLE_FEATURE_CLEAN_UP) fclose(passwd);
-
- if (error) {
- if (ENABLE_FEATURE_DEL_USER_FROM_GROUP && error == MEMBER_NOT_FOUND) {
- /* Set the correct values for error message */
- filename = name;
- name = args[1];
- }
- bb_error_msg("can't find %s in %s", name, filename);
- } else {
- passwd = fopen_func(filename, "w");
- if (passwd) {
- fputs(new, passwd);
- if (ENABLE_FEATURE_CLEAN_UP) fclose(passwd);
- }
- }