常用函数

directory

opendir

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
/*
 * On success, a pointer to the directory stream is returned. 
 * On error, NULL is returned, and errno is set.
 */

readdir

#include <dirent.h>
struct dirent *readdir(IDR *dirp);
/*
 * On success, a pointer to the directory stream is returned. 
 * On error, NULL is returned, and errno is set.
 */

closedir

#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
/*
 * return 0 on success. 
 * On error, -1 is returned, and errno is set.
 */

file

read

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
// On error, -1 is returned and errno is set.

write

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
// On error, -1 is returned and errno is set.

close

#include <unistd.h>
int close(int fd);
// On error, -1 is returned and errno is set.

input characters and strings

getc

fgetc

getchar

gets

fgets

ungetc

#include <stdio.h>
int fgetc(FILE *stream);
int getc(FILE *stream);    // may be a macro
int getchar(void);
char *gets(char *s);       // never use
/*
 * reads most one less than size characters. Newline 
 * can be read and null byte is stored after the last 
 * character in the buffer.
 */
char *fgets(char *s, int size, FILE *stream);
int ungetc(int c, FILE *stream);

Output of characters and strings

putc

fputc

putchar

puts

fputs

#include <stdio.h>
int putc(int c, FILE *stream);   // may be a macro
int fputc(int c, FILE *stream);
int putchar(int c);
int puts(const char *s);
int fputs(const char *s, FILE *stream);

process

getpid

getppid

#include <sys/types.h>
#include <unistd.h>
// get process identification
pid_t getpid(void);
pid_t getppid(void);

fork

#include <unistd.h>
// create a child process
pid_t fork(void);
/*
 * On Success, the PID of the child process is returned 
 * in the parent, 0 is returned in the child. On failure, 
 * -1 is returned in the parent, no child process is created, 
 * and errno is set.
 */

exec

#include <unistd.h>
extern char **environ;
// execute a file
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ..., char *const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[], char *const envp[]);

/*
 * They replace the current process image with a new 
 * process image. They return only if an error has 
 * occurred. The returned value is -1, and errno is set 
 * to indicate the error.
 */

wait

waitpid

#include <sys/types.h>
#include <sys/wait.h>
// wait for process to change state
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
/* 
 * wait(&status) is equivalent to waitpid(-1, &status, 0)
 * A state change is considered to be:
 *   the child terminated;
 *   the child was stopped by a signal;
 *   the child was resumed by s signal;
*/

error

strerror

perror

#include <string.h>
char *strerror(int errnum);

#include <stdio.h>
void perror(const char *s);

user identity

getuid

geteuid

#include <unistd.h>
#include <sys/types.h>
// real user ID of the calling process
uid_t getuid(void);
// effective user ID of the calling process
uid_t geteuid(void);

getgid

getegid

#include <unistd.h>
#include <sys/types.h>
// real group ID of the calling process
gid_t getgid(void);
// effective group ID of the calling process
git_t getegit(void);

Data structure

struct dirent

struct dirent {
    ino_t          d_ino;       // inode number
    off_t          d_off;       // not an offset
    unsigned short d_reclen;    // length of this record
    unsigned char  d_type;      // type of file
    char           d_name[256]; // file name
};

/* d_type
   DT_BLK      block device
   DT_CHR      character device
   DT_DIR      directory
   DT_FIFO     named pipe(FIFO)
   DT_LNK      symbolic link
   DT_REG      regular file
   DT_SOCK     UNIX domain socket
   DT_UNKNOWN  unknown
*/

Predefined file descriptor

#define STDIN_FILENO  0  // standard input 
#define STDOUT_FILENO 1  // standard output
#define STDERR_FILENO 2  // standard error output

Others

Signal

/*
 * SIGINT    Ctrl + C    interrupt key
 * SIGQUIT   Ctrl + /    quit key 
 */
posted @ 2017-10-22 15:50  枫竹梦  阅读(165)  评论(0编辑  收藏  举报