Skip to content

Commit 34ca696

Browse files
chenzhijiaxiaoxiang781216
chenzhijia
authored andcommitted
Add The Linux Programming Interface under examples
https://man7.org/tlpi/index.html Signed-off-by: chenzhijia <[email protected]>
1 parent 4ba65cc commit 34ca696

8 files changed

+810
-0
lines changed

examples/tlpi/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/tlpi
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
From 57d210aa51e9706c3e21156d873bf356527be8c7 Mon Sep 17 00:00:00 2001
2+
From: chenzhijia <[email protected]>
3+
Date: Tue, 19 Mar 2024 11:26:27 +0800
4+
Subject: [PATCH] Linux case base:add ename.c.inc
5+
6+
add source code ename.c.inc
7+
8+
Change-Id: Ie31e2ac3908ce46cfb22102d8cc18fa1b961deb4
9+
Signed-off-by: chenzhijia <[email protected]>
10+
---
11+
12+
diff --git a/lib/ename.c.inc b/lib/ename.c.inc
13+
new file mode 100644
14+
index 0000000..42642e1
15+
--- /dev/null
16+
+++ b/lib/ename.c.inc
17+
@@ -0,0 +1,39 @@
18+
+/* ename.c.inc
19+
+
20+
+ Built on GNU/Linux x86_64 with glibc 2.38
21+
+*/
22+
+static char *ename[] = {
23+
+ /* 0 */ "",
24+
+ /* 1 */ "EPERM", "ENOENT", "ESRCH", "EINTR", "EIO", "ENXIO",
25+
+ /* 7 */ "E2BIG", "ENOEXEC", "EBADF", "ECHILD",
26+
+ /* 11 */ "EAGAIN/EWOULDBLOCK", "ENOMEM", "EACCES", "EFAULT",
27+
+ /* 15 */ "ENOTBLK", "EBUSY", "EEXIST", "EXDEV", "ENODEV",
28+
+ /* 20 */ "ENOTDIR", "EISDIR", "EINVAL", "ENFILE", "EMFILE",
29+
+ /* 25 */ "ENOTTY", "ETXTBSY", "EFBIG", "ENOSPC", "ESPIPE",
30+
+ /* 30 */ "EROFS", "EMLINK", "EPIPE", "EDOM", "ERANGE",
31+
+ /* 35 */ "EDEADLK/EDEADLOCK", "ENAMETOOLONG", "ENOLCK", "ENOSYS",
32+
+ /* 39 */ "ENOTEMPTY", "ELOOP", "", "ENOMSG", "EIDRM", "ECHRNG",
33+
+ /* 45 */ "EL2NSYNC", "EL3HLT", "EL3RST", "ELNRNG", "EUNATCH",
34+
+ /* 50 */ "ENOCSI", "EL2HLT", "EBADE", "EBADR", "EXFULL", "ENOANO",
35+
+ /* 56 */ "EBADRQC", "EBADSLT", "", "EBFONT", "ENOSTR", "ENODATA",
36+
+ /* 62 */ "ETIME", "ENOSR", "ENONET", "ENOPKG", "EREMOTE",
37+
+ /* 67 */ "ENOLINK", "EADV", "ESRMNT", "ECOMM", "EPROTO",
38+
+ /* 72 */ "EMULTIHOP", "EDOTDOT", "EBADMSG", "EOVERFLOW",
39+
+ /* 76 */ "ENOTUNIQ", "EBADFD", "EREMCHG", "ELIBACC", "ELIBBAD",
40+
+ /* 81 */ "ELIBSCN", "ELIBMAX", "ELIBEXEC", "EILSEQ", "ERESTART",
41+
+ /* 86 */ "ESTRPIPE", "EUSERS", "ENOTSOCK", "EDESTADDRREQ",
42+
+ /* 90 */ "EMSGSIZE", "EPROTOTYPE", "ENOPROTOOPT",
43+
+ /* 93 */ "EPROTONOSUPPORT", "ESOCKTNOSUPPORT",
44+
+ /* 95 */ "EOPNOTSUPP/ENOTSUP", "EPFNOSUPPORT", "EAFNOSUPPORT",
45+
+ /* 98 */ "EADDRINUSE", "EADDRNOTAVAIL", "ENETDOWN", "ENETUNREACH",
46+
+ /* 102 */ "ENETRESET", "ECONNABORTED", "ECONNRESET", "ENOBUFS",
47+
+ /* 106 */ "EISCONN", "ENOTCONN", "ESHUTDOWN", "ETOOMANYREFS",
48+
+ /* 110 */ "ETIMEDOUT", "ECONNREFUSED", "EHOSTDOWN", "EHOSTUNREACH",
49+
+ /* 114 */ "EALREADY", "EINPROGRESS", "ESTALE", "EUCLEAN",
50+
+ /* 118 */ "ENOTNAM", "ENAVAIL", "EISNAM", "EREMOTEIO", "EDQUOT",
51+
+ /* 123 */ "ENOMEDIUM", "EMEDIUMTYPE", "ECANCELED", "ENOKEY",
52+
+ /* 127 */ "EKEYEXPIRED", "EKEYREVOKED", "EKEYREJECTED",
53+
+ /* 130 */ "EOWNERDEAD", "ENOTRECOVERABLE", "ERFKILL", "EHWPOISON"
54+
+};
55+
+
56+
+#define MAX_ENAME 133
57+
\ No newline at end of file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
From f1154cd6558b99aa5827ac98b20a20753dabd865 Mon Sep 17 00:00:00 2001
2+
From: chenzhijia <[email protected]>
3+
Date: Tue, 18 Jun 2024 18:19:18 +0800
4+
Subject: [PATCH] tlpi/proc:fix setenv case
5+
6+
Remove duplicate functions and invalid commands
7+
8+
Change-Id: I59d84511356f6da7313844bfad63ac466fd3075f
9+
Signed-off-by: chenzhijia <[email protected]>
10+
---
11+
12+
diff --git a/proc/setenv.c b/proc/setenv.c
13+
index d396628..d75ae35 100644
14+
--- a/proc/setenv.c
15+
+++ b/proc/setenv.c
16+
@@ -21,92 +21,25 @@
17+
#include <errno.h>
18+
19+
int
20+
-unsetenv(const char *name)
21+
-{
22+
- extern char **environ;
23+
- char **ep, **sp;
24+
- size_t len;
25+
-
26+
- if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL) {
27+
- errno = EINVAL;
28+
- return -1;
29+
- }
30+
-
31+
- len = strlen(name);
32+
-
33+
- for (ep = environ; *ep != NULL; )
34+
- if (strncmp(*ep, name, len) == 0 && (*ep)[len] == '=') {
35+
-
36+
- /* Remove found entry by shifting all successive entries
37+
- back one element */
38+
-
39+
- for (sp = ep; *sp != NULL; sp++)
40+
- *sp = *(sp + 1);
41+
-
42+
- /* Continue around the loop to further instances of 'name' */
43+
-
44+
- } else {
45+
- ep++;
46+
- }
47+
-
48+
- return 0;
49+
-}
50+
-
51+
-int
52+
-setenv(const char *name, const char *value, int overwrite)
53+
-{
54+
- char *es;
55+
-
56+
- if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL ||
57+
- value == NULL) {
58+
- errno = EINVAL;
59+
- return -1;
60+
- }
61+
-
62+
- if (getenv(name) != NULL && overwrite == 0)
63+
- return 0;
64+
-
65+
- unsetenv(name); /* Remove all occurrences */
66+
-
67+
- es = malloc(strlen(name) + strlen(value) + 2);
68+
- /* +2 for '=' and null terminator */
69+
- if (es == NULL)
70+
- return -1;
71+
-
72+
- strcpy(es, name);
73+
- strcat(es, "=");
74+
- strcat(es, value);
75+
-
76+
- return (putenv(es) != 0) ? -1 : 0;
77+
-}
78+
-
79+
-#ifdef TEST_IT
80+
-
81+
-int
82+
main()
83+
{
84+
if (putenv("TT=xxxxx") != 0)
85+
perror("putenv");
86+
87+
system("echo '***** Environment before unsetenv(TT)'; "
88+
- "printenv | grep ^TT");
89+
- system("echo 'Total lines from printenv:' `printenv | wc -l`");
90+
+ "env");
91+
92+
unsetenv("TT");
93+
94+
system("echo '***** Environment after unsetenv(TT)'; "
95+
- "printenv | grep ^TT");
96+
- system("echo 'Total lines from printenv:' `printenv | wc -l`");
97+
+ "env");
98+
99+
setenv("xyz", "one", 1);
100+
setenv("xyz", "two", 0);
101+
setenv("xyz2", "222", 0);
102+
103+
system("echo '***** Environment after setenv() calls'; "
104+
- "printenv | grep ^x");
105+
- system("echo 'Total lines from printenv:' `printenv | wc -l`");
106+
+ "env");
107+
108+
exit(EXIT_SUCCESS);
109+
}
110+
-
111+
-#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
From 03445e7bf9916a58a4b80d7062d577f38ed2b279 Mon Sep 17 00:00:00 2001
2+
From: chenzhijia <[email protected]>
3+
Date: Tue, 14 May 2024 16:40:28 +0800
4+
Subject: [PATCH] tlpi/users_groups:Fix memory leak in check_password
5+
6+
free memory before program exits
7+
8+
Change-Id: I28789b5ceaec4aaa45bb13f66cb3f61de1111841
9+
Signed-off-by: chenzhijia <[email protected]>
10+
---
11+
12+
diff --git a/users_groups/check_password.c b/users_groups/check_password.c
13+
index c142ac7..a70cbb6 100644
14+
--- a/users_groups/check_password.c
15+
+++ b/users_groups/check_password.c
16+
@@ -30,6 +30,22 @@
17+
#include <shadow.h>
18+
#include "tlpi_hdr.h"
19+
20+
+#define fatal(msg) \
21+
+ do \
22+
+ { \
23+
+ free(username); \
24+
+ fatal(msg); \
25+
+ } \
26+
+ while (0)
27+
+
28+
+#define errExit(msg) \
29+
+ do \
30+
+ { \
31+
+ free(username); \
32+
+ errExit(msg); \
33+
+ } \
34+
+ while (0)
35+
+
36+
int
37+
main(int argc, char *argv[])
38+
{
39+
@@ -53,7 +69,7 @@
40+
printf("Username: ");
41+
fflush(stdout);
42+
if (fgets(username, lnmax, stdin) == NULL)
43+
- exit(EXIT_FAILURE); /* Exit on EOF */
44+
+ goto err; /* Exit on EOF */
45+
46+
len = strlen(username);
47+
if (username[len - 1] == '\n')
48+
@@ -85,12 +101,17 @@
49+
authOk = strcmp(encrypted, pwd->pw_passwd) == 0;
50+
if (!authOk) {
51+
printf("Incorrect password\n");
52+
- exit(EXIT_FAILURE);
53+
+ goto err;
54+
}
55+
56+
printf("Successfully authenticated: UID=%ld\n", (long) pwd->pw_uid);
57+
58+
/* Now do authenticated work... */
59+
60+
+ free(username);
61+
exit(EXIT_SUCCESS);
62+
+
63+
+err:
64+
+ free(username);
65+
+ exit(EXIT_FAILURE);
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
From 65230c70d69b253028c0f912dcd1ae0cb7446015 Mon Sep 17 00:00:00 2001
2+
From: chenzhijia <[email protected]>
3+
Date: Wed, 30 Oct 2024 10:52:48 +0800
4+
Subject: [PATCH] tlpi/sockets:Fix compilation error
5+
6+
Fix compilation error in qemu
7+
8+
Change-Id: I854ed64d163398807a07fa697bdea89bfab77236
9+
Signed-off-by: chenzhijia <[email protected]>
10+
---
11+
12+
diff --git a/sockets/list_host_addresses.c b/sockets/list_host_addresses.c
13+
index 4db58f6..e1864c8 100644
14+
--- a/sockets/list_host_addresses.c
15+
+++ b/sockets/list_host_addresses.c
16+
@@ -23,7 +23,7 @@
17+
#include <stdio.h>
18+
#include <stdlib.h>
19+
#include <unistd.h>
20+
-#include <linux/if_link.h>
21+
+//#include <linux/if_link.h>
22+
23+
int
24+
main(int argc, char *argv[])
25+
diff --git a/sockets/sendfile.c b/sockets/sendfile.c
26+
index 54549d7..3697099 100644
27+
--- a/sockets/sendfile.c
28+
+++ b/sockets/sendfile.c
29+
@@ -17,7 +17,7 @@
30+
#include "tlpi_hdr.h"
31+
#define BUF_SIZE 8192
32+
33+
-ssize_t
34+
+static ssize_t
35+
sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
36+
{
37+
off_t orig;

examples/tlpi/Kconfig

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config EXAMPLES_TLPI
7+
tristate "The Linux Programming Interface Example"
8+
default n
9+
depends on ALLOW_GPL_COMPONENTS
10+
---help---
11+
Enable the tlpi
12+
13+
if EXAMPLES_TLPI
14+
15+
config EXAMPLES_TLPI_PRIORITY
16+
int "Task priority"
17+
default 100
18+
19+
config EXAMPLES_TLPI_STACKSIZE
20+
int "Stack size"
21+
default DEFAULT_TASK_STACKSIZE
22+
23+
endif

examples/tlpi/Make.defs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# apps/examples/tlpi/Make.defs
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
ifneq ($(CONFIG_EXAMPLES_TLPI),)
22+
CONFIGURED_APPS += $(APPDIR)/examples/tlpi
23+
endif

0 commit comments

Comments
 (0)