]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/tar.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / tar.m
1 ## Copyright (C) 2005-2012 Søren Hauberg
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {@var{entries} =} tar (@var{tarfile}, @var{files})
21 ## @deftypefnx {Function File} {@var{entries} =} tar (@var{tarfile}, @var{files}, @var{root})
22 ## Pack @var{files} @var{files} into the TAR archive @var{tarfile}.  The
23 ## list of files must be a string or a cell array of strings.
24 ##
25 ## The optional argument @var{root} changes the relative path of @var{files}
26 ## from the current directory.
27 ##
28 ## If an output argument is requested the entries in the archive are
29 ## returned in a cell array.
30 ## @seealso{untar, bzip2, gzip, zip}
31 ## @end deftypefn
32
33 ## Author: Søren Hauberg <hauberg@gmail.com>
34
35 function entries = tar (tarfile, files, root = ".")
36
37   if (nargin < 2 || nargin > 3)
38     print_usage ();
39   endif
40
41   if (ischar (files))
42     files = cellstr (files);
43   endif
44
45   if (! (ischar (tarfile) && iscellstr (files) && ischar (root)))
46     error ("tar: all arguments must be character strings");
47   endif
48
49   cmd = sprintf ("tar cvf %s -C %s %s", tarfile, root,
50                  sprintf (" %s", files{:}));
51
52   [status, output] = system (cmd);
53
54   if (status)
55     error ("tar: tar exited with status = %d", status);
56   endif
57
58   if (nargout > 0)
59     if (output(end) == "\n")
60       output(end) = [];
61     endif
62     entries = strsplit (output, "\n");
63     entries = entries';
64   endif
65
66 endfunction