home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.internals
- Path: sparky!uunet!gatech!hubcap!ncrcae!grok101.ColumbiaSC.NCR.COM!raj
- From: raj@grok101.ColumbiaSC.NCR.COM ()
- Subject: Re: System Call which Returns Executing Program Name?
- Message-ID: <1993Jan22.114837.9253@ncrcae.ColumbiaSC.NCR.COM>
- Nntp-Posting-Host: grok101.columbiasc.ncr.com
- Organization: Your Organization Here
- References: <1993Jan21.220641.5858@organpipe.uug.arizona.edu>
- Date: Fri, 22 Jan 93 16:48:37 GMT
- Lines: 56
-
- In article <1993Jan21.220641.5858@organpipe.uug.arizona.edu>, brian@lpl.arizona.edu (Brian Ceccarelli 602/621-9615) writes:
- |> This should be an easy one for your UNIX gurus out there. 8-)
- |>
- |> Is there a system function I can call which returns the
- |> name of the executing program?
- |>
- |>
- |> I need to place such a function in some lower object library.
- |> Information in the argv of main(argc, argv) is not available
- |> at this level.
- |>
- |> Thanks!
- |>
- |> Brian Ceccarelli
- |> ----------------
- |> brian@gamma1.lpl.arizona.edu
- |>
-
- One way of getting the name of the current (or any) executing program
- is to use the proc filesystem on SVR4. The following C program
- will do the needful.
-
- /*
- * Gets the name of the current command/process - raj
- */
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/procfs.h>
-
-
- main()
- {
- int procfd; /* fd for /proc/xxxxx */
- char pname[20];
- int pid = getpid();
- struct prpsinfo info; /* process ps info */
-
-
- printf ("my pid = %d\n",pid); /* use the pid of process */
- sprintf(pname, "/proc/0%d",pid);
- if ((procfd = open(pname, O_RDONLY)) == -1) {
- printf("could not open /proc/%s\n",pname);
- exit(1);
- }
- /* read the info from /proc */
- if (ioctl(procfd, PIOCPSINFO, (char*)&info) == -1) {
- printf("ioctl on open /proc/%s failed\n",pname);
- close (procfd);
- exit(2);
- }
- close(procfd);
- printf("my command name = %s\n",info.pr_fname);
- }
-