FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ununpack-ar.c
Go to the documentation of this file.
1 /*******************************************************************
2  Copyright (C) 2007-2011 Hewlett-Packard Development Company, L.P.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  *******************************************************************/
17 
18 #include "ununpack.h"
19 #include "externs.h"
20 
37 int ExtractAR (char *Source, char *Destination)
38 {
39  char Cmd[FILENAME_MAX*4]; /* command to run */
40  char Line[FILENAME_MAX];
41  char *s; /* generic string pointer */
42  FILE *Fin;
43  int rc;
44  char TempSource[FILENAME_MAX];
45  char CWD[FILENAME_MAX];
46 
47  /* judge if the parameters are empty */
48  if ((NULL == Source) || (!strcmp(Source, "")) || (NULL == Destination) || (!strcmp(Destination, "")))
49  return 1;
50 
51  if (getcwd(CWD,sizeof(CWD)) == NULL)
52  {
53  fprintf(stderr,"ERROR: directory name longer than %d characters\n",(int)sizeof(CWD));
54  return(-1);
55  }
56  if (Verbose > 1)
57  {
58  printf("CWD: %s\n",CWD);
59  if (!Quiet) fprintf(stderr,"Extracting ar: %s\n",Source);
60  }
61 
62  if(chdir(Destination) != 0)
63  {
64  fprintf(stderr, "ERROR %s.%d: Unable to change directory to %s\n",
65  __FILE__, __LINE__, Destination);
66  fprintf(stderr, "ERROR: errno is: %s\n", strerror(errno));
67  }
68 
69  if (TaintString(TempSource,FILENAME_MAX,Source,1,NULL))
70  return(-1);
71  memset(Cmd,'\0',sizeof(Cmd));
72 
73  /* get list of directories and make the directories */
74  /* Cmd: ar t %s 2>/dev/null | grep '^Directory' */
75  if (TempSource[0] != '/')
76  snprintf(Cmd,sizeof(Cmd)," (ar t '%s/%s') 2>/dev/null",CWD,TempSource);
77  else
78  snprintf(Cmd,sizeof(Cmd)," (ar t '%s') 2>/dev/null",TempSource);
79 
80  Fin = popen(Cmd,"r");
81  if (!Fin)
82  {
83  fprintf(stderr,"ERROR: ar failed: %s\n",Cmd);
84  if(chdir(CWD) != 0)
85  {
86  fprintf(stderr, "ERROR %s.%d: Unable to change directory to %s\n",
87  __FILE__, __LINE__, CWD);
88  fprintf(stderr, "ERROR: errno is: %s\n", strerror(errno));
89  }
90  return(-1);
91  }
92  while(ReadLine(Fin,Line,sizeof(Line)-1) >= 0)
93  {
94  /* each line is a file. Check for directories. */
95  if (Line[0]=='/') { pclose(Fin); return(1); } /* NO ABSOLUTE PATHS! */
96  s=strrchr(Line,'/'); /* find the last slash */
97  if (s == NULL) continue;
98  s[0]='\0';
99  if (MkDir(Line))
100  {
101  fprintf(stderr,"ERROR: Unable to mkdir(%s) in ExtractAR\n",Line);
102  if (!ForceContinue) exit(-1);
103  }
104  }
105  pclose(Fin);
106 
107  /* Now let's extract each file */
108  if (TempSource[0] != '/')
109  snprintf(Cmd,sizeof(Cmd)," (ar x '%s/%s') 2>/dev/null",CWD,TempSource);
110  else
111  snprintf(Cmd,sizeof(Cmd)," (ar x '%s') 2>/dev/null",TempSource);
112  rc = WEXITSTATUS(system(Cmd));
113  if (rc)
114  {
115  fprintf(stderr,"ERROR: Command failed (rc=%d): %s\n",rc,Cmd);
116  }
117 
118  /* All done */
119  if(chdir(CWD) != 0)
120  {
121  fprintf(stderr, "ERROR %s.%d: Unable to change directory to %s\n",
122  __FILE__, __LINE__, CWD);
123  fprintf(stderr, "ERROR: errno is: %s\n", strerror(errno));
124  }
125  return(rc);
126 } /* ExtractAR() */
int Quiet
Run in quiet mode?
int Verbose
Verbose level.
Definition: util.c:28
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:48
int ExtractAR(char *Source, char *Destination)
Given an AR file, extract the contents to the directory. This uses the command ar.
Definition: ununpack-ar.c:37
int MkDir(char *Fname)
Smart mkdir.
Definition: utils.c:314
Stores all extern variables used by the agent.
char * TaintString(char *S)
Create a string with taint quoting.
Definition: finder.c:46
int ReadLine(FILE *Fin, char *Line, int MaxLine)
Definition: repcopyin.c:77
int ForceContinue
Force continue when unpack tool fails?