c - 如何在 C 中创建硬/软链接?
问题描述
我需要创建一个指向现有文件或目录的硬链接。我还需要创建一个指向现有文件或目录的软/符号链接。这是一个更大的程序的一部分,如下所示。通过键入“-f pathname linkname”来执行命令,每个中断参数都是不同的命令。我相信到目前为止已经正确创建了函数 -f、-d 和 -h。但是我无法创建这些链接。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
//Handles filename, absolute pathname
//Need relative pathname for -f
int main(int argc, char **argv)
{
char command[200], flag[20], pathname[100], linkname[100];
struct stat st = {0};
char cmd[200];
char *token; //Pointer
int counter = 1; //Counter variable
FILE *fp;
char mode2[] = "0750"; //To set the permission of a file/path
long j;
char mode[] = "0640"; //To set the permission of a file/path
long i;
printf("Enter command: ");
fgets(cmd, 420, stdin);
//User input is tokenized to determine the proper commands are entered and executed
token = strtok(cmd, " "); //Input is tokenized by white spaces.
if(token == NULL)
{
printf("Error with command input.\n");
exit(EXIT_FAILURE);
}
strcpy(command, token);
token = strtok(NULL, " ");
if(token != NULL)
strcpy(flag, token);
token = strtok(NULL, " ");
if(token != NULL)
strcpy(pathname, token);
token = strtok(NULL, " ");
if(token != NULL)
strcpy(linkname, token);
//Switch statement to determine which command the user is choosing and execute that command.
switch(flag[1]) {
//The f case will create a file who's name is chosen by the user.
case 'f':
fp=fopen(pathname,"w");
fclose(fp);
char mode[] = "0640"; //Sets the permission of file to 0640.
i = strtol(mode, 0, 8);
if (chmod(pathname, i) < 0)
{
fprintf(stderr, "%s: error in chmod(%s, %s) - %d (%s)\n",
argv[0], pathname, mode, errno, strerror(errno));
return -1;
}
return 0;
break;
//The d case will create a new directory chosen by the user.
case 'd':
if (stat(pathname, &st) == -1) {
mkdir(pathname, 0750); //Directory is given permission 0750.
}
j = strtol(mode, 0, 8);
if (chmod (pathname,j) < 0)
{
fprintf(stderr, "%s: error in chmod(%s, %s) - %d (%s)\n",
argv[0], pathname, mode, errno, strerror(errno));
return -1;
}
return 0;
break;
//The h case will create a hardlink to an existing file.
case 'h':
char *pathname; //Existing file
char *linkname; //Name of desired hardlink
int hlink; //Stores path
hlink = link(pathname, linkname); //Links linkname to pathname
if (chmod (pathname,j) < 0)
{
fprintf(stderr, "%s: error linking(%s, %s) - %d (%s)\n",
argv[0], pathname, mode, errno, strerror(errno));
return -1;
}
return 0;
break;
//The s case will create a symbol link to an existing file.
case 's':
char *pathname;
char *linkname;
int slink;
slink = symlink(pathname, linkname); //Using the symlink function to create a symbolic link of pathname
if (chmod (pathname,j) < 0)
{
fprintf(stderr, "%s: error linking(%s, %s) - %d (%s)\n",
argv[0], pathname, mode, errno, strerror(errno));
return -1;
}
return 0;
break;
}
}
解决方案
POSIX
http://pubs.opengroup.org/onlinepubs/009695399/functions/link.html
http://pubs.opengroup.org/onlinepubs/009695399/functions/symlink.html
#include <unistd.h>
char *path1 = "/home/cnd/mod1";
char *path2 = "/modules/pass1";
int status;
...
status = link (path1, path2);
视窗
https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-createhardlinka
https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-createsymboliclinka
BOOL fCreatedLink = CreateHardLink( pszNewLinkName,
pszExistingFileName,
NULL ); // reserved, must be NULL
if ( fCreatedLink == FALSE )
{
;// handle error condition
}
推荐阅读
- python - SARIMAX - 没有支持的索引可用。预测结果将以“start”开始的整数索引给出
- java - 反序列化 JSON 文档中未在 JSON 数组中编码的多个值(连接 JSON)
- time-complexity - 为什么多展树 O(loglogn) 具有竞争力?
- javascript - 无效的FilterExpression:语法错误;令牌:\",\",附近:\":ordrId, #timestamp\"
- excel - 如何将堆积柱形图中的一个数据点拆分为多个对象?
- javascript - 如何使用 discord.js 在没有发送消息权限的情况下创建角色
- sql - 列名中带有井号的 SQL 下划线
- python-2.7 - Gimp python-fu ,打开目录选择对话框
- mysql - 使用 SQL 计算文本出现的次数
- ajax - 一种让 webapp 监听服务器的方法