コード例 #1
0
ファイル: c.c プロジェクト: LucasAbreu/c
// Prints all the files in either ascending or descending order
void printAllOrder(char newpath[], bool desc) {
    printCurrentComment(newpath);
    printf("\n");

    FILE *fp;
    int status;
    char path[1035];

    /* Open the command for reading. */
    if (desc) {
        fp = popen("/bin/ls -tr  ", "r");
    } else {
        fp = popen("/bin/ls -t  ", "r");
    }
    if (fp == NULL) {
        printf("Failed to run command\n" );
        exit;
    }

    /* Read the output a line at a time - output it. */
    while (fgets(path, sizeof(path)-1, fp) != NULL) {
        strip(path);
        // printf("%s", path);
        checkComment(path, newpath);
    }
    /* close */
    pclose(fp);
}
コード例 #2
0
ファイル: c.c プロジェクト: AlwaysLately/c
// Prints all the files in the current directory
void printFiles(const char *path) {
	// Print the current directories comment
	printCurrentComment(path);
	printf("\n");

	DIR *mydir = opendir(path);
	struct dirent *entry = NULL;

	while((entry = readdir(mydir))) { // If we get EOF, the expression is NULL and the loop stops
		// We will not show the dot files
		if (entry->d_name[0] == '.') {
			continue;
		}
		checkComment(entry->d_name, path);
	}
	closedir(mydir);
}
コード例 #3
0
ファイル: c.c プロジェクト: LucasAbreu/c
int main (int argc, const char * argv[]) {

    // Get the current directory
    char cwd[1024];
    if (getcwd(cwd, sizeof(cwd)) != NULL)
        ;
    else
        perror("getcwd() error");

    int numargs, all=0, asc=0, desc=0, push=0, uk=0, help=0, current=0, clean=0;
    const char *c;

    numargs = argc;

    while (--numargs > 0) {
        c = argv[numargs];

        if (!strcmp(argv[numargs], "all"))
            all = 1;
        else if (!strcmp(argv[numargs], "-h") || !strcmp(argv[numargs], "--help"))
            help = 1;
        else if (!strcmp(argv[numargs], "."))
            current = 1;
        else if (!strcmp(argv[numargs], "-p") || !strcmp(argv[numargs], "--push"))
            push = 1;
        else if (!strcmp(argv[numargs], "-"))
            desc = 1;
        else if (!strcmp(argv[numargs], "+"))
            asc = 1;
        else if (!strcmp(argv[numargs], "clean"))
            clean = 1;
        else
            uk++;
    }

    int args;
    args = argc;

    if (uk > 2) {
        printUsage();
        return 1;
    }

    switch(args) {
        // Show the comment for the current directory
        case 1: printUsage();
                return 1;
                break;
        case 2:
                // Show the help menu
                if (help) {
                    printUsage();
                    return 1;
                }
                // Show all the files
                else if (all) {
                    makeDir(cwd);
                    printFiles(cwd);
                }
                // Show comment for the current directory
                else if (current) {
                    // checkComment(argv[1], cwd);
                    printCurrentComment(cwd);
                }
                // Show comment for the entered file
                else {
                    // char *file = argv[1];
                    makeDir(cwd);
                    checkComment(argv[1], cwd);
                }
                break;
        case 3:

                if ( current && !clean ) {										// Add comment to the current directory
                    // char *file;
                    // file = "CURRENT";
                    addComment(argv[1], cwd, argv[2], false);
                } else if (all && desc) {					// Print all files in descending order
                    makeDir(cwd);
                    printAllOrder(cwd, true);
                } else if (all && asc) {					// Print all files in ascending order
                    makeDir(cwd);
                    printAllOrder(cwd, false);
                } else if ( clean ){
                    cleanComment( argv[1] );                 // Clean file comment
                }
                else {														// Add comment to to given file
                    makeDir(cwd);
                    addComment(argv[1], cwd, argv[2], false);
                }
                break;

        case 4:
                // Push comment to the current directory
                if (current && push) {
                    addComment(argv[1], cwd, argv[3], true);
                    // Add a comment to the given filename
                }
                // Push comment to given file
                else	if ( push) {
                    makeDir(cwd);
                    addComment(argv[1], cwd, argv[3], true);
                }
                break;

                // Not certain
        default: printUsage();
    }
    return 0;
}
コード例 #4
0
ファイル: c.c プロジェクト: AlwaysLately/c
int main (int argc, const char *argv[]) {
	// Get the current directory
	char cwd[1024];
	if (!getcwd(cwd, sizeof(cwd))) {
		perror("getcwd() error");
	}

	int numargs, all = 0, asc = 0, desc = 0, push = 0, uk = 0, help = 0, current = 0;
	const char *c;

	numargs = argc;

	// TODO: Convert to getopt
	while (--numargs > 0) {
		c = argv[numargs];

		if (!strcmp(argv[numargs], "all"))
			all = 1;
		else if (!strcmp(argv[numargs], "-h") || !strcmp(argv[numargs], "--help"))
			help = 1;
		else if (!strcmp(argv[numargs], "."))
			current = 1;
		else if (!strcmp(argv[numargs], "-p") || !strcmp(argv[numargs], "--push"))
			push = 1;
		else if (!strcmp(argv[numargs], "-"))
			desc = 1;
		else if (!strcmp(argv[numargs], "+"))
			asc = 1;
		else
			uk++;
	}

	int args;
	args = argc;

	if (uk > 2) {
		printUsage();
		return EXIT_FAILURE;
	}

	if (args == 1) {
			printUsage();
			return EXIT_FAILURE;
	} else if (args == 2) {
		// Show the help menu
		if (help) {
			printUsage();
			return EXIT_FAILURE;
		}
		// Show all the files
		else if (all) {
			makeDir(cwd);
			printFiles(cwd);
		}
		// Show comment for the current directory
		else if (current) {
			// checkComment(argv[1], cwd);
			printCurrentComment(cwd);
		}
		// Show comment for the entered file
		else {
			// char *file = argv[1];
			makeDir(cwd);
			checkComment(argv[1], cwd);
		}
	} else if (args == 3) {
		if (current) {                               // Add comment to the current directory
			// char *file;
			// file = "CURRENT";
			addComment(argv[1], cwd, argv[2], false);
		} else if (all && desc) {                    // Print all files in descending order
			makeDir(cwd);
			printAllOrder(cwd, true);
		} else if (all && asc) {                     // Print all files in ascending order
			makeDir(cwd);
			printAllOrder(cwd, false);
		} else {                                     // Add comment to to given file
			makeDir(cwd);
			addComment(argv[1], cwd, argv[2], false);
		}
	} else if (args == 4) {
		if (current && push) { // Push comment to the current directory
			// Add a comment to the given filename
			addComment(argv[1], cwd, argv[3], true);
		} else if (push) {       // Push comment to given file
			makeDir(cwd);
			addComment(argv[1], cwd, argv[3], true);
		}
	} else {
		// Not certain
		printUsage();
	}
	return EXIT_SUCCESS;
}