// Checks if entered file/dir has a comment // If so prints it. If not just prints file/dir name void checkComment(const char file[], char path[]) { char *dir; char *s; // First add the comment path to the end of the current path if (dirOrFileExists(file)) { int ford = fileOrDirectory(file); switch(ford) { // Directory was entered case 0: // Appends the directory name to the end of current path dir = allocFilename("%s/%s", path, file); makeDir(dir); // Add .comment directory to that path char * commentDir = allocFilename("%s/%s", dir, COMMENT); // Ands ..comment to that path to get the comment for that directory char * fin = allocFilename("%s/%s.comment", commentDir, DOT); if (dirOrFileExists(fin)) { printComment(file, fin); } else { printf(BLUE "%s" RESETCOLOR "\n", file); } freeFilename(commentDir); freeFilename(fin); freeFilename(dir); break; // File is entered case 1: // makeDir(path); // Adds /.comment to the end of the current path s = allocFilename("%s/%s", path, COMMENT); //Next add the file to the end of that path char * r = allocFilename("%s/%s.comment", s, file); if (dirOrFileExists(r)) { printComment(file, r); } else { printf(BLUE "%s" RESETCOLOR "\n", file); } freeFilename(s); freeFilename(r); break; // Unknown what was entered default: printf("Not sure what to do here..."); break; } } else { printf("Sorry cant find a file called %s\n", file); } }
// Still not working well with current directory when refered // as a dot (.). It tries to put a comment "clean" instead. void cleanComment( char* filename){ int fileOrDir = fileOrDirectory( filename ); char *s, path[50] = "\0"; if( !strcmp( filename, ".") ) { // Case is current directory strcat( path, ".comment/..comment" ); // s = path; }else if( fileOrDir == 1 ){ // Case is file strcat( path, ".comment/" ); strcat( path, filename ); strcat( path, ".comment" ); // s = path; }else if( fileOrDir == 0 ){ // Case is directory strcat( path, filename ); if( path[strlen(path) -1] != '/' ) // Checks and corrects if has a '/' at the end of argument. path[strlen(path)] = '/'; strcat( path, ".comment/..comment"); // s = path; } if( !dirOrFileExists( path ) ){ fprintf(stderr, "There is no comment for the file or dir \'%s\'.\n", filename ); return; } unlink( path ); }
// Prints the comment for the current directory void printCurrentComment(const char *path) { makeDir(path); // Append /.comment to the end of the current path // Append ..comment to the end of that path char *fin = NULL; asprintf(&fin, "%s/" COMMENT "/" DOT ".comment", path); if (dirOrFileExists(fin)) { // Open that file to print the conents FILE *fp; char ch; char newch[1000]; fp = fopen(fin, "r"); int i = 0; while (1) { ch = fgetc(fp); newch[i] = ch; if (ch == EOF) { newch[i] = '\0'; break; } i++; } printf(BLUE "Current Directory:" RESETCOLOR "\t"); put_multiline(newch, 50); printf("\n"); fclose(fp); } else { printf(BLUE "Current directory" RESETCOLOR "\thas no comment\n"); } free(fin); }
// Makes the comment directory at the path given void makeDir(char path[]) { // Append this to the path of the directory we want to put it in char * s = allocFilename("%s/%s", path, COMMENT); if (!dirOrFileExists(s)) mkdir(s, S_IRWXU); freeFilename(s); }
// Makes the comment directory at the path given void makeDir(const char *path) { // Append this to the path of the directory we want to put it in char *s = NULL; asprintf(&s, "%s/" COMMENT, path); if (!dirOrFileExists(s)) { mkdir(s, S_IRWXU); } free(s); }
// Checks if entered file/dir has a comment // If so prints it. If not just prints file/dir name void checkComment(const char *file, const char *path) { // First add the comment path to the end of the current path if (dirOrFileExists(file)) { char *dir = NULL; char *branch = NULL; const char *leaf = DOT; int ford = fileOrDirectory(file); if (ford == 0) { // Directory was entered // Appends the directory name to the end of current path asprintf(&branch, "%s/%s", path, file); makeDir(branch); } else if (ford == 1) { // File is entered // makeDir(path); branch = strdup(path); leaf = file; } else { // Unknown what was entered printf("Not sure what to do here..."); return; } asprintf(&dir, "%s/" COMMENT "/%s.comment", branch, leaf); if (dirOrFileExists(dir)) { printComment(file, dir); } else { printf(BLUE "%s" RESETCOLOR "\n", file); } free(dir); } else { printf("Sorry cant find a file called %s\n", file); } }
// Adds a comment to the given file/directory void addComment(const char *file, char *path, const char *comment, bool append) { if (dirOrFileExists(file)) { char *dir = NULL; const char *leaf = DOT; int ford = fileOrDirectory(file); if (ford == 0) { // Adds the file entered to the end of the current path asprintf(&dir, "%s/%s", path, file); } else if (ford == 1) { // File is entered dir = path; leaf = file; } else { // Unknown what was entered printf("Not sure what to do here..."); return; } makeDir(path); char *full = NULL; asprintf(&full, "%s/" COMMENT "/%s.comment", dir, leaf); free(full); FILE *fp; if (append) { fp = fopen(full, "a"); fprintf(fp, " %s", comment); } else { fp = fopen(full, "w+"); fprintf(fp, "%s", comment); } fclose(fp); if (ford == 0) { free(dir); } } else { printf("Sorry cant find a file called %s\n", file); } }
// Adds a comment to the given file/directory void addComment(const char file[], char path[], const char comment[], bool append) { char *dir; if (dirOrFileExists(file)) { int ford = fileOrDirectory(file); switch(ford) { // File was entered case 0: // Adds the file entered to the end of the current path dir = allocFilename("%s/%s", path, file); // Creates the directory .comment at that path makeDir(dir); // Add .comment directory to the path char * commentDir = allocFilename("%s/%s", dir, COMMENT); // Add ..comment in that folder char * fin = allocFilename("%s/%s.comment", commentDir, DOT); FILE *fp2; if (append) { fp2 = fopen(fin, "a"); fprintf(fp2, " %s", comment); } else { fp2 = fopen(fin, "w+"); fprintf(fp2, "%s", comment); } fclose(fp2); freeFilename(dir); freeFilename(commentDir); freeFilename(fin); break; // File is entered case 1: makeDir(path); // This will get a string with the path/.comment // Append this to the path of the directory we want to put it in char * s = allocFilename("%s/%s", path, COMMENT); // Now add the file we want to that char * full = allocFilename("%s/%s.comment", s, file); FILE *fp; if (append) { fp = fopen(full, "a"); fprintf(fp, " %s", comment); } else { fp = fopen(full, "w+"); fprintf(fp, "%s", comment); } fclose(fp); freeFilename(s); freeFilename(full); break; // Unknown what was entered default: printf("Not sure what to do here..."); break; } } else { printf("Sorry cant find a file called %s\n", file); } }