Skip to content

Commit

Permalink
new lseek
Browse files Browse the repository at this point in the history
  • Loading branch information
jkool702 committed Aug 2, 2024
1 parent 64858cf commit 3b21c4d
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 2 deletions.
4 changes: 2 additions & 2 deletions forkrun.bash
Original file line number Diff line number Diff line change
Expand Up @@ -1614,12 +1614,12 @@ forkrun_lseek_setup() {
case "${USER}" in
root)
mkdir -p /usr/local/lib/bash
curl -o /usr/local/lib/bash/lseek 'https://github.com/jkool702/forkrun/raw/main/lseek'
curl -o /usr/local/lib/bash/lseek 'https://github.com/jkool702/forkrun/raw/main/lseek_builtin/lseek'
;;
*)
mkdir -p /dev/shm/.forkrun.lseek
export BASH_LOADABLES_PATH=/dev/shm/.forkrun.lseek:${BASH_LOADABLES_PATH}
curl -o /dev/shm/.forkrun.lseek/lseek 'https://github.com/jkool702/forkrun/raw/main/lseek'
curl -o /dev/shm/.forkrun.lseek/lseek 'https://github.com/jkool702/forkrun/raw/main/lseek_builtin/lseek'
;;
esac

Expand Down
Binary file removed lseek
Binary file not shown.
Binary file added lseek_builtin/lseek
Binary file not shown.
104 changes: 104 additions & 0 deletions lseek_builtin/lseek.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include "command.h"
#include "builtins.h"
#include "shell.h"
#include "common.h"
#include "bashgetopt.h"


#ifdef USING_BASH_MALLOC
#include <malloc/shmalloc.h>
#endif

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef MAJOR_IN_MKDEV
# include <sys/mkdev.h>
#endif

#ifdef MAJOR_IN_SYSMACROS
# include <sys/sysmacros.h>
#endif

#ifndef errno
extern int errno;
#endif

// Function declaration for our builtin
static int lseek_main();
int lseek_builtin();
extern char **make_builtin_argv();

// Metadata about the builtin
static char *lseek_doc[] = {
"",
"----------------------------------------------",
"USAGE: lseek <FD> <REL_OFFSET>",
"",
"Move the file descriptor <FD> by <REL_OFFSET>",
"bytes relative to its current byte offset.",
"",
"positive <REL_OFFSET> advances the <FD>",
"negative <REL_OFFSET> rewinds the <FD>",
"----------------------------------------------",
"",
NULL
};

// Struct to register the builtin with bash
struct builtin lseek_struct = {
"lseek", // Name of the builtin
lseek_builtin, // Function to call
BUILTIN_ENABLED, // Default status
lseek_doc, // Documentation strings
"lseek <FD> <REL_OFFSET>", // Array of long options
0 // Number of long options
};

static int lseek_main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "\nIncorrect number of arguments.\nUSAGE: lseek <FD> <REL_OFFSET>\n");
return 1;
}

int fd = atoi(argv[1]);
if (fd == 0 && strcmp(argv[1], "0") != 0) {
fprintf(stderr, "\nInvalid file descriptor.\n");
return 1;
}

off_t offset = atoll(argv[2]);
if (errno == ERANGE) {
fprintf(stderr, "\nOffset out of range.\n");
return 1;
}

if (lseek(fd, offset, SEEK_CUR) == (off_t) -1) {
fprintf(stderr, strerror(errno));
return 1;
}

return 0;
}

int lseek_builtin(WORD_LIST *list) {
int c, r;
char **v;
WORD_LIST *l;

v = make_builtin_argv(list, &c);
r = lseek_main(c, v);

return r;
}

Binary file added lseek_builtin/lseek.o
Binary file not shown.
20 changes: 20 additions & 0 deletions lseek_builtin/lseek_compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
git clone https://git.savannah.gnu.org/git/bash.git

cd bash

./configure

make -j$(nproc)
make -j$(nproc) all

cd examples/loadables

curl -o ./lseek.c 'https://github.com/jkool702/forkrun/raw/main/lseek_builtin/lseek.c'

gcc -v -fPIC -DHAVE_CONFIG_H -DSHELL -DLOADABLE_BUILTIN -DSELECT_COMMAND -DUSING_BASH_MALLOC -g -O3 -Wno-parentheses -Wno-format-security -I. -I.. -I../.. -I../../lib -I../../builtins -I. -I../../include -I../../lib/malloc -I/mnt/ramdisk/bash -I/mnt/ramdisk/bash/lib -I/mnt/ramdisk/bash/builtins -L ../../lib/malloc -L ../.. -L /usr/lib/bash -I/usr/include/bash -I/usr/include/bash/builtins -I/usr/include/bash/include -c -o lseek.o lseek.c;
gcc -v -DSELECT_COMMAND -DLOADABLE_BUILTIN -DHAVE_CONFIG_H -DSHELL -DUSING_BASH_MALLOC -shared -Wl,-soname,lseek -L ../../lib/malloc -L ../.. -L /usr/lib/bash -O3 -o lseek lseek.o;

mkdir -p /usr/local/lib/bash
cp lseek /usr/local/lib/bash

enable lseek

0 comments on commit 3b21c4d

Please sign in to comment.