rpm  4.13.0
cliutils.c
Go to the documentation of this file.
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 
19 void argerror(const char * desc)
20 {
21  fprintf(stderr, _("%s: %s\n"), __progname, 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) signal(SIGPIPE, SIG_DFL);
59  (void) close(p[1]);
60  (void) dup2(p[0], STDIN_FILENO);
61  (void) close(p[0]);
62 #ifndef __OS2__
63  (void) execl("/bin/sh", "/bin/sh", "-c", rpmcliPipeOutput, NULL);
64 #else
65  (void) execl("/@unixroot/usr/bin/sh", "/@unixroot/usr/bin/sh", "-c", rpmcliPipeOutput, NULL);
66 #endif
67  fprintf(stderr, _("exec failed\n"));
68  exit(EXIT_FAILURE);
69  }
70 
71  (void) close(p[0]);
72  (void) dup2(p[1], STDOUT_FILENO);
73  (void) close(p[1]);
74  return 0;
75 }
76 
77 int finishPipe(void)
78 {
79  int rc = 0;
80  if (pipeChild) {
81  int status;
82  pid_t reaped;
83 
84  (void) fclose(stdout);
85  do {
86  reaped = waitpid(pipeChild, &status, 0);
87  } while (reaped == -1 && errno == EINTR);
88 
89  if (reaped == -1 || !WIFEXITED(status) || WEXITSTATUS(status))
90  rc = 1;
91  }
92  return rc;
93 }
_
#define _(Text)
Definition: system.h:112
rpmcliPipeOutput
const char * rpmcliPipeOutput
rpmEVR
const char *const rpmEVR
initPipe
int initPipe(void)
Definition: cliutils.c:48
__progname
#define __progname
Definition: system.h:97
printUsage
void printUsage(poptContext con, FILE *fp, int flags)
Definition: cliutils.c:36
argerror
void argerror(const char *desc)
Definition: cliutils.c:19
pipeChild
static pid_t pipeChild
Definition: cliutils.c:16
RPM_GNUC_NORETURN
#define RPM_GNUC_NORETURN
Definition: rpmutil.h:70
finishPipe
int finishPipe(void)
Definition: cliutils.c:77
system.h
rpmIsVerbose
#define rpmIsVerbose()
Definition: rpmlog.h:272
cliutils.h
debug.h
printBanner
static void printBanner(FILE *fp)
Definition: cliutils.c:30
printVersion
static void printVersion(FILE *fp)
Definition: cliutils.c:25