home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / internal / 2148 < prev    next >
Encoding:
Text File  |  1993-01-22  |  2.1 KB  |  68 lines

  1. Newsgroups: comp.unix.internals
  2. Path: sparky!uunet!gatech!hubcap!ncrcae!grok101.ColumbiaSC.NCR.COM!raj
  3. From: raj@grok101.ColumbiaSC.NCR.COM ()
  4. Subject: Re: System Call which Returns Executing Program Name?
  5. Message-ID: <1993Jan22.114837.9253@ncrcae.ColumbiaSC.NCR.COM>
  6. Nntp-Posting-Host: grok101.columbiasc.ncr.com
  7. Organization: Your Organization Here
  8. References: <1993Jan21.220641.5858@organpipe.uug.arizona.edu>
  9. Date: Fri, 22 Jan 93 16:48:37 GMT
  10. Lines: 56
  11.  
  12. In article <1993Jan21.220641.5858@organpipe.uug.arizona.edu>, brian@lpl.arizona.edu (Brian Ceccarelli 602/621-9615) writes:
  13. |> This should be an easy one for your UNIX gurus out there. 8-)
  14. |> 
  15. |> Is there a system function I can call which returns the
  16. |> name of the executing program?   
  17. |> 
  18. |> 
  19. |> I need to place such a function in some lower object library.
  20. |> Information in the argv of main(argc, argv) is not available
  21. |> at this level.
  22. |> 
  23. |> Thanks!
  24. |> 
  25. |> Brian Ceccarelli
  26. |> ----------------
  27. |> brian@gamma1.lpl.arizona.edu
  28. |>   
  29.  
  30. One way of getting the name of the current (or any) executing program 
  31. is to use the proc filesystem on SVR4. The following C program 
  32. will do the needful.
  33.  
  34. /*
  35.  * Gets the name of the current command/process  - raj
  36.  */
  37. #include <stdio.h>
  38. #include <ctype.h>
  39. #include <string.h>
  40. #include <fcntl.h>
  41. #include <sys/stat.h>
  42. #include <sys/procfs.h>
  43.  
  44.  
  45. main()
  46. {
  47.         int     procfd;                 /* fd for /proc/xxxxx */
  48.         char    pname[20];
  49.         int     pid = getpid();
  50.         struct  prpsinfo info;          /* process ps info */
  51.  
  52.  
  53.         printf ("my pid = %d\n",pid);   /* use the pid of process */
  54.         sprintf(pname, "/proc/0%d",pid);
  55.         if ((procfd = open(pname, O_RDONLY)) == -1) {
  56.                 printf("could not open /proc/%s\n",pname);
  57.                 exit(1);
  58.         }                                    
  59.                           /* read the info from /proc */
  60.         if (ioctl(procfd, PIOCPSINFO, (char*)&info) == -1) {
  61.                 printf("ioctl on open /proc/%s failed\n",pname);
  62.                 close (procfd);
  63.                 exit(2);
  64.         }
  65.         close(procfd);
  66.         printf("my command name = %s\n",info.pr_fname);
  67. }
  68.