"UNIX Environment Advanced Programming" third edition routine study notes and summary of questions


This article records the routine problems in the advanced programming book of the unix environment. The best shortcut to learn programming is to run the program once, which is less efficient than reading ten times.

1. Resource Download

Official source code:
http://www.apuebook.com/code3e.html

2. fatal error:

apue.h

Example one is myls.c

root@ubuntu:/# cc myls.c
myls.c:1:18: fatal error: apue.h: No such file or directory

This error indicates that the file apue.h was not found. At this time, find the source code downloaded from the first step, unzip it, and copy the apute.h under include to the /usr/includedirectory

cp ./apue.3e/include/apue.h /usr/include/

Execute gcc myls.c again, the following error is displayed:

myls.c:(.text+0x22): undefined reference to `err_quit'
myls.c:(.text+0x55): undefined reference to `err_sys'

This is because err_quit and err_sys are error handling functions defined by the author. Do the following two steps.

cp ./apue.3e/lib/error.c /usr/include/
然后
vim apue.h
在endif前添加 #include "error.c"

Insert picture description here

3. Chapter Three

hole.c

This routine demonstrates that the lseek function affects the value of the pointer at the current position of the file.

#include "apue.h"
#include <fcntl.h>

char    buf1[] = "abcdefghij";
char    buf2[] = "ABCDEFGHIJ";

int
main(void)
{
    
    
        int             fd;

        if ((fd = creat("file.hole", FILE_MODE)) < 0)
                err_sys("creat error");

        if (write(fd, buf1, 10) != 10)
                err_sys("buf1 write error");
        /* offset now = 10 */

        if (lseek(fd, 1024, SEEK_SET) == -1)
                err_sys("lseek error");
        /* offset now = 16384 */

        if (write(fd, buf2, 10) != 10)
                err_sys("buf2 write error");
        /* offset now = 16394 */

        exit(0);
}

argc[], arcv[]

argc is the abbreviation of argument count, which means the number of parameters passed into the main function;
argv is the abbreviation of argument vector, which means the parameter sequence or pointer passed into the main function, and the first parameter argv[0] must be the name of the program. And it contains the full path of the program, so the exact number of parameters of the main function that we need to input should be argc-1;

The following example shows that the filetype.c
function reads parameters from the shell command line. Each space is considered as a parameter, the number of parameters is assigned to argc, and the first address of each parameter is assigned to argv.

#include "apue.h"

int
main(int argc, char *argv[])
{
    
    
	int			i;
	struct stat	buf;
	char		*ptr;

	for (i = 1; i < argc; i++) {
    
    
		printf("%s: ", argv[i]);
		if (lstat(argv[i], &buf) < 0) {
    
    
			err_ret("lstat error");
			continue;
		}
		if (S_ISREG(buf.st_mode))
			ptr = "regular";
		else if (S_ISDIR(buf.st_mode))
			ptr = "directory";
		else if (S_ISCHR(buf.st_mode))
			ptr = "character special";
		else if (S_ISBLK(buf.st_mode))
			ptr = "block special";
		else if (S_ISFIFO(buf.st_mode))
			ptr = "fifo";
		else if (S_ISLNK(buf.st_mode))
			ptr = "symbolic link";
		else if (S_ISSOCK(buf.st_mode))
			ptr = "socket";
		else
			ptr = "** unknown mode **";
		printf("%s\n", ptr);
	}
	exit(0);
}

operation result
Insert picture description here

4. Chapter Four

umask.c

Path apue.3e/filedir

Set file permissions, a macro variable is defined in the function, and referencing the corresponding variable means turning off the function.
S_IRUSR: User read permission
S_IWUSR: User write permission
S_IRGRP: Group read
S_IWGRP: Group write
S_IROTH: Other users read
S_IWOTH: Other users write
Corresponding macros are defined in this statstructure, the st_mode variable corresponding to file permissions, this variable is also a structure Types of.
Insert picture description here
st_mode has the following values

Insert picture description here
We know that under the chmod command, the file permissions can be changed by entering a number. The corresponding number values ​​are as follows:

Insert picture description here

umask.c attached to ebook:

#include "apue.h"
#include <fcntl.h>

#define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)

int
main(void)
{
    
    
        umask(0);
        if (creat("foo", RWRWRW) < 0)
                err_sys("creat error for foo");
        umask(S_IRGRP|S_IWGRP | S_IROTH | S_IWOTH);
        if (creat("bar", RWRWRW) < 0)
                err_sys("creat error for bar");
        exit(0);
}

mgm@ubuntu:~/usr/apue.3e/filedir$ gcc umask.c -o umask
mgm@ubuntu:~/usr/apue.3e/filedir$ umask //这里的umask是系统函数
0002
mgm@ubuntu:~/usr/apue.3e/filedir$ ./umask
mgm@ubuntu:~/usr/apue.3e/filedir$ ls -l foo bar
-rw------- 1 mgm mgm 0 Sep 18 02:24 bar
-rw-rw-rw- 1 mgm mgm 0 Sep 18 02:24 foo

If we modify the umask value in the program,

#include "apue.h"
#include <fcntl.h>

#define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)

int
main(void)
{
    
    
        umask(0);
        if (creat("foo", RWRWRW) < 0)
                err_sys("creat error for foo");
        umask(S_IWGRP | S_IROTH | S_IWOTH);
        if (creat("bar", RWRWRW) < 0)
                err_sys("creat error for bar");
        exit(0);
}

Remember to delete the last compiled foo and bar functions before compiling.
Execute again to see the changes. One more group permission to read.
mgm@ubuntu:~/usr/apue.3e/filedir$ rm bar
mgm@ubuntu:~/usr/apue.3e/filedir$ rm foo
mgm@ubuntu:~/usr/apue.3e/filedir$ ./umask2
mgm @ubuntu:~/usr/apue.3e/filedir$ ls -l foo bar
-rw-r----- 1 mgm mgm 0 Sep 18 02:26 bar
-rw-rw-rw- 1 mgm mgm 0 Sep 18 02:26 foo

changemod.c

First call the stat function to get the value in the structure and save it in statbuf.
Then use the chmod function to modify the value of st_mode.

#include "apue.h"

int
main(void)
{
    
    
        struct stat             statbuf;

        /* turn on set-group-ID and turn off group-execute */

        if (stat("foo", &statbuf) < 0)
                err_sys("stat error for foo");
        if (chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0)
                err_sys("chmod error for foo");

        /* set absolute mode to "rw-r--r--" */

        if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0)
                err_sys("chmod error for bar");

        exit(0);
}

Guess you like

Origin blog.csdn.net/malcolm_110/article/details/108297338