~ubuntu-branches/ubuntu/gutsy/pygame/gutsy

« back to all changes in this revision

Viewing changes to src/setproctitle.c

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2002-02-20 06:39:24 UTC
  • Revision ID: james.westby@ubuntu.com-20020220063924-amlzj7tqkeods4eq
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   I don't know if it's a good thing to do NXArgc or NXArgv, 
 
3
   but it makes finding the beginning a few steps simpler and
 
4
   resetting their values might be a good thing for WindowServer
 
5
   and the like? 
 
6
 
 
7
   bob@redivi.com
 
8
*/
 
9
 
 
10
 
 
11
#include "setproctitle.h"
 
12
void setproctitle(const char *fmt, ...)
 
13
{
 
14
  int olen,mib[4];
 
15
  struct kinfo_proc kp;
 
16
  size_t bufSize=sizeof(kp);
 
17
  static char newargs[SPT_BUFSIZE];
 
18
  char *p1,*minpos,*endorig;
 
19
  va_list ap;
 
20
 
 
21
  /* write out the formatted string, or quit */
 
22
  va_start(ap,fmt);
 
23
  if (fmt) {
 
24
    newargs[sizeof(newargs)-1] = 0;
 
25
    (void)vsnprintf(newargs,sizeof(newargs),fmt,ap);
 
26
  } else {
 
27
    mib[0]=CTL_KERN;
 
28
    mib[1]=KERN_PROC;
 
29
    mib[2]=KERN_PROC_PID;
 
30
    mib[3]=getpid();
 
31
    if (sysctl(mib,4,&kp,&bufSize,NULL,0)) { printf("setproctitle: i dont know my own pid!\n"); return; }
 
32
    strcpy(newargs,kp.kp_proc.p_comm);
 
33
  }  
 
34
  va_end(ap);
 
35
 
 
36
  /* find the end of the original string cause we're stackbackwards! */
 
37
  endorig = NXArgv[NXArgc-1]+strlen(NXArgv[NXArgc-1]);
 
38
 
 
39
  /* kill the original */
 
40
  bzero(NXArgv[0],(unsigned int)(endorig-NXArgv[0]));
 
41
  for (p1=NXArgv[0]-2;*p1;--p1) *p1=0;
 
42
 
 
43
  /* new length (all args) */
 
44
  olen=strlen(newargs);
 
45
 
 
46
  /* new NXArgv[0] */
 
47
  minpos = endorig-olen;
 
48
  NXArgv[0] = minpos;
 
49
 
 
50
  /* copy the new string to the old place */
 
51
  strcpy(NXArgv[0],newargs);
 
52
 
 
53
  /* search for spaces, replace with nulls and increment the argc */
 
54
  NXArgc=1;
 
55
  for (p1=NXArgv[0];*p1;++p1) 
 
56
    if (*p1==' ') { *p1=0; NXArgv[NXArgc++] = p1+1; }
 
57
  NXArgv[NXArgc]=NULL;
 
58
 
 
59
  /* why this is here or what for is beyond me.. theres a copy of the executable name before NXArgv[0] */
 
60
  strcpy(NXArgv[0]-strlen(NXArgv[0])-2,NXArgv[0]);
 
61
 
 
62
  /* is this even necessary? */
 
63
  p1=endorig;
 
64
  olen=NXArgc;
 
65
  while (++p1<(char *)(USRSTACK-4)) if (!*p1) NXArgv[++olen]=p1+1;  
 
66
/*  while (1) {} */
 
67
}