Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / lib / tt / bin / tttar / tttar.C
1 //%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                  
2 //%%  (c) Copyright 1993, 1994 International Business Machines Corp.    
3 //%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                   
4 //%%  (c) Copyright 1993, 1994 Novell, Inc.                             
5 //%%  $XConsortium: tttar.C /main/4 1995/10/20 16:59:46 rswiston $                                                      
6 /*
7  * tttar.cc - Link Service/ToolTalk object archive tool.
8  *
9  * Copyright (c) 1990 by Sun Microsystems, Inc.
10  *
11  */
12
13 #include <stdio.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <sys/param.h>
17 #include <sys/stat.h>
18 #if defined(__osf__)
19 #include <unistd.h>
20 #else
21 #if !defined(USL) && !defined(__uxp__)
22 #include <osfcn.h>
23 #endif
24 #endif
25 #include <netdb.h>
26 #include <locale.h>
27 #include <errno.h>
28 #include "util/tt_port.h"
29 #include "util/copyright.h"
30 #include "tt_options.h"
31 #include "archiver.h"
32
33 #if defined(OPT_BUG_USL) || defined(OPT_BUG_UXP) 
34 #include <unistd.h>
35 #endif
36
37 TT_INSERT_COPYRIGHT
38
39 #ifdef OPT_PATCH
40 static char PatchID[] = "Patch Id: 100626_03.";
41 static int Patch_ID100626_03;
42 #endif
43
44 /*
45  * Global variables
46  */
47
48 char    our_process_name[80];
49 int     verbosity = 0;
50
51 /*
52  * main()
53  */
54 int
55 main(int argc, char **argv)
56 {
57         archiver               *tttar = new archiver( argv[0] );
58         _Tt_string              tttarfile_name;
59         _Tt_string              tttardir;
60         bool_t                  tttar_worked    = FALSE;
61         bool_t                  should_tttar    = FALSE;
62         
63         setlocale( LC_ALL, "" );
64
65         tttar->parse_args( argc, argv );
66         sprintf( our_process_name, "%s", (char *)tttar->process_name());
67         verbosity = tttar->verbosity();
68
69         if (tttar->should_tar()) {
70                 char *tempnam_result;
71                 /*
72                  * Create a temporary directory to which we can
73                  * chdir() in order to create or extract the
74                  * tttarfile for or from the tarfile.
75                  */
76                 tttardir = tempnam_result = tempnam(NULL, "tttardir");
77                 free(tempnam_result);
78                 if (mkdir( (char *)tttardir, S_IRWXU ) == 0) {
79                         tttarfile_name = tttardir.cat( "/tttarfile" );
80                         should_tttar = TRUE;
81                         if (    (tttar->mode() == EXTRACT)
82                              || (tttar->mode() == LIST))
83                         {
84                                 tttar->do_tar( tttardir, FALSE );
85                                 /*
86                                  * Extract the tttarfile silently
87                                  */
88                                 int old_verbosity = tttar->verbosity();
89                                 Archive_mode old_mode = tttar->mode();
90                                 tttar->verbosity_set( 0 );
91                                 tttar->mode_set( EXTRACT );
92                                 /*
93                                  * If we're not extracting from stdin, we
94                                  * can invoke tar(1) again.  Otherwise,
95                                  * do_tar() was smart enough to extract
96                                  * the tttarfile into the cwd the first
97                                  * time we called it.
98                                  */
99                                 if (! tttar->only_1_look_at_tarfile()) {
100                                         tttar->do_tar( tttardir, TRUE );
101                                 } else if (tttar->mode() == EXTRACT) {
102                                         /*
103                                          * The tttarfile was already
104                                          * extracted into cwd.
105                                          */
106                                         tttarfile_name = "tttarfile";
107                                 }
108                                 tttar->verbosity_set( old_verbosity );
109                                 tttar->mode_set( old_mode );
110                         }
111                 }
112         } else {
113                 tttarfile_name = tttar->tarfile_arg();
114                 should_tttar = TRUE;
115         }
116
117         if (should_tttar) {
118                 tttar_worked = tttar->do_tttar( (char *)tttarfile_name,
119                                                 tttar->should_tar() );
120         }
121
122         if (tttar->should_tar() && (tttar->mode() == CREATE)) {
123                 tttar->do_tar( tttardir, tttar_worked );
124         }
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
149         return(0);
150 }