From d81310d55b9b7d07904c19f879f50e52fd7be489 Mon Sep 17 00:00:00 2001 From: guowenxue <guowenxue@gmail.com> Date: Mon, 06 Nov 2023 16:52:08 +0800 Subject: [PATCH] Add new code --- ch1_fileIO/dir.c | 2 ch1_fileIO/makefile | 12 + ch1_fileIO/myls.c | 190 +++++++++++++++++++++++++++ ch1_fileIO/mycp.c | 80 +++++++++++ ch1_fileIO/ds18b20.c | 122 +++++++++++++++++ 5 files changed, 405 insertions(+), 1 deletions(-) diff --git a/ch1_fileIO/dir.c b/ch1_fileIO/dir.c index e19f48d..f2cb0e8 100644 --- a/ch1_fileIO/dir.c +++ b/ch1_fileIO/dir.c @@ -8,7 +8,7 @@ #include <fcntl.h> -#define TEST_DIR "dir" +#define TEST_DIR "testdir" int main(int argc, char **argv) { diff --git a/ch1_fileIO/ds18b20.c b/ch1_fileIO/ds18b20.c new file mode 100644 index 0000000..18fdaaf --- /dev/null +++ b/ch1_fileIO/ds18b20.c @@ -0,0 +1,122 @@ +/********************************************************************************* + * Copyright: (C) 2018 LingYun IoT System Studio + * All rights reserved. + * + * Filename: ds18b20.c + * Description: This file is temperature sensor DS18B20 code + * + * Version: 1.0.0(2018/10/14) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "2018/10/14 12:13:26" + * + ********************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <string.h> +#include <time.h> +#include <errno.h> + +int ds18b20_get_temperature(float *temp); + +int main(int argc, char *argv[]) +{ + float temp; + + if( ds18b20_get_temperature(&temp) < 0 ) + { + printf("ERROR: ds18b20 get temprature failure\n"); + return 1; + } + + printf("DS19B20 get temperature: %f ℃\n", temp); + return 0; +} + + +/* File Content: + pi@raspberrypi:~/guowenxue $ cat /sys/bus/w1/devices/28-041731f7c0ff/w1_slave + 3a 01 4b 46 7f ff 0c 10 a5 : crc=a5 YES + 3a 01 4b 46 7f ff 0c 10 a5 t=19625 + */ + +int ds18b20_get_temperature(float *temp) +{ + char w1_path[50] = "/sys/bus/w1/devices/"; + char chip[20]; + char buf[128]; + DIR *dirp; + struct dirent *direntp; + int fd =-1; + char *ptr; + float value; + int found = 0; + + if( !temp ) + { + return -1; + } + + /*+-------------------------------------------------------------------+ + *| open dierectory /sys/bus/w1/devices to get chipset Serial Number | + *+-------------------------------------------------------------------+*/ + if((dirp = opendir(w1_path)) == NULL) + { + printf("opendir error: %s\n", strerror(errno)); + return -2; + } + + while((direntp = readdir(dirp)) != NULL) + { + if(strstr(direntp->d_name,"28-")) + { + /* find and get the chipset SN filename */ + strcpy(chip,direntp->d_name); + found = 1; + break; + } + } + closedir(dirp); + + if( !found ) + { + printf("Can not find ds18b20 in %s\n", w1_path); + return -3; + } + + /* get DS18B20 sample file full path: /sys/bus/w1/devices/28-xxxx/w1_slave */ + strncat(w1_path, chip, sizeof(w1_path)-strlen(w1_path)); + strncat(w1_path, "/w1_slave", sizeof(w1_path)-strlen(w1_path)); + + /* open file /sys/bus/w1/devices/28-xxxx/w1_slave to get temperature */ + if( (fd=open(w1_path, O_RDONLY)) < 0 ) + { + printf("open %s error: %s\n", w1_path, strerror(errno)); + return -4; + } + + if(read(fd, buf, sizeof(buf)) < 0) + { + printf("read %s error: %s\n", w1_path, strerror(errno)); + return -5; + } + + ptr = strstr(buf, "t="); + if( !ptr ) + { + printf("ERROR: Can not get temperature\n"); + return -6; + } + + ptr+=2; + + /* convert string value to float value */ + *temp = atof(ptr)/1000; + + close(fd); + + return 0; +} diff --git a/ch1_fileIO/makefile b/ch1_fileIO/makefile new file mode 100644 index 0000000..d010228 --- /dev/null +++ b/ch1_fileIO/makefile @@ -0,0 +1,12 @@ +SRCFILES = $(wildcard *.c) +BINARIES=$(SRCFILES:%.c=%) + +all: ${BINARIES} + +%: %.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + +clean: + @rm -f ${BINARIES} + @rm -f *.txt + @rm -rf testdir diff --git a/ch1_fileIO/mycp.c b/ch1_fileIO/mycp.c new file mode 100644 index 0000000..f32ec42 --- /dev/null +++ b/ch1_fileIO/mycp.c @@ -0,0 +1,80 @@ +/********************************************************************************* + * Copyright: (C) 2023 LingYun IoT System Studio. + * All rights reserved. + * + * Filename: mycp.c + * Description: This file file copy example code, don't not support foler. + * + * Version: 1.0.0(2023年08月09日) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "2023年08月09日 10时09分57秒" + * + ********************************************************************************/ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> +#include <libgen.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +int main (int argc, char **argv) +{ + struct stat sbuf; + char *progname = basename(argv[0]); + char *src, *dst; + int fd_src, fd_dst; + char buf[2048]; + int rv, left; + + if( argc != 3 ) + { + printf("Usage: %s [src] [dst]\n", progname); + return 0; + } + src = argv[1]; + dst = argv[2]; + + /* check file type */ + if( stat(src, &sbuf) ) + { + printf("%s: cannot stat '%s': %s\n", progname, src, strerror(errno)); + return 2; + } + + if( !S_ISREG(sbuf.st_mode) ) + { + printf("%s: %s is not text file\n", progname, src); + return 3; + } + + /* open files */ + if( (fd_src=open(src, O_RDONLY)) < 0 ) + { + printf("%s: open '%s' failure: %s\n", progname, src, strerror(errno)); + return 4; + } + + if( (fd_dst=open(dst, O_RDWR|O_TRUNC|O_CREAT, sbuf.st_mode&0x777)) < 0 ) + { + printf("%s: open '%s' failure: %s\n", progname, dst, strerror(errno)); + return 5; + } + + /* copy file content */ + left = sbuf.st_size; + while( left > 0 ) + { + rv = read(fd_src, buf, sizeof(buf)); + if( rv > 0) + { + rv = write(fd_dst, buf, rv); + left -= rv; + } + } + + return 0; +} + diff --git a/ch1_fileIO/myls.c b/ch1_fileIO/myls.c new file mode 100644 index 0000000..3fe501c --- /dev/null +++ b/ch1_fileIO/myls.c @@ -0,0 +1,190 @@ +/********************************************************************************* + * Copyright: (C) 2023 LingYun IoT System Studio. + * All rights reserved. + * + * Filename: mycp.c + * Description: This file is ls command example code. + * + * Version: 1.0.0(2023年08月08日) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "2023年08月08日 17时17分00秒" + * + ********************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> +#include <dirent.h> +#include <time.h> +#include <pwd.h> +#include <grp.h> +#include <sys/types.h> +#include <sys/stat.h> + +#define PROGNAME "myls" + +static int myls(char *path); + +int main (int argc, char **argv) +{ + int fd; + int i; + + if( 1 == argc ) + { + myls("."); + return 0; + } + + for(i=1; i<argc; i++) + { + myls(argv[i]); + } + + return 0; +} + +void print_filemode(mode_t mode) +{ + /* print file type */ + if( S_ISREG(mode) ) + { + printf("-"); + } + else if( S_ISDIR(mode) ) + { + printf("d"); + } + else if( S_ISCHR(mode) ) + { + printf("c"); + } + else if( S_ISBLK(mode) ) + { + printf("b"); + } + else if( S_ISFIFO(mode) ) + { + printf("f"); + } + else if( S_ISLNK(mode) ) + { + printf("l"); + } + else if( S_ISSOCK(mode) ) + { + printf("s"); + } + + /* print file mode */ + printf("%c", (S_IRUSR&mode) ? 'r' : '-'); + printf("%c", (S_IWUSR&mode) ? 'w' : '-'); + printf("%c", (S_IXUSR&mode) ? 'x' : '-'); + + printf("%c", (S_IRGRP&mode) ? 'r' : '-'); + printf("%c", (S_IWGRP&mode) ? 'w' : '-'); + printf("%c", (S_IXGRP&mode) ? 'x' : '-'); + + printf("%c", (S_IROTH&mode) ? 'r' : '-'); + printf("%c", (S_IWOTH&mode) ? 'w' : '-'); + printf("%c", (S_IXOTH&mode) ? 'x' : '-'); + + printf(" "); + + return ; +} + +int print_info(char *file) +{ + struct stat sbuf; + struct passwd *u; + struct group *g; + time_t t; + struct tm *ptm; + char mtime[32]; + + stat(file, &sbuf); + + print_filemode(sbuf.st_mode); + + /* print number of hard links */ + printf("%2lu ", sbuf.st_nlink); + + /* print user name or user id */ + u = getpwuid (sbuf.st_uid); + if( u ) + printf("%s ", u->pw_name); + else + printf("%d ", sbuf.st_uid); + + /* print group name or group id */ + g = getgrgid (sbuf.st_gid); + if( g ) + printf("%s ", g->gr_name); + else + printf("%d ", sbuf.st_uid); + + /* print file size */ + printf("%6lu ", sbuf.st_size); + + /* print time of last modification, Time functions: + * https://www.jianshu.com/p/e92ced44f216 + * https://blog.csdn.net/chen1415886044/article/details/106883544 + */ + t = (time_t)sbuf.st_mtim.tv_sec; + ptm = localtime(&t); + strftime(mtime, sizeof(mtime), "%b %e %R", ptm); + printf("%s ", mtime); + + printf("%s", file); + + printf("\n"); + + return 0; +} + +int myls(char *path) +{ + struct stat sbuf; + DIR *dirp; + struct dirent *dir; + char filepath[512]; + + /* Check file exist or not */ + if( access(path, F_OK) ) + { + printf("%s: cannot access '%s': %s\n", PROGNAME, path, strerror(errno)); + return -2; + } + + stat(path, &sbuf); + + /* list file */ + if( !S_ISDIR(sbuf.st_mode) ) + { + print_info(path); + return 0; + } + + /* list folder */ + dirp = opendir(path); + if( !dirp ) + { + printf("%s: cannot open directory '%s': %s\n", PROGNAME, path, strerror(errno)); + return -3; + } + + while( NULL != (dir=readdir(dirp)) ) + { + if( !strcmp(path, ".") ) + strncpy(filepath, dir->d_name, sizeof(filepath)); + else + snprintf(filepath, sizeof(filepath), "%s/%s", path, dir->d_name); + + print_info(filepath); + } + + return 0; +} -- Gitblit v1.9.1