linux系统编程源代码(linux编程开发)
admin 发布:2022-12-19 06:23 109
本篇文章给大家谈谈linux系统编程源代码,以及linux编程开发对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
如何查看linux命令的源代码
1. 以搜索ls命令源码为例,先搜索命令所在包,命令如下:
lpj@lpj-linux:~$ which ls
/bin/ls
2. 用命令搜索该软件所在包,代码如下:
lpj@lpj-linux:~$ dpkg -S /bin/ls
coreutils: /bin/ls
3. 从上一步中可以知道ls命令的实现在包coreutils中,用apt安装(说安装有些歧义,主要是区分apt-get -d)该包的源代码然后解压,代码如下:
sudo apt-get source coreutils
cd /usr/src/coreutils-XXX #XXX表示版本号
sudo tar zxvf coreutils-XXX.tar.gz
或者只下载源码,然后手动打补丁再解压,代码如下:
sudo apt-get -d source coreutils
cd /usr/src
tar zxvf coreutils-XXX.tar.gz
gzip -d coreutils-XXX.diff.gz #这一步会生成coreutils-XXX.diff文件
patch -p0 coreutils-XXX.diff
cd coreutils-XXX
tar zxvf coreutils-XXX.tar.gz
OK,这几步执行完后,就可以进入/usr/src/coreutils-XXX/coreutils-XXX/src中查看各命令对应的源代码了
求两个源代码(操作系统,linux)
/*****************************************
ls1.c
This is my own ls
By this program you can do :
ls
ls -a
ls -l
******************************************/
/********** Header Files ***************/
#include stdio.h
#include stdlib.h
#include sys/types.h
#include dirent.h
#include sys/stat.h
#include string.h
/*** constant Define****/
#define true 1
#define false 0
/*static flag varible*/
int flag_l = 0,
flag_a = 0;
/******* Function Decleartion *********/
void do_ls ( char [] );
void print_name (char [], int);
void dostat (char *, char *);
void show_file_info( char *, char *, struct stat *);
void mode_to_letters( int , char [] );
char *uid_to_name( uid_t );
char *gid_to_name( gid_t );
void do_ls( char dirname[])
/*
List Files' Dirname;
*/
{
DIR *dir_ptr;
struct dirent *direntp;
/*This Can Explan a fullpath*/
char full_path[1024];
int total;
if ( ( dir_ptr = opendir (dirname) ) == NULL )
printf ( "myls : Cannot open \033[0;34m%s\033[0m\n", dirname );
else
{
printf ( "\033[0;34m%s \033[0m :\n", dirname );
total = 0;
while ((direntp = readdir( dir_ptr ) ) != NULL )/*get filename of dir*/
{
if ( !flag_a direntp - d_name[0] == '.') continue;
sprintf(full_path, "%s/%s", dirname, direntp - d_name);/*full path=dirname + '/'+filename*/
dostat( full_path , direntp - d_name);
++total;
}
printf("TOTAL : %d\n", total);
closedir ( dir_ptr );
}
}
void print_name (char filename[], int mode)
/* DIR can be printed in blue and others will be printed in green
*/
{
if ( S_ISDIR(mode) ) printf("\033[0;34m%s", filename); /* directory? */
else
printf("\033[0;32m%s", filename);
}
void dostat( char *full_path, char *filename )
{
struct stat info;
if ( stat(full_path, info) == -1 ) /* cannot stat */
perror( full_path ); /* say why */
else /* else show info */
{
if(flag_l)
show_file_info( full_path, filename, info );
else
{
print_name(filename, info.st_mode);
printf("\033[0m\n");
}
}
}
void show_file_info( char *full_path, char *filename, struct stat *info_p )
/*
* display the info about 'filename'. The info is stored in struct at *info_p
*/
{
char *uid_to_name(), *ctime(), *gid_to_name(), *filemode();
void mode_to_letters();
char modestr[11];
void mode_to_letters( info_p-st_mode, modestr );
printf( "%s" , modestr );
printf( "%4d " , (int) info_p-st_nlink);
printf( "%-8s " , uid_to_name(info_p-st_uid) );
printf( "%-8s " , gid_to_name(info_p-st_gid) );
printf( "%8ld " , (long)info_p-st_size);
printf( "%.12s ", 4+ctime(info_p-st_mtime));
print_name(filename, info_p-st_mode );
printf("\033[0m\n");
}
/*
* utility functions
*/
void mode_to_letters( int mode, char str[] )
{
strcpy( str, "----------" ); /* default=no perms */
if ( S_ISDIR(mode) ) str[0] = 'd'; /* directory? */
if ( S_ISCHR(mode) ) str[0] = 'c'; /* char devices */
if ( S_ISBLK(mode) ) str[0] = 'b'; /* block device */
if ( mode S_IRUSR ) str[1] = 'r'; /* 3 bits for user */
if ( mode S_IWUSR ) str[2] = 'w';
if ( mode S_IXUSR ) str[3] = 'x';
if ( mode S_IRGRP ) str[4] = 'r'; /* 3 bits for group */
if ( mode S_IWGRP ) str[5] = 'w';
if ( mode S_IXGRP ) str[6] = 'x';
if ( mode S_IROTH ) str[7] = 'r'; /* 3 bits for other */
if ( mode S_IWOTH ) str[8] = 'w';
if ( mode S_IXOTH ) str[9] = 'x';
}
#include pwd.h
char *uid_to_name( uid_t uid )
/*
* returns pointer to username associated with uid, uses getpw()
*/
{
struct passwd *getpwuid(), *pw_ptr;
static char numstr[10];
if ( ( pw_ptr = getpwuid( uid ) ) == NULL ){
sprintf(numstr,"%d", uid);
return numstr;
}
else
return pw_ptr-pw_name ;
}
#include grp.h
char *gid_to_name( gid_t gid )
/*
* returns pointer to group number gid. used getgrgid(3)
*/
{
struct group *getgrgid(), *grp_ptr;
static char numstr[10];
if ( ( grp_ptr = getgrgid(gid) ) == NULL ){
sprintf(numstr,"%d", gid);
return numstr;
}
else
return grp_ptr-gr_name;
}
/* main ---*/
main ( int ac, char *av[] )
{
int accnt;
if (ac==1) do_ls (".");
else
{
accnt = 1;
/*Get Param -l -a*/
while ( accnt ac (*(av+1) )[0] == '-')
{
++av;
++accnt;
switch ( (*av)[1] )
{
case 'l' : flag_l = true; break;
case 'a': flag_a = true; break;
default : break;
}
}
if (accnt == ac)
do_ls ( "." );
else
while (accnt ac)
{
++accnt;
++av;
if ( strlen (*av) = MAXPATHLEN )
{
printf("myls : %s is a so long name!\n", *av);
}
else
{
do_ls ( *av );
}
}
}
}
如何查看Linux命令源代码
以find命令为例:
第一步,确定命令属于哪个rpm包:
#rpm -qf /usr/bin/find
findutils-4.1-34
第二步,查找findutils的源码包findutils-4.1-34.src.rpm,这个一般在发行版的网站上可以找到(比如FC6的源码在这里可以找到:)。
也可以到rpmfind.net上查找,输入rpm包的名字就可以找到相关的源码包,有的也可以得到软件对应的网站。
第三步,安装源码包:
rpm -ivh findutils-4.1-34.src.rpm
安装好之后可以到/usr/src/RedHat/SOURCE下找到相关的源码包
或者直接编译源码包:
rpmbuild -bb findutils-4.1-34.src.rpm
如何编译Linux操作系统的源代码?
1. 检测/usr/src/linux/include/linux/version.h是否存在,以判断是否以安装内核源码,而如果只将内核源码包解压,并不存在version.h这个文件,所以需要做如下步骤:
在内核源码目录下(/usr/src/linux)
1)make include/linux/version.h 生成version.h文件
2)make include/asm 创建
如果从下载的源码包,使用下列命令安装:
cd /usr/src 进入/usr/src目录中,将源码包放置在这里。
tar jxvf linux-2.6.18.tar.bz2 解压源码包,会生成linux-2.6.9目录
ln -s /usr/src/linux-2.6.18 /usr/src/linux 编译内核建议做个软链接。
并且要确保/lib/modules/2.6.18/下的build,source目录
正确指向/usr/src/linux-2.6.18的:
[root@localhost modules]#ls -l /lib/modules/2.6.18
lrwxrwxrwx 1 root root 21 2008-07-03 14:28 build - /usr/src/linux-2.6.18/
lrwxrwxrwx 1 root root 21 2008-07-03 14:28 source - /usr/src/linux-2.6.18/
做完这些,我去安装NVIDIA驱动,结果编译时出错。
将内核源码编译一次,不安装,就可以正常编译了
2.4.x内核不知是否如此,如果编译NVIDIA过程中出现错误,就先编译一下内核
方法很简单:
从/boot目录下复制config-2.6.10到/usr/src/linux下
命名为.config
对于2.6.x内核使用下列命令编译:
cd /usr/src/linux 进入源码目录
make 编译内核及模块
注意:不要去make install 只要编译就可以了。
2.4.x使用下列命令编译内核,同样,只编译不安装。
cd /usr/src/linux
make dep 建立依赖关系(不知道是否有必要,做下没坏处)
make bzImage 编译内核
make modules 编译模块
通常发行版中/boot中应该有当前内核的配置文件,如上面的config-2.6.9
如果没有的话,可以通过下列命令创建出.config
[root@localhost linux]# cd /usr/src/linux
[root@localhost linux]#make menuconfig 字符界面配置内核
[root@NEWLFS linux]#make xconfig 图形界面配置内核
这两种方法任选其一,喜欢的话,可以自己配置一下。
或者直接使用默认值,保存退出即可。这样编译时间会长一些。
make menuconfig: 假如是在图形界面中的终端运行,要保证
该终端窗口为全屏,才不会出错。
如何查看Linux源代码
一般在Linux系统中的/usr/src/linux*.*.*(*.*.*代表的是内核版本,如2.4.23)目录下就是内核源代码(如果没有类似目录,是因为还没安装内核代码)。另外还可从互连网上免费下载。注意,不要总到去下载,最好使用它的镜像站点下载。请在里找一个合适的下载点,再到pub/linux/kernel/v2.6/目录下去下载2.4.23内核。
关于linux系统编程源代码和linux编程开发的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
- 上一篇:键盘记录代码(记录键盘输入)
- 下一篇:占卜程序源代码(占卜小程序源码)
相关推荐
- 05-18网站建设开发,网站建设开发流程步骤
- 05-18seo积分系统,seo评分
- 05-16如何做一个网站,如何做一个网站系统
- 05-15免费开发软件制作平台,软件免费开发网站
- 05-14上海网站推广系统,上海网站推广服务公司
- 05-13电脑系统优化软件哪个好用,电脑系统优化在哪里
- 05-13web前端开发工程师,Web前端开发工程师是做什么的
- 05-12网站自然优化,网站自动优化系统
- 05-12小程序定制开发,小程序定制开发需要多少钱
- 05-12cms系统,喜来健CMS系统
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接