diff -Naur patchtool-0.2/cmd.h patchtool-0.3/cmd.h --- patchtool-0.2/cmd.h 2005-03-28 17:40:17.000000000 +0000 +++ patchtool-0.3/cmd.h 2005-04-10 23:50:51.000000000 +0000 @@ -18,6 +18,7 @@ static char * cmd_usage[] = { "split [-l ][-p ][-o ] PATCHFILE", "list [-d ] PATCHFILE", + "sparse PATCHFILE dir", "get PATCHFILE part", NULL }; @@ -30,6 +31,7 @@ fprintf(stderr, "usage: patchtool SUBCOMMAND\n\n"); fprintf(stderr, " split Split files from a patch\n"); fprintf(stderr, " list List modified files from a patch\n"); + fprintf(stderr, " sparse Sparse a patch with a directory layout\n"); fprintf(stderr, " get Get a part of a patch\n"); fprintf(stderr, "\n"); } else @@ -45,5 +47,6 @@ int cmd_split(int argc, char **argv); int cmd_list(int argc, char **argv); int cmd_get(int argc, char **argv); +int cmd_sparse(int argc, char **argv); #endif /* !_CMD_H */ diff -Naur patchtool-0.2/cmd_sparse.c patchtool-0.3/cmd_sparse.c --- patchtool-0.2/cmd_sparse.c 1970-01-01 00:00:00.000000000 +0000 +++ patchtool-0.3/cmd_sparse.c 2005-04-11 00:06:39.000000000 +0000 @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2005 Vincent Hanquez + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; version 2 only. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include +#include +#include +#include +#include +#include "patch.h" +#include "cmd.h" + +static char * strip_str(char *str, int strip) +{ + char *start, *tmp; + int i; + + for (start = str, i = 0; i < strip; i++) { + tmp = strchr(start, '/'); + if (tmp) { + start = tmp; + start++; + } + } + return start; +} + +static void export_part(struct part *part) +{ + write(STDOUT_FILENO, part->start, part->end - part->start); +} + +int cmd_sparse(int argc, char **argv) +{ + struct patch *patch; + char *filename = NULL; + char *dir = NULL; + char save_dir[1024]; + int strip_level = 0; + struct list_head *tmp; + int i; + + if (argc < 1) + usage("sparse"); + for (i = 0; i < argc; i++) { + if (!argv[i]) + break; + if (strcmp(argv[i], "-p") == 0) + strip_level = atoi(argv[++i]); + else + if (filename) + dir = argv[i]; + else + filename = argv[i]; + } + if (!filename || !dir) + usage("sparse"); + + if (!getcwd(save_dir, 1024)) { + fprintf(stderr, "warning: unable to save current directory\n"); + memset(save_dir, '\0', 1024); + } + + patch = open_patch(filename); + if (chdir(dir)) { + fprintf(stderr, "error: unable to change directory to %s\n", dir); + goto out; + } + + + list_for_each(&patch->parts, tmp) { + char *ptr; + int fd; + struct part *part = list_entry(tmp, struct part, list); + + ptr = strip_str(part->filename[1], strip_level); + fd = open(ptr, O_RDONLY); + if (fd != -1) + export_part(part); + close(fd); + } + + if (strlen(save_dir) > 0) + chdir(save_dir); +out: + close_patch(patch); + + return 0; +} diff -Naur patchtool-0.2/Makefile patchtool-0.3/Makefile --- patchtool-0.2/Makefile 2005-03-28 17:34:12.000000000 +0000 +++ patchtool-0.3/Makefile 2005-04-10 23:48:57.000000000 +0000 @@ -8,6 +8,7 @@ OBJS += cmd_split.o OBJS += cmd_list.o OBJS += cmd_get.o +OBJS += cmd_sparse.o all: patchtool diff -Naur patchtool-0.2/patchtool.c patchtool-0.3/patchtool.c --- patchtool-0.2/patchtool.c 2005-03-28 17:40:11.000000000 +0000 +++ patchtool-0.3/patchtool.c 2005-04-11 00:00:20.000000000 +0000 @@ -19,6 +19,8 @@ #include #include "cmd.h" +#define match_cmd(str) if (strcmp(argv[1], #str) == 0) cmd_##str (argc - 2, &argv[2]); + int main(int argc, char **argv) { if (argc < 2) @@ -28,6 +30,8 @@ cmd_split(argc - 2, &argv[2]); else if (strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) cmd_list(argc - 2, &argv[2]); + else if (strcmp(argv[1], "sparse") == 0) + cmd_sparse(argc - 2, &argv[2]); else if (strcmp(argv[1], "get") == 0) cmd_get(argc - 2, &argv[2]); else