01 Utility Routines:
1 Manipulating Character Strings:
int strcasecmp(const char *s1, const char *s2);
int strncasecmp(const char *s1, const char *s2, int n);
char *strcat(char *dst, const char *src);
char *strncat(char *dst, const char *src, size_t n);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
char *strcpy(char *dst, const char *src);
char *strncpy(char *dst, const char *src, size_t n);
size_t strcspn(const char *s1, const char *s2);
size_t strspn(const char *s1, const char *s2);
char *strdup(const char *s1);
size_t strlen(const char *s);
char *strpbrk(const char *s1, const char *s2);
char *strstr(const char *s1, const char *s2);
char *strtok(char *s1, const char *s2);
char *strtok_r(char *s1, const char *s2, char **lasts);
2 Manipulating Byte Strings:
void *memccpy(void *s1, const void *s2, int c, size_t n);
void *memchr(const void *s, int c, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
void *memcpy(void *s1, const void *s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
void *memset(void *s, int c, size_t n);
3 Manipulating Characters:
int isalpha(int c);
int isupper(int c);
int islower(int c);
int isdigit(int c);
int isxdigit(int c);
int isalnum(int c);
int isspace(int c);
int ispunct(int c);
int isprint(int c);
int isgraph(int c);
int iscntrl(int c);
int isascii(int c);
int toupper(int c);
int tolower(int c);
int toascii(int c);
4 Memory Allocation:
void *malloc(size_t size);
void *calloc(size_t nelem, size_t elsize);
void free(void *ptr);
void *memalign(size_t alignment, size_t size);
void *realloc(void *ptr, size_t size);
void *valloc(size_t size);
void *alloca(size_t size);
5 String to Number Conversion:
long strtol(const char *str, char **endptr, int base);
long long strtoll(const char *str, char **endptr, int base);
long atol(const char *str);
long long atoll(const char *str);
int atoi(const char *str);
char *lltostr(long long value, char *endptr);
char *ulltostr(unsigned long long value, char *endptr);
02 Low-Leve I/O Routines:
int open(const char *path, int oflag, /* mode_t mode */ ...);
int creat(const char *path, mode_t mode);
ssize_t read(int fd, void *buf, size_t nbytes);
ssize_t write(int fd, const void *buf, size_t nbytes);
int close(int fildes);
off_t lseek(int fildes, off_t offset, int whence);
int dup(int fildes);
int dup2(int fildes, int fildes2);
03 Standard I/O Library:
FILE *fdopen(int fildes, const char *mode);
int fclose(FILE *stream);
int getc(FILE *stream);
int getc_unlocked(FILE *stream);
int getchar(void);
int getchar_unlocked(void);
int fgetc(FILE *stream);
int getw(FILE *stream);
int putc(int c, FILE *stream);
int putc_unlocked(int c, FILE *stream);
int putchar(int c);
int putchar_unlocked(int c);
int fputc(int c, FILE *stream);
int putw(int w, FILE *stream);
char *gets(char *s);
char *fgets(char *s, int n, FILE *stream);
int puts(const char *s);
int fputs(const char *s, FILE *stream);
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
int printf(const char *format, /* args */ ... );
int fprintf(FILE *strm, const char *format, /* args */ ... );
int sprintf(char *s, const char *format, /* args */ ...);
int snprintf(char *s, size_t n, const char *format, /* args */ ...);
int scanf(const char *format, ...);
int fscanf(FILE *strm, const char *format, ...);
int sscanf(const char *s, const char *format, ...);
int fseek(FILE *stream, long offset, int whence);
int fseeko(FILE *stream, off_t offset, int whence);
int fflush(FILE *stream);
void setbuf(FILE *stream, char *buf);
int setvbuf(FILE *stream, char *buf, int type, size_t size);
04 Files and Directories:
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
int fstat(int fildes, struct stat *buf);
int readlink(const char *path, char *buf, size_t bufsiz);
int access(const char *path, int amode);
int chown(const char *path, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, gid_t group);
int fchown(int fildes, uid_t owner, gid_t group);
int truncate(const char *path, off_t length);
int ftruncate(int fildes, off_t length);
int utime(const char *path, const struct utimbuf *times);
int symlink(const char *name1, const char *name2);
int remove(const char *path);
int link(const char *existing, const char *new);
int unlink(const char *path);
int rmdir(const char *path);
int mkdir(const char *path, mode_t mode);
int rename(const char *old, const char *new);
char *getwd(char *path_name);
extern char *getcwd(char *buf, size_t size);
int chdir(const char *path);
int fchdir(int fildes);
DIR *opendir(const char *path);
int closedir(DIR *dirp);
struct dirent *readdir(DIR *dp);
long telldir(DIR *dp);
void seekdir(DIR *dp, long pos);
void rewinddir(DIR *dp);
05 Special-Purpose File Operations:
06 Time of Day Operations:
time_t time(time_t *tloc)
gettimeofday(struct timeval *tp, struct timezone *tz)
void tzset() --> timezone, altzone, tzname[2], and daylight available
int ftimestruct timeb *tp)
char *timezone(int zone, int dst)
struct tm *gmttime(const time_t clock)
struct tm *localtime(const time_t clock)
time_t mktime(struct tm *tp)
double difftime(time_t t1, time_t t2)
char *ctime(const time_t *clock)
char *asctime(const struct tm *tm)
size_t *strftime(const char *s,size_t maxsize,const char *format,
const struct tm *tm)
int cftime(char *s, const char *format, const time_t *clock)
int ascftime(char *s, const char *format, const struct tm *tm)
struct tm *getdate(const char *string)
07 Users and Groups
char *getlogin():
searches /var/adm/utmp, but user can run a prog without terminal, -->
char *cuserid()
uid_t getuid()
uid_t geteuid()
int setuid(uid_t uid)
int seeteuid(uid_t uid)
uid_t getgid()
uid_t getegid()
int setgid(uid_t uid)
int seetegid(uid_t uid)
int getgroups(int gidsetsize, gid_t *grouplist)
int setgroups(int ngroups, const gid_t *grouplist)
struct passwd *getpwname(const char *name)
struct passwd *getpwuid(uid_t uid)
struct passwd *getpwent()
void setpwent()
void endpwent()
struct passwd *fgetpwent(FILE *tp)
struct spwd *getspnam(const char *name)
struct spwd *getspent()
struct spwd *fgetspent(FILE *fp)
void setspent()
void endspent()
struct group *getgrname(const char *name)
struct group *getgrgid(gid_t gid)
struct group *fgetgrent(FILE *fp)
struct group *getgrent()
void setgrent()
void endgrent()
int initgroups(const char *name, gid_t basegid)
5) The utmp(x) and wtmp(x) files (under /var/adm)
struct utmp *getutent()
struct utmp *getutid(const struct utmp *id)
struct utmp *getutline(const struct utmp *line)
struct utmp *pututline(const struct utmp *utmp)
void setutent(void)
void endutent()
int utmpname(const char *filename)
struct utmpx *getutxent()
struct utmpx *getutxid(const struct utmp *id)
struct utmpx *getutxline(const struct utmp *line)
struct utmpx *pututxline(const struct utmp *utmp)
void setutxent()
void endutxent()
int utmpxname(const char *filename)
void getutmp(struct utmpx *utmpx, struct utmp *utmp)
void getutmpx(struct utmp *utmpx, struct utmpx *utmpx)
void updwtmp(char *wfile, struct utmpx *utmpx)
void updwtmpx(char *wfilex, struct utmpx *utmpx)
6) The lastlog file (under /var/adm)
7) The /etc/shells file
char *getusershell()
void setusershell()
void endusershell()
8) Set-user_id and set_group_id programs
main() {
...
euid = geteuid();
seteuid(getuid());
...non-privileged code...
seteuid(euid);
...privileged code...
seteuid(getuid());
...non-privileged code...
}
08 System configuration and resource limits
1) General system info
int uname(struct utsname *name) // 'name' contains the result.
long sysinfo(int command, char *buf, long counf)
2) System resource limit (see limits.h)
long sysconf(int name) // 'name' is one of:
long fpathconf(int fd, int name)
long pathconf(const char *path, int name)
3) Process resource limits
long ulimit(int cmd, long newlimit)
'cmd' is one of UL_GETFSIZE, UL_SETFSIZE, UL_GETMEMLIM,
UL_GETDESLIM and UL_GETMAXBRK
int getrlimit(int resource, struct rlimit *rlp)
int setrlimit(int resource, const struct rlimit *rlp)
'rlimit' is {rlim_t rlim_cur; rlim_t rlim_max;}
'resource' is one of RLIMIT_CORE, RLIMIT_CPU, RLIMIT_DATA,
RLIMIT_FSIZE, RLIMIT_NOFILE, RLIMIT_STACK,
RLIMIT_VMEM
4) Resource utilization info
clock_t times(struct tms *buf): current proc and its children have used
09 Signals
10
11
12 Interprocess Communication
1) Pipes
FILE *popen(const char *cmd, const char *type)
int pclose(FILE *stream)
int pipe(int fd[2])
2) Fifos
int mkfifo(const char *path, mode_t mode)
3) Unix-domain sockets
a) Socket-stream based
int socket(int domain, int type, int protocol)
int socketpair(int domain, int type, int protocol, int sv[2])
server:
int bind(int s, const struct sockaddr *name, int addrlen)
int listen(int s, int backlog)
int accept(int s, struct sockaddr *name, int *addrlen)
client:
int connect(int s, struct sockaddr *name, int addrlen)
int recv(int s, char *buf, int len, int flags)
int send(int s, const char *buf, int len, int flags)
int close(int s) or shutdown(int s, int how)
b) Socket-datagram based
int socket(int domain, int type, int protocol)
int socketpair(int domain, int type, int protocol, int sv[2])
server:
int bind(int s, const struct sockaddr *name, int addrlen)
int recvfrom(int s, char *buf, int len, int flags,
struct sockaddr *from, int *fromlen)
int sendto(int s, const char *buf, int len, int flags,
struct sockaddr *to, int tolen)
int close(int s) or shutdown(int s, int how)
4) Sys V IPC (message queues, shared memmory, and semaphores)
13 Networking with Sockets
1) Host names and addresses
int gethostname(char *name, int len)
struct hostent *gethostent()
struct hostent *gethostbyname(const char *name)
struct hostent *gethostbyaddr(const char *addr, int len, int type)
int sethostent(int stayopen)
int endhostent()
'hostent' is {char *h_name; char **h_aliases; int h_addrtype;
int h_length; char *h_addr_list;}
2) Services and port numbers (/etc/services)
struct servent *getservent();
struct servent *getservbyname(const char *name, char *proto);
struct servent *getservbyport(int port, char *proto);
int setservent(int stayopen)
int endservent()
'servent' is {char *s_name;char **s_aliases;int s_port; char *s_proto;}
'proto' is either "udp" or "tcp"
3) Network byte order: insures that all traffic arriving at a host from
the network will be in the same format
u_short htons(u_short hostshort)
u_long htonl(u_long hostlong)
u_short ntohs(u_short netshort)
u_long ntohl(u_long netlong)
4) Socket-stream based
int socket(int domain, int type, int protocol)
int socketpair(int domain, int type, int protocol, int sv[2])
server:
int bind(int s, const struct sockaddr *name, int addrlen)
int listen(int s, int backlog)
int accept(int s, struct sockaddr *name, int *addrlen)
client:
int connect(int s, struct sockaddr *name, int addrlen)
int recv(int s, char *buf, int len, int flags)
int send(int s, const char *buf, int len, int flags)
int close(int s) or shutdown(int s, int how)
5) Socket-datagram based
int socket(int domain, int type, int protocol)
int socketpair(int domain, int type, int protocol, int sv[2])
server:
int bind(int s, const struct sockaddr *name, int addrlen)
int recvfrom(int s, char *buf, int len, int flags,
struct sockaddr *from, int *fromlen)
int sendto(int s, const char *buf, int len, int flags,
struct sockaddr *to, int tolen)
int close(int s) or shutdown(int s, int how)
14
15 Miscellaneous Routines
1) Errors
void abort()
void assert(int expression), e.g. assert(total > 222)
2) Error logging (syslog daemon)
void openlog(char *ident, int logopts, int facility)
void syslog(int priority, char *mesg, /* args */ ...)
int vsyslog(int priority, char *mesg, valist ap)
void closelog()
int setlogmask(int maskpri)
3) Searching and sorting
a) linear
void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
int (*compar)(const void *, const void *));
void *lfind(const void *key, const void *base, size_t *nelp, size_t width,
int (*compar)(const void *, const void *));
b) binary
void *bsearch(const void *key, const void *base, size_t nel, size_t size,
int (*compar)(const void *, const void *));
c) hash tables
int hcreate(size_t nel)
void hdestroy()
typedef enum {FIND, ENTER} ACTION
ENTRY *hsearch(ENTRY item, ACTION action)
'ENTRY' is {char *key; char *data;}
d) binary tree
typedef enum {preorder, postorder, endorder, leaf} VISIT
void *tsearch(const void *key, void **rootp,
int (*compar)(const void *, const void *));
void *tfind(const void *key, const void **rootp,
int (*compar)(const void *, const void *));
void *tdelete(const void *key, void **rootp,
int (*compar)(const void *, const void *));
void twalk(void *rootp, void (*action)(void **, VISIT, int));
e) queues
void insque(struct qelm *elem, struct qelem *pred);
void remque(struct qelem *elem);
struct qelem { struct qelem *q_forw, *q_back; char *q_data;};
d) quick sort
void qsort(void *base, size_t nel, size_t width,
int (*compar)(const void *, const void *));
4) Environment variables
int main(int argc, char **argv, char **envp)
char *getenv(char *name)
int putenv(char *string)
5) Random numbers
void srand(int seed) // need to be called first
int rand() // returns random number [0, 2^15 - 1]
double srand48(long seed), ........
--
ShuangxiXu? - 29 Aug 2002