Skip to content

Instantly share code, notes, and snippets.

@MonkeyIsNull
Created May 2, 2012 12:27
Show Gist options
  • Save MonkeyIsNull/2576219 to your computer and use it in GitHub Desktop.
Save MonkeyIsNull/2576219 to your computer and use it in GitHub Desktop.
Linked List file spitter
// Spits out the lines of a file forwards and backwards, via a linked list
// All Hail Zed Shaw and (Re)Learn C the Hard Way
// This Bitch be free of Memory leaks, Yo!
// ==9234==
// ==9234== HEAP SUMMARY:
// ==9234== in use at exit: 8,280 bytes in 3 blocks
// ==9234== total heap usage: 134 allocs, 131 frees, 14,531 bytes allocated
// ==9234==
// ==9234== LEAK SUMMARY:
// ==9234== definitely lost: 0 bytes in 0 blocks
// ==9234== indirectly lost: 0 bytes in 0 blocks
// ==9234== possibly lost: 0 bytes in 0 blocks
// ==9234== still reachable: 8,280 bytes in 3 blocks
// ==9234== suppressed: 0 bytes in 0 blocks
// ==9234== Rerun with --leak-check=full to see details of leaked memory
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
typedef struct Node
{
struct Node *next;
struct Node *prev;
char *line;
} Node;
typedef struct List
{
struct Node *first;
struct Node *last;
int count;
} List;
// Adds to the front
void
List_add(List *list, char *line)
{
Node *node = malloc(sizeof(Node));
// The line below does NOT work!
/* node->line = line; */
node->line = malloc(strlen(line)+1);
memcpy(node->line, line, strlen(line)+1);
if(list->last == NULL) {
list->first = node;
list->last = node;
} else {
list->last->next = node;
node->prev = list->last;
list->last = node;
}
list->count++;
}
void
List_backwards_walk(List *list)
{
Node *current_node = list->last;
//Walk the list and print each node data
// TODO: change this to a do-while
while(current_node != list->first)
{
printf("%s\n", current_node->line);
current_node = current_node->prev;
}
// Don't forget the last guy
printf("%s\n", list->first->line);
}
void
List_walk_print(List *list)
{
Node *current_node = list->first;
//Walk the list and print each node data
// TODO: change this to a do-while
while(current_node != list->last)
{
printf("%s\n", current_node->line);
current_node = current_node->next;
}
// Don't forget the last guy
printf("%s\n", list->last->line);
}
void
List_walk_destroy(List *list)
{
Node *current_node = list->first;
Node *tmp;
//Walk the list and print each node data
while(current_node != list->last)
{
free(current_node->line);
tmp = current_node;
current_node = current_node->next;
free(tmp);
}
free(list->last->line);
free(list->last);
}
int
main(int argc, char *argv[])
{
FILE *fp;
fp = fopen("/etc/passwd", "r");
int ch;
int line_count = 0;
char *current_line = malloc(1024);
struct List LineList;
LineList.first = NULL;
LineList.last = NULL;
int count = 0;
for(; (ch = getc(fp)) != EOF;)
{
if(ch != '\n')
{
if(strlen(current_line) > 1010) // getting close, malloc up some more
current_line = realloc(current_line, strlen(current_line)+1024);
memcpy(current_line+count, &ch, sizeof(int));
count++;
}
else
{
line_count++;
count = 0;
List_add(&LineList, current_line);
}
}
List_walk_print(&LineList);
printf("------ Backwards Now! ------\n");
List_backwards_walk(&LineList);
// Now, destroy the son of a bitch
List_walk_destroy(&LineList);
// Don't forget the free this one.
free(current_line);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment