#include
#include
#include
#include
#include
static off_t flen(const char *fname){
struct stat statres;
if (stat(fname,&statres) < 0) {
perror("tata()");
exit(1);
}
return statres.st_size;
}
int main(int argc,char **argv)
{
if (argc < 2) {
fprintf(stderr,"Usage...\n");
exit(1);
}
long len = flen(argv[1]);
printf("st_size = %ld\n",len);
exit(0);
}
注意,在unix中文件大小size
只是一个属性,不一定代表文件真正的大小(与文件系统相关)
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
if (argc < 2) {
fprintf(stderr,"Usage...\n");
exit(1);
}
int fd = open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,0600);
if (fd < 0) {
perror("open()");
exit(1);
}
long err = lseek(fd,5LL*1024LL*1024LL*1024LL-1LL,SEEK_SET);
if (err == -1) {
perror("lseek");
exit(1);
}
write(fd,"",1);
return 0;
}
b c d - l s p
不错的-老色批
//文件类型
static int ftype(const char* fname) {
if (stat(fname,&statres) < 0) {
perror("rstat()");
exit(1);
}
if (S_ISREG(statres.st_mode)) {
return '-';
}else if (S_ISDIR(statres.st_mode)) {
return 'd';
}else{
return '?';
}
#include
int chmod(const char *path,mode_t mode);
int fchmod(int fd,mode_t mode); //修改一个已经成功打开的文件
t位
$ ls -l /
drwxrwxrwt 1 root root 3.6K 2月 8 17:58 tmp
硬链接
其实就是在目录项
中添加一条映射单独调用
//glob解析路径
static void Glob(){
glob_t globres;
int err = glob(PAT,0,&errfunc,&globres);
if (err) {
printf("Error code = %d\n",err);
}
for (int i = 0;globres.gl_pathv[i]!= NULL;i++) {
fprintf(stdout,"%s\n",globres.gl_pathv[i]);
}
}
组合调用
//组合解析路径
static void PathParse(char *Path) {
DIR *dp;
struct dirent *cur;
dp = opendir(Path);
if (dp == NULL) {
perror("opendir");
exit(1);
}
while((cur = readdir(dp)) != NULL) {
fprintf(stdout,"%s ",cur->d_name);
fprintf(stdout,"type:%d ",cur->d_type);
}
closedir(dp);
}
//getcwd()的使用
char pwd[1024];
getcwd(pwd,1024);
fprintf(stdout,"%s\n",pwd);
PathParse(pwd);
mydu.c
#include
#include
#include
#include
#include
#include
#include
#include
#define PATHSIZE 1024
static int path_noloop(const char *path) {
char *pos;
pos = strrchr(path,'/');
if (pos == NULL) {
exit(1);
}
if (strcmp(pos+1,".") == 0||strcmp(pos+1,"..")== 0) {
return 0;
}
return 1;
}
static int64_t mydu(const char *path) {
static struct stat statres;
static char nextpath[PATHSIZE];
glob_t globres;
int64_t sum = 0;
//非目录
if (lstat(path,&statres) < 0) {
perror("lstat()");
exit(1);
}
if (!S_ISDIR(statres.st_mode)){
fprintf(stdout,"%ld\t%s\n",statres.st_blocks / 2,path);
return statres.st_blocks;
}
//目录
//拼接路径
strncpy(nextpath,path,PATHSIZE);
strncat(nextpath,"/*",PATHSIZE);
if (glob(nextpath,0,NULL,&globres) < 0) {
fprintf(stderr,"glob()");
exit(1);
}
strncpy(nextpath,path,PATHSIZE);
strncat(nextpath,"/.*",PATHSIZE);
if (glob(nextpath,GLOB_APPEND,NULL,&globres) < 0) {
fprintf(stderr,"glob()");
exit(1);
}
sum = statres.st_blocks;
for (int i = 0;i < globres.gl_pathc;i++){
if (path_noloop(globres.gl_pathv[i]))
sum += mydu(globres.gl_pathv[i]);
}
globfree(&globres);//回收资源
return sum;
}
int main(int argc,char **argv)
{
if (argc < 2) {
fprintf(stderr,"%s\n","Usage...");
exit(1);
}
printf("%ld\t%s\n",mydu(argv[1])/2,argv[1]);
return 0;
}
time_t => char * => struct_tm
struct_tm
格林威治时间struct_tm
本地时间time_t stamp;
time(&stamp);
stamp = time(NULL);
tm = localtime(&stamp);
strftime(buf,BUFSIZE,"%Y-%m-%d",tm);
puts(buf);
#include
#include
#include
#include
#include
#define BUFSIZE 1024
int main()
{
char fmttime[BUFSIZ];
int count = 0;
FILE *fptr = fopen("./log","a+");
if (fptr == NULL) {
perror("fopen()");
exit(1);
}
char buf[BUFSIZE];
while(fgets(buf,BUFSIZE,fptr) != NULL){
count++;
}
char res[BUFSIZE];
while (1){
time_t stamp;
stamp = time(NULL);
struct tm *struct_tm;
struct_tm = localtime(&stamp);
strftime(fmttime,BUFSIZE,"%Y-%m-%d %H:%M:%S",struct_tm);
fprintf(fptr,"%d %s\n",++count,fmttime);
fflush(fptr);
sleep(1);
}
fclose(fptr);
exit(0);
}
int main(int argc,char **argv)
_exit
或者_Exit
pthread_exit
abort
本质就是 KEY = VALUE
export
#include
#include
extern char **environ;
static void getEnv(char *key){
puts(getenv(key));
}
int main()
{
for (int i = 0;environ[i] != NULL;i++){
puts(environ[i]);
}
getEnv("ZSH");
return 0;
}
#ifndef LLIST_H__
#define LLIST_H__
enum{
F = 1,
B = 2,
};
//普通节点
struct llist_node_st{
void *data;
struct llist_node_st *prev;
struct llist_node_st *next;
};
//头节点
typedef struct {
int size;
struct llist_node_st head;
} LLIST; //LLIST就是一个双向链表的头节点类型,对于链表的操作都是用head来进行的
//传入 每个数据节点的数据类型大小
LLIST *llist_careate(int size);
//传入 一个已经创好的链表的头节点,插入的数据,插入的模式
int llist_insert(LLIST *,const void *data,int mode);
//传入
void *llist_find(LLIST *head,const void* ,int (*func)(const void*,const void*));
//
int llist_delete(LLIST *head,const void* ,int (*func)(const void*,const void*));
//
int llist_fetch(LLIST *head,const void* ,int (*func)(const void*,const void*),void *);
//传入 一个已经创建好的链表头节点
void llist_travel(LLIST* head,void (*func)(const void*));
void llist_destroy(LLIST *);
#endif
#include
#include
#include
#include "llist.h"
//传入 每个数据节点的数据类型大小
LLIST *llist_careate(int size){
LLIST *new;
new = malloc(sizeof(*new));
if (new == NULL){
return NULL;
}
new->size = size;
new->head.data = NULL;
new->head.prev = &new->head;
new->head.next = &new->head;
return new;
}
//传入 一个已经创好的链表的头节点,插入的数据,插入的模式
int llist_insert(LLIST *head,const void *data,int mode){
struct llist_node_st *newnode;
newnode = malloc(sizeof(*newnode));
if (newnode == NULL)
return -1;
newnode->data = malloc(head->size);
if (newnode->data == NULL){
return -2;
}
memcpy(newnode->data,data,head->size);
switch (mode) {
case F:
newnode->prev = &head->head;
newnode->next = head->head.next;
break;
case B:
newnode->prev = head->head.prev;
newnode->next = &head->head;
break;
default:
return -3;
}
newnode->prev->next = newnode;
newnode->next->prev = newnode;
return 0;
}
//传入 一个已经创建好的链表头节点,一个辅助遍历函数
void llist_travel(LLIST* head,void (*func)(const void *)){
struct llist_node_st *cur,*next;
for (cur = head->head.next;cur != &head->head;cur = next) {
func(cur->data);
next = cur->next;
}
}
//辅助函数
static struct llist_node_st *find_(LLIST *head,const void *key,int (*func)(const void *,const void *)){
struct llist_node_st *cur;
for (cur = head->head.next;cur != &head->head;cur = cur->next){
if (func(key,cur->data) == 0){
return cur;
}
}
return &head->head;
}
void *llist_find(LLIST *head,const void* key,int (*func)(const void*,const void*)){
return find_(head,key,func)->data;
}
//
int llist_delete(LLIST *head,const void* key,int (*func)(const void*,const void*)){
struct llist_node_st *node;
node = find_(head,key,func);
if (node == &head->head){
return -1;
}else {
node->prev->next = node->next;
node->next->prev = node->prev;
free(node->data);
free(node);
return 0;
}
}
//
int llist_fetch(LLIST *head,const void* key,int (*func)(const void*,const void*),void *data){
struct llist_node_st *node;
node = find_(head,key,func);
if (node == &head->head){
return -1;
}else {
node->prev->next = node->next;
node->next->prev = node->prev;
data = node->data;
free(node->data);
free(node);
return 0;
}
}
void llist_destroy(LLIST *head) {
struct llist_node_st *cur,*next;
for (cur = head->head.next;cur != &head->head;cur = next) {
next = cur->next;
free(cur->data);
free(cur);
}
free(head);
}
CFLAGS +=-Wall -g -lstdc++ -D_FILE_OFFSET_BITS=64
CC =gcc
TARGET =DoubleLinkList
OBJ =llist.o
src =llist.c
$(TARGET):$(OBJ)
$(CC) main.c $(OBJ) -o $@
$(OBJ):$(src)
$(CC) $(src) $(CFLAGS) -c -o $@
clean:
-rm -f $(OBJ)
-rm -f $(TARGET)
ar -cr libxx.a yyy.o
/usr/local/include
/usr/local/lib
gcc -L/usr/local/lib -o main mian.o -lxx
-l
参数必须在最后,有依赖make
ar -cr libllist.a llist.o
gcc -L./ -o main main.c -lllist
libxx.so
xx是库名gcc -shared -fpic -o libxx.so yyy.c
/usr/local/include
/usr/local/lib
(.h 与 .so)/etc/ld.so.conf
gcc -I/usr/local/include -L/usr/local/lib -o ... lxx
重名用动态库
适用场景: 在树结构中查找元素,找到后直接回到第一次调用处(跨函数),不用一层一层返回
#include
#include
#include
static jmp_buf save;
static void d(){
printf("%s is called\n",__FUNCTION__);
longjmp(save,2);
printf("%s is returned\n",__FUNCTION__);
}
static void c(){
printf("%s is called\n",__FUNCTION__);
d();
printf("%s is returned\n",__FUNCTION__);
}
static void b(){
printf("%s is called\n",__FUNCTION__);
c();
printf("%s is returned\n",__FUNCTION__);
}
static void a(){
int ret = setjmp(save);
if (ret == 0) {
printf("%s is called\n",__FUNCTION__);
b();
printf("%s is returned\n",__FUNCTION__);
}else {
printf("code %d return \n",ret);
}
}
int main()
{
a();
return 0;
}
getrlimit
setrlimit