Ejemplo n.º 1
0
int lsdir(const char *path)
{
	struct stat __stat, *stat = &__stat;
	static char cwdbuf[BUFSIZE];
	int ret;
	if ((ret = getcwd(cwdbuf, BUFSIZE)) != 0) {
		return ret;
	}
	if ((ret = chdir(path)) != 0) {
		return ret;
	}
	DIR *dirp = opendir(".");
	if (dirp == NULL) {
		return -1;
	}
	struct dirent *direntp;
	while ((direntp = readdir(dirp)) != NULL) {
		if ((ret = getstat(direntp->name, stat)) != 0) {
			goto failed;
		}
		lsstat(stat, direntp->name);
	}
	closedir(dirp);
	return chdir(cwdbuf);

failed:
	closedir(dirp);
	chdir(cwdbuf);
	return ret;
}
Ejemplo n.º 2
0
Archivo: ls.c Proyecto: lishuhuakai/CS
int
lsdir(const char *path) {
    struct stat __stat, *stat = &__stat;
    int ret;
    DIR *dirp = opendir(".");
    
    if (dirp == NULL) {
        return -1;
    }
    struct dirent *direntp;
    while ((direntp = readdir(dirp)) != NULL) {
        if ((ret = getstat(direntp->name, stat)) != 0) {
            goto failed;
        }
        lsstat(stat, direntp->name);
    }
    printf("lsdir: step 4\n");
    closedir(dirp);
    return 0;
failed:
    closedir(dirp);
    return ret;
}