bd90698c9beb477127b3fc8bd26daa51b2b43e23
[oweals/cde.git] / cde / lib / tt / bin / tttar / tttar.C
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
35 #include <stdio.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #if defined(__osf__) || defined(linux) || defined(sun) || defined(CSRG_BASED)
41 #include <unistd.h>
42 #else
43 #if !defined(USL) && !defined(__uxp__) && !defined(sun)
44 #include <osfcn.h>
45 #endif
46 #endif
47 #include <netdb.h>
48 #include <locale.h>
49 #include <errno.h>
50 #include "util/tt_port.h"
51 #include "util/copyright.h"
52 #include "tt_options.h"
53 #include "archiver.h"
54
55 #if defined(OPT_BUG_USL) || defined(OPT_BUG_UXP) 
56 #include <unistd.h>
57 #endif
58
59 TT_INSERT_COPYRIGHT
60
61 #ifdef OPT_PATCH
62 static char PatchID[] = "Patch Id: 100626_03.";
63 static int Patch_ID100626_03;
64 #endif
65
66 /*
67  * Global variables
68  */
69
70 char    our_process_name[80];
71 int     verbosity = 0;
72
73 /*
74  * main()
75  */
76 int
77 main(int argc, char **argv)
78 {
79         archiver               *tttar = new archiver( argv[0] );
80         _Tt_string              tttarfile_name;
81         _Tt_string              tttardir;
82         bool_t                  tttar_worked    = FALSE;
83         bool_t                  should_tttar    = FALSE;
84         
85         setlocale( LC_ALL, "" );
86
87         tttar->parse_args( argc, argv );
88         sprintf( our_process_name, "%s", (char *)tttar->process_name());
89         verbosity = tttar->verbosity();
90
91         if (tttar->should_tar()) {
92                 char *tempnam_result;
93                 /*
94                  * Create a temporary directory to which we can
95                  * chdir() in order to create or extract the
96                  * tttarfile for or from the tarfile.
97                  */
98                 tttardir = tempnam_result = tempnam(NULL, "tttardir");
99                 free(tempnam_result);
100                 if (mkdir( (char *)tttardir, S_IRWXU ) == 0) {
101                         tttarfile_name = tttardir.cat( "/tttarfile" );
102                         should_tttar = TRUE;
103                         if (    (tttar->mode() == EXTRACT)
104                              || (tttar->mode() == LIST))
105                         {
106                                 tttar->do_tar( tttardir, FALSE );
107                                 /*
108                                  * Extract the tttarfile silently
109                                  */
110                                 int old_verbosity = tttar->verbosity();
111                                 Archive_mode old_mode = tttar->mode();
112                                 tttar->verbosity_set( 0 );
113                                 tttar->mode_set( EXTRACT );
114                                 /*
115                                  * If we're not extracting from stdin, we
116                                  * can invoke tar(1) again.  Otherwise,
117                                  * do_tar() was smart enough to extract
118                                  * the tttarfile into the cwd the first
119                                  * time we called it.
120                                  */
121                                 if (! tttar->only_1_look_at_tarfile()) {
122                                         tttar->do_tar( tttardir, TRUE );
123                                 } else if (tttar->mode() == EXTRACT) {
124                                         /*
125                                          * The tttarfile was already
126                                          * extracted into cwd.
127                                          */
128                                         tttarfile_name = "tttarfile";
129                                 }
130                                 tttar->verbosity_set( old_verbosity );
131                                 tttar->mode_set( old_mode );
132                         }
133                 }
134         } else {
135                 tttarfile_name = tttar->tarfile_arg();
136                 should_tttar = TRUE;
137         }
138
139         if (should_tttar) {
140                 tttar_worked = tttar->do_tttar( (char *)tttarfile_name,
141                                                 tttar->should_tar() );
142         }
143
144         if (tttar->should_tar() && (tttar->mode() == CREATE)) {
145                 tttar->do_tar( tttardir, tttar_worked );
146         }
147
148         if (tttar->should_tar()) {
149                 /*
150                  * Remove any temporary tttarfile.
151                  */
152                 int status = unlink( (char *)tttarfile_name );
153                 /*
154                  * We can't really expect an tttarfile in every tarfile,
155                  * so don't complain if there isn't one to remove.
156                 if (status != 0) {
157                         fprintf( stderr, "%s: %s: %s\n",
158                                  our_process_name, (char *)tttarfile_name,
159                                  strerror(errno));
160                 }
161                 */
162                 status = rmdir( (char *)tttardir );
163                 if (status != 0) {
164                         fprintf( stderr, "%s: %s: %s\n",
165                                  our_process_name, (char *)tttardir,
166                                  strerror(errno));
167                         exit(status);
168                 }
169         }
170
171         return(0);
172 }