~ubuntu-branches/ubuntu/vivid/sup/vivid

« back to all changes in this revision

Viewing changes to supscan.c

  • Committer: Bazaar Package Importer
  • Author(s): Jochen Friedrich
  • Date: 2006-11-02 15:03:30 UTC
  • mfrom: (1.1.1 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20061102150330-yt4yavw4rj1hz8yj
Tags: 20060803-2
Fix CFLAGS, so libwrap is built into sup again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      $NetBSD: supscan.c,v 1.14 2006/04/02 01:39:48 christos Exp $    */
 
2
 
1
3
/*
2
4
 * Copyright (c) 1992 Carnegie Mellon University
3
5
 * All Rights Reserved.
4
 
 * 
 
6
 *
5
7
 * Permission to use, copy, modify and distribute this software and its
6
8
 * documentation is hereby granted, provided that both the copyright
7
9
 * notice and this permission notice appear in all copies of the
14
16
 *
15
17
 * Carnegie Mellon requests users of this software to return to
16
18
 *
17
 
 *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
 
19
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
18
20
 *  School of Computer Science
19
21
 *  Carnegie Mellon University
20
22
 *  Pittsburgh PA 15213-3890
38
40
 *
39
41
 **********************************************************************
40
42
 * HISTORY
41
 
 * $Log: supscan.c,v $
42
 
 * Revision 1.1.1.1  1993/08/21  00:46:35  jkh
43
 
 * Current sup with compression support.
44
 
 *
45
 
 * Revision 1.1.1.1  1993/05/21  14:52:19  cgd
46
 
 * initial import of CMU's SUP to NetBSD
47
 
 *
48
43
 * Revision 1.14  92/08/11  12:08:30  mrt
49
44
 *      Picked up Brad's deliniting and variable argument changes
50
45
 *      [92/08/10            mrt]
51
 
 * 
 
46
 *
52
47
 * Revision 1.13  92/02/08  18:04:44  dlc
53
48
 *      Once again revised localhost().  Do not use gethostbyname() at
54
49
 *      all, but assume that the host names in the coll.host file are at
55
50
 *      least a prefix of the fully qualified name.  Modcoll (and related
56
51
 *      scripts) will maintain this fact.
57
52
 *      [92/02/08            dlc]
58
 
 * 
 
53
 *
59
54
 * Revision 1.12  91/08/17  23:35:31  dlc
60
55
 *      Changes to localhost() function:
61
56
 *              - Use host name in kernel for local host name; assume it is
64
59
 *                fails, with TRY_AGAIN or NO_RECOVERY, then use the "host"
65
60
 *                parameter.  Print a diagnostic in this case.
66
61
 *      [91/08/17            dlc]
67
 
 * 
 
62
 *
68
63
 * Revision 1.11  90/04/04  10:53:01  dlc
69
64
 *      Changed localhost to retry getting the local host name 4 times with
70
65
 *      30 second sleep intervals before aborting; after 4 tries, things are
71
66
 *      probably too messed up for the supscan to do anything useful
72
67
 *      [90/04/04            dlc]
73
 
 * 
 
68
 *
74
69
 * Revision 1.10  89/08/03  19:49:33  mja
75
70
 *      Updated to use v*printf() in place of _doprnt().
76
71
 *      [89/04/19            mja]
77
 
 * 
 
72
 *
78
73
 * Revision 1.9  89/06/18  14:41:37  gm0w
79
74
 *      Fixed up some notify messages of errors to use "SUP:" prefix.
80
75
 *      [89/06/18            gm0w]
81
 
 * 
 
76
 *
82
77
 * 13-May-88  Glenn Marcy (gm0w) at Carnegie-Mellon University
83
78
 *      Changed goaway to longjmp back to top-level to scan next
84
79
 *      collection. [V7.6]
116
111
 **********************************************************************
117
112
 */
118
113
 
119
 
#include <libc.h>
120
 
#include <stdlib.h>
121
 
#include <c.h>
 
114
#include "libc.h"
 
115
#include "c.h"
122
116
#include <netdb.h>
123
117
#include <setjmp.h>
124
 
#if __STDC__
125
118
#include <stdarg.h>
126
 
#else
127
 
#include <varargs.h>
128
 
#endif
129
119
#include <sys/time.h>
130
 
#include <sys/types.h>
131
 
#include "sup.h"
132
 
 
133
 
#ifdef  lint
134
 
/*VARARGS1*//*ARGSUSED*/
135
 
static void quit(status) {};
136
 
#endif  /* lint */
 
120
#include <sys/resource.h>
 
121
#include <sys/param.h>
 
122
#include "supcdefs.h"
 
123
#include "supextern.h"
137
124
 
138
125
#define PGMVERSION 6
139
126
 
141
128
 ***    D A T A   S T R U C T U R E S    ***
142
129
 *******************************************/
143
130
 
144
 
struct collstruct {                     /* one per collection to be upgraded */
145
 
        char *Cname;                    /* collection name */
146
 
        char *Cbase;                    /* local base directory */
147
 
        char *Cprefix;                  /* local collection pathname prefix */
148
 
        struct collstruct *Cnext;       /* next collection */
 
131
struct scan_collstruct {        /* one per collection to be upgraded */
 
132
        char *Cname;            /* collection name */
 
133
        char *Cbase;            /* local base directory */
 
134
        char *Cprefix;          /* local collection pathname prefix */
 
135
        struct scan_collstruct *Cnext;  /* next collection */
149
136
};
150
 
typedef struct collstruct COLLECTION;
 
137
typedef struct scan_collstruct SCAN_COLLECTION;
151
138
 
152
139
/*********************************************
153
140
 ***    G L O B A L   V A R I A B L E S    ***
154
141
 *********************************************/
155
142
 
156
 
int trace;                              /* -v flag */
157
 
 
158
 
COLLECTION *firstC;                     /* collection list pointer */
159
 
char *collname;                         /* collection name */
160
 
char *basedir;                          /* base directory name */
161
 
char *prefix;                           /* collection pathname prefix */
162
 
long lasttime = 0;                      /* time of last upgrade */
163
 
long scantime;                          /* time of this scan */
164
 
int newonly = FALSE;                    /* new files only */
165
 
jmp_buf sjbuf;                          /* jump location for errors */
166
 
 
167
 
TREELIST *listTL;       /* list of all files specified by <coll>.list */
168
 
TREE *listT;            /* final list of files in collection */
169
 
TREE *refuseT = NULL;   /* list of all files specified by <coll>.list */
170
 
 
171
 
long time ();
 
143
int trace;                      /* -v flag */
 
144
int quiet;                      /* -q flag */
 
145
 
 
146
SCAN_COLLECTION *firstC;        /* collection list pointer */
 
147
char *collname;                 /* collection name */
 
148
char *basedir;                  /* base directory name */
 
149
char *prefix;                   /* collection pathname prefix */
 
150
time_t lasttime = 0;            /* time of last upgrade */
 
151
time_t scantime;                /* time of this scan */
 
152
int newonly = FALSE;            /* new files only */
 
153
jmp_buf sjbuf;                  /* jump location for errors */
 
154
 
 
155
TREELIST *listTL;               /* list of all files specified by <coll>.list */
 
156
TREE *listT;                    /* final list of files in collection */
 
157
TREE *refuseT = NULL;           /* list of all files specified by <coll>.list */
 
158
 
 
159
 
 
160
void usage(void);
 
161
void init(int, char **);
 
162
static SCAN_COLLECTION *getscancoll(char *, char *, char *);
 
163
int localhost(char *);
 
164
int main(int, char **);
172
165
 
173
166
/*************************************
174
167
 ***    M A I N   R O U T I N E    ***
175
168
 *************************************/
176
169
 
177
 
main (argc,argv)
178
 
int argc;
179
 
char **argv;
 
170
int
 
171
main(int argc, char **argv)
180
172
{
181
 
        register COLLECTION *c;
182
 
 
183
 
        init (argc,argv);               /* process arguments */
 
173
        SCAN_COLLECTION *c;
 
174
#ifdef RLIMIT_DATA
 
175
        struct rlimit dlim;
 
176
 
 
177
        if (getrlimit(RLIMIT_DATA, &dlim) == -1)
 
178
                goaway("Error getting resource limit (%s)",
 
179
                    strerror(errno));
 
180
        if (dlim.rlim_cur != dlim.rlim_max) {
 
181
                dlim.rlim_cur = dlim.rlim_max;
 
182
                if (setrlimit(RLIMIT_DATA, &dlim) == -1)
 
183
                        goaway("Error setting resource limit (%s)",
 
184
                            strerror(errno));
 
185
        }
 
186
#endif
 
187
 
 
188
#if __GNUC__
 
189
        /* Avoid longjmp clobbering */
 
190
        (void) &c;
 
191
#endif
 
192
 
 
193
        init(argc, argv);       /* process arguments */
184
194
        for (c = firstC; c; c = c->Cnext) {
185
195
                collname = c->Cname;
186
196
                basedir = c->Cbase;
187
197
                prefix = c->Cprefix;
188
 
                (void) chdir (basedir);
189
 
                scantime = time ((long *)NULL);
190
 
                printf ("SUP Scan for %s starting at %s",collname,
191
 
                        ctime (&scantime));
192
 
                (void) fflush (stdout);
193
 
                if (!setjmp (sjbuf)) {
194
 
                        makescanlists (); /* record names in scan files */
195
 
                        scantime = time ((long *)NULL);
196
 
                        printf ("SUP Scan for %s completed at %s",collname,
197
 
                                ctime (&scantime));
 
198
                (void) chdir(basedir);
 
199
                scantime = time((time_t *) NULL);
 
200
                if (!quiet)
 
201
                        printf("SUP Scan for %s starting at %s", collname,
 
202
                            ctime(&scantime));
 
203
                (void) fflush(stdout);
 
204
                if (!setjmp(sjbuf)) {
 
205
                        makescanlists();        /* record names in scan files */
 
206
                        scantime = time((time_t *) NULL);
 
207
                        if (!quiet)
 
208
                                printf("SUP Scan for %s completed at %s",
 
209
                                    collname, ctime(&scantime));
198
210
                } else
199
 
                        printf ("SUP: Scan for %s aborted at %s",collname,
200
 
                                ctime (&scantime));
201
 
                (void) fflush (stdout);
 
211
                        fprintf(stderr,
 
212
                            "SUP: Scan for %s aborted at %s", collname,
 
213
                            ctime(&scantime));
 
214
                if (!quiet)
 
215
                        (void) fflush(stdout);
202
216
        }
203
 
        while (c = firstC) {
 
217
        while ((c = firstC) != NULL) {
204
218
                firstC = firstC->Cnext;
205
 
                free (c->Cname);
206
 
                free (c->Cbase);
207
 
                if (c->Cprefix)  free (c->Cprefix);
208
 
                free ((char *)c);
 
219
                free(c->Cname);
 
220
                free(c->Cbase);
 
221
                if (c->Cprefix)
 
222
                        free(c->Cprefix);
 
223
                free(c);
209
224
        }
210
 
        exit (0);
 
225
        exit(0);
211
226
}
212
 
 
213
227
/*****************************************
214
228
 ***    I N I T I A L I Z A T I O N    ***
215
229
 *****************************************/
216
230
 
217
 
usage ()
 
231
void
 
232
usage(void)
218
233
{
219
 
        fprintf (stderr,"Usage: supscan [ -v ] collection [ basedir ]\n");
220
 
        fprintf (stderr,"       supscan [ -v ] -f dirfile\n");
221
 
        fprintf (stderr,"       supscan [ -v ] -s\n");
222
 
        exit (1);
 
234
        fprintf(stderr, "Usage: supscan [ -vq ] collection [ basedir ]\n");
 
235
        fprintf(stderr, "       supscan [ -vq ] -f dirfile\n");
 
236
        fprintf(stderr, "       supscan [ -vq ] -s\n");
 
237
        exit(1);
223
238
}
224
239
 
225
 
init (argc,argv)
226
 
int argc;
227
 
char **argv;
 
240
void
 
241
init(int argc, char **argv)
228
242
{
229
 
        char buf[STRINGLENGTH],fbuf[STRINGLENGTH],*p,*q;
 
243
        char buf[STRINGLENGTH], fbuf[STRINGLENGTH], *p, *q;
230
244
        FILE *f;
231
 
        COLLECTION **c, *getcoll();
232
 
        int fflag,sflag;
233
 
        char *filename;
 
245
        SCAN_COLLECTION **c;
 
246
        int fflag, sflag;
 
247
        char *filename = NULL;
234
248
 
 
249
        quiet = FALSE;
235
250
        trace = FALSE;
236
251
        fflag = FALSE;
237
252
        sflag = FALSE;
240
255
                case 'f':
241
256
                        fflag = TRUE;
242
257
                        if (argc == 2)
243
 
                                usage ();
 
258
                                usage();
244
259
                        --argc;
245
260
                        argv++;
246
261
                        filename = argv[1];
247
262
                        break;
 
263
                case 'q':
 
264
                        quiet = TRUE;
 
265
                        break;
248
266
                case 'v':
249
267
                        trace = TRUE;
250
268
                        break;
252
270
                        sflag = TRUE;
253
271
                        break;
254
272
                default:
255
 
                        fprintf (stderr,"supscan: Invalid flag %s ignored\n",argv[1]);
256
 
                        (void) fflush (stderr);
 
273
                        fprintf(stderr, "supscan: Invalid flag %s ignored\n",
 
274
                            argv[1]);
 
275
                        (void) fflush(stderr);
257
276
                }
258
277
                --argc;
259
278
                argv++;
260
279
        }
261
280
        if (!fflag) {
262
 
                (void) sprintf (fbuf,FILEDIRS,DEFDIR);
 
281
                (void) sprintf(fbuf, FILEDIRS, DEFDIR);
263
282
                filename = fbuf;
264
283
        }
265
284
        if (sflag) {
266
285
                if (argc != 1)
267
 
                        usage ();
 
286
                        usage();
268
287
                firstC = NULL;
269
288
                c = &firstC;
270
 
                (void) sprintf (buf,FILEHOSTS,DEFDIR);
271
 
                if ((f = fopen (buf,"r")) == NULL)
272
 
                        quit (1,"supscan: Unable to open %s\n",buf);
273
 
                while ((p = fgets (buf,STRINGLENGTH,f)) != NULL) {
274
 
                        q = index (p,'\n');
275
 
                        if (q)  *q = 0;
276
 
                        if (index ("#;:",*p))  continue;
277
 
                        collname = nxtarg (&p," \t=");
278
 
                        p = skipover (p," \t=");
279
 
                        if (!localhost (p))  continue;
280
 
                        *c = getcoll(filename,salloc (collname),
281
 
                                        (char *)NULL);
282
 
                        if (*c)  c = &((*c)->Cnext);
 
289
                (void) sprintf(buf, FILEHOSTS, DEFDIR);
 
290
                if ((f = fopen(buf, "r")) == NULL)
 
291
                        quit(1, "supscan: Unable to open %s\n", buf);
 
292
                while ((p = fgets(buf, STRINGLENGTH, f)) != NULL) {
 
293
                        q = index(p, '\n');
 
294
                        if (q)
 
295
                                *q = 0;
 
296
                        if (index("#;:", *p))
 
297
                                continue;
 
298
                        collname = nxtarg(&p, " \t=");
 
299
                        p = skipover(p, " \t=");
 
300
                        if (!localhost(p))
 
301
                                continue;
 
302
                        *c = getscancoll(filename, estrdup(collname),
 
303
                            (char *) NULL);
 
304
                        if (*c)
 
305
                                c = &((*c)->Cnext);
283
306
                }
284
 
                (void) fclose (f);
 
307
                (void) fclose(f);
285
308
                return;
286
309
        }
287
310
        if (argc < 2 && fflag) {
288
311
                firstC = NULL;
289
312
                c = &firstC;
290
 
                if ((f = fopen (filename,"r")) == NULL)
291
 
                        quit (1,"supscan: Unable to open %s\n",filename);
292
 
                while (p = fgets (buf,STRINGLENGTH,f)) {
293
 
                        q = index (p,'\n');
294
 
                        if (q)  *q = 0;
295
 
                        if (index ("#;:",*p))  continue;
296
 
                        q = nxtarg (&p," \t=");
297
 
                        p = skipover (p," \t=");
298
 
                        *c = getcoll(filename,salloc (q),salloc (p));
299
 
                        if (*c)  c = &((*c)->Cnext);
 
313
                if ((f = fopen(filename, "r")) == NULL)
 
314
                        quit(1, "supscan: Unable to open %s\n", filename);
 
315
                while ((p = fgets(buf, STRINGLENGTH, f)) != NULL) {
 
316
                        q = index(p, '\n');
 
317
                        if (q)
 
318
                                *q = 0;
 
319
                        if (index("#;:", *p))
 
320
                                continue;
 
321
                        q = nxtarg(&p, " \t=");
 
322
                        p = skipover(p, " \t=");
 
323
                        *c = getscancoll(filename, estrdup(q), estrdup(p));
 
324
                        if (*c)
 
325
                                c = &((*c)->Cnext);
300
326
                }
301
 
                (void) fclose (f);
 
327
                (void) fclose(f);
302
328
                return;
303
329
        }
304
330
        if (argc < 2 || argc > 3)
305
 
                usage ();
306
 
        firstC = getcoll(filename,salloc (argv[1]),
307
 
                        argc > 2 ? salloc (argv[2]) : (char *)NULL);
 
331
                usage();
 
332
        firstC = getscancoll(filename, estrdup(argv[1]),
 
333
            argc > 2 ? estrdup(argv[2]) : (char *) NULL);
308
334
}
309
335
 
310
 
COLLECTION *
311
 
getcoll(filename, collname, basedir)
312
 
register char *filename,*collname,*basedir;
 
336
static SCAN_COLLECTION *
 
337
getscancoll(char *filename, char *collname, char *basedir)
313
338
{
314
 
        char buf[STRINGLENGTH],*p,*q;
 
339
        char buf[STRINGLENGTH], *p, *q;
315
340
        FILE *f;
316
 
        COLLECTION *c;
 
341
        SCAN_COLLECTION *c;
317
342
 
318
343
        if (basedir == NULL) {
319
 
                if (f = fopen (filename,"r")) {
320
 
                        while (p = fgets (buf,STRINGLENGTH,f)) {
321
 
                                q = index (p,'\n');
322
 
                                if (q)  *q = 0;
323
 
                                if (index ("#;:",*p))  continue;
324
 
                                q = nxtarg (&p," \t=");
325
 
                                if (strcmp (q,collname) == 0) {
326
 
                                        p = skipover (p," \t=");
327
 
                                        basedir = salloc (p);
 
344
                if ((f = fopen(filename, "r")) != NULL) {
 
345
                        while ((p = fgets(buf, STRINGLENGTH, f)) != NULL) {
 
346
                                q = index(p, '\n');
 
347
                                if (q)
 
348
                                        *q = 0;
 
349
                                if (index("#;:", *p))
 
350
                                        continue;
 
351
                                q = nxtarg(&p, " \t=");
 
352
                                if (strcmp(q, collname) == 0) {
 
353
                                        p = skipover(p, " \t=");
 
354
                                        basedir = estrdup(p);
328
355
                                        break;
329
356
                                }
330
357
                        }
331
 
                        (void) fclose (f);
 
358
                        (void) fclose(f);
332
359
                }
333
360
                if (basedir == NULL) {
334
 
                        (void) sprintf (buf,FILEBASEDEFAULT,collname);
335
 
                        basedir = salloc (buf);
 
361
                        (void) sprintf(buf, FILEBASEDEFAULT, collname);
 
362
                        basedir = estrdup(buf);
336
363
                }
337
364
        }
338
365
        if (chdir(basedir) < 0) {
339
 
                fprintf (stderr,"supscan:  Can't chdir to base directory %s for %s\n",
340
 
                        basedir,collname);
 
366
                fprintf(stderr, "supscan:  Can't chdir to base directory %s for %s\n",
 
367
                    basedir, collname);
341
368
                return (NULL);
342
369
        }
343
370
        prefix = NULL;
344
 
        (void) sprintf (buf,FILEPREFIX,collname);
345
 
        if (f = fopen (buf,"r")) {
346
 
                while (p = fgets (buf,STRINGLENGTH,f)) {
347
 
                        q = index (p,'\n');
348
 
                        if (q) *q = 0;
349
 
                        if (index ("#;:",*p))  continue;
350
 
                        prefix = salloc (p);
 
371
        (void) sprintf(buf, FILEPREFIX, collname);
 
372
        if ((f = fopen(buf, "r")) != NULL) {
 
373
                while ((p = fgets(buf, STRINGLENGTH, f)) != NULL) {
 
374
                        q = index(p, '\n');
 
375
                        if (q)
 
376
                                *q = 0;
 
377
                        if (index("#;:", *p))
 
378
                                continue;
 
379
                        prefix = estrdup(p);
351
380
                        if (chdir(prefix) < 0) {
352
 
                                fprintf (stderr,"supscan: can't chdir to %s from base directory %s for %s\n",
353
 
                                        prefix,basedir,collname);
 
381
                                fprintf(stderr, "supscan: can't chdir to %s from base directory %s for %s\n",
 
382
                                    prefix, basedir, collname);
 
383
                                fclose(f);
 
384
                                free(prefix);
354
385
                                return (NULL);
355
386
                        }
356
387
                        break;
357
388
                }
358
 
                (void) fclose (f);
 
389
                (void) fclose(f);
359
390
        }
360
 
        if ((c = (COLLECTION *) malloc (sizeof(COLLECTION))) == NULL)
361
 
                quit (1,"supscan: can't malloc collection structure\n");
 
391
        if ((c = (SCAN_COLLECTION *) malloc(sizeof(SCAN_COLLECTION))) == NULL)
 
392
                quit(1, "supscan: can't malloc collection structure\n");
362
393
        c->Cname = collname;
363
394
        c->Cbase = basedir;
364
395
        c->Cprefix = prefix;
365
396
        c->Cnext = NULL;
366
397
        return (c);
367
398
}
368
 
#if __STDC__
369
 
goaway (char *fmt,...)
370
 
#else
371
 
/*VARARGS*//*ARGSUSED*/
372
 
goaway (va_alist)
373
 
va_dcl
374
 
#endif
 
399
 
 
400
void
 
401
goaway(char *fmt, ...)
375
402
{
376
 
#if !__STDC__
377
 
        char *fmt;
378
 
#endif
379
403
        va_list ap;
380
404
 
381
 
#if __STDC__
382
 
        va_start(ap,fmt);
383
 
#else
384
 
        va_start(ap);
385
 
        fmt = va_arg(ap,char *);
386
 
#endif
387
 
 
 
405
        va_start(ap, fmt);
388
406
        vfprintf(stderr, fmt, ap);
389
407
        va_end(ap);
390
 
        (void) putc ('\n',stderr);
391
 
        (void) fflush (stderr);
392
 
        longjmp (sjbuf,TRUE);
 
408
        (void) putc('\n', stderr);
 
409
        (void) fflush(stderr);
 
410
        longjmp(sjbuf, TRUE);
393
411
}
394
412
 
395
 
int localhost (host)
396
 
register char *host;
 
413
int 
 
414
localhost(char *host)
397
415
{
398
 
        static char myhost[STRINGLENGTH];
399
 
        static int myhostlen;
400
 
        register int hostlen;
 
416
        static char myhost[MAXHOSTNAMELEN + 1];
 
417
        static unsigned int myhostlen;
 
418
        unsigned int hostlen;
401
419
 
402
420
        if (*myhost == '\0') {
403
421
                /*
404
422
                 * We assume that the host name in the kernel is the
405
423
                 * fully qualified form.
406
424
                 */
407
 
                if (gethostname (myhost,sizeof (myhost)) < 0) {
408
 
                    quit (1,"supscan: can't get kernel host name\n");
 
425
                if (gethostname(myhost, sizeof(myhost)) < 0) {
 
426
                        quit(1, "supscan: can't get kernel host name\n");
409
427
                }
 
428
                myhost[sizeof(myhost) - 1] = '\0';
410
429
                myhostlen = strlen(myhost);
411
430
        }
412
 
 
413
431
        /*
414
432
         * Here, we assume that the 'host' parameter from the
415
433
         * coll.host file is at least a prefix of the fully qualified
423
441
 
424
442
        hostlen = strlen(host);
425
443
 
426
 
        return(strncasecmp (myhost,
427
 
                            host,
428
 
                            hostlen < myhostlen ? hostlen : myhostlen) == 0);
 
444
        return (strncasecmp(myhost, host,
 
445
                hostlen < myhostlen ? hostlen : myhostlen) == 0);
429
446
}