Created
January 13, 2022 11:07
-
-
Save dipankardas011/bb03cf132411d533e4982a34aa706b14 to your computer and use it in GitHub Desktop.
Linux system utility commands in C programming language E.g. cp, mv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* @author Dipankar Das | |
* cc -o util copy.c | |
* | |
* ./util cp <source> <destination> | |
* | |
* ./util mv <source> <destination> | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
void __mv__(FILE* dest, FILE* src) { | |
while (!feof(src)) { | |
char ch = fgetc(src); | |
fputc(ch, dest); | |
} | |
} | |
void __cp__(FILE* dest, FILE* src) { | |
while (!feof(src)) { | |
char ch = fgetc(src); | |
fputc(ch, dest); | |
} | |
} | |
int main(int argc, char* argv[]) | |
{ | |
FILE* src = NULL; | |
FILE* dest = NULL; | |
src = fopen(argv[2], "r"); | |
dest = fopen(argv[3], "w"); | |
if (strcmp(argv[1], "cp") == 0) | |
{ | |
printf("copying\n"); | |
__cp__(dest, src); | |
} | |
else if (strcmp(argv[1], "mv") == 0) | |
{ | |
__mv__(dest, src); | |
printf("moving\n"); | |
// system("rm %s", argv[1]); | |
char ss[512]; | |
memset(ss, '\0', sizeof(512)); | |
sprintf(ss, "rm %s", argv[2]); | |
system(ss); | |
} | |
else | |
{ | |
system("echo '❌'"); | |
} | |
fclose(src); | |
fclose(dest); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment