现在的位置: 首页 > 综合 > 正文

linux相关代码程序,小而精悍

2013年08月13日 ⁄ 综合 ⁄ 共 3318字 ⁄ 字号 评论关闭

 

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <string.h> //strerror()

#include <errno.h> //errno

 

 

int main(void){

 

int fd ;

static const char* name="tmpfile.txt";

// if( mknod(name,S_IFCHR | 0600 , (1<<8)| 3) == 0){

 

fd = open(name , O_RDWR);

unlink(name);

printf("unlink/n");

if( fd >= 0){

 

dup2(fd , 0);

dup2(fd , 1);

dup2(fd , 2);

if( fd >2 )

close(fd);

}

 

// }

/* 打印错误信息 */

// printf("error: %s/n",strerror(errno));

sleep(10);

printf("done/n");

exit(0);

}

说明:1、这里unlink用于删除一个现有的目录项,上面程序中调用unlink并不是立即删除文件(虽然在执行unlink函数后,在当前目录中已经看不到tmpfile.txt了),而是等待进程结束后才删除文件,释放文件占用的空间。可以通过df命令查看文件系统大小资源来分析unlink前后的变化。只有当文件的链接数为0时,才能够被删除,另一个条件就是当该文件被进程打开时,也不会被删除,而是要等到进程结束后,才删除。2、这里使用了dup2,它用于复制一个现有的文件描述符,类似的一个函数为dup,返回的新的文件描述符和原来的文件描述符指向同一个文件表项,所以就共享双方的文件标志(读、写、添写)和当前文件偏移量。上面的程序中dup2函数执行后,标准输出、标准输入、标准错误都会只和文件tmpfile.txt打交道,而和终端就没有交互了。

 

 

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h> //strchr()

 

int main(void){

 

char cmdline[] = "hello world , baby!";

char* ptr = cmdline ;

char *pos = strchr(ptr,'a');

 

printf("*pos = %c /n" , *pos); //'a'

// printf("*pos++ = %c /n ",*pos++); //'a'

*pos++ =0;

 

printf("cmdline = %s /n", cmdline); //hello world , b

 

exit(0);

}

说明:1strchr用于在指定的字符串中查找第一个指定的字符的位置,返回的是对应的字符。

1、第二个要注意的情况是:*pos++*++pos,的区别在于前者先执行*pos,再执行pos++,而后者就不一样,先执行++pos,再执行*pos,这里能够通过*pos++ =0;修改原先字符串中的字符,如果原先cmdlinechar*类型,就不能直接修改,会出现段错误。

 

linux中可以通过/proc/cmdline获取内核命令行参数。

 

 

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h> //strcpy strncpy

 

#define MAXLINE 100

 

int main(void){

 

//char str[] ="Android , a mazing platform!";

char* str ="Android , a mazing platform!";

 

char dest[MAXLINE];

strcpy(dest , str ); //strcpy(char* to , char* from);

puts(dest);

 

/* the standard use of "strncpy" */

char buf[10];

strncpy( buf , "abcdefghefg" , sizeof(buf)-1 ); //strncpy(char* to ,const char* from, size_t n);

buf[sizeof(buf)]=0;

puts(buf);

 

/* the use of strlcpy */

char buffer[20];

int src_len = strlcpy(buffer , str , sizeof(buffer)); //size_t strlcpy(char *dst, const char *src, size_t siz);

if( src_len > strlen(buffer) )

printf("truncated happened!/n");

puts(buffer);

 

char buff[20];

//int snprintf(char *str, size_t size, const char *format, ...); 

int len = snprintf(buff , sizeof(buff) , "This is String : %s" , "hello world!");

puts(buff);

exit(0);

 

exit(0);

}

说明:strcpy可能造成to字符串因为长度不够而溢出的问题,直接将from对应的字符串拷贝到to字符串中;strncpy拷贝from字符串中的前n个字符到to字符串中,不保证to字符串的最后一个字符为结束符,这一点要非常小心;strlcpy函数就避免了strncpy中的这个缺陷,它拷贝src字符串中的最多siz-1个字符到dst字符串中,保证dst字符串以null结束,同时返回strlen(src),如果src的长度大于dst,则发生truncatesnprintfformat格式的字符串拷贝到str字符串中,至多拷贝size-1个字符,保证str字符串以null字符结束,如果format长度过长,则发生truncate,返回实际拷贝的字符数。

 

 

 

 

 

 

 

 

 说明:通过宏定义__FILE__、__FUNCTION__、__LINE__进行调试。

 

说明:strtoul把字符串对应的数字转化为对应进制的无符号长整数。第一个参数表示对应的字符串指针,第二个参数一般使用0,表示以null结尾,第三个参数表示对应的转化进制,0表示十进制,16表示十六进制。

 

抱歉!评论已关闭.