EvilZone
Programming and Scripting => C - C++ => Topic started by: flowjob on June 20, 2012, 02:38:06 PM
-
Hey,
I'm working on a project right now,and have to do the following:
My program should execute scripts and commands. If there's no arg an interpreter is started,otherwise it should check if it is an file (check also if correct path),or a command.
To explain it better,here's pseudo-code:
/* Pseudo Code */
if is argv /* Got arguments ? */
if maybepath(argv) == True /* Could be a path to a file */
if realpath(argv) == True /* Path does exist */
runfile(argv)
else
print 'File does not exist' /* Path does not exist */
else
runcmd(argv) /* No path -> is command -> run command */
else /* Got no args -> run interpreter */
runinterpreter()
But I don't have the faithest idea how I could check if the given argument COULD be a path.
Any idea? (should work on linux and windows)
-
I don't really understand what you mean, I guess you want to check if the given argument is a file or a path?
Just do:
FILE *file;
if (!(file = fopen(argv[1], "r"))){
if (pathexists(argv[1])
do something;
else
do something with the file;
-
I know how to check if a file does exist...
I want to check if the path is valid (syntax,etc...), so the file COULD exist.
e.g.: (in Windows)
'C:\Windows\System32\cmd.exe' (valid,exists)
'C:\Windows\SysX\cmd.exe' (valid,doesn't exist)
'C:\\Windows\\System32\\cmd.exe' (valid,exists)
'X:\\cmd.exe' (valid,doesn't exist)
'X:\foo\\bar\cmd.exe' (not valid,doesn't exist)
'\Windows\System32\cmd.exe' (not valid,doesn't exist)
'FOO:\Windows\System32\cmd.exe' (not valid,doesn't exist)
-
why not try and create a folder in the "could-be-path" and see if it throws an exception or some error, then the path is not valid.
-
Simplify.
if maybepath(argv) == True /* Could be a path to a file */
if realpath(argv) == True /* Path does exist */
could be just
if fileexists(argv)
and its working would be the same.
-
@Kulverstukas:
But when deleting the folder again,I won't know wich folders did not exist...
eg:
before 'C:\Users\Me\'
after 'C:\Users\Me\new\newtoo\'
I won't know if 'new' did already exist or not when I created the folder 'newtoo' in 'C:\Users\Me\new\' ...
@ca0s
Yes,but if the file does not exist,I won't know if the user just wrote something wrong (e.g. 'C:\Windows\Sys32\cmd.exe') or he typed a command.
Because if he just typed something wrong I want to prompt him that the path does not exist.
But also still be able to exec it as a command if the given arg is no valid path ('e.g. print 'hi') ...
-
Maybe you could look into stat() sd function:
struct stat st;
if(stat("/tmp",&st) == 0)
printf(" /tmp is present\n");
I would just check for specific flags in argv. For example everything after "-e" flag is a command, but after a "-f" flag is a file, or "-d" for a directory.
-
Ok,that's an idea, I'll check it out.. :D