1
0
Fork 0

07: C: It’s just DFS so don’t look up dirs

This commit is contained in:
Mia Herkt 2022-12-08 05:27:45 +01:00
parent c09be6c757
commit 5bd76d8db7
Signed by: mia
GPG Key ID: 72E154B8622EC191
1 changed files with 5 additions and 30 deletions

View File

@ -1,41 +1,20 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdint.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <ctype.h>
uint32_t fnv1a_32(const char *buf, size_t len)
{
uint32_t h = 0x811c9dc5U;
for (; len > 0; len--)
h = 0x01000193U * (h ^ *buf++);
return h;
}
typedef struct entry { typedef struct entry {
uint32_t id;
size_t size; size_t size;
size_t n_entries; size_t n_entries;
size_t n_alloc; size_t n_alloc;
size_t visit;
struct entry **entries; struct entry **entries;
struct entry *parent; struct entry *parent;
} entry; } entry;
entry *findent(entry *e, const char *name, size_t nlen) { entry *addent(entry *parent) {
uint32_t id = fnv1a_32(name, nlen);
for (size_t i = 0; i < e->n_entries; i++) {
if (e->entries[i]->id == id)
return e->entries[i];
}
return NULL;
}
entry *addent(entry *parent, const char *name, size_t nlen) {
if (parent->n_entries >= parent->n_alloc) { if (parent->n_entries >= parent->n_alloc) {
parent->n_alloc += 5; parent->n_alloc += 5;
if (parent->n_entries) if (parent->n_entries)
@ -46,7 +25,6 @@ entry *addent(entry *parent, const char *name, size_t nlen) {
parent->n_entries++; parent->n_entries++;
entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry)); entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry));
e->id = fnv1a_32(name, nlen);
e->parent = parent; e->parent = parent;
return e; return e;
@ -71,8 +49,7 @@ int main(int argc, char **argv) {
while (l != in + s.st_size) { while (l != in + s.st_size) {
char *nl = strchr(l, '\n'); char *nl = strchr(l, '\n');
if (l[0] == 'd') { if (l[0] == 'd') {
l += 4; addent(current);
addent(current, l, nl - l);
} else if (l[0] == '$') { } else if (l[0] == '$') {
if (l[2] == 'c') { if (l[2] == 'c') {
if (l[5] == '/') if (l[5] == '/')
@ -81,10 +58,8 @@ int main(int argc, char **argv) {
if (current->parent != root) if (current->parent != root)
current->parent->size += current->size; current->parent->size += current->size;
current = current->parent; current = current->parent;
} else { } else
l += 5; current = current->entries[current->visit++];
current = findent(current, l, nl - l);
}
} }
} else { } else {
size_t sz = 0; size_t sz = 0;