fileutils.c
TinyMAZE/src/util/fileutils.c · 1.4 KB
From the archive TinyMAZEv2.5a.tar.gz
Download the archive « Back to the archive contents
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "filelist.h"
#include "db.h"
#include "externs.h"
/* Returns a pointer to the next line that isn't blank and doesn't begin
with a '#' */
char *get_next_line(FILE *fp)
{
char buf[1024];
while(1)
{
if(!fgets(buf, sizeof(buf), fp))
return(NULL);
*(buf+strlen(buf)-1) = '\0';
if(!*buf || *buf == '#')
continue;
break;
}
return(stack_string_alloc(buf, 0));
}
int getref(FILE *f)
{
char buf[4096];
fgets(buf, sizeof(buf), f);
return(atoi(buf));
}
void putref(FILE *f, int ref)
{
fprintf(f, "%d\n", ref);
}
unsigned long file_size(char *file)
{
struct stat statbuf;
stat(file, &statbuf);
return(statbuf.st_size);
}
FILELIST *get_filelist(char *dirname, int directories)
{
FILELIST *flist = NULL, *new;
DIR *dir;
struct dirent *entry;
struct stat stinfo;
if(!(dir = opendir(dirname)))
return(NULL);
while((entry = readdir(dir)))
{
lstat(tprintf("%s/%s", dirname, entry->d_name), &stinfo);
if(S_ISDIR(stinfo.st_mode) && !directories)
continue;
new = (FILELIST *)stack_alloc(sizeof(FILELIST), 0, 0);
new->name = stack_string_alloc(entry->d_name, 0);
new->flags = (S_ISDIR(stinfo.st_mode))?FILE_DIRECTORY:0;
new->next = flist;
flist = new;
}
closedir(dir);
return(flist);
}