]> andersk Git - moira.git/blob - gen/passwd.pc
Code style cleanup. (No functional changes)
[moira.git] / gen / passwd.pc
1 /* $Header$
2  *
3  * This generates a master /etc/passwd containing all active (status != 0)
4  * accounts.
5  *
6  *  (c) Copyright 1988, 1990 by the Massachusetts Institute of Technology.
7  *  For copying and distribution information, please see the file
8  *  <mit-copyright.h>.
9  */
10
11 #include <mit-copyright.h>
12 #include <errno.h>
13 #include <stdio.h>
14 #include <moira.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 EXEC SQL INCLUDE sqlca;
19
20 char *whoami = "passwd.gen";
21 char *db = "moira/moira";
22
23 int main(int argc, char **argv)
24 {
25   FILE *out = stdout;
26   char *outf = NULL, outft[64];
27   struct stat sb;
28   int flag;
29   EXEC SQL BEGIN DECLARE SECTION;
30   char login[9], shell[33], fullname[33], oa[17], op[13], hp[17];
31   char nickname[17];
32   int uid;
33   EXEC SQL END DECLARE SECTION;
34
35   EXEC SQL CONNECT :db;
36
37   if (argc == 2)
38     {
39       if (stat(argv[1], &sb) == 0)
40         {
41           if (ModDiff (&flag, "users", sb.st_mtime))
42             exit(MR_DATE);
43           if (flag < 0)
44             {
45               fprintf(stderr, "File %s does not need to be rebuilt.\n",
46                       argv[1]);
47               exit(MR_NO_CHANGE);
48             }
49         }
50       outf = argv[1];
51       sprintf(outft, "%s~", outf);
52       if (!(out = fopen(outft, "w")))
53         {
54           fprintf(stderr, "unable to open %s for output\n", outf);
55           exit(MR_OCONFIG);
56         }
57     }
58   else if (argc != 1)
59     {
60       fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
61       exit(MR_ARGS);
62     }
63   else
64     outf = NULL;
65
66   EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
67
68   EXEC SQL DECLARE x CURSOR FOR SELECT
69     login, unix_uid, shell, fullname, office_addr, nickname,
70     office_phone, home_phone
71     FROM users WHERE status = 1;
72   EXEC SQL OPEN x;
73   while (1)
74     {
75       EXEC SQL FETCH x INTO :login, :uid, :shell, :fullname, :oa, :nickname,
76         :op, :hp;
77       if (sqlca.sqlcode)
78         break;
79       strtrim(login);
80       strtrim(fullname);
81       strtrim(nickname);
82       strtrim(oa);
83       strtrim(op);
84       strtrim(hp);
85       strtrim(shell);
86       fprintf(out, "%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\n",
87               login, uid, fullname, nickname, oa, op, hp, login, shell);
88     }
89
90   EXEC SQL CLOSE x;
91
92   EXEC SQL COMMIT;
93
94   if (fclose(out))
95     {
96       perror("close failed");
97       exit(MR_CCONFIG);
98     }
99   if (outf)
100     fix_file(outf);
101   exit(MR_SUCCESS);
102
103 sqlerr:
104   db_error(sqlca.sqlcode);
105   exit(MR_DBMS_ERR);
106 }
This page took 0.044329 seconds and 5 git commands to generate.