简体   繁体   English

区分可选参数,路径名还是文件? C语言

[英]Distinguish between optional arguments, pathname or file? c language

I am very new with c and less experienced with any other language :/ For an assignment at uni, I am a little stuck on this small part. 我对c很陌生,对其他任何语言的经验都很少:/对于uni的作业,我在这小部分上有些困惑。 Essentially I am required to write a 'ls' function that has 4 optional arguments, for example: 本质上,我需要编写一个具有4个可选参数的'ls'函数,例如:

list [-l] [-f] [pathname] [localfile] 

Now, the first two are straight forward. 现在,前两个很简单。 To make things more difficult, the 'localfile' doesn't necessarily exist and the 'pathname'(if given) will be located on the server I'm connecting to through a socket (so checking if it is a file is out and checking the pathname is out). 为了使事情变得更加困难,“ localfile”不一定存在,并且“ pathname”(如果有)将位于我通过套接字连接到的服务器上(因此检查它是否是文件,然后检查路径名已删除)。 I was thinking, check last 4 chars in the string for a '.txt' or something similar. 我在想,检查字符串中的最后4个字符是否为“ .txt”或类似内容。 I'm actually completely stumped and will present this problem to my course conveyor tomorrow, if I can't find a solution. 实际上,我很沮丧,如果找不到解决方案,明天将向我的课程传送带提出此问题。

This is a very small part of what I actually have to do but any push in the right direction would be appreciated. 这只是我实际上要做的一小部分,但朝正确方向的任何推动将不胜感激。

You will need to process argc and argv to get your command line arguments. 您将需要处理argc和argv以获得命令行参数。 That is the first thing to work on, getting the arguments - ensuring they are correct, and determining what is being asked for. 这是要做的第一件事,获取参数-确保参数正确,并确定要求的内容。

int main(int argc, char  *argv[])

Assuming your are on Linux/Unix, you will need to use the directory functions opendir()/readdir()/closedir() - dirent.h . 假设您在Linux / Unix上,则需要使用目录函数opendir()/ readdir()/ closedir()- dirent.h The stat() function will be required to satisfy the -l requirement. 需要使用stat()函数来满足-l要求。 access() will determine if a file exists and then stat() will tell you if the file is a regular file or a directory. access()将确定文件是否存在,然后stat()会告诉您该文件是常规文件还是目录。

I'd make a struct to hold the four optional arguments and return it from a function called "process_arguments" that takes argc and argv as parameters. 我将制作一个结构来保存四个可选参数,然后从一个名为“ process_arguments”的函数(使用argc和argv作为参数)返回它。

struct args {
  bool valid;
  bool l_option;
  bool f_option
  char directory[200]; 
  char filename[200];
}

With the requirement for a socket connection you will have to write a "server program" that will be constantly running on the server and a "client program" that it will fork to handle the requests from your local program. 根据套接字连接的要求,您将必须编写一个将在服务器上不断运行的“服务器程序”和一个将其分叉以处理来自本地程序的请求的“客户端程序”。 Try and locate examples of socket programs. 尝试找到套接字程序的示例。

Another check for whether you have a path string or a filename is to look for the path separator character - '/' if the server is Unix/Linux. 如果服务器是Unix / Linux,则要检查是否具有路径字符串或文件名是查找路径分隔符-'/'。 This scheme shouldn't have any path separators in filenames, so the presence of one tells you it is a path. 此方案在文件名中不应包含任何路径分隔符,因此如果出现一个,则表示它是路径。

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

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