This forum is in archive mode. You will not be able to post new content.

Author Topic: [C] Could-Be-A-Path Checker  (Read 2058 times)

0 Members and 1 Guest are viewing this topic.

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
[C] Could-Be-A-Path Checker
« 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:

Code: [Select]
/* 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)
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [C] Could-Be-A-Path Checker
« Reply #1 on: June 20, 2012, 03:48:34 PM »
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:
Code: [Select]
FILE *file;

if (!(file = fopen(argv[1], "r"))){
    if (pathexists(argv[1])
        do something;
else
    do something with the file;
Easter egg in all *nix systems: E(){ E|E& };E

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: [C] Could-Be-A-Path Checker
« Reply #2 on: June 20, 2012, 04:34:56 PM »
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)

« Last Edit: June 20, 2012, 04:35:20 PM by Area_13 »
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [C] Could-Be-A-Path Checker
« Reply #3 on: June 20, 2012, 04:42:13 PM »
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.

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: [C] Could-Be-A-Path Checker
« Reply #4 on: June 20, 2012, 04:54:33 PM »
Simplify.
Code: [Select]
if maybepath(argv) == True    /* Could be a path to a file */
        if realpath(argv) == True     /* Path does exist */
could be just
Code: [Select]
if fileexists(argv)
and its working would be the same.

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: [C] Could-Be-A-Path Checker
« Reply #5 on: June 20, 2012, 05:03:59 PM »
@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') ...
« Last Edit: June 20, 2012, 05:04:40 PM by Area_13 »
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [C] Could-Be-A-Path Checker
« Reply #6 on: June 21, 2012, 12:59:18 AM »
Maybe you could look into stat() sd function:
Code: [Select]
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.
Easter egg in all *nix systems: E(){ E|E& };E

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: [C] Could-Be-A-Path Checker
« Reply #7 on: June 21, 2012, 06:36:02 AM »
Ok,that's an idea, I'll check it out..  :D
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.