Rizin
unix-like reverse engineering framework and cli tools
untgz.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include "zlib.h"
#include <direct.h>
#include <io.h>
#include <utime.h>

Go to the source code of this file.

Classes

struct  tar_header
 
union  tar_buffer
 
struct  attr_item
 

Macros

#define REGTYPE   '0' /* regular file */
 
#define AREGTYPE   '\0' /* regular file */
 
#define LNKTYPE   '1' /* link */
 
#define SYMTYPE   '2' /* reserved */
 
#define CHRTYPE   '3' /* character special */
 
#define BLKTYPE   '4' /* block special */
 
#define DIRTYPE   '5' /* directory */
 
#define FIFOTYPE   '6' /* FIFO special */
 
#define CONTTYPE   '7' /* reserved */
 
#define GNUTYPE_DUMPDIR   'D' /* file names from dumped directory */
 
#define GNUTYPE_LONGLINK   'K' /* long link name */
 
#define GNUTYPE_LONGNAME   'L' /* long file name */
 
#define GNUTYPE_MULTIVOL   'M' /* continuation of file from another volume */
 
#define GNUTYPE_NAMES   'N' /* file name that does not fit into main hdr */
 
#define GNUTYPE_SPARSE   'S' /* sparse file */
 
#define GNUTYPE_VOLHDR   'V' /* tape/volume header */
 
#define BLOCKSIZE   512
 
#define SHORTNAMESIZE   100
 
#define ISSPECIAL(c)   (((c) == '*') || ((c) == '/'))
 

Enumerations

enum  { TGZ_EXTRACT , TGZ_LIST , TGZ_INVALID }
 

Functions

char *TGZfname OF ((const char *))
 
int getoct OF ((char *, int))
 
char *strtime OF ((time_t *))
 
void push_attr OF ((struct attr_item **, char *, int, time_t))
 
void restore_attr OF ((struct attr_item **))
 
int ExprMatch OF ((char *, char *))
 
int makedir OF ((char *))
 
int matchname OF ((int, int, char **, char *))
 
int tar OF ((gzFile, int, int, int, char **))
 
void help OF ((int))
 
int main OF ((int, char **))
 
char * TGZfname (const char *arcname)
 
void TGZnotfound (const char *arcname)
 
int getoct (char *p, int width)
 
char * strtime (time_t *t)
 
int setfiletime (char *fname, time_t ftime)
 
void push_attr (struct attr_item **list, char *fname, int mode, time_t time)
 
void restore_attr (struct attr_item **list)
 
int ExprMatch (char *string, char *expr)
 
int makedir (char *newdir)
 
int matchname (int arg, int argc, char **argv, char *fname)
 
int tar (gzFile in, int action, int arg, int argc, char **argv)
 
void help (int exitval)
 
void error (const char *msg)
 
int main (int argc, char **argv)
 

Variables

char * prog
 
const char * TGZsuffix [] = { "\0", ".tar", ".tar.gz", ".taz", ".tgz", NULL }
 

Macro Definition Documentation

◆ AREGTYPE

#define AREGTYPE   '\0' /* regular file */

Definition at line 43 of file untgz.c.

◆ BLKTYPE

#define BLKTYPE   '4' /* block special */

Definition at line 47 of file untgz.c.

◆ BLOCKSIZE

#define BLOCKSIZE   512

Definition at line 65 of file untgz.c.

◆ CHRTYPE

#define CHRTYPE   '3' /* character special */

Definition at line 46 of file untgz.c.

◆ CONTTYPE

#define CONTTYPE   '7' /* reserved */

Definition at line 50 of file untgz.c.

◆ DIRTYPE

#define DIRTYPE   '5' /* directory */

Definition at line 48 of file untgz.c.

◆ FIFOTYPE

#define FIFOTYPE   '6' /* FIFO special */

Definition at line 49 of file untgz.c.

◆ GNUTYPE_DUMPDIR

#define GNUTYPE_DUMPDIR   'D' /* file names from dumped directory */

Definition at line 54 of file untgz.c.

◆ GNUTYPE_LONGLINK

#define GNUTYPE_LONGLINK   'K' /* long link name */

Definition at line 55 of file untgz.c.

◆ GNUTYPE_LONGNAME

#define GNUTYPE_LONGNAME   'L' /* long file name */

Definition at line 56 of file untgz.c.

◆ GNUTYPE_MULTIVOL

#define GNUTYPE_MULTIVOL   'M' /* continuation of file from another volume */

