Просмотр исходного кода

new utility functions, started init functionality

master
mort 10 лет назад
Родитель
Сommit
7df8b9116c
3 измененных файлов: 84 добавлений и 13 удалений
  1. 74
    9
      src/cms_util.c
  2. 9
    3
      src/cms_util.h
  3. 1
    1
      src/main.c

+ 74
- 9
src/cms_util.c Просмотреть файл

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>


#include "cms_util.h" #include "cms_util.h"


int cms_util_file_exists(char* fname) int cms_util_file_exists(char* fname)
{ {
FILE* f;
if (f = fopen(fname, "r"))
{
fclose(f);
int f = open(fname, O_RDONLY);

if (errno == ENOENT)
return 0;
else
return 1; return 1;
}


return 0;
close(f);
} }


void cms_util_file_create(char* fname) void cms_util_file_create(char* fname)
{ {
FILE* f = fopen(fname, "ab+");
fclose(f);
int f = open(fname, O_CREAT);
close(f);
}

int cms_util_file_copy(char* fname1, char* fname2)
{
int f1 = open(fname1, O_RDONLY);
int f2 = open(fname2, O_WRONLY);

struct stat* s = malloc(sizeof(struct stat));
if (s == NULL)
return 1;

fstat(f1, s);

void* buf = malloc(s->st_size);
if (buf == NULL)
return 1;

read(f1, buf, s->st_size);
write(f2, buf, s->st_size);

close(f1);
close(f2);

free(s);
free(buf);

return 0;
}

int cms_util_dir_copy_recursive(char* dir1, char* dir2)
{
mkdir(dir2, 0777);
DIR* dp = opendir(dir1);

struct dirent* ep;

if (dp == NULL)
return 1;

while (ep = readdir(dp))
{
char* path1 = cms_util_path_join(dir1, ep->d_name);
char* path2 = cms_util_path_join(dir2, ep->d_name);
struct stat* s = malloc(sizeof(struct stat));
stat(path1, s);

if (S_ISDIR(s->st_mode))
{
cms_util_dir_copy_recursive(path1, path2);
}
else
{
cms_util_file_copy(path1, path2);
}

free(s);
}

return 0;
} }


char* cms_util_join_paths(char* str1, char* str2)
char* cms_util_path_join(char* str1, char* str2)
{ {
size_t len1 = strlen(str1); size_t len1 = strlen(str1);
size_t len2 = strlen(str2); size_t len2 = strlen(str2);

+ 9
- 3
src/cms_util.h Просмотреть файл

#ifndef CMS_UTIL_H #ifndef CMS_UTIL_H
#define CMS_UTIL_H #define CMS_UTIL_H


//Returns 1 if a file exists, 0 if it doesn't.
//Return 1 if a file exists, 0 if it doesn't
int cms_util_file_exists(char* fname); int cms_util_file_exists(char* fname);


//Create a file //Create a file
void cms_util_file_create(char* fname); void cms_util_file_create(char* fname);


//Joins together two paths.
char* cms_util_join_paths(char* str1, char* str2);
//Copy a file
int cms_util_file_copy(char* fname1, char* fname2);

//Recursively copy a directory
int cms_util_dir_copy_recursive(char* dir1, char* dir2);

//Join together two paths
char* cms_util_path_join(char* str1, char* str2);


#endif #endif

+ 1
- 1
src/main.c Просмотреть файл



//Get the path of .cmsinited, which tells us //Get the path of .cmsinited, which tells us
//whether or not the directory is already inited //whether or not the directory is already inited
char* initedPath = cms_util_join_paths(dirname, CMS_FILE_INITED);
char* initedPath = cms_util_path_join(dirname, CMS_FILE_INITED);
if (initedPath == NULL) if (initedPath == NULL)
cms_log_panic("Memory allocation failed."); cms_log_panic("Memory allocation failed.");



Загрузка…
Отмена
Сохранить