Bladeren bron

Add xstrdup() and xrealpath()

master
sin 10 jaren geleden
bovenliggende
commit
9d25101ad9
1 gewijzigde bestanden met toevoegingen van 35 en 17 verwijderingen
  1. +35
    -17
      noice.c

+ 35
- 17
noice.c Bestand weergeven

@@ -82,18 +82,40 @@ xmalloc(size_t size)
void *p; void *p;


p = malloc(size); p = malloc(size);
if (!p) if (p == NULL)
printerr(1, "malloc"); printerr(1, "malloc");
return p; return p;
} }


void * void *
xrealloc(void *ptr, size_t size) xrealloc(void *p, size_t size)
{ {
ptr = realloc(ptr, size); p = realloc(p, size);
if (!ptr) if (p == NULL)
printerr(1, "realloc"); printerr(1, "realloc");
return ptr; return p;
}

char *
xstrdup(const char *s)
{
char *p;

p = strdup(s);
if (p == NULL)
printerr(1, "strdup");
return p;
}

char *
xrealpath(const char *pathname)
{
char *p;

p = realpath(pathname, NULL);
if (p == NULL)
printerr(1, "realpath");
return p;
} }


void void
@@ -359,9 +381,7 @@ printent(struct entry *ent, int active)
char cm = 0; char cm = 0;


/* Copy name locally */ /* Copy name locally */
name = strdup(ent->name); name = xstrdup(ent->name);
if (name == NULL)
printerr(1, "strdup name");


if (S_ISDIR(ent->mode)) { if (S_ISDIR(ent->mode)) {
cm = '/'; cm = '/';
@@ -395,8 +415,8 @@ browse(const char *ipath, const char *ifilter)
struct entry *dents; struct entry *dents;
int i, n, cur; int i, n, cur;
int r, ret; int r, ret;
char *path = realpath(ipath, NULL); char *path = xrealpath(ipath);
char *filter = strdup(ifilter); char *filter = xstrdup(ifilter);
regex_t filter_re; regex_t filter_re;
char *cwd; char *cwd;
struct stat sb; struct stat sb;
@@ -432,9 +452,7 @@ begin:
continue; continue;
/* Deep copy because readdir(3) reuses the entries */ /* Deep copy because readdir(3) reuses the entries */
dents = xrealloc(dents, (n + 1) * sizeof(*dents)); dents = xrealloc(dents, (n + 1) * sizeof(*dents));
dents[n].name = strdup(dp->d_name); dents[n].name = xstrdup(dp->d_name);
if (dents[n].name == NULL)
printerr(1, "strdup");
/* Handle root case */ /* Handle root case */
if (strcmp(path, "/") == 0) if (strcmp(path, "/") == 0)
asprintf(&name, "/%s", dents[n].name); asprintf(&name, "/%s", dents[n].name);
@@ -528,7 +546,7 @@ nochange:
free(path); free(path);
path = tmp; path = tmp;
free(filter); free(filter);
filter = strdup(ifilter); /* Reset filter */ filter = xstrdup(ifilter); /* Reset filter */
/* Recall history */ /* Recall history */
hist = SLIST_FIRST(&histhead); hist = SLIST_FIRST(&histhead);
if (hist != NULL) { if (hist != NULL) {
@@ -577,7 +595,7 @@ nochange:
free(path); free(path);
path = pathnew; path = pathnew;
free(filter); free(filter);
filter = strdup(ifilter); /* Reset filter */ filter = xstrdup(ifilter); /* Reset filter */
/* Save history */ /* Save history */
hist = xmalloc(sizeof(struct history)); hist = xmalloc(sizeof(struct history));
hist->pos = cur; hist->pos = cur;
@@ -647,10 +665,10 @@ nochange:
goto nochange; goto nochange;
} else { } else {
free(path); free(path);
path = realpath(tmp, NULL); path = xrealpath(tmp);
free(tmp); free(tmp);
free(filter); free(filter);
filter = strdup(ifilter); /* Reset filter */ filter = xstrdup(ifilter); /* Reset filter */
DPRINTF_S(path); DPRINTF_S(path);
cur = 0; cur = 0;
goto out; goto out;


||||||
x
 
000:0
Laden…
Annuleren
Opslaan