Definition at line 57 of file untgz.c.

◆ GNUTYPE_NAMES

#define GNUTYPE_NAMES   'N' /* file name that does not fit into main hdr */

Definition at line 58 of file untgz.c.

◆ GNUTYPE_SPARSE

#define GNUTYPE_SPARSE   'S' /* sparse file */

Definition at line 59 of file untgz.c.

◆ GNUTYPE_VOLHDR

#define GNUTYPE_VOLHDR   'V' /* tape/volume header */

Definition at line 60 of file untgz.c.

◆ ISSPECIAL

#define ISSPECIAL (   c)    (((c) == '*') || ((c) == '/'))

Definition at line 288 of file untgz.c.

◆ LNKTYPE

#define LNKTYPE   '1' /* link */

Definition at line 44 of file untgz.c.

◆ REGTYPE

#define REGTYPE   '0' /* regular file */

Definition at line 42 of file untgz.c.

◆ SHORTNAMESIZE

#define SHORTNAMESIZE   100

Definition at line 66 of file untgz.c.

◆ SYMTYPE

#define SYMTYPE   '2' /* reserved */

Definition at line 45 of file untgz.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
TGZ_EXTRACT 
TGZ_LIST 
TGZ_INVALID 

Definition at line 103 of file untgz.c.

@ TGZ_LIST
Definition: untgz.c:103
@ TGZ_INVALID
Definition: untgz.c:103
@ TGZ_EXTRACT
Definition: untgz.c:103

Function Documentation

◆ error()

void error ( const char *  msg)

Definition at line 593 of file untgz.c.

594 {
595  fprintf(stderr, "%s: %s\n", prog, msg);
596  exit(1);
597 }
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119
char * prog
Definition: untgz.c:125

References test-lz4-list::exit, msg, and prog.

