#include // for snprintf #include // for EXIT_SUCCESS, NULL #include // for strrchr, strcmp, strlen #include // for bool #include "ro_file.h" /*** HELPER FUNCTION DECLARATIONS ******************************************/ // Returns whether or not a filename ends in ".txt". bool IsTxtFile(char* filename); // Concatenate the directory and file names into a full path. The caller is // responsible for freeing the allocated string. Exits if an error occurs. char* Concatenate(char* dirname, char* filename); /* * This program: * - Accepts a directory name as a command-line argument. * - Scans through the directory looking for all files whose names end with * the four characters ".txt". * - For every such file found, write the contents of those files to stdout * without adding any additional characters or formatting. * Eventually reading the files with ro_file module. */ int main(int argc, char** argv) { // TODO: Write this function return EXIT_SUCCESS; } /*** HELPER FUNCTION DEFINITIONS *******************************************/ bool IsTxtFile(char* filename) { char* dot = strrchr(filename, '.'); return dot && !strcmp(dot, ".txt"); } char* Concatenate(char* dirname, char* filename) { bool has_trailing_slash = dirname[strlen(dirname) - 1] == '/'; // Concatenate directory and file name. size_t dlen = strlen(dirname); size_t flen = strlen(filename); // Malloc space for full path name: // dlen + strlen("/") + flen + strlen('\0') = dlen + flen + 2 int size_to_malloc = has_trailing_slash ? dlen + flen + 1 : dlen + flen + 2; char* fullpath = (char*) malloc(sizeof(char) * (size_to_malloc)); if (fullpath == NULL) { fprintf(stderr, "Error on malloc.\n"); exit(EXIT_FAILURE); } if (has_trailing_slash) { snprintf(fullpath, size_to_malloc, "%s%s", dirname, filename); } else { snprintf(fullpath, size_to_malloc, "%s/%s", dirname, filename); } return fullpath; }