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