Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CSEP551
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Krishna Vinnakota
CSEP551
Commits
1b789e1d
Commit
1b789e1d
authored
17 years ago
by
rsc
Browse files
Options
Downloads
Patches
Plain Diff
Remove puts in favor of printf. Allow multiple arguments to ls.
parent
8e88f9e2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
cat.c
+15
-20
15 additions, 20 deletions
cat.c
echo.c
+2
-5
2 additions, 5 deletions
echo.c
init.c
+4
-4
4 additions, 4 deletions
init.c
ls.c
+60
-60
60 additions, 60 deletions
ls.c
sh.c
+1
-1
1 addition, 1 deletion
sh.c
ulib.c
+0
-6
0 additions, 6 deletions
ulib.c
user.h
+0
-1
0 additions, 1 deletion
user.h
with
82 additions
and
97 deletions
cat.c
+
15
−
20
View file @
1b789e1d
...
...
@@ -2,19 +2,17 @@
#include
"stat.h"
#include
"user.h"
char
buf
[
51
3
];
char
buf
[
51
2
];
void
rfile
(
int
fd
)
{
int
cc
;
int
n
;
while
((
cc
=
read
(
fd
,
buf
,
sizeof
(
buf
)
-
1
))
>
0
){
buf
[
cc
]
=
'\0'
;
puts
(
buf
);
}
if
(
cc
<
0
){
puts
(
"cat: read error
\n
"
);
while
((
n
=
read
(
fd
,
buf
,
sizeof
(
buf
)))
>
0
)
write
(
1
,
buf
,
n
);
if
(
n
<
0
){
printf
(
1
,
"cat: read error
\n
"
);
exit
();
}
}
...
...
@@ -26,19 +24,16 @@ main(int argc, char *argv[])
if
(
argc
<=
1
)
{
rfile
(
0
);
}
else
{
for
(
i
=
1
;
i
<
argc
;
i
++
){
fd
=
open
(
argv
[
i
],
0
);
if
(
fd
<
0
){
puts
(
"cat: cannot open "
);
puts
(
argv
[
i
]);
puts
(
"
\n
"
);
exit
();
}
rfile
(
fd
);
close
(
fd
);
}
exit
();
}
for
(
i
=
1
;
i
<
argc
;
i
++
){
if
((
fd
=
open
(
argv
[
i
],
0
))
<
0
){
printf
(
1
,
"cat: cannot open %s
\n
"
,
argv
[
i
]);
exit
();
}
rfile
(
fd
);
close
(
fd
);
}
exit
();
}
This diff is collapsed.
Click to expand it.
echo.c
+
2
−
5
View file @
1b789e1d
...
...
@@ -7,10 +7,7 @@ main(int argc, char *argv[])
{
int
i
;
for
(
i
=
1
;
i
<
argc
;
i
++
){
puts
(
argv
[
i
]);
puts
(
" "
);
}
puts
(
"
\n
"
);
for
(
i
=
1
;
i
<
argc
;
i
++
)
printf
(
1
,
"%s%s"
,
argv
[
i
],
i
+
1
<
argc
?
" "
:
"
\n
"
);
exit
();
}
This diff is collapsed.
Click to expand it.
init.c
+
4
−
4
View file @
1b789e1d
...
...
@@ -21,18 +21,18 @@ main(void)
dup
(
0
);
// stderr
for
(;;){
p
uts
(
"init: starting sh
\n
"
);
p
rintf
(
1
,
"init: starting sh
\n
"
);
pid
=
fork
();
if
(
pid
<
0
){
p
uts
(
"init: fork failed
\n
"
);
p
rintf
(
1
,
"init: fork failed
\n
"
);
exit
();
}
if
(
pid
==
0
){
exec
(
"sh"
,
sh_args
);
p
uts
(
"init: exec sh failed
\n
"
);
p
rintf
(
1
,
"init: exec sh failed
\n
"
);
exit
();
}
while
((
wpid
=
wait
())
>=
0
&&
wpid
!=
pid
)
p
uts
(
"zombie!
\n
"
);
p
rintf
(
1
,
"zombie!
\n
"
);
}
}
This diff is collapsed.
Click to expand it.
ls.c
+
60
−
60
View file @
1b789e1d
...
...
@@ -3,83 +3,83 @@
#include
"user.h"
#include
"fs.h"
void
p
name
(
char
*
n
)
char
*
fmt
name
(
char
*
path
)
{
int
i
;
for
(
i
=
0
;
(
i
<
DIRSIZ
)
&&
(
n
[
i
]
!=
'\0'
)
;
i
++
)
{
printf
(
1
,
"%c"
,
n
[
i
]);
}
for
(;
i
<
DIRSIZ
;
i
++
)
printf
(
1
,
" "
);
static
char
buf
[
DIRSIZ
+
1
];
char
*
p
;
// Find first character after last slash.
for
(
p
=
path
+
strlen
(
path
);
p
>=
path
&&
*
p
!=
'/'
;
p
--
)
;
p
++
;
// Return blank-padded name.
if
(
strlen
(
p
)
>=
DIRSIZ
)
return
p
;
memmove
(
buf
,
p
,
strlen
(
p
));
memset
(
buf
+
strlen
(
p
),
' '
,
DIRSIZ
-
strlen
(
p
));
return
buf
;
}
int
main
(
int
argc
,
char
*
argv
[]
)
void
ls
(
char
*
path
)
{
char
buf
[
512
],
*
p
;
int
fd
;
uint
off
,
sz
;
struct
dirent
de
;
struct
stat
st
;
if
(
argc
>
2
){
p
uts
(
"Usage: ls [dir]
\n
"
);
exit
()
;
if
(
(
fd
=
open
(
path
,
0
))
<
0
){
p
rintf
(
2
,
"ls: cannot open %s
\n
"
,
path
);
return
;
}
if
(
argc
==
2
)
{
fd
=
open
(
argv
[
1
],
0
);
if
(
fd
<
0
){
printf
(
2
,
"ls: cannot open %s
\n
"
,
argv
[
1
]);
exit
();
}
}
else
{
fd
=
open
(
"."
,
0
);
if
(
fd
<
0
){
printf
(
2
,
"ls: cannot open .
\n
"
);
exit
();
}
if
(
fstat
(
fd
,
&
st
)
<
0
){
printf
(
2
,
"ls: cannot stat %s
\n
"
,
path
);
close
(
fd
);
return
;
}
if
(
fstat
(
fd
,
&
st
)
<
0
)
{
printf
(
2
,
"ls: cannot stat dir
\n
"
);
exit
();
}
switch
(
st
.
type
)
{
switch
(
st
.
type
){
case
T_FILE
:
pname
(
argv
[
1
]);
printf
(
1
,
"%d %d %d
\n
"
,
st
.
type
,
st
.
ino
,
st
.
size
);
printf
(
1
,
"%s %d %d %d
\n
"
,
fmtname
(
path
),
st
.
type
,
st
.
ino
,
st
.
size
);
break
;
case
T_DIR
:
sz
=
st
.
size
;
for
(
off
=
0
;
off
<
sz
;
off
+=
sizeof
(
de
))
{
if
(
read
(
fd
,
&
de
,
sizeof
(
de
))
!=
sizeof
(
de
))
{
printf
(
1
,
"ls: read error
\n
"
);
break
;
}
if
(
de
.
inum
!=
0
)
{
p
=
buf
;
if
(
argc
==
2
)
{
strcpy
(
p
,
argv
[
1
]);
p
+=
strlen
(
p
);
if
(
*
(
p
-
1
)
!=
'/'
)
*
p
++
=
'/'
;
}
memmove
(
p
,
de
.
name
,
DIRSIZ
);
p
[
DIRSIZ
]
=
0
;
if
(
stat
(
buf
,
&
st
)
<
0
)
{
printf
(
1
,
"stat: failed %s
\n
"
,
de
.
name
);
continue
;
}
pname
(
de
.
name
);
printf
(
1
,
"%d %d %d
\n
"
,
st
.
type
,
de
.
inum
,
st
.
size
);
if
(
strlen
(
path
)
+
1
+
DIRSIZ
+
1
>
sizeof
buf
){
printf
(
1
,
"ls: path too long
\n
"
);
break
;
}
strcpy
(
buf
,
path
);
p
=
buf
+
strlen
(
buf
);
*
p
++
=
'/'
;
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
){
printf
(
1
,
"ls: cannot stat %s
\n
"
,
buf
);
continue
;
}
printf
(
1
,
"%s %d %d %d
\n
"
,
fmtname
(
buf
),
st
.
type
,
st
.
ino
,
st
.
size
);
}
break
;
}
close
(
fd
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
int
i
;
if
(
argc
<
2
){
ls
(
"."
);
exit
();
}
for
(
i
=
1
;
i
<
argc
;
i
++
)
ls
(
argv
[
i
]);
exit
();
}
This diff is collapsed.
Click to expand it.
sh.c
+
1
−
1
View file @
1b789e1d
...
...
@@ -51,7 +51,7 @@ main(void)
int
getcmd
(
char
*
buf
,
int
nbuf
)
{
p
uts
(
"$ "
);
p
rintf
(
2
,
"$ "
);
memset
(
buf
,
0
,
nbuf
);
gets
(
buf
,
nbuf
);
if
(
buf
[
0
]
==
0
)
// EOF
...
...
This diff is collapsed.
Click to expand it.
ulib.c
+
0
−
6
View file @
1b789e1d
...
...
@@ -3,12 +3,6 @@
#include
"fcntl.h"
#include
"user.h"
int
puts
(
char
*
s
)
{
return
write
(
1
,
s
,
strlen
(
s
));
}
char
*
strcpy
(
char
*
s
,
char
*
t
)
{
...
...
This diff is collapsed.
Click to expand it.
user.h
+
0
−
1
View file @
1b789e1d
...
...
@@ -21,7 +21,6 @@ char* sbrk(int);
// ulib.c
int
stat
(
char
*
,
struct
stat
*
);
int
puts
(
char
*
);
char
*
strcpy
(
char
*
,
char
*
);
void
*
memmove
(
void
*
,
void
*
,
int
);
char
*
strchr
(
const
char
*
,
char
c
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment