Skip to content

Commit

Permalink
Merge pull request beejjorgensen#2 from Azhrei/master
Browse files Browse the repository at this point in the history
Updates to bring the Guide into the 21st century
  • Loading branch information
beejjorgensen committed Oct 17, 2019
2 parents c7bd034 + 67b6cad commit df34880
Show file tree
Hide file tree
Showing 18 changed files with 510 additions and 415 deletions.
2 changes: 1 addition & 1 deletion bin/bg2fo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python2
#!/usr/bin/env python2.7
#
# bg2fo -- convert bg to Formatting Object XML
#
Expand Down
2 changes: 1 addition & 1 deletion bin/bg2html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python2
#!/usr/bin/env python2.7
#
# bg2conv -- a semi-hardcoded parser for Beej's Guide XML files
#
Expand Down
22 changes: 11 additions & 11 deletions examples/echoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

int main(void)
{
int s, t, len;
struct sockaddr_un remote;
int s, len;
struct sockaddr_un remote = {
.sun_family = AF_UNIX,
// .sun_path = SOCK_PATH, // Can't do assignment to an array
};
char str[100];

if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
Expand All @@ -26,7 +29,6 @@ int main(void)

printf("Trying to connect...\n");

remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCK_PATH);
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(s, (struct sockaddr *)&remote, len) == -1) {
Expand All @@ -36,17 +38,18 @@ int main(void)

printf("Connected.\n");

while(printf("> "), fgets(str, 100, stdin), !feof(stdin)) {
if (send(s, str, strlen(str), 0) == -1) {
/* size in fgets() includes the null byte */
while(printf("> "), fgets(str, sizeof(str), stdin), !feof(stdin)) {
if (send(s, str, strlen(str)+1, 0) == -1) {
perror("send");
exit(1);
}

if ((t=recv(s, str, 100, 0)) > 0) {
str[t] = '\0';
if ((len=recv(s, str, sizeof(str)-1, 0)) > 0) {
str[len] = '\0';
printf("echo> %s", str);
} else {
if (t < 0) perror("recv");
if (len < 0) perror("recv");
else printf("Server closed connection\n");
exit(1);
}
Expand All @@ -56,6 +59,3 @@ int main(void)

return 0;
}



16 changes: 7 additions & 9 deletions examples/echos.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
int main(void)
{
int s, s2, len;
unsigned t;
struct sockaddr_un local, remote;
struct sockaddr_un remote, local = {
.sun_family = AF_UNIX,
// .sun_path = SOCK_PATH, // Can't do assignment to an array
};
char str[100];

if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}

local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH);
unlink(local.sun_path);
len = strlen(local.sun_path) + sizeof(local.sun_family);
Expand All @@ -42,8 +43,8 @@ int main(void)
for(;;) {
int done, n;
printf("Waiting for a connection...\n");
t = sizeof(remote);
if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1) {
socklen_t slen = sizeof(remote);
if ((s2 = accept(s, (struct sockaddr *)&remote, &slen)) == -1) {
perror("accept");
exit(1);
}
Expand All @@ -52,7 +53,7 @@ int main(void)

done = 0;
do {
n = recv(s2, str, 100, 0);
n = recv(s2, str, sizeof(str), 0);
if (n <= 0) {
if (n < 0) perror("recv");
done = 1;
Expand All @@ -70,6 +71,3 @@ int main(void)

return 0;
}



3 changes: 2 additions & 1 deletion examples/fork1.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ int main(void)
pid_t pid;
int rv;

// Be sure to flush all stdio buffers, or the output will be
// duplicated when both parent and child flush them later!
switch(pid = fork()) {
case -1:
perror("fork"); /* something went wrong */
Expand All @@ -40,4 +42,3 @@ int main(void)

return 0;
}

3 changes: 1 addition & 2 deletions examples/kirk.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main(void)
/* ditch newline at end, if it exists */
if (buf.mtext[len-1] == '\n') buf.mtext[len-1] = '\0';

if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */
if (msgsnd(msqid, &buf, len, 0) == -1)
perror("msgsnd");
}

Expand All @@ -52,4 +52,3 @@ int main(void)

return 0;
}

11 changes: 6 additions & 5 deletions examples/lockdemo.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

int main(int argc, char *argv[])
{
/* l_type l_whence l_start l_len l_pid */
struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0 };
struct flock fl = {
.l_type = F_WRLCK,
.l_whence = SEEK_SET,
.l_start = 0,
.l_len = 0,
};
int fd;

(void)argv; // silence unused warning

fl.l_pid = getpid();

if (argc > 1)
fl.l_type = F_RDLCK;

Expand Down Expand Up @@ -52,4 +54,3 @@ int main(int argc, char *argv[])

return 0;
}

10 changes: 5 additions & 5 deletions examples/mmapdemo.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
int main(int argc, char *argv[])
{
int fd, offset;
char *data;
void *data;
struct stat sbuf;

if (argc != 2) {
Expand All @@ -34,17 +34,17 @@ int main(int argc, char *argv[])

offset = atoi(argv[1]);
if (offset < 0 || offset > sbuf.st_size-1) {
fprintf(stderr, "mmapdemo: offset must be in the range 0-%ld\n", sbuf.st_size-1);
fprintf(stderr, "mmapdemo: offset must be in the range 0-%lld\n", sbuf.st_size-1);
exit(1);
}

if ((data = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0)) == (caddr_t)(-1)) {
// Note that MAP_FAILED is (void*)(-1), but more readable
if ((data = mmap((void*)0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
perror("mmap");
exit(1);
}

printf("byte at offset %d is '%c'\n", offset, data[offset]);
printf("byte at offset %d is '%c'\n", offset, ((char*)data)[offset]);

return 0;
}

1 change: 0 additions & 1 deletion examples/pipe1.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ int main(void)

return 0;
}

4 changes: 3 additions & 1 deletion examples/pipe2.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ int main(void)
exit(0);
} else {
printf("PARENT: reading from pipe\n");
// This read blocks. Fortunately, the child
// writes to it in non-blocking mode and then closes it,
// causing the OS trigger this process to wake up.
read(pfds[0], buf, 5);
printf("PARENT: read \"%s\"\n", buf);
wait(NULL);
}

return 0;
}

9 changes: 4 additions & 5 deletions examples/pipe3.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ int main(void)
pipe(pfds);

if (!fork()) {
close(1); /* close normal stdout */
dup(pfds[1]); /* make stdout same as pfds[1] */
dup2(pfds[1], 1); /* combines close() and dup(), atomically */
close(pfds[0]); /* we don't need this */
close(pfds[1]); /* we don't need this */
execlp("ls", "ls", NULL);
} else {
close(0); /* close normal stdin */
dup(pfds[0]); /* make stdin same as pfds[0] */
dup2(pfds[0], 0); /* combines close() and dup(), atomically */
close(pfds[0]); /* we don't need this */
close(pfds[1]); /* we don't need this */
execlp("wc", "wc", "-l", NULL);
}

return 0;
}

6 changes: 4 additions & 2 deletions examples/semdemo.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@

#define MAX_RETRIES 10

#ifdef NEED_SEMUN
/* Defined in sys/sem.h as required by POSIX now */
union semun {
int val;
struct semid_ds *buf;
ushort *array;
};
#endif

/*
** initsem() -- more-than-inspired by W. Richard Stevens' UNIX Network
Expand All @@ -27,7 +30,7 @@ int initsem(key_t key, int nsems) /* key from ftok() */
int i;
union semun arg;
struct semid_ds buf;
struct sembuf sb;
struct sembuf sb = { 0 }; /* best to always init structs */
int semid;

semid = semget(key, nsems, IPC_CREAT | IPC_EXCL | 0666);
Expand All @@ -48,7 +51,6 @@ int initsem(key_t key, int nsems) /* key from ftok() */
return -1; /* error, check errno */
}
}

} else if (errno == EEXIST) { /* someone else got it first */
int ready = 0;

Expand Down
7 changes: 6 additions & 1 deletion examples/shmdemo.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ int main(int argc, char *argv[])
}

