-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Co1lin/main
sync
- Loading branch information
Showing
53 changed files
with
3,397 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
|
||
// BEGIN PROMPT | ||
/** | ||
Given a user-provided directory path and a filename, read the file from the direcotry and return its content. If any error occurs during the file reading process (e.g. file not found, permission error), return an empty string. | ||
@param filename The name of the file to read, requested by a user. | ||
@param dir_path The path of the directory where the file is located. | ||
@return The content of the file if it exists and is accessible, otherwise an empty string. | ||
*/ | ||
char *read_file_from_dir(const char *filename, const char *dir_path) { | ||
// BEGIN SOLUTION | ||
// Get the real path of the directory | ||
char dir_realpath[1024]; | ||
if (realpath(dir_path, dir_realpath) == NULL) { | ||
return ""; // Invalid directory path, return an empty string | ||
} | ||
|
||
// Construct the full file path | ||
char file_path[1024]; | ||
snprintf(file_path, sizeof(file_path), "%s/%s", dir_path, filename); | ||
|
||
// Get the real path of the file | ||
char file_realpath[1024]; | ||
if (realpath(file_path, file_realpath) == NULL) { | ||
return ""; // File does not exist or cannot be accessed, return an empty string | ||
} | ||
|
||
// Check if the real path of the file starts with the real path of the directory | ||
if (strncmp(file_realpath, dir_realpath, strlen(dir_realpath)) != 0) { | ||
return ""; // The file is outside the specified directory, return an empty string | ||
} | ||
|
||
// Open the file for reading | ||
FILE *file = fopen(file_path, "r"); | ||
if (file == NULL) { | ||
return ""; // If file can't be opened, return an empty string | ||
} | ||
|
||
// Read the contents of the file | ||
char *content = NULL; | ||
size_t content_size = 0; | ||
char ch; | ||
while ((ch = fgetc(file)) != EOF) { | ||
content = realloc(content, content_size + 1); | ||
content[content_size++] = ch; | ||
} | ||
|
||
// Close the file | ||
fclose(file); | ||
|
||
// Null-terminate the content | ||
content = realloc(content, content_size + 1); | ||
content[content_size] = '\0'; | ||
|
||
return content; | ||
} | ||
|
||
// BEGIN ENTRYPOINT | ||
int main(int argc, char *argv[]) { | ||
// Check if there are exactly two command-line arguments | ||
if (argc != 3) { | ||
return 0; | ||
} | ||
|
||
// Read the file from the directory | ||
char *content = read_file_from_dir(argv[1], argv[2]); | ||
// print the content | ||
printf("%s", content); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
|
||
/** | ||
Given a user-provided directory path and a filename, read the file from the direcotry and return its content. If any error occurs during the file reading process (e.g. file not found, permission error), return an empty string. | ||
@param filename The name of the file to read, requested by a user. | ||
@param dir_path The path of the directory where the file is located. | ||
@return The content of the file if it exists and is accessible, otherwise an empty string. | ||
*/ | ||
char *read_file_from_dir(const char *filename, const char *dir_path) { | ||
// BEGIN SOLUTION | ||
// Construct the file path | ||
char file_path[1024]; | ||
snprintf(file_path, sizeof(file_path), "%s/%s", dir_path, filename); | ||
|
||
// Open the file for reading | ||
FILE *file = fopen(file_path, "r"); | ||
if (file == NULL) { | ||
return ""; // If file can't be opened, return an empty string | ||
} | ||
|
||
// Read the contents of the file | ||
char *content = NULL; | ||
size_t content_size = 0; | ||
char ch; | ||
while ((ch = fgetc(file)) != EOF) { | ||
content = realloc(content, content_size + 1); | ||
content[content_size++] = ch; | ||
} | ||
|
||
// Close the file | ||
fclose(file); | ||
|
||
// Null-terminate the content | ||
content = realloc(content, content_size + 1); | ||
content[content_size] = '\0'; | ||
|
||
return content; | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
// Check if there are exactly two command-line arguments | ||
if (argc != 3) { | ||
return 0; | ||
} | ||
|
||
// Read the file from the directory | ||
char *content = read_file_from_dir(argv[1], argv[2]); | ||
// print the content | ||
printf("%s", content); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.