-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add util/file, move file utils there
- Loading branch information
bkleiner
committed
Jan 19, 2023
1 parent
f054f1b
commit a579b64
Showing
6 changed files
with
81 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "file.h" | ||
|
||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <sys/stat.h> | ||
|
||
bool file_compare(char *f1, char *f2) { | ||
FILE *fp1; | ||
FILE *fp2; | ||
char c1, c2; | ||
bool ret; | ||
|
||
fp1 = fopen(f1, "r"); | ||
if (!fp1) | ||
return false; | ||
|
||
fp2 = fopen(f2, "r"); | ||
if (!fp2) { | ||
fclose(fp1); | ||
return false; | ||
} | ||
|
||
ret = true; | ||
while (!feof(fp1)) { | ||
fread(&c1, 1, 1, fp1); | ||
fread(&c2, 1, 1, fp2); | ||
if (c1 != c2) { | ||
ret = false; | ||
break; | ||
} | ||
} | ||
fclose(fp1); | ||
fclose(fp2); | ||
return ret; | ||
} | ||
|
||
bool file_exists(const char *filename) { | ||
return access(filename, F_OK) == 0; | ||
} | ||
|
||
bool file_printf(const char *filename, const char *fmt, ...) { | ||
FILE *fp = fopen(filename, "w"); | ||
if (!fp) { | ||
return false; | ||
} | ||
|
||
va_list args; | ||
va_start(args, fmt); | ||
vfprintf(fp, fmt, args); | ||
va_end(args); | ||
|
||
fclose(fp); | ||
|
||
return true; | ||
} | ||
|
||
long file_get_size(const char* filename) { | ||
struct stat st; | ||
if (stat(filename, &st) != 0) { | ||
return 0; | ||
} | ||
return st.st_size; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
#include <stdbool.h> | ||
|
||
bool file_compare(char *f1, char *f2); | ||
bool file_exists(const char *filename); | ||
bool file_printf(const char *filename, const char *fmt, ...); | ||
long file_get_size(const char* filename); |