]> andersk Git - moira.git/blob - gen/directory.pc
910c8072352e292a1898561dd91e3335bfa901ae
[moira.git] / gen / directory.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 <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 = "directory.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], last_name[17], first_name[17], middle_name[16];
31   char office_address[17], office_phone[13];
32   char home_address[82], home_phone[17];
33   char id[17], type[9];
34   EXEC SQL END DECLARE SECTION;
35
36   EXEC SQL CONNECT :db;
37
38   if (argc == 2)
39     {
40       if (stat(argv[1], &sb) == 0)
41         {
42           if (ModDiff (&flag, "users", sb.st_mtime))
43             exit(MR_DATE);
44           if (flag < 0)
45             {
46               fprintf(stderr, "File %s does not need to be rebuilt.\n",
47                       argv[1]);
48               exit(MR_NO_CHANGE);
49             }
50         }
51       outf = argv[1];
52       sprintf(outft, "%s~", outf);
53       if (!(out = fopen(outft, "w")))
54         {
55           fprintf(stderr, "unable to open %s for output\n", outf);
56           exit(MR_OCONFIG);
57         }
58     }
59   else if (argc != 1)
60     {
61       fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
62       exit(MR_ARGS);
63     }
64   else
65     outf = NULL;
66
67   EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
68
69   EXEC SQL DECLARE x CURSOR FOR SELECT 
70     login, last, first, middle, office_addr, office_phone, 
71     home_addr, home_phone, clearid, type 
72     FROM users WHERE status = 1 AND type != 'SYSTEM' AND type != 'STAFF'
73     AND type NOT LIKE 'GUES%';
74   EXEC SQL OPEN x;
75   while (1)
76     {
77       EXEC SQL FETCH x INTO :login, :last_name, :first_name, :middle_name,
78         :office_address, :office_phone, :home_address, :home_phone,
79         :id, :type;
80       if (sqlca.sqlcode)
81         break;
82       strtrim(login);
83       strtrim(last_name);
84       strtrim(first_name);
85       strtrim(middle_name);
86       strtrim(office_address);
87       strtrim(office_phone);
88       strtrim(home_address);
89       strtrim(home_phone);
90       strtrim(id);
91       strtrim(type);
92 #ifdef notdef
93       if(isdigit(*id))
94         fprintf(out, "%s^4:p\t24:%s %s\t7:%s, %s %s\t3:%s, %s "
95                 "%s\t5:%s\t2:%s@mit.edu\t1:%s\t0:%s\t14:%s\t15:%s\t10:"
96                 "Unlisted Account\n", id, last_name, login, last_name,
97                 first_name, middle_name, last_name, first_name,
98                 middle_name, login, login, office_phone, office_address,
99                 home_phone, home_address);
100 #else
101       if(isdigit(*id))
102         fprintf(out, "%s^4:p\t24:%s %s\t7:%s, %s %s\t3:%s, %s "
103                 "%s\t5:%s\t2:%s@mit.edu\t1:%s\t0:%s\t14:%s\t15:%s\t10:%s\n",
104                 id, last_name, login, last_name, first_name, middle_name,
105                 last_name, first_name, middle_name, login, login,
106                 "", "", "", "", strcmp(type, "G") == 0 ? "G" : 
107                 !strncmp(type, "19", 2) ? "Undergraduate" : 
108                 !strncmp(type, "20", 2) ? "Undergraduate" : "MIT Affiliate");
109 #endif
110     }
111   EXEC SQL CLOSE x;
112
113   EXEC SQL COMMIT;
114
115   if (fclose(out))
116     {
117       perror("close failed");
118       exit(MR_CCONFIG);
119     }
120   if (outf)
121     fix_file(outf);
122   exit(MR_SUCCESS);
123
124 sqlerr:
125   db_error(sqlca.sqlcode);
126   exit(MR_DBMS_ERR);
127 }
This page took 0.052148 seconds and 3 git commands to generate.