-
getpwuid() 給出用戶的具體數(shù)據(jù)
函數(shù)原型:
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);
結(jié)構(gòu)體成員:
struct passwd
{
char *pw_name; /* username */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* user information */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
-
memset();清零
memset:作用是在一段內(nèi)存塊中填充某個(gè)給定的值,它是對(duì)較大的結(jié)構(gòu)體或數(shù)組進(jìn)行清零操作的一種最快方法。
常見錯(cuò)誤
第一:搞反了 ch 和 n 的位置. 一定要記住如果要把一個(gè)char a[20]清零,一定是 memset(a,0,20); 而不是 memset(a,20,0);
ctime();
getgrgid();獲得給出組的信息
getpid();獲得當(dāng)前進(jìn)程的pid號(hào)
getppid();獲得當(dāng)前進(jìn)程的父進(jìn)程pid號(hào)
fork(); 復(fù)制生成子進(jìn)程
exec();替換子進(jìn)程
getcwd();查看當(dāng)前的工作目錄
-
wait();
會(huì)暫時(shí)停止目前進(jìn)程的執(zhí)行, 直到有信號(hào)來到或子進(jìn)程結(jié)束. 如果在調(diào)用wait()時(shí)子進(jìn)程已經(jīng)結(jié)束, 則wait()會(huì)立即返回子進(jìn)程結(jié)束狀態(tài)值. 子進(jìn)程的結(jié)束狀態(tài)值會(huì)由參數(shù)status 返回, 而子進(jìn)程的進(jìn)程識(shí)別碼也會(huì)一快返回. 如果不在意結(jié)束狀態(tài)值, 則參數(shù) status 可以設(shè)成NULL. 子進(jìn)程的結(jié)束狀態(tài)值請(qǐng)參考waitpid().
strtok();字符串按格式分割
open(); 打開一個(gè)文件
read(); 讀取打開的文件
write(); 寫入讀取到的緩存字符
access();判斷文件的權(quán)限 R_OK W_OK X_OK F_OK
opendir();打開一個(gè)目錄并返回一個(gè)DIR *類型的指針指向目錄項(xiàng)
readdir();讀取一個(gè)目錄流并將信息保存在結(jié)構(gòu)體中struct dirent 中
closedir();與opendir配對(duì),用來關(guān)閉打開的目錄
atexit() 設(shè)置程序正常結(jié)束前調(diào)用的函數(shù)
execl() 執(zhí)行文件函數(shù)
execlp() 從PATH 環(huán)境變量中查找文件并執(zhí)行
execv() 執(zhí)行文件函數(shù)
execve() 執(zhí)行文件函數(shù)
execvp() 執(zhí)行文件函數(shù)
-
幾個(gè)用的命令
ps aux 查看所有進(jìn)程
top 打開一個(gè)類似于任務(wù)管理器的界面,可看到zombie進(jìn)程
kill 傳入信號(hào),-l是查看所有的參數(shù)
練習(xí)
1.實(shí)現(xiàn)ls
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
int main(int argc,char *argv[])
{
DIR *dirp = NULL;
struct dirent *dp = NULL;
struct stat sp;
char str[11] = {"\0"};
if((dirp = opendir("/home/w/桌面/f")) == NULL)d
{
perror("opendir preeor");
return 1;
}
while((dp = readdir(dirp)) != NULL)
{
if(dp->d_name[0] != '.')
{
stat(dp->d_name , &sp);
str[0] = '-';
if(sp.st_mode & S_IFBLK) str[0] = 'b';
if(sp.st_mode & S_IFDIR) str[0] = 'd';
if(sp.st_mode & S_IFCHR) str[0] = 'c';
if(sp.st_mode & S_IFIFO) str[0] = 'p';
if(sp.st_mode & S_IRUSR) str[1] = 'r';
else str[1] = '-';
if(sp.st_mode & S_IWUSR) str[2] = 'w';
else str[2] = '-';
if(sp.st_mode & S_IXUSR) str[3] = 'x';
else str[3] = '-';
if(sp.st_mode & S_IRGRP) str[4] = 'r';
else str[4] = '-';
if(sp.st_mode & S_IWGRP) str[5] = 'w';
else str[5] = '-';
if(sp.st_mode & S_IXGRP) str[6] = 'x';
else str[6] = '-';
if(sp.st_mode & S_IROTH) str[7] = 'r';
else str[7] = '-';
if(sp.st_mode & S_IWOTH) str[8] = 'w';
else str[8] = '-';
if(sp.st_mode & S_IXOTH) str[9] = 'x';
else str[9] = '-';
printf("%s %ld %ld \t%s \n",str,sp.st_nlink,sp.st_size,dp->d_name);
}
}
closedir(dirp);
return 0;
}
2.實(shí)現(xiàn)cp命令并改進(jìn)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFER_S 100
int main(int argc,char *argv[])
{
int src_fd = 0;
int dst_fd = 0;
int n = 0;
char ch = '0';
char buf[BUFFER_S] = {'\0'};
if(argc != 3)
{
printf("usage : %s <src_file><dst_file>\n",argv[0]);
return 1;
}
if((src_fd = open(argv[1],O_RDONLY)) == -1)
{
perror("open dst_fd perror");
return 1;
}
while(1)
{
if(access(argv[2],F_OK) == 0)
{
printf("目的文件名已經(jīng)存在!是否覆蓋掉?(y ro n)");
scanf("%c",&ch);
getchar();
if(ch == 'y')
{
if((dst_fd = open(argv[2],O_WRONLY | O_TRUNC,S_IRWXU)) == -1)
{
perror("open dst_fd perror");
return 1;
}
break;
}
else if(ch == 'n')
{
printf("是否重新輸入目的路徑?(y ro n)");
scanf("%c",&ch);
getchar();
if(ch == 'y')
scanf("%s",argv[2]);//最好不占用不改變argv的值
else
return 1;
}
else
break;
}
else
{
if((dst_fd = open(argv[2],O_WRONLY | O_CREAT,S_IRWXU)) == -1)
{
perror("open dst_fd perror");
return 1;
}
break;
}
}
while((n = read(src_fd,buf,BUFFER_S)) > 0)
{
write(dst_fd,buf,n);
}
close(src_fd);
close(dst_fd);
return 0;
}
3.實(shí)現(xiàn)ls命令
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include <string.h>
#define MAX_CMD_LEN 128
void main(int argc,char *argv[])
{
pid_t pid;
char in[MAX_CMD_LEN] = {'\0'};
char cmd[MAX_CMD_LEN] = {'\0'};
char cmdl[MAX_CMD_LEN] = {'\0'};
char pwd[MAX_CMD_LEN] = {'\0'};
char* delim = " ";
char* p = NULL;
int status = 0;
struct passwd *pw;
while(1)
{
pw = getpwuid(getuid());
getcwd(pwd,MAX_CMD_LEN);
printf(" %s@|%s>",pw->pw_name,pwd);
fgets(in,MAX_CMD_LEN,stdin);
in[strlen(in) - 1] = '\0';
p = strtok(in,delim);
if(p == NULL)
continue;
strcpy(cmd,p);
if(strcmp(cmd,".exit") == 0)
return ;
p = strtok(NULL,delim);
if(p != NULL)
{
strcpy(cmdl,p);
if(fork() == 0)
{
execlp(cmd,cmd,cmdl,NULL);
return ;
}
}
else
{
strcpy(cmdl,"NULL");
if(fork() == 0)
{
execlp(cmd,cmd,NULL);
return ;
}
}
wait(&status);
}
}