기출 creat system call function

agnusdei·2025년 8월 9일

CTF

목록 보기
63/185

0. Problem

Explain the creat system call function.


1. Introduction

The creat system call is a traditional Unix system call used to create a new file or rewrite an existing file. Originating in the early Unix operating systems in the 1970s, its primary purpose is to simplify the creation of files by opening them in write-only mode and truncating existing files to zero length.


2. Main Body

2.1 Definition and Prototype

The creat function has the following prototype in C:

int creat(const char *pathname, mode_t mode);
  • pathname: Path to the file to be created.
  • mode: File permission bits (e.g., 0644), which specify the access permissions for the newly created file.
  • Return value: On success, returns a non-negative file descriptor; on failure, returns -1 and sets errno.

2.2 Operation and Mechanism

  • If the file specified by pathname does not exist, creat creates it with the specified permissions.
  • If the file exists, it truncates the file size to zero (clearing all content).
  • The file is opened in write-only mode.
  • Internally, creat is equivalent to calling:
open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);

Here,

  • O_CREAT: create the file if it does not exist,
  • O_WRONLY: open for write only,
  • O_TRUNC: truncate the file to zero length if it exists.

2.3 Features, Advantages, and Disadvantages

FeatureAdvantagesDisadvantages
Simple file creation & truncationConvenient for quick file creation & clearingOnly supports write-only mode
Permission setting via modeCan specify file permission bitsLimited flexibility compared to open
Legacy Unix system callEasy to use and widely supportedDeprecated in favor of open syscall
  • The open() syscall is more versatile and recommended for modern applications because it allows for multiple flags (read/write, append, sync, etc.).
  • creat can be viewed as a shorthand for a specific use of open.

2.5 Practical Use in Real-World

  • Often used in simple scripts or programs that only need to create or overwrite a file quickly.
  • Not recommended for applications requiring complex file access modes or advanced permission handling.
  • Programs needing both reading and writing should prefer open.
  • Modern POSIX standards and Linux programming discourage direct use of creat.
  • Most developers now use open() with appropriate flags for better clarity and control.

3. Conclusion

The creat system call is a legacy Unix function that creates or truncates a file, opening it in write-only mode. While it simplifies file creation, it lacks flexibility compared to the more general open system call. Contemporary software development prefers open for file creation due to its enhanced options and clearer semantics.


Child-friendly Summary

creat is like a simple tool in the computer that makes a new empty file or wipes clean an old one so you can write fresh stuff. But nowadays, people mostly use a better tool called open that can do many more things.


Summary Table

ItemDescription
Function Namecreat
PurposeCreate a new file or truncate existing one
Prototypeint creat(const char *pathname, mode_t mode)
Internal Equivalent`open(pathname, O_CREATO_WRONLYO_TRUNC, mode)`
ReturnsFile descriptor (>=0) on success, -1 on error
PermissionsYes, via mode parameter
AdvantagesSimple file creation & truncation
DisadvantagesOnly write-only mode, less flexible
Modern UsageGenerally replaced by open syscall
profile
DevSecOps, Pentest, Cloud(OpenStack), Develop, Data Engineering, AI-Agent

0개의 댓글