Linux命令的实现 -- ls pwd cd

时间:2021-10-18 09:17:43

      其实这些命令的实现都是 C 语言提供的一些库函数,我们只需要使用这些函数,并对得到的结果进行相应的格式化即可,好的,我们开始吧:

ls 用到的函数是 opendir ,readdir,这些都是读目录的函数,读取目录中的内容之后 ,我们可以对它进行解析,它是一条一条的读的,所以,我们可以得到全部的文件。

pwd 用到的函数是 getcwd ,这个函数的意思是 get current working directory ,很明显,它的意图就是得到当前的工作目录

cd 用到的函数是chdir ,这个函数的意思是 change directory,也是一样的明显,改变目录。

      好了,我们来看以个例子吧:

/*
 * linux_cmd.c
 *
 *  Created on: 2013年12月1日
 *      Author: DLUTBruceZhang
 */


#include <stdio.h>
#include <time.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

void my_ls(char *cur_dir);
void my_cd(const char *cd_to);
char * my_pwd();

char buffer[512];

int main(int argc, char **argv, char **environ)
{
	my_cd("/home/administrator/BruceZhang");
	my_ls(my_pwd());
	return 0;
}

void my_ls(char *cur_dir)
{
	DIR *dir;
	struct dirent *link;
	struct stat buf;
	struct tm *p;
	char file_type[11] = {0};
	char t_buffer[128] = {0};
	char sbuf[512] = {0};

	printf("%s\n", cur_dir);
	dir = opendir(cur_dir);
	while ((link = readdir(dir)) != NULL)
	{
		if (lstat(link -> d_name, &buf) == -1)
		{
			perror("lstat");
			continue;
		}
		if (strcmp(link -> d_name, ".") == 0 || strcmp(link -> d_name, "..") == 0)
		{
			continue;
		}
		strcpy(file_type, "----------");
		switch (buf.st_mode & S_IFMT)
		{
		case S_IFSOCK :
			file_type[0] = 's';
			break;
		case S_IFLNK :
			file_type[0] = 'l';
			break;
		case S_IFBLK :
			file_type[0] = 'b';
			break;
		case S_IFDIR :
			file_type[0] = 'd';
			break;
		case S_IFCHR :
			file_type[0] = 'c';
			break;
		case S_IFIFO :
			file_type[0] = 'f';
			break;
		default :
			break;
		}
		if (buf.st_mode & S_IRUSR)
		{
			file_type[1] = 'r';
		}
		if (buf.st_mode & S_IWUSR)
		{
			file_type[2] = 'w';
		}
		if (buf.st_mode & S_IXUSR)
		{
			file_type[3] = 'x';
		}
		if (buf.st_mode & S_IRGRP)
		{
			file_type[4] = 'r';
		}
		if (buf.st_mode & S_IWGRP)
		{
			file_type[5] = 'w';
		}
		if (buf.st_mode & S_IXGRP)
		{
			file_type[6] = 'x';
		}
		if (buf.st_mode & S_IROTH)
		{
			file_type[7] = 'r';
		}
		if (buf.st_mode & S_IWOTH)
		{
			file_type[8] = 'w';
		}
		if (buf.st_mode & S_IXOTH)
		{
			file_type[9] = 'x';
		}
		p = gmtime((time_t *)&(buf.st_mtim));
		strftime(t_buffer, 128, "%b %e %H:%M", p);
		sprintf(sbuf, "%s  %3d %-8d %-8d %8lu %s %s", file_type,
				(int)buf.st_nlink, buf.st_uid, buf.st_gid, (long)buf.st_size,
				t_buffer, link -> d_name);
		printf("%s\n", sbuf);
	}
}

void my_cd(const char *cd_to)
{
	chdir(cd_to);
}

char * my_pwd()
{
	memset(buffer, 0, sizeof(buffer));
	getcwd(buffer, sizeof(buffer));
	printf("%s\n", buffer);
	return buffer;
}
      输出的结果如下所示:
/home/administrator/BruceZhang
/home/administrator/BruceZhang
-rw-r--r--    1 1000     1000        32089 Oct  8 07:49 change_wlan.c~
-rw-r--r--    1 1000     1000        33257 Sep 23 06:35 手把手教你如何写Makefile.pdf
-rwxr-xr-x    1 1000     1000         7425 Aug 31 06:27 keygen_ue
drwxrwxr-x    2 1000     1000         4096 Mar 19 06:13 android
drwx------    3 1000     1000         4096 Jul 10 11:57 Shell To C
-rw-rw-r--    1 1000     1000     157949932 Nov 25 01:26 wps-office_8.1.0.3724~b1p2_i386.deb
drwxrwxr-x    3 1000     1000         4096 Sep  9 11:15 gedit-source-code-browser-master
-rw-rw-r--    1 1000     1000     26072675 Oct 28 08:50 fcitx-sougou.7z
drwxrwxr-x   15 1000     1000         4096 Nov 10 10:58 tests
drwxrwxr-x    2 1000     1000         4096 Nov  2 01:55 pthread_pool_test
drwxrwxr-x    2 1000     1000         4096 Nov  2 02:39 sort_list
drwxrwxr-x    2 1000     1000         4096 Nov  1 13:31 cpp_pthread_pool_test
drwx------    2 1000     1000         4096 Jul  9 10:40 a
-rw-rw-r--    1 1000     1000      1782257 Dec  1 02:16 cpp_primer4_cn.chm
drwxrwxr-x    2 1000     1000         4096 Nov  2 02:09 threadPool
-rw-rw-r--    1 1000     1000          888 Nov  1 10:25 CThread.h
drwxrwxr-x    4 1000     1000         4096 Nov 10 05:02 command_test
-rw-r--r--    1 1000     1000        16876 Oct 10 06:31 iw_change_change.sh
drwxrwxr-x    2 1000     1000         4096 May  3 15:03 coding
drwx------    2 1000     1000         4096 Oct  5 11:44 ultraedit3.3