Skip to content

Commit ca040d4

Browse files
committed
lib2,gitignore: correct wrong config, add loss files.
1 parent 2a6f444 commit ca040d4

File tree

4 files changed

+157
-1
lines changed

4 files changed

+157
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/wdbbs.conf
2-
*.c
32
*.o
43
*.a
54
*.p

lib2/sys/args.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* initsetproctitle() 是從 sendmail 的 source code 裡面挖來的...
2+
* setproctitle() 我重寫過... countproctitle() 是寫好玩的...
3+
* 試試看... 有毛病記得告訴我... :)
4+
* -- Beagle
5+
*/
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
#include <stdarg.h>
10+
#define newstr(s) strcpy(malloc(strlen(s) + 1), s)
11+
12+
/*
13+
** Pointers for setproctitle.
14+
**This allows "ps" listings to give more useful information.
15+
*/
16+
17+
char **Argv = NULL; /* pointer to argument vector */
18+
char *LastArgv = NULL; /* end of argv */
19+
20+
void initsetproctitle(argc, argv, envp)
21+
int argc;
22+
char **argv;
23+
char **envp;
24+
{
25+
register int i;
26+
extern char **environ;
27+
28+
/*
29+
** Move the environment so setproctitle can use the space at
30+
** the top of memory.
31+
*/
32+
33+
for (i = 0; envp[i] != NULL; i++)
34+
continue;
35+
environ = (char **) malloc(sizeof (char *) * (i + 1));
36+
for (i = 0; envp[i] != NULL; i++)
37+
environ[i] = newstr(envp[i]);
38+
environ[i] = NULL;
39+
40+
/*
41+
** Save start and extent of argv for setproctitle.
42+
*/
43+
44+
Argv = argv;
45+
if (i > 0)
46+
LastArgv = envp[i - 1] + strlen(envp[i - 1]);
47+
else
48+
LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
49+
}
50+
51+
void setproctitle(const char* cmdline) {
52+
char buf[256], *p;
53+
int i;
54+
55+
strncpy(buf, cmdline, 256);
56+
buf[255] = '\0';
57+
i = strlen(buf);
58+
if (i > LastArgv - Argv[0] - 2) {
59+
i = LastArgv - Argv[0] - 2;
60+
}
61+
strcpy(Argv[0], buf);
62+
p=&Argv[0][i];
63+
while (p < LastArgv) *p++='\0';
64+
Argv[1] = NULL;
65+
}
66+
67+
void printpt(const char* format, ...) {
68+
char buf[256];
69+
va_list args;
70+
va_start(args, format);
71+
vsprintf(buf, format,args);
72+
setproctitle(buf);
73+
va_end(args);
74+
}
75+
76+
int countproctitle() {
77+
return (LastArgv - Argv[0] - 2);
78+
}
79+

lib2/sys/sem.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <sys/types.h>
2+
#include <sys/ipc.h>
3+
#include <sys/sem.h>
4+
5+
/* ----------------------------------------------------- */
6+
/* semaphore : for critical section */
7+
/* ----------------------------------------------------- */
8+
9+
#define SEM_FLG 0600 /* semaphore mode */
10+
11+
12+
void /* sem_init(BSEM_KEY,&ap_semid) */
13+
sem_init(int semkey,int *semid)
14+
{
15+
#if defined ( __linux__ ) || defined ( __CYGWIN__ )
16+
int semval = 1;
17+
#else
18+
union semun semval; // 解決某些 OS 會把 1 認為是 int 之問題
19+
semval.val = 1;
20+
#endif
21+
22+
*semid = semget(semkey, 1, 0);
23+
if (*semid == -1)
24+
{
25+
*semid = semget(semkey, 1, IPC_CREAT | SEM_FLG);
26+
// if (*semid == -1)
27+
// attach_err(semkey, "semget");
28+
semctl(*semid, 0, SETVAL, semval);
29+
}
30+
}
31+
32+
void
33+
sem_lock(int op,int semid) /* sem_lock(SEM_LEAVE,ap_semid) */
34+
{
35+
struct sembuf sops;
36+
37+
sops.sem_num = 0;
38+
sops.sem_flg = SEM_UNDO;
39+
sops.sem_op = op;
40+
semop(semid, &sops, 1);
41+
}

lib2/sys/shm.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <unistd.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <sys/types.h>
5+
#include <sys/ipc.h>
6+
#include <sys/shm.h>
7+
8+
9+
void *
10+
shm_new(shmkey, shmsize)
11+
int shmkey, shmsize;
12+
{
13+
void *shmptr;
14+
int shmid;
15+
16+
shmid = shmget(shmkey, shmsize, 0);
17+
if (shmid < 0)
18+
{
19+
shmid = shmget(shmkey, shmsize, IPC_CREAT | 0600);
20+
if (shmid < 0)
21+
exit(-1);
22+
}
23+
else
24+
{
25+
shmsize = 0;
26+
}
27+
28+
shmptr = (void *) shmat(shmid, 0, 0);
29+
if (shmptr == (void *) -1)
30+
exit(-2);
31+
32+
if (shmsize)
33+
memset(shmptr, 0, shmsize);
34+
35+
return shmptr;
36+
}
37+

0 commit comments

Comments
 (0)