summaryrefslogtreecommitdiffstats
path: root/util/tunepasswd.c
diff options
context:
space:
mode:
authorin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-03-07 23:13:44 +0800
committerin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-03-07 23:13:44 +0800
commitae31e19f92e717919ac8e3db9039eb38d2b89aae (patch)
treec70164d6a1852344f44b04a653ae2815043512af /util/tunepasswd.c
downloadpttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.gz
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.bz2
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.lz
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.xz
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.tar.zst
pttbbs-ae31e19f92e717919ac8e3db9039eb38d2b89aae.zip
Initial revision
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@1 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'util/tunepasswd.c')
-rw-r--r--util/tunepasswd.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/util/tunepasswd.c b/util/tunepasswd.c
new file mode 100644
index 00000000..15a3fe1f
--- /dev/null
+++ b/util/tunepasswd.c
@@ -0,0 +1,77 @@
+/* $Id: tunepasswd.c,v 1.1 2002/03/07 15:13:46 in2 Exp $ */
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include "config.h"
+#include "pttstruct.h"
+#include "common.h"
+
+int tune(int num) {
+ int i, j, fin, fout;
+ userec_t u;
+
+ if((fin = open(FN_PASSWD, O_RDONLY)) == -1) {
+ perror(FN_PASSWD);
+ return 1;
+ }
+ if(flock(fin, LOCK_EX)) {
+ printf("Lock failed!\n");
+ return 1;
+ }
+ if((fout = open(FN_PASSWD ".tune" , O_WRONLY | O_CREAT, 0600)) == -1) {
+ perror(FN_PASSWD ".tune");
+ flock(fin, LOCK_UN);
+ close(fin);
+ return 1;
+ }
+
+ for(i = j = 0; i < num; i++) {
+ read(fin, &u, sizeof(u));
+ if(u.userid[0]) {
+ if(j == MAX_USERS) {
+ printf("MAX_USERS is too small!\n");
+ close(fout);
+ unlink(FN_PASSWD ".tune");
+ flock(fin, LOCK_UN);
+ close(fin);
+ return 1;
+ }
+ write(fout, &u, sizeof(u));
+ j++;
+ }
+ }
+ for(memset(&u, 0, sizeof(u)); j < MAX_USERS; j++) {
+ write(fout, &u, sizeof(u));
+ }
+ close(fout);
+
+ /* backup */
+ unlink(FN_PASSWD "~");
+ link(FN_PASSWD, FN_PASSWD "~");
+ unlink(FN_PASSWD);
+ link(FN_PASSWD ".tune", FN_PASSWD);
+ unlink(FN_PASSWD ".tune");
+
+ flock(fin, LOCK_UN);
+ close(fin);
+ return 0;
+}
+
+int main() {
+ struct stat sb;
+
+ if(stat(FN_PASSWD, &sb)) {
+ perror("stat");
+ return 1;
+ }
+ if(sb.st_size != sizeof(userec_t) * MAX_USERS) {
+ printf("size and MAX_USERS do not match!\n");
+ if(tune(sb.st_size / sizeof(userec_t)) == 0)
+ printf(FN_PASSWD " has been tuned successfully!\n");
+ } else
+ printf("Nothing to do.\n");
+ return 0;
+}