a quick C cat

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2020-06-03 07:00:06 +00:00
parent 17ed296e2d
commit 6202b44eba
5 changed files with 88 additions and 68 deletions

13
userland/c/cat.c Normal file
View File

@@ -0,0 +1,13 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <lkmc/file_io.h>
int main(int argc, char **argv) {
assert(argc == 2);
char *s = lkmc_file_read(argv[1]);
puts(s);
free(s);
return EXIT_SUCCESS;
}

View File

@@ -1,38 +1,12 @@
/* https://cirosantilli.com/linux-kernel-module-cheat#c */
#include <lkmc.h>
#include <lkmc/file_io.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
/* Returns the size of the given open `FILE*`.
*
* If an error occurs, returns `-1L`.
*
* Does not work for pipes.
*/
long fget_file_size(FILE *fp) {
long oldpos;
long return_value;
oldpos = ftell(fp);
if (oldpos == -1L) {
return -1L;
}
if (fseek(fp, 0, SEEK_END) != 0) {
return -1L;
}
return_value = ftell(fp);
if (return_value == -1L) {
return -1L;
}
/* Restore the old position. */
if (fseek(fp, oldpos , SEEK_SET) != 0) {
return -1L;
}
return return_value;
}
/* Same as `file_size`, but takes the path instead of a `FILE*`. */
long file_size(char *path) {
FILE *fp;
@@ -41,48 +15,13 @@ long file_size(char *path) {
if (fp == NULL) {
return -1L;
}
return_value = fget_file_size(fp);
return_value = lkmc_fget_file_size(fp);
if (fclose(fp) == EOF) {
return -1L;
}
return return_value;
}
/* Read the entire file to a char[] dynamically allocated inside this function.
*
* Returns a pointer to the start of that array.
*
* In case of any error, returns NULL.
*
* http://stackoverflow.com/questions/174531/easiest-way-to-get-files-contents-in-c
*/
char *file_read(char *path) {
FILE *fp;
char *buffer;
long fsize;
fp = fopen(path , "rb");
if (fp == NULL) {
return NULL;
}
fsize = fget_file_size(fp);
if (fsize < 0){
fprintf(stderr, "could not determine lenght of:\n%s\n", path);
return NULL;
}
buffer = (char*)malloc(fsize);
if (buffer == NULL) {
return NULL;
}
if (fread(buffer, 1, fsize, fp) != (size_t)fsize) {
return NULL;
}
if (fclose(fp) == EOF){
return NULL;
}
return buffer;
}
/* Write a null terminated string to file
*
* Return -1 on failure, 0 on success.
@@ -117,9 +56,9 @@ int main(void) {
/* Read entire file at once to a string. */
{
char *output = file_read(path);
char *output = lkmc_file_read(path);
if (output == NULL) {
LKMC_IO_ERROR("file_read", path);
LKMC_IO_ERROR("lkmc_file_read", path);
}
assert(strcmp(data, output) == 0);
free(output);