1 /* vi: set sw=4 ts=4: */
3 * deluser (remove lusers from the system ;) for TinyLogin
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>
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
20 /* where to start and stop deletion */
26 /* An interesting side-effect of boundary()'s
27 * implementation is that the first user (typically root)
28 * cannot be removed. Let's call it a feature. */
29 static inline Bounds boundary(const char *buffer, const char *login)
36 snprintf(needle, 256, "\n%s:", login);
38 start = strstr(buffer, needle);
46 stop = strchr(start, '\n');
47 b.start = start - buffer;
48 b.stop = stop - buffer;
52 /* grep -v ^login (except it only deletes the first match) */
53 /* ...in fact, I think I'm going to simplify this later */
54 static void del_line_matching(const char *login, const char *filename)
62 if ((passwd = bb_wfopen(filename, "r"))) {
63 // Remove pointless const.
64 xstat((char *)filename, &statbuf);
65 buffer = (char *) xmalloc(statbuf.st_size * sizeof(char));
66 fread(buffer, statbuf.st_size, sizeof(char), passwd);
68 /* find the user to remove */
69 b = boundary(buffer, login);
71 /* write the file w/o the user */
72 if ((passwd = bb_wfopen(filename, "w"))) {
73 fwrite(buffer, (b.start - 1), sizeof(char), passwd);
74 fwrite(&buffer[b.stop], (statbuf.st_size - b.stop), sizeof(char), passwd);
78 bb_error_msg("Can't find '%s' in '%s'", login, filename);
84 int deluser_main(int argc, char **argv)
89 if (ENABLE_DELUSER && bb_applet_name[3] == 'u') {
90 del_line_matching(argv[1], bb_path_passwd_file);
91 if (ENABLE_FEATURE_SHADOWPASSWDS)
92 del_line_matching(argv[1], bb_path_shadow_file);
94 del_line_matching(argv[1], bb_path_group_file);
95 if (ENABLE_FEATURE_SHADOWPASSWDS)
96 del_line_matching(argv[1], bb_path_gshadow_file);
98 return (EXIT_SUCCESS);
101 /* $Id: deluser.c,v 1.4 2003/07/14 20:20:45 andersen Exp $ */