Mercurial > octave-antonio
comparison scripts/miscellaneous/tar.m @ 5807:29c4fb42b210
[project @ 2006-05-11 01:48:56 by jwe]
author | jwe |
---|---|
date | Thu, 11 May 2006 01:48:56 +0000 |
parents | |
children | a18d85bdff31 |
comparison
equal
deleted
inserted
replaced
5806:b2a802aa0cda | 5807:29c4fb42b210 |
---|---|
1 ## Copyright (C) 2005 Søren Hauberg | |
2 ## | |
3 ## This program is free software; you can redistribute it and/or modify | |
4 ## it under the terms of the GNU General Public License as published by | |
5 ## the Free Software Foundation; either version 2 of the License, or | |
6 ## (at your option) any later version. | |
7 ## | |
8 ## This program is distributed in the hope that it will be useful, | |
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 ## GNU General Public License for more details. | |
12 ## | |
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 | |
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | |
17 ## -*- texinfo -*- | |
18 ## @deftypefn {Function File} tar (@var{filename}, @var{files}) | |
19 ## @deftypefnx{Function File} tar (@var{filename}, @var{files}, @var{root}) | |
20 ## @deftypefnx{Function File} @var{entries} = tar (...) | |
21 ## Packs the files listed in @var{files} into @var{filename} using | |
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} | |
35 ## from the current directory. | |
36 ## | |
37 ## If an output argument is requested the filename entries in the archive | |
38 ## is returned. | |
39 ## | |
40 ## @end deftypefn | |
41 ## @seealso{untar, gzip, gunzip, zip, unzip} | |
42 | |
43 ## Author: Søren Hauberg <hauberg at gmail dot com> | |
44 | |
45 function entries = tar(filename, files, root) | |
46 if (nargin < 2 || nargin > 3) | |
47 print_usage("tar"); | |
48 elseif (nargin == 2) | |
49 root = "."; | |
50 endif | |
51 | |
52 supported_extensions = {"tar", "tar.gz", "tgz", "tar.bz", "tar.bz2", "tbz", "tbz2"}; | |
53 | |
54 ## Test type of input | |
55 if (ischar(files)) | |
56 files = {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 | |
77 | |
78 ## Determine which flags to use with tar | |
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 | |
85 ## Call tar | |
86 [output, status] = system(["tar -" flag "cvf " filename " -C " root sprintf(" %s", files{:})]); | |
87 if (status != 0) | |
88 error("tar returned the following error: %s\n", output); | |
89 endif | |
90 | |
91 if (nargout) | |
92 entries = split(output(1:end-1), "\n"); | |
93 endif | |
94 endfunction |