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