argv[0]
Patrik Ostrihon
pa3k na pa3k.sk
Středa Duben 2 21:20:53 CEST 2003
Dňa St, 2003-04-02 at 13:04, Petr Stehlik napísal:
> Zdar vsem,
>
> int main(int argc, char *argv[])
>
> odjakziva jsem mel za to, ze argv[0] obsahuje celou cestu ke spustenemu
> programu, tj. napriklad pokud mam /usr/local/bin/printargv0 a jsem treba
> v /tmp a pustim-li "printargv0", tak mi vypise
> "/usr/local/bin/printargv0". Ted jsem na tomhle zalozil kus nejakeho
> sveho programu (cilem je spustit PRG2 z PRG1, pricemz PRG2 lezi tam, kde
> PRG1 a ja nevim predem, kde to je).
skuste si precitat http://www.advancedlinuxprogramming.com/
riesenie vaseho problemu:
/***********************************************************************
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
* Copyright (C) 2001 by New Riders Publishing *
* See COPYRIGHT for license information. *
***********************************************************************/
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/* Finds the path containing the currently-running program executable.
The path is placed into BUFFER, which is of length LEN. Returns
the number of characters in the path, or -1 on error. */
size_t get_executable_path (char* buffer, size_t len)
{
char* path_end;
/* Read the target of /proc/self/exe. */
if (readlink ("/proc/self/exe", buffer, len) <= 0)
return -1;
/* Find the last occurence of a forward slash, the path separator. */
path_end = strrchr (buffer, '/');
if (path_end == NULL)
return -1;
/* Advance to the character past the last slash. */
++path_end;
/* Obtain the directory containing the program by truncating the
path after the last slash. */
*path_end = '\0';
/* The length of the path is the number of characters up through the
last slash. */
return (size_t) (path_end - buffer);
}
int main ()
{
char path[PATH_MAX];
get_executable_path (path, sizeof (path));
printf ("this program is in the directory %s\n", path);
return 0;
}
--
Patrik Ostrihon
Technicky riaditel
ProWeb Consulting, a.s.
Werferova 1, 040 11 Kosice
Tel./fax: 055 / 7898 401
Navstivte nas na: www.pwc.sk
Další informace o konferenci Linux