The provided C program implements a simple command-line utility for...
The provided C program implements a simple command-line utility for managing text documents. Here's what each part of the program does:
Overview:
The program uses system calls to execute operations like creating, listing, reading, and deleting text documents in a specific directory named TEST/
. It repetitively interacts with the user through a menu-driven interface until the user chooses to exit.
Key Features and Behavior:
-
Initialization (
int main()
):- The program ensures the directory
TEST/
exists by using themkdir
function with permissions0777
. - Presents a persistent menu to the user with options to manage documents.
- The program ensures the directory
-
Functions:
-
creaDocumento()
:- Prompts the user to provide a document name and its content.
- Executes a system command to:
- Create the file within the
TEST/
directory. - Write the provided content into the file.
- Example command:
mkdir -p TEST/ && echo 'content' > TEST/document_name.txt
.
- Create the file within the
- Displays confirmation that the document has been created.
-
listaDocumenti()
:- Lists all files stored in the
TEST/
directory. - Executes a system command:
ls -1 TEST/
, showing one file per line. - Displays the list of files to the user.
- Lists all files stored in the
-
leggiDocumento()
:- Prompts the user to enter the name of a document they want to read (file name without
.txt
). - Executes a system command to display the content of the specified file using
cat
.- Example command:
cat TEST/document_name.txt
.
- Example command:
- Displays the content if the file exists.
- Prompts the user to enter the name of a document they want to read (file name without
-
eliminaDocumento()
:- Prompts the user to enter the name of a document they want to delete.
- Executes a system command to remove the specified file using
rm
.- Example command:
rm -f TEST/document_name.txt
.
- Example command:
- Displays a message confirming the deletion of the document if it existed.
-
-
Menu (
do-while
loop):- Displays the following options on a loop:
- Create a document.
- List documents.
- Read a document.
- Delete a document.
- Exit the program.
- Based on the user's input (
scelta
), calls the corresponding function. - If an invalid option is entered, notifies the user before redisplaying the menu.
- Displays the following options on a loop:
Notes on Behavior:
- Directory Handling: The program creates the directory
TEST/
at the start and saves all files there. It assumes this directory is writable. - Input Handling: User input is taken using
fgets
for strings andscanf
for integers. The use ofstrcspn
helps remove trailing newline characters thatfgets
may introduce. - System Commands: The program heavily relies on
system()
calls, making it simple but exposing it to potential risks like shell injection if inputs aren't carefully sanitized. - Permissions: The program assumes the user has appropriate permissions to perform file operations in the current working directory.
Example Interaction:
-
Menu Prompt:
Gestore Documenti 1) Crea Documento 2) Lista Documento 3) Leggi Documento 4) Elimina Documento 5) Finisci Gestione Documenti Scelta:
-
Creating a Document (Option 1):
- User enters document name:
doc1
- User enters content:
Hello, World!
- The program creates
TEST/doc1.txt
containing the textHello, World!
.
- User enters document name:
-
Listing Documents (Option 2):
- The program displays:
Lista documenti: doc1.txt
- The program displays:
-
Reading a Document (Option 3):
- User specifies the document
doc1
. - The program displays:
Contenuto documento: Hello, World!
- User specifies the document
-
Deleting a Document (Option 4):
- User specifies the document
doc1
. - The program removes
TEST/doc1.txt
and confirms deletion.
- User specifies the document
-
Exiting (Option 5):
- The program terminates.
Risks & Potential Issues:
-
Security Risk:
- Using
system()
with user input directly introduces vulnerabilities (e.g., shell injection). - Inputs like
name; rm -rf /
or$(rm -rf /)
could execute unintended destructive commands. - Solution: Avoid
system()
calls; use dedicated file I/O operations (fopen
,fwrite
,fread
, etc.) for safer functionality.
- Using
-
Error Handling:
- The program doesn't check if the entered documents exist before reading or deleting.
- It should verify the success of file operations to avoid errors or misleading confirmations.
-
Cross-Platform Compatibility:
- System commands (
mkdir
,ls
,cat
,rm
) may not work on non-Unix-based operating systems (e.g., Windows without additional tools).
- System commands (
Summary:
The program is a basic document management system leveraging system commands to create, list, read, and delete text files in a dedicated directory (TEST/
). While it demonstrates important programming concepts, it contains vulnerabilities and could be improved by using safer alternatives to system calls.