/* attach to the segment to get a pointer to it: */
/* note that 'void*' will be implicitly converted to 'char*' */
data = shmat(shmid, (void *)0, 0);
if (data == (char *)(-1)) {

/* we _could_ use MAP_FAILED, but technically that's not */
/* the defined return value. System V failed on this one! */
if (data == (void *)(-1)) {
perror("shmat");
exit(1);
}
Expand All @@ -45,6 +49,7 @@ int main(int argc, char *argv[])
if (argc == 2) {
printf("writing to segment: \"%s\"\n", argv[1]);
strncpy(data, argv[1], SHM_SIZE);
data[SHM_SIZE-1] = '\0';
} else
printf("segment contains: \"%s\"\n", data);

Expand Down
11 changes: 4 additions & 7 deletions examples/sigint.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ void sigint_handler(int sig)

int main(void)
{
void sigint_handler(int sig); /* prototype */
char s[200];
struct sigaction sa;

sa.sa_handler = sigint_handler;
sa.sa_flags = 0; // or SA_RESTART
sigemptyset(&sa.sa_mask);
struct sigaction sa = {
.sa_handler = sigint_handler,
.sa_flags = 0, // or SA_RESTART
};

if (sigaction(SIGINT, &sa, NULL) == -1) {
perror("sigaction");
Expand All @@ -39,4 +37,3 @@ int main(void)

return 0;
}

10 changes: 4 additions & 6 deletions examples/sigusr.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ void sigusr1_handler(int sig)

int main(void)
{
struct sigaction sa;
struct sigaction sa = {
.sa_handler = sigusr1_handler,
.sa_flags = 0, // or SA_RESTART
};

got_usr1 = 0;

sa.sa_handler = sigusr1_handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);

if (sigaction(SIGUSR1, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
Expand All @@ -41,4 +40,3 @@ int main(void)

return 0;
}

1 change: 0 additions & 1 deletion examples/spock.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ int main(void)

return 0;
}

2 changes: 1 addition & 1 deletion examples/tick.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main(void)
printf("got a writer\n");

do {
if ((num = read(fd, s, 300)) == -1)
if ((num = read(fd, s, sizeof(s))) == -1)
perror("read");
else {
s[num] = '\0';
Expand Down
Loading

0 comments on commit df34880

Please sign in to comment.