1 /*
2 * wiggle - apply rejected patches
3 *
4 * Copyright (C) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5 * Copyright (C) 2010-2013 Neil Brown <neilb@suse.de>
6 * Copyright (C) 2014-2020 Neil Brown <neil@brown.name>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program.
21 *
22 * Author: Neil Brown
23 * Email: <neil@brown.name>
24 */
25
26 #include "wiggle.h"
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <sys/stat.h>
30
31 char *wiggle_Cmd = "wiggle";
32
33 int wiggle_do_trace = 0;
34
wiggle_xmalloc(int size)35 void *wiggle_xmalloc(int size)
36 {
37 void *rv = malloc(size);
38 if (size && !rv) {
39 char *msg = "Failed to allocate memory - aborting\n";
40 write(2, msg, strlen(msg));
41 exit(3);
42 }
43 return rv;
44 }
45
wiggle_printword(FILE * f,struct elmnt e)46 void wiggle_printword(FILE *f, struct elmnt e)
47 {
48 if (e.start[0])
49 fprintf(f, "%.*s", e.plen + e.prefix,
50 e.start - e.prefix);
51 else {
52 int a, b, c;
53 sscanf(e.start+1, "%d %d %d", &a, &b, &c);
54 fprintf(f, "*** %d,%d **** %d%s", b, c, a, e.start+18);
55 }
56 }
57
wiggle_die(char * reason)58 void wiggle_die(char *reason)
59 {
60 fprintf(stderr, "%s: fatal error: %s failure\n", wiggle_Cmd, reason);
61 exit(3);
62 }
63
wiggle_check_dir(char * name,int fd)64 void wiggle_check_dir(char *name, int fd)
65 {
66 struct stat st;
67 if (fstat(fd, &st) != 0) {
68 fprintf(stderr, "%s: fatal: %s is strange\n", wiggle_Cmd, name);
69 exit(3);
70 }
71 if (S_ISDIR(st.st_mode)) {
72 fprintf(stderr, "%s: %s is a directory\n", wiggle_Cmd, name);
73 exit(3);
74 }
75 }
76
77