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