Attempt to get more applets compile for NOMMU.
[oweals/busybox.git] / loginutils / deluser.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * deluser (remove lusers from the system ;) for TinyLogin
4  *
5  * Copyright (C) 1999 by Lineo, inc. and John Beppu
6  * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7  * Unified with delgroup by Tito Ragusa <farmatito@tiscali.it>
8  *
9  * Licensed under GPL version 2, see file LICENSE in this tarball for details.
10  *
11  */
12
13 #include "busybox.h"
14
15 static void del_line_matching(const char *login, const char *filename)
16 {
17         char *line;
18         FILE *passwd;
19         int len = strlen(login);
20         int found = 0;
21         llist_t *plist = NULL;
22
23         passwd = fopen_or_warn(filename, "r");
24         if (!passwd) return;
25
26         while ((line = xmalloc_fgets(passwd))) {
27                 if (!strncmp(line, login, len)
28                  && line[len] == ':'
29                 ) {
30                         found++;
31                         free(line);
32                 } else {
33                         llist_add_to_end(&plist, line);
34                 }
35         }
36
37         if (!ENABLE_FEATURE_CLEAN_UP) {
38                 if (!found) {
39                         bb_error_msg("can't find '%s' in '%s'", login, filename);
40                         return;
41                 }
42                 passwd = fopen_or_warn(filename, "w");
43                 if (passwd)
44                         while ((line = llist_pop(&plist)))
45                                 fputs(line, passwd);
46         } else {
47                 if (!found) {
48                         bb_error_msg("can't find '%s' in '%s'", login, filename);
49                         goto clean_up;
50                 }
51                 fclose(passwd);
52                 passwd = fopen_or_warn(filename, "w");
53                 if (passwd) {
54  clean_up:
55                         while ((line = llist_pop(&plist))) {
56                                 if (found) fputs(line, passwd);
57                                 free(line);
58                         }
59                         fclose(passwd);
60                 }
61         }
62 }
63
64 int deluser_main(int argc, char **argv);
65 int deluser_main(int argc, char **argv)
66 {
67         if (argc != 2)
68                 bb_show_usage();
69
70         if (ENABLE_DELUSER
71          && (!ENABLE_DELGROUP || applet_name[3] == 'u')
72         ) {
73                 del_line_matching(argv[1], bb_path_passwd_file);
74                 if (ENABLE_FEATURE_SHADOWPASSWDS)
75                         del_line_matching(argv[1], bb_path_shadow_file);
76         }
77         del_line_matching(argv[1], bb_path_group_file);
78         if (ENABLE_FEATURE_SHADOWPASSWDS)
79                 del_line_matching(argv[1], bb_path_gshadow_file);
80
81         return EXIT_SUCCESS;
82 }
83