Referenced by __core_analysis_fcn(), _zip_allocate_new(), _zip_buffer_new_from_source(), _zip_cdir_grow(), _zip_cdir_new(), _zip_checkcons(), _zip_cp437_to_utf8(), _zip_crypto_aes_new(), _zip_crypto_hmac_new(), _zip_dirent_process_winzip_aes(), _zip_dirent_read(), _zip_dirent_size(), _zip_ef_clone(), _zip_ef_get_by_id(), _zip_ef_parse(), _zip_ef_utf8(), _zip_file_exists(), _zip_file_get_end(), _zip_file_get_offset(), _zip_find_central_dir(), _zip_get_dirent(), _zip_get_name(), _zip_hash_add(), _zip_hash_delete(), _zip_hash_lookup(), _zip_hash_new(), _zip_hash_reserve_capacity(), _zip_hash_revert(), _zip_memdup(), _zip_name_locate(), _zip_new(), _zip_open(), _zip_read(), _zip_read_cdir(), _zip_read_data(), _zip_read_eocd(), _zip_read_eocd64(), _zip_read_string(), _zip_source_new(), _zip_source_window_new(), _zip_source_zip_new(), _zip_string_get(), _zip_string_new(), _zip_win32_named_op_stat(), _zip_winzip_aes_new(), add_from_zip(), allocate(), basefind_create_array_of_addresses(), bin_pe_dotnet_init_clr(), bin_pe_parse_imports(), buffer_clone(), buffer_from_file(), buffer_grow_fragments(), buffer_new(), buffer_read_file(), buffer_seek(), buffer_to_file(), buffer_write(), cabd_extract(), cabd_open(), cabd_read_string(), cat(), check_token_coverage(), chmd_real_open(), coder_loop(), compress_allocate(), construct_rop_gadget(), decompress_allocate(), do_io_reply(), file_error(), file_error_core(), fs__create_junction(), fs__mkdtemp_func(), fs__mkstemp_func(), fs__open(), fs__opendir(), fs__read(), fs__readdir(), fs__stat_impl(), fs__unlink(), fs__utime_impl(), get_ascii_interned_object(), get_ascii_object(), get_atomic_type(), get_binary_complex_object(), get_binary_float_object(), get_callable_type(), get_code_object(), get_complex_object(), get_enum_type(), get_float64(), get_float_object(), get_int64_object(), get_int_object(), get_interned_object(), get_ivar_list_t(), get_list_object(), get_long_object(), get_method_list_t(), get_objc_property_list(), get_object(), get_ref_object(), get_set_object(), get_short_ascii_interned_object(), get_short_ascii_object(), get_small_tuple_object(), get_st32(), get_st64(), get_string_object(), get_stringref_object(), get_struct_type(), get_tuple_object(), get_typedef_type(), get_unicode_object(), get_union_type(), get_ut16(), get_ut32(), get_ut8(), hash_resize(), hole_new(), indx(), io_open_src(), io_open_src_real(), kwajd_decompress(), list_zip(), LLVMFuzzerTestOneInput(), LZ4_decompress_generic(), LZMA_API(), lzma_lzma_props_decode(), magic_int_within(), main(), make_program_args(), merge_zip(), meta_load_cb(), mpc_parse_run(), mszipd_decompress(), mszipd_decompress_kwaj(), open_compressed(), open_file(), Operand_ar0_encode(), Operand_ar12_encode(), Operand_ar4_encode(), Operand_ar8_encode(), Operand_arr_encode(), Operand_ars_encode(), Operand_ars_entry_encode(), Operand_art_encode(), Operand_br16_encode(), Operand_br2_encode(), Operand_br4_encode(), Operand_br8_encode(), Operand_br_encode(), Operand_brall_encode(), Operand_bs16_encode(), Operand_bs2_encode(), Operand_bs4_encode(), Operand_bs8_encode(), Operand_bs_encode(), Operand_bt16_encode(), Operand_bt2_encode(), Operand_bt4_encode(), Operand_bt8_encode(), Operand_bt_encode(), Operand_frr_encode(), Operand_frs_encode(), Operand_frt_encode(), Operand_mr0_encode(), Operand_mr1_encode(), Operand_mr2_encode(), Operand_mr3_encode(), Operand_mw_encode(), Operand_mx_encode(), Operand_my_encode(), parse_categories(), parse_indexes(), parse_symtab(), parse_type_fieldlist(), parse_type_methodlist(), parse_type_pointer(), parser__repair_error(), parser__skip_preceding_trees(), pdb7_extract_msf_stream_directory(), pdb7_parse(), printerr(), push_attr(), rabin_do_operation(), read_from_file(), read_hole(), read_image_metadata_tilde_header(), read_to_memory(), read_variable_length(), rz_analysis_cc_error(), rz_analysis_cc_get(), rz_analysis_cc_set_error(), rz_analysis_esil_trace_new(), rz_analysis_rzil_trace_new(), rz_axml_decode(), rz_bin_pdb_parse_from_buf(), rz_heap_tcache_content(), rz_regs_fpu_handler(), rz_socket_connect(), rz_socket_is_connected(), rz_socket_proc_open(), rz_subprocess_start_opt(), rz_uleb128(), scandir(), session_sdb_load(), set_error(), set_other_error(), set_out_of_range_error(), set_syntax_error(), source_hole_create(), str_to_uint64(), szddd_decompress(), tar(), trad_pkware_new(), ts_parser__recover_to_state(), uv__create_stdio_pipe_pair(), uv__fast_poll_create_peer_socket(), uv__fast_poll_process_poll_req(), uv__fs_opendir(), uv__fs_readdir(), uv__pipe_getname(), uv__pipe_read_ipc(), uv__stdio_create(), uv__stream_connect(), uv__stream_flush_write_queue(), uv__tty_try_write(), uv__write(), uv_barrier_init(), uv_cond_init(), uv_cpu_info(), uv_exepath(), uv_fs_event_start(), uv_fs_poll_start(), uv_getaddrinfo(), uv_msafd_poll(), uv_os_uname(), uv_pipe_bind(), uv_pipe_chmod(), uv_pipe_connect(), uv_pipe_queue_read(), uv_pipe_read_error(), uv_pipe_read_error_or_eof(), uv_poll_close(), uv_resident_set_memory(), uv_spawn(), uv_stdio_pipe_server(), uv_tty_clear(), uv_tty_emit_text(), uv_tty_move_caret(), uv_tty_reset(), uv_tty_restore_state(), uv_tty_save_state(), uv_tty_set_cursor_shape(), uv_tty_set_cursor_visibility(), uv_tty_set_style(), uv_tty_write(), uv_tty_write_bufs(), uv_wsarecv_workaround(), uv_wsarecvfrom_workaround(), win32_error_file_func(), win32_read_file_func(), win32_seek64_file_func(), win32_seek_file_func(), win32_tell64_file_func(), win32_tell_file_func(), win32_write_file_func(), windows_open(), winzip_aes_new(), xnu_generate_corefile(), xnu_write_mem_maps_to_buffer(), xrefs_load_cb(), zip_close(), zip_error_code_system(), zip_error_code_zip(), zip_error_init_with_code(), zip_error_strerror(), zip_error_system_type(), zip_error_to_data(), zip_fdopen(), zip_open(), zip_open_from_source(), zip_source_buffer_create(), zip_source_buffer_fragment_create(), zip_source_buffer_fragment_with_attributes_create(), zip_source_buffer_with_attributes_create(), zip_source_crc_create(), zip_source_file_common_new(), zip_source_file_create(), zip_source_filep_create(), zip_source_function_create(), zip_source_layered_create(), zip_source_seek_compute_offset(), zip_source_win32a_create(), zip_source_win32handle_create(), zip_source_win32w_create(), zip_source_window_create(), zip_source_zip_create(), and ziptool_open().

