-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jkool702
committed
Aug 2, 2024
1 parent
64858cf
commit 3b21c4d
Showing
6 changed files
with
126 additions
and
2 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
Binary file not shown.
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,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 not shown.
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,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 |