Вы, вероятно, хотите, чтобы пропустить определенные строки, те, которые соответствуют модели. Это действительно лучше сделать с языком, который хорошо выполняет регулярные выражения (например: awk, perl, ruby, python и др.).
Вы можете прочитать каждую строку, но совпадение и пропустить определенные строки, которые легко обнаруживают шаблоны, такие как строки, начинающиеся с «total», пустых строк и «.». а также ".."Специальные каталоги,
linebuffer[1024];
while(fgets(linbuffer,sizeof(linebuffer),stdin)) {
if(strlen(linebuffer) < 2) continue; //short lines
if(strncmp(linebuffer,"total",strlen("total")==0) continue; //line begins with "total"
//do you want to skip ".", ".."?
if(strcmp(linebuffer+strlen(linebuffer)-2,".\n")==0) continue; //skip ".", ".."
printf("%s",linebuffer); //or use puts
}
Другой подход, чтобы открыть каталог, читать и декодировать информацию о файле (см стат (2)), то это будет показывать„общий“линии довольно ловко,
int res;
struct stat statbuf;
char pathname[1024];
DIR* dir;
struct dirent *dp=NULL;
char* fname;
if(!(dir=opendir(dname))) {
int ec=errno;
printf("error:-1:cannot opendir %s\n",dname,ec);
return(-1);
}
while ((dp = readdir(dir)) != NULL) {
//printf("file: %s\n",dp->d_name); fflush(stdout);
if(strcmp(dp->d_name,".")==0) continue;
if(strcmp(dp->d_name,"..")==0) continue;
fname=dp->d_name;
//call stat(2) here on the file,
sprintf(pathname,"%s/%s",dname,dp->d_name);
if((res=stat(dname, &statbuf)) == -1) {
int ec=errno;
printf("error:-1:cannot stat %s (%d)\n",pathname,ec);
return(-1);
}
//printf("dirwatch:check (%s)\n",pathname);
print_fileinfo(&statbuf,dp->d_name); //left as an exercise for the poster
}
closedir(dir);
Вам нужно будет декодировать и распечатать стата (2) информацию,
int
print_fileinfo(stat* statbuf, char* name)
{
//decode rwx bits for owner, group, and other
//lookup owner and group names
//file size is part of statbuf
//file type (regular, directory, et al) is part of statbuf
}
Вам нужно будет смотреть на стат (2) человек страницы, но вот некоторые полезные значения, макросы,
All of these system calls return a stat structure, which contains the following fields:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
The following POSIX macros are defined to check the file type using the st_mode field:
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
The following flags are defined for the st_mode field:
S_IFMT 0170000 bit mask for the file type bit fields
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 0004000 set UID bit
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 mask for permissions for others (not in group)
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
Не прямой ответ, но вы попробовали 'ls -l | хвост -n + 2'? – SukkoPera
Каков ваш ожидаемый результат? вы werent, что ясно – smushi
@SukkoPera 'sed '1d'' делает то же самое. Однако в этом случае я проголосовал бы за 'tail' – starrify