◆ ExprMatch()

int ExprMatch ( char *  string,
char *  expr 
)

Definition at line 290 of file untgz.c.

291 {
292  while (1)
293  {
294  if (ISSPECIAL(*expr))
295  {
296  if (*expr == '/')
297  {
298  if (*string != '\\' && *string != '/')
299  return 0;
300  string ++; expr++;
301  }
302  else if (*expr == '*')
303  {
304  if (*expr ++ == 0)
305  return 1;
306  while (*++string != *expr)
307  if (*string == 0)
308  return 0;
309  }
310  }
311  else
312  {
313  if (*string != *expr)
314  return 0;
315  if (*expr++ == 0)
316  return 1;
317  string++;
318  }
319  }
320 }
static RzNumCalcValue expr(RzNum *, RzNumCalc *, int)
Definition: calc.c:167
#define ISSPECIAL(c)
Definition: untgz.c:288

References expr(), and ISSPECIAL.

Referenced by matchname().

◆ getoct()

int getoct ( char *  p,
int  width 
)

Definition at line 168 of file untgz.c.

169 {
170  int result = 0;
171  char c;
172 
173  while (width--)
174  {
175  c = *p++;
176  if (c == 0)
177  break;
178  if (c == ' ')
179  continue;
180  if (c < '0' || c > '7')
181  return -1;
182  result = result * 8 + (c - '0');
183  }
184  return result;
185 }
void * p
Definition: libc.cpp:67
#define c(i)
Definition: sha256.c:43
int width
Definition: main.c:10

References c, p, and width.

Referenced by tar().

◆ help()

void help ( int  exitval)

Definition at line 581 of file untgz.c.

582 {
583  printf("untgz version 0.2.1\n"
584  " using zlib version %s\n\n",
585  zlibVersion());
586  printf("Usage: untgz file.tgz extract all files\n"
587  " untgz file.tgz fname ... extract selected files\n"
588  " untgz -l file.tgz list archive contents\n"
589  " untgz -h display this help\n");
590  exit(exitval);
591 }
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
const char *ZEXPORT zlibVersion()
Definition: zutil.c:27

References test-lz4-list::exit, printf(), and zlibVersion().

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 606 of file untgz.c.

607 {
608  int action = TGZ_EXTRACT;
609  int arg = 1;
610  char *TGZfile;
611  gzFile *f;
612 
613  prog = strrchr(argv[0],'\\');
614  if (prog == NULL)
615  {
616  prog = strrchr(argv[0],'/');
617  if (prog == NULL)
618  {
619  prog = strrchr(argv[0],':');
620  if (prog == NULL)
621  prog = argv[0];
622  else
623  prog++;
624  }
625  else
626  prog++;
627  }
628  else
629  prog++;
630 
631  if (argc == 1)
632  help(0);
633 
634  if (strcmp(argv[arg],"-l") == 0)
635  {
636  action = TGZ_LIST;
637  if (argc == ++arg)
638  help(0);
639  }
640  else if (strcmp(argv[arg],"-h") == 0)
641  {
642  help(0);
643  }
644 
645  if ((TGZfile = TGZfname(argv[arg])) == NULL)
646  TGZnotfound(argv[arg]);
647 
648  ++arg;
649  if ((action == TGZ_LIST) && (arg != argc))
650  help(1);
651 
652 /*
653  * Process the TGZ file
654  */
655  switch(action)
656  {
657  case TGZ_LIST:
658  case TGZ_EXTRACT:
659  f = gzopen(TGZfile,"rb");
660  if (f == NULL)
661  {
662  fprintf(stderr,"%s: Couldn't gzopen %s\n",prog,TGZfile);
663  return 1;
664  }
665  exit(tar(f, action, arg, argc, argv));
666  break;
667 
668  default:
669  error("Unknown option");
670  exit(1);
671  }
672 
673  return 0;
674 }
static const char * arg(RzAnalysis *a, csh *handle, cs_insn *insn, char *buf, int n)
Definition: arm_esil32.c:136
#define NULL
Definition: cris-opc.c:27
gzFile ZEXPORT gzopen(char *path, const char *mode) const
Definition: gzlib.c:272
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define f(i)
Definition: sha256.c:46
void TGZnotfound(const char *arcname)
Definition: untgz.c:152
void error(const char *msg)
Definition: untgz.c:593
int tar(gzFile in, int action, int arg, int argc, char **argv)
Definition: untgz.c:386
void help(int exitval)
Definition: untgz.c:581
char * TGZfname(const char *arcname)
Definition: untgz.c:132

