Add macro %isu_package to generate ISU Package
[platform/upstream/rpm.git] / cliutils.c
1 #include "system.h"
2 #if HAVE_MCHECK_H
3 #include <mcheck.h>
4 #endif
5 #include <errno.h>
6 #include <sys/wait.h>
7
8 #include <rpm/rpmlog.h>
9 #include <rpm/rpmlib.h>
10 #include <rpm/rpmfileutil.h>
11 #include <rpm/rpmmacro.h>
12 #include <rpm/rpmcli.h>
13 #include "cliutils.h"
14 #include "debug.h"
15
16 static pid_t pipeChild = 0;
17
18 RPM_GNUC_NORETURN
19 void argerror(const char * desc)
20 {
21     fprintf(stderr, _("%s: %s\n"), xgetprogname(), desc);
22     exit(EXIT_FAILURE);
23 }
24
25 static void printVersion(FILE * fp)
26 {
27     fprintf(fp, _("RPM version %s\n"), rpmEVR);
28 }
29
30 static void printBanner(FILE * fp)
31 {
32     fprintf(fp, _("Copyright (C) 1998-2002 - Red Hat, Inc.\n"));
33     fprintf(fp, _("This program may be freely redistributed under the terms of the GNU GPL\n"));
34 }
35
36 void printUsage(poptContext con, FILE * fp, int flags)
37 {
38     printVersion(fp);
39     printBanner(fp);
40     fprintf(fp, "\n");
41
42     if (rpmIsVerbose())
43         poptPrintHelp(con, fp, flags);
44     else
45         poptPrintUsage(con, fp, flags);
46 }
47
48 int initPipe(void)
49 {
50     int p[2];
51
52     if (pipe(p) < 0) {
53         fprintf(stderr, _("creating a pipe for --pipe failed: %m\n"));
54         return -1;
55     }
56
57     if (!(pipeChild = fork())) {
58         (void) close(p[1]);
59         (void) dup2(p[0], STDIN_FILENO);
60         (void) close(p[0]);
61         (void) execl("/bin/sh", "/bin/sh", "-c", rpmcliPipeOutput, NULL);
62         fprintf(stderr, _("exec failed\n"));
63         exit(EXIT_FAILURE);
64     }
65
66     (void) close(p[0]);
67     (void) dup2(p[1], STDOUT_FILENO);
68     (void) close(p[1]);
69     return 0;
70 }
71
72 int finishPipe(void)
73 {
74     int rc = 0;
75     if (pipeChild) {
76         int status;
77         pid_t reaped;
78
79         (void) fclose(stdout);
80         do {
81             reaped = waitpid(pipeChild, &status, 0);
82         } while (reaped == -1 && errno == EINTR);
83             
84         if (reaped == -1 || !WIFEXITED(status) || WEXITSTATUS(status))
85             rc = 1;
86     }
87     return rc;
88 }