tttar.C 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
  24. //%% (c) Copyright 1993, 1994 International Business Machines Corp.
  25. //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  26. //%% (c) Copyright 1993, 1994 Novell, Inc.
  27. //%% $XConsortium: tttar.C /main/4 1995/10/20 16:59:46 rswiston $
  28. /*
  29. * tttar.cc - Link Service/ToolTalk object archive tool.
  30. *
  31. * Copyright (c) 1990 by Sun Microsystems, Inc.
  32. *
  33. */
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #include <stdlib.h>
  37. #include <sys/param.h>
  38. #include <sys/stat.h>
  39. #include <unistd.h>
  40. #include <netdb.h>
  41. #include <locale.h>
  42. #include <errno.h>
  43. #include "util/tt_port.h"
  44. #include "util/copyright.h"
  45. #include "tt_options.h"
  46. #include "archiver.h"
  47. TT_INSERT_COPYRIGHT
  48. #ifdef OPT_PATCH
  49. static char PatchID[] = "Patch Id: 100626_03.";
  50. static int Patch_ID100626_03;
  51. #endif
  52. /*
  53. * Global variables
  54. */
  55. char our_process_name[80];
  56. int verbosity = 0;
  57. /*
  58. * main()
  59. */
  60. int
  61. main(int argc, char **argv)
  62. {
  63. archiver *tttar = new archiver( argv[0] );
  64. _Tt_string tttarfile_name;
  65. _Tt_string tttardir;
  66. bool_t tttar_worked = FALSE;
  67. bool_t should_tttar = FALSE;
  68. setlocale( LC_ALL, "" );
  69. tttar->parse_args( argc, argv );
  70. sprintf( our_process_name, "%s", (char *)tttar->process_name());
  71. verbosity = tttar->verbosity();
  72. if (tttar->should_tar()) {
  73. char *tempnam_result;
  74. /*
  75. * Create a temporary directory to which we can
  76. * chdir() in order to create or extract the
  77. * tttarfile for or from the tarfile.
  78. */
  79. tttardir = tempnam_result = tempnam(NULL, "tttardir");
  80. free(tempnam_result);
  81. if (mkdir( (char *)tttardir, S_IRWXU ) == 0) {
  82. tttarfile_name = tttardir.cat( "/tttarfile" );
  83. should_tttar = TRUE;
  84. if ( (tttar->mode() == EXTRACT)
  85. || (tttar->mode() == LIST))
  86. {
  87. tttar->do_tar( tttardir, FALSE );
  88. /*
  89. * Extract the tttarfile silently
  90. */
  91. int old_verbosity = tttar->verbosity();
  92. Archive_mode old_mode = tttar->mode();
  93. tttar->verbosity_set( 0 );
  94. tttar->mode_set( EXTRACT );
  95. /*
  96. * If we're not extracting from stdin, we
  97. * can invoke tar(1) again. Otherwise,
  98. * do_tar() was smart enough to extract
  99. * the tttarfile into the cwd the first
  100. * time we called it.
  101. */
  102. if (! tttar->only_1_look_at_tarfile()) {
  103. tttar->do_tar( tttardir, TRUE );
  104. } else if (tttar->mode() == EXTRACT) {
  105. /*
  106. * The tttarfile was already
  107. * extracted into cwd.
  108. */
  109. tttarfile_name = "tttarfile";
  110. }
  111. tttar->verbosity_set( old_verbosity );
  112. tttar->mode_set( old_mode );
  113. }
  114. }
  115. } else {
  116. tttarfile_name = tttar->tarfile_arg();
  117. should_tttar = TRUE;
  118. }
  119. if (should_tttar) {
  120. tttar_worked = tttar->do_tttar( (char *)tttarfile_name,
  121. tttar->should_tar() );
  122. }
  123. if (tttar->should_tar() && (tttar->mode() == CREATE)) {
  124. tttar->do_tar( tttardir, tttar_worked );
  125. }
  126. if (tttar->should_tar()) {
  127. /*
  128. * Remove any temporary tttarfile.
  129. */
  130. int status = unlink( (char *)tttarfile_name );
  131. /*
  132. * We can't really expect an tttarfile in every tarfile,
  133. * so don't complain if there isn't one to remove.
  134. if (status != 0) {
  135. fprintf( stderr, "%s: %s: %s\n",
  136. our_process_name, (char *)tttarfile_name,
  137. strerror(errno));
  138. }
  139. */
  140. status = rmdir( (char *)tttardir );
  141. if (status != 0) {
  142. fprintf( stderr, "%s: %s: %s\n",
  143. our_process_name, (char *)tttardir,
  144. strerror(errno));
  145. exit(status);
  146. }
  147. }
  148. return(0);
  149. }