References test-lz4-speed::action, arg(), argv, error(), test-lz4-list::exit, f, gzopen(), help(), NULL, prog, tar(), TGZ_EXTRACT, TGZ_LIST, TGZfname(), and TGZnotfound().

◆ makedir()

int makedir ( char *  newdir)

Definition at line 328 of file untgz.c.

329 {
330  char *buffer = strdup(newdir);
331  char *p;
332  int len = strlen(buffer);
333 
334  if (len <= 0) {
335  free(buffer);
336  return 0;
337  }
338  if (buffer[len-1] == '/') {
339  buffer[len-1] = '\0';
340  }
341  if (mkdir(buffer, 0755) == 0)
342  {
343  free(buffer);
344  return 1;
345  }
346 
347  p = buffer+1;
348  while (1)
349  {
350  char hold;
351 
352  while(*p && *p != '\\' && *p != '/')
353  p++;
354  hold = *p;
355  *p = 0;
356  if ((mkdir(buffer, 0755) == -1) && (errno == ENOENT))
357  {
358  fprintf(stderr,"%s: Couldn't create directory %s\n",prog,buffer);
359  free(buffer);
360  return 0;
361  }
362  if (hold == 0)
363  break;
364  *p++ = hold;
365  }
366  free(buffer);
367  return 1;
368 }
size_t len
Definition: 6502dis.c:15
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig mkdir
Definition: sflib.h:66
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
#define ENOENT
Definition: sftypes.h:112
Definition: buffer.h:15

References ENOENT, free(), len, mkdir, p, prog, and strdup().

Referenced by tar().

◆ matchname()

int matchname ( int  arg,
int  argc,
char **  argv,
char *  fname 
)

Definition at line 371 of file untgz.c.

372 {
373  if (arg == argc) /* no arguments given (untgz tgzarchive) */
374  return 1;
375 
376  while (arg < argc)
377  if (ExprMatch(fname,argv[arg++]))
378  return 1;
379 
380  return 0; /* ignore this for the moment being */
381 }
int ExprMatch(char *string, char *expr)
Definition: untgz.c:290

References argv, ExprMatch(), and create_tags_rz::fname.

Referenced by tar().

◆ OF() [1/11]

int makedir OF ( (char *)  )

◆ OF() [2/11]

int ExprMatch OF ( (char *, char *)  )

◆ OF() [3/11]

