【发布时间】:2017-07-25 06:53:00
【问题描述】:
出于教育用途,我正在尝试实现一个简单的查找程序(类似于 find(1),但功能要少得多)。
在实现第一个功能“-user”时,我遇到了将两个不同的功能(一次用于 uid,一次用于名称)与一个“控制功能?”结合起来的挑战。我想我正在寻找类似 C# 中的重载函数之类的东西,在 C 中解决此类问题的最佳实践是什么?
我希望有一种解决方法,它允许我的程序在调用它时处理这两个选项: 例如:
- “my_find -user vincent”
- “my_find -user 1000”
如果您在我的示例中发现一些编码或样式问题,请指出,我愿意学习!我知道错过了一些干净的内存管理,但稍后会添加。
更新 我正在考虑一个函数,它将参数作为 char 数组处理,并检查每个 char 的 ASCII 代码,如果没有找到“char”,则调用该函数。这样的东西“合法”吗?
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
void entries_with_uid(char *init_path, int pattern_uid);
void entries_with_name(char *init_path, char *pattern_name);
int main(int argc, char **argv)
{
/*
char *path = argv[1];
*/
entries_with_name("/home/mario/Documents/codes", "mario");
entries_with_uid("/home/mario/test_project", 1000);
return 0;
}
void entries_with_uid(char *init_path, int pattern_uid)
{
DIR *dir;
struct dirent *entry;
dir = opendir(init_path);
entry = readdir(dir);
if(dir == NULL) return;
if(entry == NULL) return;
do {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", init_path, entry->d_name);
path[len] = 0;
struct stat sb;
stat(path, &sb);
if(sb.st_uid == pattern_uid) printf("%s\n", path);
if (entry->d_type == DT_DIR) entries_with_uid(path, pattern_uid);
} while (entry = readdir(dir));
closedir(dir);
}
void entries_with_name(char *init_path, char *pattern_name)
{
DIR *dir;
struct dirent *entry;
dir = opendir(init_path);
entry = readdir(dir);
if(dir == NULL) return;
if(entry == NULL) return;
do {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", init_path, entry->d_name);
path[len] = 0;
struct stat sb;
stat(path, &sb);
struct passwd *pwd = getpwuid(sb.st_uid);
if(strcmp(pwd->pw_name, pattern_name) == 0) printf("%s\n", path);
if (entry->d_type == DT_DIR) entries_with_name(path, pattern_name);
} while (entry = readdir(dir));
closedir(dir);
}
谢谢!
更新
我同时用一个检查参数中的字母数字字符串的函数解决了这个问题:
int count_alphanumeric(char *string)
{
int alphanumeric_counter = 0;
int i = 0;
while(string[i]!='\0')
{
if (string[i] <= 57 && string[i] >= 48)
{
i++;
continue;
}
else alphanumeric_counter++;
i++;
}
return alphanumeric_counter;
}
仍在寻找更优雅的选择! ;)
干杯!
【问题讨论】:
-
C 标准的 2011 修订版添加了generic selections。因此可以编写宏来模拟重载函数。不幸的是,行为有点under specified。
-
是否有专业人士最常使用的最佳或最佳实践?
-
好吧,如果您编写选择以接受
char*并在传递字符串时始终转换为它(有点麻烦,但可移植),您可以避免歧义。 -
您的意思是在使用
时将其转换为字符串吗? -
为什么不使用清晰的名称? -userid 和 -username?
标签: c parameters arguments parameter-passing