3cca7df4cc900c89e0d63916d059e5245d5374e9
[dragonfly.git] / usr.sbin / pkg_install / sign / common.c
1 /* $OpenBSD: common.c,v 1.3 1999/10/07 16:30:32 espie Exp $ */
2 /*-
3  * Copyright (c) 1999 Marc Espie.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Marc Espie for the OpenBSD
16  * Project.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS 
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
21  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
22  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD: src/usr.sbin/pkg_install/sign/common.c,v 1.1.2.2 2002/08/20 06:35:08 obrien Exp $
31  * $DragonFly: src/usr.sbin/pkg_install/sign/Attic/common.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
32  */
33
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <sys/stat.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include "stand.h"
41 #include "gzip.h"
42 #include "pgp.h"
43 #include "extern.h"
44
45 /* Ensure consistent diagnostics */
46 int 
47 read_header_and_diagnose(file, h, sign, filename)
48         FILE *file;
49         struct mygzip_header *h;
50         struct signature **sign;
51         const char *filename;
52 {
53         switch(gzip_read_header(file, h, sign)) {
54         case GZIP_SIGNED:
55                 if (sign == NULL) {
56                         fprintf(stderr, "File %s is already signed\n", filename);
57                         return 0;
58                 } else
59                         return 1;
60         case GZIP_UNSIGNED:
61                 if (sign != NULL) {
62                         fprintf(stderr, "File %s is not a signed gzip file\n", filename);
63                         return 0;
64                 } else
65                         return 1;
66         case GZIP_NOT_GZIP:
67                 fprintf(stderr, "File %s is not a gzip file\n", filename);
68                 return 0;
69         case GZIP_NOT_PGPSIGNED:
70                 fprintf(stderr, "File %s contains an unknown extension\n", filename);
71                 return 0;
72         default:
73                 /* this should not happen */
74                 abort();
75         }
76 }
77
78 int 
79 reap(pid)
80         pid_t pid;
81 {
82         int pstat;
83         pid_t result;
84
85         do {
86                 result = waitpid(pid, &pstat, 0);
87         } while (result == -1 && errno == EINTR);
88         return result == -1 ? -1 : pstat;
89 }
90