summaryrefslogtreecommitdiff
path: root/md
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2022-08-12 18:02:51 +0100
committerFreeArtMan <dos21h@gmail.com>2022-08-12 18:02:51 +0100
commitc830b365f52bdd8dcaab604614658ce70d158055 (patch)
tree88280767b019d3f83c0f1725d7beba2ed935c828 /md
parent4fe12ecb448214f5c449ad5b821df5d450ade76f (diff)
downloadmd-content-c830b365f52bdd8dcaab604614658ce70d158055.tar.gz
md-content-c830b365f52bdd8dcaab604614658ce70d158055.zip
Add syscalls
Diffstat (limited to 'md')
-rw-r--r--md/notes/undefined_c/titles.md79
1 files changed, 70 insertions, 9 deletions
diff --git a/md/notes/undefined_c/titles.md b/md/notes/undefined_c/titles.md
index 7753e65..513b533 100644
--- a/md/notes/undefined_c/titles.md
+++ b/md/notes/undefined_c/titles.md
@@ -635,6 +635,9 @@ So now executable runs libshare from local directory. Ofc there is possible to i
### Static library
+
+
+
### Static binary
Static binary don't use any shared libraries, and its possible to built it once and distribute on other platforms
@@ -676,33 +679,91 @@ $ ldd static_elf
Statically compiled file should work on most platforms.
+<!--
+### stdin,stdout,stderr
+### Styles
+--->
-### Dynamic binary
-Dynamic binary is required to load dynamically linked libraries, they must be in known location.
-Replacing dynamic libraries with your own
+## Basic usage
+### File manipulation with libc
-Point to custom location of dynamic library
+Create file open data using libc functions
+```c
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+int main() {
+ FILE *f = fopen("file.txt","w+");
+ char *s = "Hello";
+ fwrite(s,1,strlen(s),f);
+ fclose(f);
+}
+```
+Open file and read data back
-### stdin,stdout,stderr
+```c
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+int main() {
+ FILE *f = fopen("file.txt","r");
+ char buf[128];
+ int r;
+ r = fread(buf,1,128,f);
+ buf[r] = 0;
+ printf("->%s\n",buf,r);
+ fclose(f);
+}
+```
-### Styles
+### File manipulation with syscalls
+Now lets do the same without using libc functions using syscall function to directly use syscalls,
+its also straightforward to rewrite example for assembly.
+```c
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/syscall.h>
+#include <string.h>
+int main(void) {
+ int fd = syscall(SYS_open, "sys.txt", O_CREAT|O_WRONLY, S_IRWXU|S_IRGRP|S_IXGRP);
+ char s[] = "hello sycall\n";
+ syscall(SYS_write, fd, s, strlen(s));
+ syscall(SYS_close, fd);
+ return 0;
+}
+```
-## Basic usage
+Read data from file
+
+```c
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/syscall.h>
+#include <string.h>
+
+int main(void) {
+ int fd = syscall(SYS_open, "sys.txt", O_RDONLY);
+ char s[128];
+ int r = syscall(SYS_read, fd, s, 128);
+ s[r] = 0;
+ syscall(SYS_close, fd);
+ syscall(SYS_write, 0, s, r);
+ return 0;
+}
+```
-### File manipulation with libc
-### File manipulation with syscalls
## Base usage