int getoct OF ( (char *, int )

◆ OF() [4/11]

char* TGZfname OF ( (const char *)  )

◆ OF() [5/11]

int tar OF ( (gzFile, int, int, int, char **)  )

◆ OF() [6/11]

void help OF ( (int )

◆ OF() [7/11]

int main OF ( (int, char **)  )

◆ OF() [8/11]

int matchname OF ( (int, int, char **, char *)  )

◆ OF() [9/11]

void restore_attr OF ( (struct attr_item **)  )

◆ OF() [10/11]

void push_attr OF ( (struct attr_item **, char *, int, time_t )

◆ OF() [11/11]

char* strtime OF ( (time_t *)  )

◆ push_attr()

void push_attr ( struct attr_item **  list,
char *  fname,
int  mode,
time_t  time 
)

Definition at line 253 of file untgz.c.

254 {
255  struct attr_item *item;
256 
257  item = (struct attr_item *)malloc(sizeof(struct attr_item));
258  if (item == NULL)
259  error("Out of memory");
260  item->fname = strdup(fname);
261  item->mode = mode;
262  item->time = time;
263  item->next = *list;
264  *list = item;
265 }
const char int mode
Definition: ioapi.h:137
static void list(RzEgg *egg)
Definition: rz-gg.c:52
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char const char static newpath char char char static envp time
Definition: sflib.h:42
char * fname
Definition: untgz.c:98
time_t time
Definition: untgz.c:100
int mode
Definition: untgz.c:99
struct attr_item * next
Definition: untgz.c:97

References error(), attr_item::fname, create_tags_rz::fname, list(), malloc(), attr_item::mode, attr_item::next, NULL, strdup(), time, and attr_item::time.

Referenced by tar().

◆ restore_attr()

void restore_attr ( struct attr_item **  list)

Definition at line 270 of file untgz.c.

271 {
272  struct attr_item *item, *prev;
273 
274  for (item = *list; item != NULL; )
275  {
276  setfiletime(item->fname,item->time);
277  chmod(item->fname,item->mode);
278  prev = item;
279  item = item->next;
280  free(prev);
281  }
282  *list = NULL;
283 }
static static fork const void static count static fd const char const char static newpath const char static path chmod
Definition: sflib.h:35
int setfiletime(char *fname, time_t ftime)
Definition: untgz.c:206

References chmod, attr_item::fname, free(), list(), attr_item::mode, attr_item::next, NULL, setfiletime(), and attr_item::time.

Referenced by tar().

◆ setfiletime()

int setfiletime ( char *  fname,
time_t  ftime 
)

Definition at line 206 of file untgz.c.

207 {
208 #ifdef WIN32
209  static int isWinNT = -1;
210  SYSTEMTIME st;
211  FILETIME locft, modft;
212  struct tm *loctm;
213  HANDLE hFile;
214  int result;
215 
216  loctm = localtime(&ftime);
217  if (loctm == NULL)
218  return -1;
219 
220  st.wYear = (WORD)loctm->tm_year + 1900;
221  st.wMonth = (WORD)loctm->tm_mon + 1;
222  st.wDayOfWeek = (WORD)loctm->tm_wday;
223  st.wDay = (WORD)loctm->tm_mday;
224  st.wHour = (WORD)loctm->tm_hour;
225  st.wMinute = (WORD)loctm->tm_min;
226  st.wSecond = (WORD)loctm->tm_sec;
227  st.wMilliseconds = 0;
228  if (!SystemTimeToFileTime(&st, &locft) ||
229  !LocalFileTimeToFileTime(&locft, &modft))
230  return -1;
231 
232  if (isWinNT < 0)
233  isWinNT = (GetVersion() < 0x80000000) ? 1 : 0;
234  hFile = CreateFile(fname, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
235  (isWinNT ? FILE_FLAG_BACKUP_SEMANTICS : 0),
236  NULL);
237  if (hFile == INVALID_HANDLE_VALUE)
238  return -1;
239  result = SetFileTime(hFile, NULL, NULL, &modft) ? 0 : -1;
240  CloseHandle(hFile);
241  return result;
242 #else
243  struct utimbuf settime;
244 
245  settime.actime = settime.modtime = ftime;
246  return utime(fname,&settime);
247 #endif
248 }
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds utime
Definition: sflib.h:57
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
DWORD * HANDLE

References create_tags_rz::fname, HANDLE, if(), INVALID_HANDLE_VALUE, NULL, and utime.

Referenced by restore_attr().

◆ strtime()

char* strtime ( time_t t)

Definition at line 191 of file untgz.c.

192 {
193  struct tm *local;
194  static char result[32];
195 
196  local = localtime(t);
197  sprintf(result,"%4d/%02d/%02d %02d:%02d:%02d",
198  local->tm_year+1900, local->tm_mon+1, local->tm_mday,
199  local->tm_hour, local->tm_min, local->tm_sec);
200  return result;
201 }
#define local
Definition: blast.c:36
sprintf
Definition: kernel.h:365

References local, and sprintf.

Referenced by tar().

◆ tar()

int tar ( gzFile  in,
int  action,
int  arg,
int  argc,
char **  argv 
)

Definition at line 386 of file untgz.c.

387 {
388  union tar_buffer buffer;
389  int len;
390  int err;
391  int getheader = 1;
392  int remaining = 0;
393  FILE *outfile = NULL;
394  char fname[BLOCKSIZE];
395  int tarmode;
396  time_t tartime;
397  struct attr_item *attributes = NULL;
398 
399  if (action == TGZ_LIST)
400  printf(" date time size file\n"
401  " ---------- -------- --------- -------------------------------------\n");
402  while (1)
403  {
404  len = gzread(in, &buffer, BLOCKSIZE);
405  if (len < 0)
406  error(gzerror(in, &err));
407  /*
408  * Always expect complete blocks to process
409  * the tar information.
410  */
411  if (len != BLOCKSIZE)
412  {
413  action = TGZ_INVALID; /* force error exit */
414  remaining = 0; /* force I/O cleanup */
415  }
416 
417  /*
418  * If we have to get a tar header
419  */
420  if (getheader >= 1)
421  {
422  /*
423  * if we met the end of the tar
424  * or the end-of-tar block,
425  * we are done
426  */
427  if (len == 0 || buffer.header.name[0] == 0)
428  break;
429 
430  tarmode = getoct(buffer.header.mode,8);
431  tartime = (time_t)getoct(buffer.header.mtime,12);
432  if (tarmode == -1 || tartime == (time_t)-1)
433  {
434  buffer.header.name[0] = 0;
436  }
437 
438  if (getheader == 1)
439  {
440  strncpy(fname,buffer.header.name,SHORTNAMESIZE);
441  if (fname[SHORTNAMESIZE-1] != 0)
442  fname[SHORTNAMESIZE] = 0;
443  }
444  else
445  {
446  /*
447  * The file name is longer than SHORTNAMESIZE
448  */
449  if (strncmp(fname,buffer.header.name,SHORTNAMESIZE-1) != 0)
450  error("bad long name");
451  getheader = 1;
452  }
453 
454  /*
455  * Act according to the type flag
456  */
457  switch (buffer.header.typeflag)
458  {
459  case DIRTYPE:
460  if (action == TGZ_LIST)
461  printf(" %s <dir> %s\n",strtime(&tartime),fname);
462  if (action == TGZ_EXTRACT)
463  {
464  makedir(fname);
465  push_attr(&attributes,fname,tarmode,tartime);
466  }
467  break;
468  case REGTYPE:
469  case AREGTYPE:
470  remaining = getoct(buffer.header.size,12);
471  if (remaining == -1)
472  {
474  break;
475  }
476  if (action == TGZ_LIST)
477  printf(" %s %9d %s\n",strtime(&tartime),remaining,fname);
478  else if (action == TGZ_EXTRACT)
479  {
480  if (matchname(arg,argc,argv,fname))
481  {
482  outfile = fopen(fname,"wb");
483  if (outfile == NULL) {
484  /* try creating directory */
485  char *p = strrchr(fname, '/');
486  if (p != NULL) {
487  *p = '\0';
488  makedir(fname);
489  *p = '/';
490  outfile = fopen(fname,"wb");
491  }
492  }
493  if (outfile != NULL)
494  printf("Extracting %s\n",fname);
495  else
496  fprintf(stderr, "%s: Couldn't create %s",prog,fname);
497  }
498  else
499  outfile = NULL;
500  }
501  getheader = 0;
502  break;
503  case GNUTYPE_LONGLINK:
504  case GNUTYPE_LONGNAME:
505  remaining = getoct(buffer.header.size,12);
506  if (remaining < 0 || remaining >= BLOCKSIZE)
507  {
509  break;
510  }
511  len = gzread(in, fname, BLOCKSIZE);
512  if (len < 0)
513  error(gzerror(in, &err));
514  if (fname[BLOCKSIZE-1] != 0 || (int)strlen(fname) > remaining)
515  {
517  break;
518  }
519  getheader = 2;
520  break;
521  default:
522  if (action == TGZ_LIST)
523  printf(" %s <---> %s\n",strtime(&tartime),fname);
524  break;
525  }
526  }
527  else
528  {
529  unsigned int bytes = (remaining > BLOCKSIZE) ? BLOCKSIZE : remaining;
530 
531  if (outfile != NULL)
532  {
533  if (fwrite(&buffer,sizeof(char),bytes,outfile) != bytes)
534  {
535  fprintf(stderr,
536  "%s: Error writing %s -- skipping\n",prog,fname);
537  fclose(outfile);
538  outfile = NULL;
539  remove(fname);
540  }
541  }
542  remaining -= bytes;
543  }
544 
545  if (remaining == 0)
546  {
547  getheader = 1;
548  if (outfile != NULL)
549  {
550  fclose(outfile);
551  outfile = NULL;
552  if (action != TGZ_INVALID)
553  push_attr(&attributes,fname,tarmode,tartime);
554  }
555  }
556 
557  /*
558  * Abandon if errors are found
559  */
560  if (action == TGZ_INVALID)
561  {
562  error("broken archive");
563  break;
564  }
565  }
566 
567  /*
568  * Restore file modes and time stamps
569  */
570  restore_attr(&attributes);
571 
572  if (gzclose(in) != Z_OK)
573  error("failed gzclose");
574 
575  return 0;
576 }
static bool err
Definition: armass.c:435
static ut8 bytes[32]
Definition: asm_arc.c:23
const lzma_allocator const uint8_t * in
Definition: block.h:527
FILE * outfile
Definition: fuzz_diff.c:16
int ZEXPORT gzclose(gzFile file)
Definition: gzclose.c:11
const char *ZEXPORT gzerror(gzFile file, int *errnum)
Definition: gzlib.c:534
int ZEXPORT gzread(gzFile file, voidp buf, unsigned len)
Definition: gzread.c:375
string FILE
Definition: benchmark.py:21
int time_t
Definition: sftypes.h:66
zip_uint64_t size
#define DIRTYPE
Definition: untgz.c:48
int getoct(char *p, int width)
Definition: untgz.c:168
#define SHORTNAMESIZE
Definition: untgz.c:66
#define GNUTYPE_LONGLINK
Definition: untgz.c:55
int makedir(char *newdir)
Definition: untgz.c:328
#define REGTYPE
Definition: untgz.c:42
void restore_attr(struct attr_item **list)
Definition: untgz.c:270
#define GNUTYPE_LONGNAME
Definition: untgz.c:56
int matchname(int arg, int argc, char **argv, char *fname)
Definition: untgz.c:371
void push_attr(struct attr_item **list, char *fname, int mode, time_t time)
Definition: untgz.c:253
#define AREGTYPE
Definition: untgz.c:43
#define BLOCKSIZE
Definition: untgz.c:65
char * strtime(time_t *t)
Definition: untgz.c:191
#define Z_OK
Definition: zlib.h:177

References test-lz4-speed::action, AREGTYPE, argv, BLOCKSIZE, bytes, DIRTYPE, err, error(), benchmark::FILE, create_tags_rz::fname, getoct(), GNUTYPE_LONGLINK, GNUTYPE_LONGNAME, gzclose(), gzerror(), gzread(), in, len, makedir(), matchname(), NULL, outfile, p, printf(), prog, push_attr(), REGTYPE, restore_attr(), SHORTNAMESIZE, buffer::size, strtime(), TGZ_EXTRACT, TGZ_INVALID, TGZ_LIST, and Z_OK.

Referenced by file_is_tar(), and main().

◆ TGZfname()

char* TGZfname ( const char *  arcname)

Definition at line 132 of file untgz.c.

133 {
134  static char buffer[1024];
135  int origlen,i;
136 
137  strcpy(buffer,arcname);
138  origlen = strlen(buffer);
139 
140  for (i=0; TGZsuffix[i]; i++)
141  {
142  strcpy(buffer+origlen,TGZsuffix[i]);
143  if (access(buffer,F_OK) == 0)
144  return buffer;
145  }
146  return NULL;
147 }
lzma_index ** i
Definition: index.h:629
struct buffer buffer
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause access
Definition: sflib.h:64
const char * TGZsuffix[]
Definition: untgz.c:127
#define F_OK
Definition: win.h:655

References access, F_OK, i, NULL, and TGZsuffix.

Referenced by main().

◆ TGZnotfound()

void TGZnotfound ( const char *  arcname)

Definition at line 152 of file untgz.c.

153 {
154  int i;
155 
156  fprintf(stderr,"%s: Couldn't find ",prog);
157  for (i=0;TGZsuffix[i];i++)
158  fprintf(stderr,(TGZsuffix[i+1]) ? "%s%s, " : "or %s%s\n",
159  arcname,
160  TGZsuffix[i]);
161  exit(1);
162 }

References test-lz4-list::exit, i, prog, and TGZsuffix.

Referenced by main().

Variable Documentation

◆ prog

char* prog

Definition at line 125 of file untgz.c.

Referenced by error(), main(), makedir(), tar(), TGZnotfound(), usage(), and uv__search_path().

◆ TGZsuffix

const char* TGZsuffix[] = { "\0", ".tar", ".tar.gz", ".taz", ".tgz", NULL }

Definition at line 127 of file untgz.c.

Referenced by TGZfname(), and TGZnotfound().