added ws202425 courses
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
CC = gcc
|
||||
|
||||
all: workdir.c print_file.c
|
||||
${CC} -o workdir workdir.c
|
||||
${CC} -o print_file print_file.c
|
||||
|
||||
clean:
|
||||
rm -f workdir print_file
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define SIZE 512
|
||||
|
||||
int main() {
|
||||
|
||||
int fd = open("./test.txt", O_RDWR);
|
||||
if (fd == -1){
|
||||
perror("could not open file");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
char * buffer = (char *) calloc(SIZE, sizeof(char));
|
||||
|
||||
if (buffer == NULL) {
|
||||
perror("could not allocate buffer");
|
||||
close(fd);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
ssize_t r = read(fd, buffer, SIZE-1);
|
||||
if (r == -1) {
|
||||
perror("could not read file");
|
||||
free(buffer);
|
||||
close(fd);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return 0;
|
||||
|
||||
printf("%.*s", SIZE-1, buffer);
|
||||
free(buffer);
|
||||
close(fd);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
char *mywdfunc(unsigned int bufsize) {
|
||||
if (bufsize < 100) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *buf = (char *)calloc(bufsize,sizeof(char));
|
||||
|
||||
if (buf == NULL){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (getcwd(buf,bufsize-1) == NULL) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char *ret = mywdfunc(512);
|
||||
|
||||
printf("%s", ret);
|
||||
free(ret);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user