NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>

#define BUF_SIZE 4096 /* use a buffer size of 4096 bytes */
#define OUTPUT_MODE 0700 /* protection bits for output file */
#define MAX_PATH_LENGTH 4096 /* maximum length of file path */
#define MAX_CMD_LENGTH 10240 /* maximum length of command */
#define MAX_CMD_ELEMENTS 3 /* maximum number of elements in a command */

/*
* Parse command that user enter
* [IN] command: command string, including name and arguments
* [OUT] arg: array of arguments. The first elements of this array is the name of command
* [IN] max: maximum number of element in arg[] array
* Return: number of arguments in the command
*/
int parseCommand(char *command, char *arg[], int max){
char *tmp;
tmp = strtok(command," ");
int count = 0;

while(tmp != NULL){
strncpy(arg[count], tmp, 4096);
count++;
tmp = strtok(NULL, " ");
if (count == max) break;
}

return count;
}

/*
* Copy file
* [IN] source: Pointer to the string containing the path of source file
* [IN] dest: Pointer to the string containing the path of destination file
* Return: 0 if successful, otherwise return -1
*/
int copyFile(char *source, char *dest){
int in_fd, out_fd, rd_count, wt_count;
char buffer[BUF_SIZE];

in_fd=open(source, O_RDONLY); /* open the source file */
if (in_fd < 0) return errno; /* if it cannot be opened, exit */

out_fd=creat(dest, OUTPUT_MODE); /* create the destination file */
if (out_fd < 0) return errno; /* if it cannot be created, exit */

/* Copy loop */
while (1) {
rd_count=read(in_fd, buffer, BUF_SIZE); /* read a block if data */
if (rd_count <= 0) break; /* if end of file or error, exit loop */

wt_count = write(out_fd, buffer, rd_count); /* write data */
if (wt_count <= 0) return errno; /* wt_count<=0 is an error */
}

/* Close the files */
close(in_fd);
close(out_fd);

if (rd_count == 0) /* no error on last read */
return 0;
else
return errno; /* error on last read */
}

/*
* List the name of files and sub-directory in a directory
* [IN] dirname: Pointer to the string containing the name of directory
* Return: 0 if successful, otherwise return -1
*/
int listDir(char *dirname){
DIR *dir;
struct dirent *subdir;

dir = opendir(dirname);

if(dir == NULL){
printf("%sn", strerror(errno));
return -1;
} else {
while ((subdir = readdir(dir)) != NULL)
if(strcmp(subdir->d_name,".") != 0 && strcmp(subdir->d_name,"..") != 0)
printf("%sn", subdir->d_name);
closedir(dir);
return 0;
}
}

/*
* Make a directory
* [IN] newDir: Pointer to the string containing the name of directory
* Return: 0 if successful, otherwise return -1
*/
int makeDir(char *newDir) {
int result = mkdir (newDir,0777);
if (result == 0){
return 0;
}
else {
return errno;
}

}


/*
* Move file
*[IN] source: Pointer to the string containing the path of source file
* [IN] dest: Pointer to the string containing the path of destination file
* Return: 0 if successful, otherwise return -1
*/
int mvFile(char *source, char *dest){
int result = rename(source, dest);

if (result == 0){
return 0;
} else {
return errno;
}

}
/*//dong file
close(in_fd);
close(out_fd);
//Xóa file nguon
if (!remove(source)) return 0;
else return errno;
}*/

int main ()
{
char cmd[MAX_CMD_LENGTH];
int numOfArg, ret;
char *args[MAX_CMD_ELEMENTS];

for(int i = 0; i < MAX_CMD_ELEMENTS; i++)
args[i] = malloc(MAX_PATH_LENGTH);

printf("Enter command: ");
fgets(cmd, MAX_CMD_LENGTH, stdin);
cmd[strlen(cmd)-1] = 0;

numOfArg = parseCommand(cmd, args, 3);

if (numOfArg >= 2){
if(strcmp(args[0],"ls") == 0){

// TO DO
int ret = listDir(args[1]);
if (ret != 0) {
printf("Loi trong qua trinh liet ke thu muc: %sn",
strerror(ret));
return 1;
}


} else if(strcmp(args[0],"mkdir") == 0){

//TO DO
ret = makeDir(args[1]);
if (ret != 0) {
printf("Lỗi trong quá trình sao chép file: %sn",
strerror(ret));
return 1;}
} else if(strcmp(args[0],"cp") == 0){
if(numOfArg >= 3){
// TO DO
ret = copyFile(args[1], args[2]);
if (ret != 0) {
printf("Lỗi trong quá trình sao chép file: %sn",
strerror(ret));
return 1;
}


} else
printf ("%s: missing destination file operand after %sn", args[0], args[1]);
} else if(strcmp(args[0],"mv") == 0){
//TO DO
if (numOfArg >= 3){
ret = mvFile (args[1], args[2]);
if (ret != 0) {
printf ("%sn", strerror(ret));
}
} else {
printf ("%s: missing destination file operand after %sn", args[0], args[1]);
}
}
else {
printf ("%s: command not foundn", args[1]);
}
} else
printf("%s: missing argumentsn", cmd);

for(int i = 0; i<3; i++)
free(args[i]);

return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.