FOSSology  4.6.0
Open Source License Compliance by Open Source Software
ununpack-lzip.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2025 Siemens Healthineers AG
3  SPDX-FileContributor: Sushant Kumar <sushant.kumar@siemens-healthineers.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #include "ununpack.h"
9 #include "externs.h"
10 
23 int ExtractLzip(char *Source, const char *OrigName, char *Destination)
24 {
25  char Cmd[FILENAME_MAX * 4]; /* command to run */
26  int rc;
27  char TempSource[FILENAME_MAX];
28  char CWD[FILENAME_MAX];
29  char OutputName[FILENAME_MAX] = {'\0'};
30  int i;
31 
32  /* judge if the parameters are empty */
33  if ((NULL == Source) || (!strcmp(Source, "")) || (NULL == Destination) ||
34  (!strcmp(Destination, ""))) {
35  return 1;
36  }
37 
38  if (getcwd(CWD, sizeof(CWD)) == NULL) {
39  fprintf(stderr, "ERROR: directory name longer than %d characters\n",
40  (int) sizeof(CWD));
41  return (-1);
42  }
43 
44  i = 0;
45  while (OrigName[i] != '\0') {
46  if (OrigName[i] == '.') {
47  const char *ext = &OrigName[i + 1];
48  if (strncasecmp(ext, "lz", 2) == 0) {
49  break;
50  }
51  else if (strncasecmp(ext, "tlz", 3) == 0) {
52  /* Usually tlz implies tar.lz, stripping extension leaves base name.
53  Subsequent file type detection will handle the resulting tar. */
54  break;
55  }
56  }
57  OutputName[i] = OrigName[i];
58  i++;
59  }
60  /* Null terminate the output name explicitly */
61  OutputName[i] = '\0';
62 
63  if (Verbose > 1) {
64  printf("CWD: %s\n", CWD);
65  if (!Quiet) { fprintf(stderr, "Extracting lzip: %s\n", Source); }
66  }
67 
68  if (chdir(Destination) != 0) {
69  fprintf(stderr, "ERROR %s.%d: Unable to change directory to %s\n",
70  __FILE__, __LINE__, Destination);
71  fprintf(stderr, "ERROR: errno is: %s\n", strerror(errno));
72  }
73 
74  if (TaintString(TempSource, FILENAME_MAX, Source, 1, NULL)) {
75  return (-1);
76  }
77  memset(Cmd, '\0', sizeof(Cmd));
78 
79  if (Verbose > 1) {
80  printf("Extracting lzip with output name: %s\n", OutputName);
81  }
82 
83  /* lzip options:
84  * -d, --decompress
85  * -k, --keep (keep input file, consistent with how standard tools behave)
86  * -f, --force (overwrite output if exists)
87  * -o, --output (specify output filename)
88  */
89  if (TempSource[0] != '/') {
90  snprintf(Cmd, sizeof(Cmd),
91  " (lzip --decompress --keep --force -o '%s/%s' "
92  "'%s/%s') 2>/dev/null", Destination, OutputName,
93  CWD, TempSource);
94  }
95  else {
96  snprintf(Cmd, sizeof(Cmd),
97  " (lzip --decompress --keep --force -o '%s/%s' "
98  "'%s') 2>/dev/null", Destination, OutputName,
99  TempSource);
100  }
101 
102  rc = WEXITSTATUS(system(Cmd));
103  if (rc) {
104  fprintf(stderr, "ERROR: Command failed (rc=%d): %s\n", rc, Cmd);
105  }
106 
107  if (chdir(CWD) != 0) {
108  fprintf(stderr, "ERROR %s.%d: Unable to change directory to %s\n", __FILE__, __LINE__, CWD);
109  fprintf(stderr, "ERROR: errno is: %s\n", strerror(errno));
110  }
111  return (rc);
112 } /* ExtractLzip() */
int Verbose
Verbose level.
Definition: util.c:19
Stores all extern variables used by the agent.
int Quiet
Run in quiet mode?
char * TaintString(char *S)
Create a string with taint quoting.
Definition: finder.c:35
int ExtractLzip(char *Source, const char *OrigName, char *Destination)
Given an lzip file, extract the contents to the directory.
Definition: ununpack-lzip.c:23