Created
March 23, 2014 13:02
-
-
Save yuikns/9722774 to your computer and use it in GitHub Desktop.
file manager
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> // for system xxx | |
#include <string.h> | |
#include <sys/stat.h> // for int mkdir(const char *path, mode_t mode); | |
#define MATCH(a,b) (strcmp(a,b) == 0) | |
void fdel(const char * name); | |
void fadd(const char * name); | |
void frname(const char * dest , const char * src); | |
int dadd(const char * name); | |
void fdel(const char * name) | |
{ | |
unlink(name); | |
} | |
//EACCES Search permission is denied on a component of the path prefix, or write permission is denied on the parent directory of the directory to be created. | |
//EEXIST The named file exists. | |
//ELOOP A loop exists in symbolic links encountered during resolution of the path argument. | |
//EMLINK The link count of the parent directory would exceed {LINK_MAX}. | |
//ENAMETOOLONG The length of the path argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}. | |
//ENOENT A component of the path prefix specified by path does not name an existing directory or path is an empty string. | |
//ENOSPC The file system does not contain enough space to hold the contents of the new directory or to extend the parent directory of the new directory. | |
//ENOTDIR A component of the path prefix is not a directory. | |
//EROFS The parent directory resides on a read-only file system. | |
//The mkdir() function may fail if: | |
//ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the path argument. | |
//ENAMETOOLONG As a result of encountering a symbolic link in resolution of the path argument, the length of the substituted pathname string exceeded {PATH_MAX}. | |
int dadd(const char * name) | |
{ | |
int nameLen = strlen(name); | |
if(nameLen == 0 ) return; | |
char * dir = (char * )malloc(sizeof(char) * (nameLen+1)); | |
memcpy(dir,name,sizeof(char) * (nameLen+1)); | |
int i = nameLen - 1; | |
for(;i > 0 ; i --) | |
{ | |
if(dir[i] == '/' || dir[i] == '\\') | |
{ | |
dir[i] = '\0'; | |
int status = dadd(dir); | |
if(status != 0) | |
{ | |
fprintf(stderr,"ERROR : %d",status); | |
} | |
break; | |
} | |
} | |
free(dir); | |
printf("add :[%s]\n",name); | |
return mkdir(name,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); | |
} | |
void fadd(const char * name) | |
{ | |
int nameLen = strlen(name); | |
if(nameLen == 0 ) return; | |
char * dir = (char * )malloc(sizeof(char) * (nameLen+1)); | |
memcpy(dir,name,sizeof(char) * (nameLen+1)); | |
int i = nameLen - 1; | |
for(;i > 0 ; i --) | |
{ | |
if(dir[i] == '/' || dir[i] == '\\') | |
{ | |
dir[i] = '\0'; | |
int status = dadd(dir); | |
if(status != 0) | |
{ | |
fprintf(stderr,"ERROR : %d",status); | |
} | |
break; | |
} | |
} | |
free(dir); | |
FILE *fp = fopen(name,"r"); | |
if(fp == NULL) | |
{ | |
fp = fopen(name,"w"); | |
} | |
if(fp != NULL) fclose(fp); | |
} | |
void frname(const char * dest , const char * src) | |
{ | |
rename(src,dest); | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
void remove_file() | |
{ | |
fdel("x/dir/abc.txt"); | |
fdel("x/dir/def.txt"); | |
} | |
void touch_file() | |
{ | |
fadd("x/dir/abc.txt"); | |
} | |
void do_rename() | |
{ | |
frname("x/dir/def.txt","x/dir/abc.txt"); | |
} | |
int main(int argc ,char *argv[]) | |
{ | |
int i; | |
if(argc == 1) | |
{ | |
touch_file(); | |
do_rename(); | |
remove_file(); | |
}else | |
{ | |
for(i=0;i<argc;i++) | |
{ | |
if(MATCH(argv[i],"remove")) remove_file(); | |
if(MATCH(argv[i],"touch")) touch_file(); | |
if(MATCH(argv[i],"rename")) do_rename(); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment