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