]> andersk Git - moira.git/blob - gen/pobox.dc
#include file and prototype fixes
[moira.git] / gen / pobox.dc
1 /* $Header$
2  *
3  * This generates a list of everyone's poboxes for the mitdir.
4  *
5  *  (c) Copyright 1992 by the Massachusetts Institute of Technology.
6  *  For copying and distribution information, please see the file
7  *  <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include <stdio.h>
12 #include <moira.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16 #include <ctype.h>
17 EXEC SQL INCLUDE sqlca;
18
19 extern int errno;
20 char *whoami = "pobox.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, i;
31     EXEC SQL BEGIN DECLARE SECTION;
32     char login[9], id[18], string[257];
33     EXEC SQL END DECLARE SECTION;
34
35     EXEC SQL CONNECT :db;
36
37     if (argc == 2) {
38         if (stat(argv[1], &sb) == 0) {
39             if (ModDiff (&flag, "users", sb.st_mtime))
40               exit(MR_DATE);
41             if (flag < 0) {
42                 fprintf(stderr, "File %s does not need to be rebuilt.\n",
43                         argv[1]);
44                 exit(MR_NO_CHANGE);
45             }
46         }
47         outf = argv[1];
48         sprintf(outft, "%s~", outf);
49         if ((out = fopen(outft, "w")) == NULL) {
50             fprintf(stderr, "unable to open %s for output\n", outf);
51             exit(MR_OCONFIG);
52         }
53     } else if (argc != 1) {
54         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
55         exit(MR_ARGS);
56     } else {
57         outf = NULL;
58     }
59
60     /* The following is declarative, not executed,
61      * and so is dependent on where it is in the file,
62      * not in the order of execution of statements.
63      */
64     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
65
66     EXEC SQL DECLARE x CURSOR FOR SELECT 
67       clearid, login
68         FROM users WHERE status != 3 and potype='POP';
69     EXEC SQL OPEN x;
70     while (1) {
71         EXEC SQL FETCH x INTO :id, :login;
72         if (sqlca.sqlcode != 0) break;
73         strtrim(login);
74         strtrim(id);
75         if (isdigit(id[0]))
76           fprintf(out, "%s %s@MIT.EDU\n", id, login);
77     }
78
79     EXEC SQL CLOSE x;
80
81     EXEC SQL DECLARE y CURSOR FOR SELECT 
82       u.clearid, s.string
83         FROM users u, strings s
84         WHERE u.status != 3 and u.potype='SMTP' and u.box_id = s.string_id;
85     EXEC SQL OPEN y;
86     while (1) {
87         EXEC SQL FETCH y INTO :id, :string;
88         if (sqlca.sqlcode != 0) break;
89         strtrim(string);
90         strtrim(id);
91         if (isdigit(id[0])) {
92             if ((i = strlen(string)) > 7 &&
93                 !strcasecmp(".local", string + i - 6)) {
94                 string[i-6] = 0;
95                 fprintf(out, "%s %s.mit.edu\n", id, string);
96             } else if ( !strchr(string, '@') ) {
97               fprintf(out, "%s %s@mit.edu\n", id, string);
98             } else
99               fprintf(out, "%s %s\n", id, string);
100         }
101     }
102
103     EXEC SQL CLOSE y;
104
105     EXEC SQL COMMIT;
106
107     if (fclose(out)) {
108         perror("close failed");
109         exit(MR_CCONFIG);
110     }
111     if (outf)
112       fix_file(outf);
113     exit(MR_SUCCESS);
114
115  sqlerr:
116     db_error(sqlca.sqlcode);
117     exit(MR_DBMS_ERR);
118 }
This page took 0.048775 seconds and 5 git commands to generate.