implementing "ls"

Cosh·2024년 4월 23일

Introduction

what is ls?

ls is program/instruction for linux which prints file list in directory or attributes(property) of each file.

Implementation

it prints each information respectively

  1. filemode
  2. nlink (link count)
  3. owner name (uid in stat, should be converted to char *)
  4. group name (gid in stat, should be converted to char *)
  5. file size
  6. modified time
  7. file name

Required function / system calls

stat() : get metadata with file name.
getpwuid() : get struct passwd data with uid(integer).
getpwgid() : get struct passwd data with gid(integer).
ctime() : convert modified time with unix_time to timestamp.

Additional macro function
S_ISDIR() : check if it's directory. if true, modeStr[0] is 'd'.
S_ISCHR() : check if it's character device file. if true, modeStr[0] is 'c'.
S_ISBLK() : check if it's blcok device file. if true, modeStr[0] is 'b'.

flow (Roughly)

  1. get argument in reverse direction (the last argument is read first)
  2. if it can be opened with opendir, then open.
  3. get stat
  4. with stat data, make modeStr and uid, gid name
  5. print respectively.

Additional Constant

Constant for file mode
1. owner : S_I [R/W/X] USR
1. group : S_I [R/W/X] GRP
1. others : S_I [R/W/X] OTH

profile
개발자지망생

0개의 댓글