linux上机作业一list命令的实现
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Linux上机习题(一)
2013211313班王令聪学号:2013211534 源代码:
#include
#include
#include
#include
#include
#include
#include
#include
#define TRUE 1
#define FALSE 0
//2013211313班王令聪学号:2013211534
int sel_r = 0, sel_a = 0, sel_l = 0, sel_h = 0, sel_m = 0;
int max = 0, min = 0, due = 0;
int depth = 0, count = 0;
int isnum(char *param)
{
int j;
for (j = 0; j < strlen(param); j++)
if (param[j] < '0' || param[j] > '9')
return FALSE;
return TRUE;
}
void cmd_err(void)
{
printf ("Command error!\n");
exit (1);
}
void mylist(char *path)
{
int limit, diff;
char fpath[100] = {'\0'};
DIR *dir;
struct dirent *entry;
struct stat st;
time_t now;
if (stat(path, &st) == -1) {
printf("Open \"%s\": %s (ERROR %d)\n", path, strerror(errno), errno);
exit(1);
}
else if (S_ISDIR(st.st_mode) && (depth == 0 || sel_r)) {
dir = opendir(path);
if (dir == NULL) {
printf("Open directory \"%s\": %s (ERROR %d)\n", path, strerror(errno), errno);
exit(1);
}
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 && ((! sel_a) && entry->d_name[0] != '.' || sel_a)) {
strcpy(fpath, path);
strcat(fpath, "/");
strcat(fpath, entry->d_name);
depth ++;
mylist(fpath);
depth --;
}
}
closedir(dir);
}
else if (S_ISREG(st.st_mode)) {
now = time(NULL);
limit = due*24*60*60;
diff = now - st.st_mtime;
if ((! (sel_l && st.st_size < min)) &&
(! (sel_h && st.st_size > max)) &&
(! (sel_m && diff > limit))) {
printf ("%s %d\n", path, (int)st.st_size);
count ++;
}
}
}
int main(int argc, char *argv[])
{
int i;
char path[100];
if (argc <= 10) {
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
if (strcmp(argv[i], "-r") == 0)
sel_r = 1;
else if (strcmp(argv[i], "-a") == 0)
sel_a = 1;
else if ((strcmp(argv[i], "-ar") == 0) || (strcmp(argv[i], "-ra") == 0)) {
sel_r = 1;
sel_a = 1;
}
else if (strcmp(argv[i], "-h") == 0) {
sel_h = 1;
i ++;
if (isnum(argv[i]))
max = atoi(argv[i]);
else
cmd_err();
if (max < min)
cmd_err();
}
else if (strcmp(argv[i], "-l") == 0) {
sel_l = 1;
i ++;