try to finish find
This commit is contained in:
7
Makefile
7
Makefile
@ -86,7 +86,7 @@ LD = $(TOOLPREFIX)ld
|
||||
OBJCOPY = $(TOOLPREFIX)objcopy
|
||||
OBJDUMP = $(TOOLPREFIX)objdump
|
||||
|
||||
CFLAGS = -Wall -Werror -O2 -fno-omit-frame-pointer -ggdb -gdwarf-2
|
||||
CFLAGS = -Wall -Werror -Ofast -fno-omit-frame-pointer -ggdb -gdwarf-2
|
||||
|
||||
ifdef LAB
|
||||
LABUPPER = $(shell echo $(LAB) | tr a-z A-Z)
|
||||
@ -191,6 +191,7 @@ UPROGS=\
|
||||
$U/_sleep\
|
||||
$U/_pingpong\
|
||||
$U/_primes\
|
||||
$U/_find\
|
||||
|
||||
|
||||
|
||||
@ -227,10 +228,10 @@ $U/_uthread: $U/uthread.o $U/uthread_switch.o $(ULIB)
|
||||
$(OBJDUMP) -S $U/_uthread > $U/uthread.asm
|
||||
|
||||
ph: notxv6/ph.c
|
||||
gcc -o ph -g -O2 $(XCFLAGS) notxv6/ph.c -pthread
|
||||
gcc -o ph -g -Ofast $(XCFLAGS) notxv6/ph.c -pthread
|
||||
|
||||
barrier: notxv6/barrier.c
|
||||
gcc -o barrier -g -O2 $(XCFLAGS) notxv6/barrier.c -pthread
|
||||
gcc -o barrier -g -Ofast $(XCFLAGS) notxv6/barrier.c -pthread
|
||||
endif
|
||||
|
||||
ifeq ($(LAB),pgtbl)
|
||||
|
||||
43
kernel/fs.h
43
kernel/fs.h
@ -1,9 +1,9 @@
|
||||
// On-disk file system format.
|
||||
// Both the kernel and user programs use this header file.
|
||||
#include "kernel/types.h"
|
||||
|
||||
|
||||
#define ROOTINO 1 // root i-number
|
||||
#define BSIZE 1024 // block size
|
||||
#define ROOTINO 1 // root i-number
|
||||
#define BSIZE 1024 // block size
|
||||
|
||||
// Disk layout:
|
||||
// [ boot block | super block | log | inode blocks |
|
||||
@ -12,14 +12,14 @@
|
||||
// mkfs computes the super block and builds an initial file system. The
|
||||
// super block describes the disk layout:
|
||||
struct superblock {
|
||||
uint magic; // Must be FSMAGIC
|
||||
uint size; // Size of file system image (blocks)
|
||||
uint nblocks; // Number of data blocks
|
||||
uint ninodes; // Number of inodes.
|
||||
uint nlog; // Number of log blocks
|
||||
uint logstart; // Block number of first log block
|
||||
uint inodestart; // Block number of first inode block
|
||||
uint bmapstart; // Block number of first free map block
|
||||
uint magic; // Must be FSMAGIC
|
||||
uint size; // Size of file system image (blocks)
|
||||
uint nblocks; // Number of data blocks
|
||||
uint ninodes; // Number of inodes.
|
||||
uint nlog; // Number of log blocks
|
||||
uint logstart; // Block number of first log block
|
||||
uint inodestart; // Block number of first inode block
|
||||
uint bmapstart; // Block number of first free map block
|
||||
};
|
||||
|
||||
#define FSMAGIC 0x10203040
|
||||
@ -30,25 +30,25 @@ struct superblock {
|
||||
|
||||
// On-disk inode structure
|
||||
struct dinode {
|
||||
short type; // File type
|
||||
short major; // Major device number (T_DEVICE only)
|
||||
short minor; // Minor device number (T_DEVICE only)
|
||||
short nlink; // Number of links to inode in file system
|
||||
uint size; // Size of file (bytes)
|
||||
uint addrs[NDIRECT+1]; // Data block addresses
|
||||
short type; // File type
|
||||
short major; // Major device number (T_DEVICE only)
|
||||
short minor; // Minor device number (T_DEVICE only)
|
||||
short nlink; // Number of links to inode in file system
|
||||
uint size; // Size of file (bytes)
|
||||
uint addrs[NDIRECT + 1]; // Data block addresses
|
||||
};
|
||||
|
||||
// Inodes per block.
|
||||
#define IPB (BSIZE / sizeof(struct dinode))
|
||||
#define IPB (BSIZE / sizeof(struct dinode))
|
||||
|
||||
// Block containing inode i
|
||||
#define IBLOCK(i, sb) ((i) / IPB + sb.inodestart)
|
||||
#define IBLOCK(i, sb) ((i) / IPB + sb.inodestart)
|
||||
|
||||
// Bitmap bits per block
|
||||
#define BPB (BSIZE*8)
|
||||
#define BPB (BSIZE * 8)
|
||||
|
||||
// Block of free map containing bit for block b
|
||||
#define BBLOCK(b, sb) ((b)/BPB + sb.bmapstart)
|
||||
#define BBLOCK(b, sb) ((b) / BPB + sb.bmapstart)
|
||||
|
||||
// Directory is a file containing a sequence of dirent structures.
|
||||
#define DIRSIZ 14
|
||||
@ -57,4 +57,3 @@ struct dirent {
|
||||
ushort inum;
|
||||
char name[DIRSIZ];
|
||||
};
|
||||
|
||||
|
||||
12
user/find.c
Normal file
12
user/find.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include "kernel/fcntl.h"
|
||||
#include "kernel/fs.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "kernel/types.h"
|
||||
#include "user/user.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 3) {
|
||||
printf("Usage: find path pattern\n");
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
48
user/ls.c
48
user/ls.c
@ -1,67 +1,63 @@
|
||||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
#include "kernel/fs.h"
|
||||
#include "kernel/fcntl.h"
|
||||
#include "kernel/fs.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "kernel/types.h"
|
||||
#include "user/user.h"
|
||||
|
||||
char*
|
||||
fmtname(char *path)
|
||||
{
|
||||
static char buf[DIRSIZ+1];
|
||||
char *fmtname(char *path) {
|
||||
static char buf[DIRSIZ + 1];
|
||||
char *p;
|
||||
|
||||
// Find first character after last slash.
|
||||
for(p=path+strlen(path); p >= path && *p != '/'; p--)
|
||||
for (p = path + strlen(path); p >= path && *p != '/'; p--)
|
||||
;
|
||||
p++;
|
||||
|
||||
// Return blank-padded name.
|
||||
if(strlen(p) >= DIRSIZ)
|
||||
if (strlen(p) >= DIRSIZ)
|
||||
return p;
|
||||
memmove(buf, p, strlen(p));
|
||||
memset(buf+strlen(p), ' ', DIRSIZ-strlen(p));
|
||||
memset(buf + strlen(p), ' ', DIRSIZ - strlen(p));
|
||||
return buf;
|
||||
}
|
||||
|
||||
void
|
||||
ls(char *path)
|
||||
{
|
||||
void ls(char *path) {
|
||||
char buf[512], *p;
|
||||
int fd;
|
||||
struct dirent de;
|
||||
struct stat st;
|
||||
|
||||
if((fd = open(path, O_RDONLY)) < 0){
|
||||
if ((fd = open(path, O_RDONLY)) < 0) {
|
||||
fprintf(2, "ls: cannot open %s\n", path);
|
||||
return;
|
||||
}
|
||||
|
||||
if(fstat(fd, &st) < 0){
|
||||
if (fstat(fd, &st) < 0) {
|
||||
fprintf(2, "ls: cannot stat %s\n", path);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
switch(st.type){
|
||||
switch (st.type) {
|
||||
case T_DEVICE:
|
||||
case T_FILE:
|
||||
printf("%s %d %d %l\n", fmtname(path), st.type, st.ino, st.size);
|
||||
break;
|
||||
|
||||
case T_DIR:
|
||||
if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
|
||||
if (strlen(path) + 1 + DIRSIZ + 1 > sizeof buf) {
|
||||
printf("ls: path too long\n");
|
||||
break;
|
||||
}
|
||||
strcpy(buf, path);
|
||||
p = buf+strlen(buf);
|
||||
p = buf + strlen(buf);
|
||||
*p++ = '/';
|
||||
while(read(fd, &de, sizeof(de)) == sizeof(de)){
|
||||
if(de.inum == 0)
|
||||
while (read(fd, &de, sizeof(de)) == sizeof(de)) {
|
||||
if (de.inum == 0)
|
||||
continue;
|
||||
memmove(p, de.name, DIRSIZ);
|
||||
p[DIRSIZ] = 0;
|
||||
if(stat(buf, &st) < 0){
|
||||
if (stat(buf, &st) < 0) {
|
||||
printf("ls: cannot stat %s\n", buf);
|
||||
continue;
|
||||
}
|
||||
@ -72,16 +68,14 @@ ls(char *path)
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
int i;
|
||||
|
||||
if(argc < 2){
|
||||
if (argc < 2) {
|
||||
ls(".");
|
||||
exit(0);
|
||||
}
|
||||
for(i=1; i<argc; i++)
|
||||
for (i = 1; i < argc; i++)
|
||||
ls(argv[i]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user