Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
prepare v16.2.1
Browse files Browse the repository at this point in the history
- Added `emalloc` function for heap allocations.
- Added unfollowed symlinks description to `-c` option in the manual.
- Replaced variable-length array use in `rvldir`.
  • Loading branch information
skippyr committed Dec 13, 2023
1 parent b0954de commit 20f56bd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion rvl.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH RVL 1 v16.2.0 RVL
.TH RVL 1 v16.2.1 RVL
.SH NAME
rvl - reveals info about file system entries.

Expand Down
41 changes: 25 additions & 16 deletions rvl.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum { DT_CTTS, DT_TYPE, DT_SIZE, DT_HSIZE, DT_PERMS, DT_OPERMS, DT_USR, DT_UID,
DT_GRP, DT_GID, DT_MDATE };

static int alphacmp(const void *str0, const void *str1);
static void *emalloc(size_t len);
static void die(char *fmt, ...);
static void rvl(char *path);
static void rvldir(char *path);
Expand All @@ -40,6 +41,15 @@ alphacmp(const void *str0, const void *str1)
return strcmp(*(char **)str0, *(char **)str1);
}

static void *
emalloc(size_t len)
{
void *p;
if (!(p = malloc(len)))
die("can't alloc memory.\n");
return p;
}

static void
die(char *fmt, ...)
{
Expand Down Expand Up @@ -94,37 +104,36 @@ static void
rvldir(char *path)
{
DIR *d = opendir(path);
struct dirent *e;
char *ent;
unsigned long entlen;
char **entnames;
char *entname;
int i;
int z;
size_t entlen;
struct dirent *e;
if (!d)
die("can't open directory \"%s\".\n", path);
for (i = -2; readdir(d); i++);
if (!i) {
closedir(d);
return;
}
char *ents[i];
if (!i)
goto close;
entnames = emalloc(sizeof(NULL) * i);
i = 0;
rewinddir(d);
while ((e = readdir(d))) {
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
continue;
entlen = strlen(e->d_name) + 1;
ent = malloc(entlen);
if (!ent)
die("can't alloc memory.\n");
strcpy(ent, e->d_name);
ents[i] = ent;
entname = emalloc(entlen);
strcpy(entname, e->d_name);
entnames[i] = entname;
i++;
}
qsort(ents, i, sizeof(char *), alphacmp);
qsort(entnames, i, sizeof(NULL), alphacmp);
for (z = 0; z < i; z++) {
printf("%s\n", ents[z]);
free(ents[z]);
printf("%s\n", entnames[z]);
free(entnames[z]);
}
free(entnames);
close:
closedir(d);
}

Expand Down

0 comments on commit 20f56bd

Please sign in to comment.