"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 #include "xlib.h"
2 #include "gui.h"
3 #include "defaults.h"
4
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <stdarg.h>
10 #include <unistd.h>
11
12 void *xmalloc(size_t len) {
13 char *p;
14
15 p = malloc(len);
16 if(!p) {
17 xerror("malloc(%lu): %s", len, strerror(errno));
18 xdie(NULL);
19 }
20 return p;
21 }
22
23 void *xrealloc(void *p, unsigned long len) {
24 p = realloc(p, len);
25 if(!p) {
26 xerror("realloc(%lu): %s", len, strerror(errno));
27 xdie(NULL);
28 }
29 return p;
30 }
31
32 char *xstrdup(const char *s) {
33 char *ret;
34 unsigned long len;
35
36 len = strlen(s) + 1;
37 ret = xmalloc(len);
38 memcpy(ret, s, len);
39 return ret;
40 }
41
42 void xerror(char *fmt, ...) {
43 va_list ap;
44
45 va_start(ap, fmt);
46 if(!gui.display_message || gui.display_message(fmt, ap) < 0) {
47 vfprintf(stderr, fmt, ap);
48 fputc('\n', stderr);
49 }
50 va_end(ap);
51 }
52
53 void xdie(const char *s) {
54 if(s)
55 xerror("%s: halting", s);
56 exit(111);
57 }
58
59 int xpopen2(const char *s, int *in, int *out) {
60 int pipefd[2][2];
61
62 if(pipe(pipefd[0]) < 0)
63 return -1;
64 if(pipe(pipefd[1]) < 0) {
65 close(pipefd[0][0]);
66 close(pipefd[0][1]);
67 return -1;
68 }
69
70 switch(fork()) {
71 case -1:
72 close(pipefd[0][0]);
73 close(pipefd[0][1]);
74 close(pipefd[1][0]);
75 close(pipefd[1][1]);
76 return -1;
77 case 0:
78 close(pipefd[0][1]);
79 close(pipefd[1][0]);
80 close(0);
81 close(1);
82 dup2(pipefd[0][0], 0);
83 dup2(pipefd[1][1], 1);
84 execl(SHELL, SHELL, "-c", s, NULL);
85 printf("NO Unable to exec %s\n", SHELL);
86 exit(1);
87 default:
88 close(pipefd[0][0]);
89 close(pipefd[1][1]);
90 *out = pipefd[0][1];
91 *in = pipefd[1][0];
92 return 0;
93 }
94 }
95
96 void xsystem(const char *s) {
97 switch(fork()) {
98 case -1:
99 case 0:
100 close(0);
101 close(1);
102 close(2);
103 execl(SHELL, SHELL, "-c", s, NULL);
104 exit(1);
105 default:
106 return;
107 }
108 }
109
110 int xchomp(char *s) {
111 char *p = s + strlen(s) - 1;
112
113 if(*p == '\n') {
114 *p-- = '\0';
115 if(*p == '\r') {
116 *p = '\0';
117 return 2;
118 }
119 return 1;
120 }
121 return 0;
122 }
123
124 int xsplit(char *delim, char *s, char **resp, int nresp) {
125 char *p;
126 int n;
127
128 if(!nresp)
129 return 0;
130
131 resp[0] = s;
132 n = 1;
133 for(p = s; *p; p++) {
134 if(strchr(delim, *p)) {
135 *p++ = '\0';
136 if(!*p)
137 return n;
138 resp[n++] = p;
139 if(n == nresp)
140 return n;
141 }
142 }
143 return n;
144 }
145
146 const char *xsyserr() {
147 return strerror(errno);
148 }