简体   繁体   English

在执行 mv 命令和从 argv 复制字符串时防止错误

[英]preventing errors when implementing the mv command and copying strings from argv

I implemented the move command from linux. I did not try to prevent errors.我从 linux 执行了移动命令。我没有尝试防止错误。 The program would check if the source file named in argv [1] exists and can be opened, how can I do that?该程序将检查argv [1]中命名的源文件是否存在并且可以打开,我该怎么做? I also think it's a problem copying strings from argv , because I need to call malloc and free which also use system calls and thus affect the performance of the program (if I'm not mistaken)我还认为从argv复制字符串是个问题,因为我需要调用mallocfree ,它们也使用系统调用,从而影响程序的性能(如果我没记错的话)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>

int main(int argc, char **argv)
{

  char *source, *destination, *new_source, *new_destination;
  char *current_directory;
  if (argc != 3) {
    printf("usage- %s source destination\n", *argv);
    exit(EXIT_FAILURE);
  }
  // work on copy
  source = (char*)malloc(strlen(argv[1]) + 1);
  strcpy(source, argv[1]);
  destination = (char*)malloc(strlen(argv[2]) + 1);
  strcpy(destination, argv[2]);


            
            current_directory = getenv("PWD");
            new_source = (char*)malloc(strlen(source) + 1 + strlen(current_directory) + 1);
            strcpy(new_source,current_directory);
            strcat(new_source,"/");
            strcat(new_source,source);

  new_destination = (char*)malloc(strlen(destination) + 1 + strlen(current_directory) + 1 + strlen(source) + 1);
  strcpy(new_destination,current_directory);
  strcat(new_destination,"/");
  strcat(new_destination,destination);
  strcat(new_destination,"/");
  strcat(new_destination,source);
  
  /*execute systemcall*/
  if(rename(new_source,new_destination) != 0){
    fprintf(stderr,"error: %s\n",strerror(errno));
  }

  free(new_source);
  free(new_destination);
  free(source);
  free(destination);

  exit(EXIT_SUCCESS)

The use-cases i see:我看到的用例:

  • src ||来源 || target is a directory目标是一个目录
  • src ||来源 || target is a symlink目标是符号链接
  • src ||来源 || target are not accessible (bad rights)目标不可访问(不良权利)

Take a look at this snippet:看看这个片段:

int main(int ac, char **av) {
    if (ac < 3)
        return (1); // bad arguments
    int sfd, tfd = -1;
    if ((sfd = open(av[1], O_RDONLY | O_SYMLINK)) < 0 || (tfd = open(av[2], O_WRONLY | O_CREAT | O_TRUNC | O_SYMLINK, 0666)) < 0)
        return (2); // opens fail
    struct stat sstat, tstat;
    if (stat(av[1], &sstat) || S_ISDIR(sstat.st_mode) || !stat(av[2], &tstat) && S_ISDIR(tstat.st_mode))
        return (3); // no source or source is a directory or target is a directory
    char *sdata = malloc(sstat.st_size);
    if (read(sfd, sdata, sstat.st_size) < 0)
        return (4); // read fail
    if (write(tfd, sdata, sstat.st_size) < 0)
        return (5); // write fail
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM