#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
// This program is pretty much a simulation of the ls command. Find out how to scan all of the files
// in the directory it's being run in and print out all the file names. Has to be in order.
int main(int argc, char* argv[])
{
// Don't worry about the else if, this is just a second part of my code that does not work entirely.
else if (strstr(argv[1], "-i")) // Long output
{
char **words = calloc(1000, sizeof(*words));
DIR *d;
struct dirent *dir; // Pointer for directory entry
d = opendir(".");
char* a = ".";
char* b = "..";
char* c = "ls";
int ret1, ret2, ret3, count = 0;
if (d) // opendir returns NULL if couldn't open directory
{
while ((dir = readdir(d)) != NULL)
{
ret1 = strcmp(dir->d_name, a); // Compare with the parent directory.
ret2 = strcmp(dir->d_name, b); // Compare with the parent directory.
ret3 = strcmp(dir->d_name, c); // Compare with the ls
if (ret1 == 0 || ret2 == 0 || ret3 == 0)
{
// Skip the ., .., and ls
}
else
{
words[count] = strdup(dir->d_name); // Put the file name in the array.
count ;
}
}
for (int i = 0; i < count; i) // Start readjusting the array in alphabetical order.
{
int imin = i;
for (int j = i 1; j < count; j)
{
char word1[1000], word2[1000];
strcpy(word1, words[j]);
strcpy(word2, words[imin]);
for(int p = 0; word1[p]; p )
{
word1[p] = tolower(word1[p]);
}
for(int p = 0; word2[p]; p )
{
word2[p] = tolower(word2[p]);
}
if (strcmp(word1, word2) < 0)
{
imin = j;
}
}
char *tmp = words[i];
words[i] = words[imin];
words[imin] = tmp;
}
// Print every word in the array, but this time with metadata!
struct stat meta;
for (int i = 0; i < count; i)
{
stat(words[i], &meta);
int size = meta.st_size;
// How am I supposed to get read, write, execute info???
struct passwd *p;
struct group *grp;
uid_t uid = 0;
gid_t gid = 0;
if ((p = getpwuid(uid)) == NULL) // If username doesn't exist
{
printf("%d ", meta.st_uid); // User id
}
else
{
printf("%s ", p->pw_name); // User Name
}
if ((grp = getgrgid(gid)) == NULL) // If group name doesn't exist
{
printf("%d ", meta.st_gid); // Group id
}
else
{
printf("%s ", grp->gr_name); // Group Name
}
printf("%d ", size); // File Size
printf("%s ", ctime(&meta.st_mtime)); // bad date format
// I don't want the file name moving down.
printf("%s \n", words[i]); // File name
}
}
// Closing and freeing
closedir(d);
for (int a = 0; a < 1000; a)
{
free(words[a]);
}
free(words);
return 0;
}
}
目前我的代碼給出了這個輸出。
root root 435 Tue Oct 5 23:52:59 2021
find.c
root root 4562 Fri Oct 8 15:31:23 2021
ls.c
root root 58 Wed Oct 6 00:14:25 2021
Makefile
root root 296 Wed Oct 6 00:04:37 2021
tree.c
我的問題是它應該是這種格式。-rwxrw-r-- tim users 1252 Sep 28 14:00 find.c
第一個“-”應該是檔案(-)或目錄(d)的指示符,然后是用戶、組和其他人的檢查、讀取、寫入、執行權限,然后是用戶名(或用戶 ID)、組名(或組 ID),然后是檔案大小,然后是修改時間,然后是檔案名。
- 我不知道如何檢查某個東西是檔案還是目錄。
- 我試過查找讀取權限的方法,但都沒有奏效。
- 我的輸出中的日期格式與我試圖獲得的輸出不匹配,我不知道如何修復它。
- 使用我目前使用的日期格式,檔案名被放在一行中,我不想要這樣。
誰能幫我解決這個問題?
uj5u.com熱心網友回復:
檔案名之前的ctime()換行符是因為回傳一個以換行符結尾的字串。所以你應該洗掉它。
char *time = ctime(&meta.st_mtime);
time[strlen(time)-1] = '\0'; // remove newline
printf("%s ", time);
要確定檔案型別,請參閱檢查檔案是目錄還是檔案
要將權限轉換為符號形式,請參閱在 C 中使用 stat(2) 列印檔案權限,如“ls -l”
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318461.html
上一篇:C-字陣列不斷被覆寫
下一篇:讀取檔案并放入多維陣列時出現垃圾
