1-1.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc,char *argv[])
{
int temp=1;
DIR *dp;
struct dirent *dirp;
if(argc <2)
{
printf("a argumen(the directory name) is required");
exit(1);
}
while(temp<argc)
{
if((dp= opendir(argv[temp]))==NULL) // DIR *opendir(const char* name);
{
printf("can't open %s/n",argv[temp]);
exit(1);
}
while((dirp=readdir(dp))!=NULL) // struct dirent *readdir(DIR *dirp);
printf("%s/t",dirp->d_name);
printf("/n");
closedir(dp); // int closedir(DIR *dir);
temp++;
}
}
-------------------------------------------------------------------------------
1-2.c
#include <unistd.h> // STDIN_FILENO STDOUT_FILENO 定义在 unistd.h头文件中
#include <stdio.h> // 它们典型值为0和1
#include <stdlib.h>
#define BUFFSIZE 8192
int main(void)
{
int n;
char buf[BUFFSIZE];
while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0) // ssize_t read(int fildes, void *buf, size_t nbyte);
if(write(STDOUT_FILENO,buf,n)!=n) // The read() function shall attempt to read nbyte bytes from the file associated with the open file
{ printf("write error/n"); // descriptor, fildes, into the buffer pointed to by buf.
exit(1); // ssize_t write(int fildes, const void *buf, size_t nbyte);
} //The write() function shall attempt to write nbyte bytes from the buffer pointed to by buf to the
// file associated with the open file descriptor, fildes.
if(n<0) // f write() is interrupted by a signal after it successfully writes some data,
// it shall return the number of bytes written.
{
printf("read error");
exit(1);
}
exit(0);
}
--------------------------------------------------------------------------------
1-3.c
# include <stdio.h>
# include <stdlib.h>
int main()
{
int c;
while((c=getc(stdin))!=EOF)
if(putc(c,stdout)==EOF)
{
printf("output error");
exit(1);
}
if(ferror(stdin))
{
printf("input error");
exit(1);
}
exit(0);
}
-----------------------------------------------------------------------------------
1-4.c
# include <sys/types.h> // pid_t getpid(void); getpid() returns the process ID of the current process.
# include <unistd.h> // pid_t getppid(void); getppid() returns the process ID of the parent of the current process.
# include <stdio.h>
int main(void)
{
printf("The pid of currenct process is %d,ppid is %d/n",getpid(),getppid());
}
---------------------------------------------------------------------------------
1-5.c
#define MAXLINE 100
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(void)
{
char buf[MAXLINE]; // define a buffer
pid_t pid;
int status; //
printf("%% "); // print prompt (printf requires %% to print %)
while(fgets(buf,MAXLINE,stdin)!=NULL) // read bytes from stream into the array pointed by buf
{
buf[strlen(buf)-1]=0; // replace newline with null
}
if((pid=fork())<0) // pid_t fork(void); fork creats a new process by duplicating the calling process.
{ // The new process referred to as the child.
printf("fork error/n");
exit(1);
}
else if(pid == 0) // child
{
execlp(buf,buf,(char*) 0); // int execlp(const char *file, const char *arg0, ... /*, (char *)0 */);
if(errno) // errno included by errno.h, errno shall changed if execlp failed
{
printf("couldn't execute %s/n",buf);
exit(127);
}
} // pid_t waitpid(pid_t pid, int *stat_loc, int options);
if(pid=waitpid(pid,&status,0)<0) // parent
{
printf("waitpid error/n");
printf("%% ");
}
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc,char *argv[])
{
int temp=1;
DIR *dp;
struct dirent *dirp;
if(argc <2)
{
printf("a argumen(the directory name) is required");
exit(1);
}
while(temp<argc)
{
if((dp= opendir(argv[temp]))==NULL) // DIR *opendir(const char* name);
{
printf("can't open %s/n",argv[temp]);
exit(1);
}
while((dirp=readdir(dp))!=NULL) // struct dirent *readdir(DIR *dirp);
printf("%s/t",dirp->d_name);
printf("/n");
closedir(dp); // int closedir(DIR *dir);
temp++;
}
}
-------------------------------------------------------------------------------
1-2.c
#include <unistd.h> // STDIN_FILENO STDOUT_FILENO 定义在 unistd.h头文件中
#include <stdio.h> // 它们典型值为0和1
#include <stdlib.h>
#define BUFFSIZE 8192
int main(void)
{
int n;
char buf[BUFFSIZE];
while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0) // ssize_t read(int fildes, void *buf, size_t nbyte);
if(write(STDOUT_FILENO,buf,n)!=n) // The read() function shall attempt to read nbyte bytes from the file associated with the open file
{ printf("write error/n"); // descriptor, fildes, into the buffer pointed to by buf.
exit(1); // ssize_t write(int fildes, const void *buf, size_t nbyte);
} //The write() function shall attempt to write nbyte bytes from the buffer pointed to by buf to the
// file associated with the open file descriptor, fildes.
if(n<0) // f write() is interrupted by a signal after it successfully writes some data,
// it shall return the number of bytes written.
{
printf("read error");
exit(1);
}
exit(0);
}
--------------------------------------------------------------------------------
1-3.c
# include <stdio.h>
# include <stdlib.h>
int main()
{
int c;
while((c=getc(stdin))!=EOF)
if(putc(c,stdout)==EOF)
{
printf("output error");
exit(1);
}
if(ferror(stdin))
{
printf("input error");
exit(1);
}
exit(0);
}
-----------------------------------------------------------------------------------
1-4.c
# include <sys/types.h> // pid_t getpid(void); getpid() returns the process ID of the current process.
# include <unistd.h> // pid_t getppid(void); getppid() returns the process ID of the parent of the current process.
# include <stdio.h>
int main(void)
{
printf("The pid of currenct process is %d,ppid is %d/n",getpid(),getppid());
}
---------------------------------------------------------------------------------
1-5.c
#define MAXLINE 100
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(void)
{
char buf[MAXLINE]; // define a buffer
pid_t pid;
int status; //
printf("%% "); // print prompt (printf requires %% to print %)
while(fgets(buf,MAXLINE,stdin)!=NULL) // read bytes from stream into the array pointed by buf
{
buf[strlen(buf)-1]=0; // replace newline with null
}
if((pid=fork())<0) // pid_t fork(void); fork creats a new process by duplicating the calling process.
{ // The new process referred to as the child.
printf("fork error/n");
exit(1);
}
else if(pid == 0) // child
{
execlp(buf,buf,(char*) 0); // int execlp(const char *file, const char *arg0, ... /*, (char *)0 */);
if(errno) // errno included by errno.h, errno shall changed if execlp failed
{
printf("couldn't execute %s/n",buf);
exit(127);
}
} // pid_t waitpid(pid_t pid, int *stat_loc, int options);
if(pid=waitpid(pid,&status,0)<0) // parent
{
printf("waitpid error/n");
printf("%% ");
}
exit(0);
}