mirror of
https://github.com/jarun/nnn.git
synced 2024-11-24 11:51:27 +00:00
Add xmalloc() + xrealloc()
This commit is contained in:
parent
8326a75762
commit
7d4d60e778
38
noice.c
38
noice.c
|
@ -76,6 +76,26 @@ void printmsg(char *msg);
|
||||||
void printwarn(void);
|
void printwarn(void);
|
||||||
void printerr(int ret, char *prefix);
|
void printerr(int ret, char *prefix);
|
||||||
|
|
||||||
|
void *
|
||||||
|
xmalloc(size_t size)
|
||||||
|
{
|
||||||
|
void *p;
|
||||||
|
|
||||||
|
p = malloc(size);
|
||||||
|
if (!p)
|
||||||
|
printerr(1, "malloc");
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
xrealloc(void *ptr, size_t size)
|
||||||
|
{
|
||||||
|
ptr = realloc(ptr, size);
|
||||||
|
if (!ptr)
|
||||||
|
printerr(1, "realloc");
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
spawn(const char *file, const char *arg)
|
spawn(const char *file, const char *arg)
|
||||||
{
|
{
|
||||||
|
@ -123,7 +143,7 @@ setfilter(regex_t *regex, char *filter)
|
||||||
|
|
||||||
r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED);
|
r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED);
|
||||||
if (r != 0) {
|
if (r != 0) {
|
||||||
errbuf = malloc(COLS * sizeof(char));
|
errbuf = xmalloc(COLS * sizeof(char));
|
||||||
regerror(r, regex, errbuf, COLS * sizeof(char));
|
regerror(r, regex, errbuf, COLS * sizeof(char));
|
||||||
printmsg(errbuf);
|
printmsg(errbuf);
|
||||||
free(errbuf);
|
free(errbuf);
|
||||||
|
@ -288,7 +308,7 @@ readln(void)
|
||||||
getyx(stdscr, y, x);
|
getyx(stdscr, y, x);
|
||||||
if (x >= x0) {
|
if (x >= x0) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
ln = realloc(ln, (i - 1) * sizeof(*ln));
|
ln = xrealloc(ln, (i - 1) * sizeof(*ln));
|
||||||
i--;
|
i--;
|
||||||
} else {
|
} else {
|
||||||
free(ln);
|
free(ln);
|
||||||
|
@ -302,12 +322,12 @@ readln(void)
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ln = realloc(ln, (i + 1) * sizeof(*ln));
|
ln = xrealloc(ln, (i + 1) * sizeof(*ln));
|
||||||
ln[i] = c;
|
ln[i] = c;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
if (ln != NULL) {
|
if (ln != NULL) {
|
||||||
ln = realloc(ln, (i + 1) * sizeof(*ln));
|
ln = xrealloc(ln, (i + 1) * sizeof(*ln));
|
||||||
ln[i] = '\0';
|
ln[i] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,9 +431,7 @@ begin:
|
||||||
if (!visible(&filter_re, dp->d_name))
|
if (!visible(&filter_re, dp->d_name))
|
||||||
continue;
|
continue;
|
||||||
/* Deep copy because readdir(3) reuses the entries */
|
/* Deep copy because readdir(3) reuses the entries */
|
||||||
dents = realloc(dents, (n + 1) * sizeof(*dents));
|
dents = xrealloc(dents, (n + 1) * sizeof(*dents));
|
||||||
if (dents == NULL)
|
|
||||||
printerr(1, "realloc");
|
|
||||||
dents[n].name = strdup(dp->d_name);
|
dents[n].name = strdup(dp->d_name);
|
||||||
if (dents[n].name == NULL)
|
if (dents[n].name == NULL)
|
||||||
printerr(1, "strdup");
|
printerr(1, "strdup");
|
||||||
|
@ -466,7 +484,7 @@ redraw:
|
||||||
DPRINTF_S(path);
|
DPRINTF_S(path);
|
||||||
|
|
||||||
/* No text wrapping in cwd line */
|
/* No text wrapping in cwd line */
|
||||||
cwd = malloc(COLS * sizeof(char));
|
cwd = xmalloc(COLS * sizeof(char));
|
||||||
strlcpy(cwd, path, COLS * sizeof(char));
|
strlcpy(cwd, path, COLS * sizeof(char));
|
||||||
cwd[COLS - strlen(CWD) - 1] = '\0';
|
cwd[COLS - strlen(CWD) - 1] = '\0';
|
||||||
|
|
||||||
|
@ -505,7 +523,7 @@ nochange:
|
||||||
goto nochange;
|
goto nochange;
|
||||||
} else {
|
} else {
|
||||||
dir = dirname(path);
|
dir = dirname(path);
|
||||||
tmp = malloc(strlen(dir) + 1);
|
tmp = xmalloc(strlen(dir) + 1);
|
||||||
strlcpy(tmp, dir, strlen(dir) + 1);
|
strlcpy(tmp, dir, strlen(dir) + 1);
|
||||||
free(path);
|
free(path);
|
||||||
path = tmp;
|
path = tmp;
|
||||||
|
@ -561,7 +579,7 @@ nochange:
|
||||||
free(filter);
|
free(filter);
|
||||||
filter = strdup(ifilter); /* Reset filter */
|
filter = strdup(ifilter); /* Reset filter */
|
||||||
/* Save history */
|
/* Save history */
|
||||||
hist = malloc(sizeof(struct history));
|
hist = xmalloc(sizeof(struct history));
|
||||||
hist->pos = cur;
|
hist->pos = cur;
|
||||||
SLIST_INSERT_HEAD(&histhead, hist, entry);
|
SLIST_INSERT_HEAD(&histhead, hist, entry);
|
||||||
cur = 0;
|
cur = 0;
|
||||||
|
|
Loading…
Reference in a new issue