comparison scripts/miscellaneous/tar.m @ 5808:a18d85bdff31

[project @ 2006-05-11 03:11:03 by jwe]
author jwe
date Thu, 11 May 2006 03:11:08 +0000
parents 29c4fb42b210
children 04c2ad6d1679
comparison
equal deleted inserted replaced
5807:29c4fb42b210 5808:a18d85bdff31
13 ## You should have received a copy of the GNU General Public License 13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software 14 ## along with this program; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 16
17 ## -*- texinfo -*- 17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} tar (@var{filename}, @var{files}) 18 ## @deftypefn {Function File} {@var{entries} =} tar (@var{tarfile}, @var{files}, @var{root})
19 ## @deftypefnx{Function File} tar (@var{filename}, @var{files}, @var{root}) 19 ## Pack @var{files} @var{files} into the TAR archive @var{tarfile}. The
20 ## @deftypefnx{Function File} @var{entries} = tar (...) 20 ## list of files must be a string or a cell array of strings.
21 ## Packs the files listed in @var{files} into @var{filename} using 21 ##
22 ## the @code{tar} program. @var{files} must either be a string or a cell
23 ## array of strings containing the files to be packed. The extension of
24 ## @var{filename} determines if the tar-file is also to be compressed
25 ## (if no extension is present @code{.tar} will be appended to @var{filename})
26 ## @table @asis
27 ## @item @code{.tar}
28 ## No compression.
29 ## @item @code{.tar.gz} or @code{.tgz}
30 ## The tar-file will be compressed using @code{gzip}.
31 ## @item @code{tar.bz}, @code{tar.bz2}, @code{tbz}, or @code{tbz2}
32 ## The tar-file will be compressed using @code{bzip2}.
33 ## @end table
34 ## The optional argument @var{root} changes the relative path of @var{files} 22 ## The optional argument @var{root} changes the relative path of @var{files}
35 ## from the current directory. 23 ## from the current directory.
36 ## 24 ##
37 ## If an output argument is requested the filename entries in the archive 25 ## If an output argument is requested the entries in the archive are
38 ## is returned. 26 ## returned in a cell array.
39 ## 27 ## @seealso{untar, gzip, gunzip, zip, unzip}
40 ## @end deftypefn 28 ## @end deftypefn
41 ## @seealso{untar, gzip, gunzip, zip, unzip}
42 29
43 ## Author: Søren Hauberg <hauberg at gmail dot com> 30 ## Author: Søren Hauberg <hauberg@gmail.com>
44 31
45 function entries = tar(filename, files, root) 32 function entries = tar (tarfile, files, root)
46 if (nargin < 2 || nargin > 3) 33
47 print_usage("tar"); 34 if (nargin == 2 || nargin == 3)
48 elseif (nargin == 2) 35
49 root = "."; 36 if (nargin == 2)
37 root = ".";
50 endif 38 endif
51
52 supported_extensions = {"tar", "tar.gz", "tgz", "tar.bz", "tar.bz2", "tbz", "tbz2"};
53 39
54 ## Test type of input 40 ## Test type of input
55 if (ischar(files)) 41 if (ischar (files))
56 files = {files}; 42 files = cellstr (files);
57 endif
58 if (!ischar(filename) || !iscellstr(files) || !ischar(root))
59 error("All arguments must be strings.\n");
60 endif
61
62 ## Get extension of filename
63 dots = find(filename == ".");
64 for dot = dots
65 curext = filename(dot+1:end);
66 if (any(strcmp(curext, supported_extensions)))
67 ext = curext;
68 break;
69 endif
70 endfor
71
72 ## If no extension was found default to "tar"
73 if (!exist("ext", "var"))
74 filename = sprintf("%s.tar", filename);
75 ext = "tar";
76 endif 43 endif
77 44
78 ## Determine which flags to use with tar 45 if (ischar (tarfile) && iscellstr (files) && ischar (root))
79 switch (ext)
80 case {"tar"} flag = "";
81 case {"tar.gz", "tgz"} flag = "z";
82 case {"tar.bz", "tar.bz2", "tbz", "tbz2"} flag = "j";
83 endswitch
84 46
85 ## Call tar 47 cmd = sprintf ("tar -c -v -f %s -C %s %s", tarfile, root,
86 [output, status] = system(["tar -" flag "cvf " filename " -C " root sprintf(" %s", files{:})]); 48 sprintf (" %s", files{:}));
87 if (status != 0) 49
88 error("tar returned the following error: %s\n", output); 50 [status, output] = system (cmd);
51
52 if (status == 0)
53 if (nargout > 0)
54 if (output(end) == "\n")
55 output(end) = [];
56 endif
57 entries = cellstr (split (output, "\n"));
58 entries = entries';
59 endif
60 else
61 error ("tar: tar exited with status = %d", status);
62 endif
63
64 else
65 error ("tar: expecting all arguments to be character strings");
89 endif 66 endif
90 67
91 if (nargout) 68 else
92 entries = split(output(1:end-1), "\n"); 69 print_usage("tar");
93 endif 70 endif
71
94 endfunction 72 endfunction