【代码1】myls.c
//directory operation!!
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#define DIR_NAME_SIZE 100
#define ABSOLUTE_PATHNAME_SIZE 100
int main (int argc, char *argv[])
{
DIR *dp;
struct dirent *ep;
struct stat st;
char dir_name_input[DIR_NAME_SIZE];
char absolute_pathname[ABSOLUTE_PATHNAME_SIZE];
char temp[DIR_NAME_SIZE];
char *time;
if (NULL == getcwd(absolute_pathname, ABSOLUTE_PATHNAME_SIZE)){
perror("getcwd");
}
printf("current work directory is %s, length of pathname is %d\n", absolute_pathname, strlen(absolute_pathname));
printf("Please input a dir name:\n");
scanf("%s", dir_name_input);
printf("%s, its length of pathname is %d\n", dir_name_input, strlen(dir_name_input));
if (0 > chdir(dir_name_input)){
perror("chdir");
exit(1);
}
memset(absolute_pathname, 0, ABSOLUTE_PATHNAME_SIZE);
if (NULL == getcwd(absolute_pathname, ABSOLUTE_PATHNAME_SIZE)){
perror("getcwd");
}
printf("current work directory is %s, length of pathname is %d\n", absolute_pathname, strlen(absolute_pathname));
if ( NULL == (dp = opendir(absolute_pathname))){
perror("opendir");
exit(1);
}
printf("filename:\ttype:\tPermission\taccesstime\tlastmodtime\tsize\n");
if (NULL != dp){
while (NULL != (ep = readdir(dp))){
if (ep->d_name[0] != '.'){
printf("%s\t", ep->d_name);
if(0 == stat(ep->d_name, &st)){
if (S_ISDIR(st.st_mode))
printf("Directory\t");
else if((st.st_mode&S_IFMT) == S_IFBLK)
printf("Block special file\t");
else if((st.st_mode&S_IFMT) == S_IFCHR)
printf("Character special file\t");
else if((st.st_mode&S_IFMT) == S_IFREG)
printf("Odinary file\t");
else if((st.st_mode&S_IFMT) == S_IFIFO)
printf("Pipefile file\t");
fflush(stdout);
printf("%o\t", st.st_mode&0x1ff);
memset(temp, 0, DIR_NAME_SIZE);
time = ctime(&(st.st_atime));
memcpy(temp, time, strlen(time)-1);
printf("%15s\t", temp);
//printf("%15s", ctime(&(st.st_atime)));
memset(temp, 0, DIR_NAME_SIZE);
time = ctime(&(st.st_mtime));
memcpy(temp, time, strlen(time)-1);
printf("%15s\t", temp);
//printf("%15s", ctime(&(st.st_mtime)));
printf("%ld\n", st.st_size);
//fflush(stdout);
}else
perror("stat");
}
}
closedir(dp);
}else
fputs("Couldn't open the diretory.\n", stdout);
return 0;
}
【代码2】test_access.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "Usage : %s + filename\n", argv[0]);
exit(1);
}
if (0 > access(argv[1], R_OK))
perror("access : read");
else
printf("access : read OK!\n");
if (0 > open(argv[1], O_RDONLY))
perror("open : read");
else
printf("open : read OK!\n");
exit(0);
}
【代码3】test_chmod.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if (2 > argc) {
fprintf(stderr, "Usage : %s + pathname\n", argv[0]);
exit(1);
}
if (0 > chmod(argv[1], S_ISUID | S_IXGRP| S_IXUSR | S_IXOTH)) {
fprintf(stderr, "chmod : %s\n", strerror(errno));
exit(1);
}
exit(0);
}
【代码4】test_fdopen.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
int fd = -1;
if (0 > (fd = open("1.txt", O_RDWR | O_CREAT | O_TRUNC, 0666))) {
fprintf(stderr, "open : %s\n", strerror(errno));
exit(1);
}
FILE *fp = NULL;
if (NULL == (fp = fdopen(fd, "w+"))) {
fprintf(stderr, "fdopen : %s\n", strerror(errno));
exit(1);
}
char buf[10] = "12345\n";
fwrite(buf, 1, strlen(buf), fp);
fflush(fp);
memcpy(buf, "abc\n", strlen("abc\n") + 1);
write(fileno(fp), buf, strlen(buf));
fclose(fp);
close(fd);
exit(0);
}
【代码5】test_lstat.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
int main(int argc, char *argv[])
{
if (2 > argc) {
fprintf(stderr, "Usage : %s + filename!\n", argv[0]);
exit(1);
}
struct stat statbuf;
if (0 > (lstat(argv[1], &statbuf))) {
fprintf(stderr, "lstat : %s\n", strerror(errno));
exit(1);
}
printf("size of %s is %ld\n", argv[1], (&statbuf)->st_size);
if (S_ISREG(statbuf.st_mode))
printf("file %s is regular file!\n", argv[1]);
if (S_ISDIR(statbuf.st_mode))
printf("file %s is directory!\n", argv[1]);
if (S_ISCHR(statbuf.st_mode))
printf("file %s is character device file!\n", argv[1]);
if (S_ISBLK(statbuf.st_mode))
printf("file %s is block device file!\n", argv[1]);
if ((S_IFMT&statbuf.st_mode) == S_IFREG)
printf("file %s is regular file!\n", argv[1]);
exit(0);
}
【代码6】test_maxfd.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
int fd = -1;
int i;
for (i = 3; i <= 1024; i++) {
if (0 > (fd = open("1.txt", O_RDWR))) {
fprintf(stderr, "open : %s\n", strerror(errno));
exit(1);
}
}
while(1);
exit(0);
}
【代码7】test_open.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd = -1;
close(1);
if (0 > (fd = open("1.txt", O_RDWR))) {
fprintf(stderr, "open : %s\n", strerror(errno));
exit(1);
}
fprintf(stderr, "%d\n", fd);
while(1);
close(fd);
exit(0);
}
【代码8】test_write_read.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd = -1;
if (0 > (fd = open("1.txt", O_RDWR | O_CREAT | O_TRUNC, 0666))) {
fprintf(stderr, "open : %s\n", strerror(errno));
exit(1);
}
char w_buf[BUFSIZ];
fgets(w_buf, sizeof(w_buf), stdin);
int ret = write(fd, w_buf, strlen(w_buf));
printf("write : %d\n", ret);
lseek(fd, 0, SEEK_SET);
char r_buf[BUFSIZ];
ret = read(fd,r_buf, sizeof(r_buf));
printf("read : %d\n", ret);
r_buf[ret] = '\0';
printf("%s", r_buf);
close(fd);
exit(0);
}