src/lib/message.c
void my_name_is(int argc, char *argv[], const char *name)
{
   char *l, *p;
   char *cpath;
   char *cargv0;
   int len;
   int path_max;
   bool respath;

   if (gethostname(host_name, sizeof(host_name)) != 0) {
      bstrncpy(host_name, "Hostname unknown", sizeof(host_name));
   }
   bstrncpy(my_name, name, sizeof(my_name));

   if (argc>0 && argv && argv[0]) {
      /* use a dynamic PATH_MAX and allocate temporary variables */
      path_max = pathconf(argv[0], _PC_PATH_MAX);
      if (path_max < 4096){
         path_max = 4096;
      }
      cpath = (char *)malloc(path_max);
      cargv0 = (char *)malloc(path_max);

      respath = false;
#ifdef HAVE_REALPATH
      /* make a canonical argv[0] */
      if (realpath(argv[0], cargv0) != NULL){
         respath = true;
      }
#endif
      if (!respath){
         /* no resolved_path available in cargv0, so populate it */
         strncpy(cargv0, argv[0], path_max);
      }
      /* strip trailing filename and save exepath */
      for (l=p=cargv0; *p; p++) {
         if (IsPathSeparator(*p)) {
            l = p;                       /* set pos of last path separator */
         }
      }
      if (IsPathSeparator(*l)) {
         l++;
      } else {
         l = cargv0;
#if defined(HAVE_WIN32)
         /* On Windows allow c: drive specification */
         if (l[1] == ':') {
            l += 2;
         }
#endif
      }
      len = strlen(l) + 1;
      if (exename) {
         free(exename);
      }
      exename = (char *)malloc(len);
      strcpy(exename, l);
      if (exepath) {
         free(exepath);
      }
      /* separate exepath from exename */
      *l = 0;
      exepath = bstrdup(cargv0);
      if (strstr(exepath, PathSeparatorUp) != NULL || strstr(exepath, PathSeparatorCur) != NULL || !IsPathSeparator(exepath[0])) {
         /* fallback to legacy code */
         if (getcwd(cpath, path_max)) {
            free(exepath);
            exepath = (char *)malloc(strlen(cpath) + 1 + len);
            strcpy(exepath, cpath);
         }
      }
      Dmsg2(500, "exepath=%s\nexename=%s\n", exepath, exename);
      free(cpath);
      free(cargv0);
   }
}
/*
 * Set daemon name. Also, find canonical execution
 *  path.  Note, exepath has spare room for tacking on
 *  the exename so that we can reconstruct the full name.
 *
 * Note, this routine can get called multiple times
 *  The second time is to put the name as found in the
 *  Resource record. On the second call, generally,
 *  argv is NULL to avoid doing the path code twice.
 */
Set daemon name. Also, find canonical execution
path.  Note, exepath has spare room for tacking on
the exename so that we can reconstruct the full name.

Note, this routine can get called multiple times
The second time is to put the name as found in the
Resource record. On the second call, generally,
argv is NULL to avoid doing the path code twice.
Set daemon name . Also find canonical execution path . Note exepath has spare room for tacking on the exename so that we can reconstruct the full name . Note this routine can get called multiple times The second time is to put the name as found in the Resource record . On the second call generally argv is NULL to avoid doing the path code twice .
void my_name_is(int argc, char *argv[], const char *name)
{
   char *l, *p;
   char *cpath;
   char *cargv0;
   int len;
   int path_max;
   bool respath;
   if (gethostname(host_name, sizeof(host_name)) != 0) {
      bstrncpy(host_name, "Hostname unknown", sizeof(host_name));
   }
   bstrncpy(my_name, name, sizeof(my_name));
   if (argc>0 && argv && argv[0]) {
      path_max = pathconf(argv[0], _PC_PATH_MAX);
      if (path_max < 4096){
         path_max = 4096;
      }
      cpath = (char *)malloc(path_max);
      cargv0 = (char *)malloc(path_max);
      respath = false;
#ifdef HAVE_REALPATH
      if (realpath(argv[0], cargv0) != NULL){
         respath = true;
      }
#endif
      if (!respath){
         strncpy(cargv0, argv[0], path_max);
      }
      for (l=p=cargv0; *p; p++) {
         if (IsPathSeparator(*p)) {
            l = p;                       
         }
      }
      if (IsPathSeparator(*l)) {
         l++;
      } else {
         l = cargv0;
#if defined(HAVE_WIN32)
         if (l[1] == ':') {
            l += 2;
         }
#endif
      }
      len = strlen(l) + 1;
      if (exename) {
         free(exename);
      }
      exename = (char *)malloc(len);
      strcpy(exename, l);
      if (exepath) {
         free(exepath);
      }
      *l = 0;
      exepath = bstrdup(cargv0);
      if (strstr(exepath, PathSeparatorUp) != NULL || strstr(exepath, PathSeparatorCur) != NULL || !IsPathSeparator(exepath[0])) {
         if (getcwd(cpath, path_max)) {
            free(exepath);
            exepath = (char *)malloc(strlen(cpath) + 1 + len);
            strcpy(exepath, cpath);
         }
      }
      Dmsg2(500, "exepath=%s\nexename=%s\n", exepath, exename);
      free(cpath);
      free(cargv0);
   }
}
void my_name_is ( int argc , char * argv [ ] , const char * name ) { char * l , * p ; char * cpath ; char * cargv0 ; int len ; int path_max ; bool respath ; if ( gethostname ( host_name , sizeof ( host_name ) ) != 0 ) { bstrncpy ( host_name , " " , sizeof ( host_name ) ) ; } bstrncpy ( my_name , name , sizeof ( my_name ) ) ; if ( argc > 0 && argv && argv [ 0 ] ) { path_max = pathconf ( argv [ 0 ] , _PC_PATH_MAX ) ; if ( path_max < 4096 ) { path_max = 4096 ; } cpath = ( char * ) malloc ( path_max ) ; cargv0 = ( char * ) malloc ( path_max ) ; respath = false ; #ifdef HAVE_REALPATH if ( realpath ( argv [ 0 ] , cargv0 ) != NULL ) { respath = true ; } #endif if ( ! respath ) { strncpy ( cargv0 , argv [ 0 ] , path_max ) ; } for ( l = p = cargv0 ; * p ; p ++ ) { if ( IsPathSeparator ( * p ) ) { l = p ; } } if ( IsPathSeparator ( * l ) ) { l ++ ; } else { l = cargv0 ; #if defined ( HAVE_WIN32 ) if ( l [ 1 ] == ' ' ) { l += 2 ; } #endif } len = strlen ( l ) + 1 ; if ( exename ) { free ( exename ) ; } exename = ( char * ) malloc ( len ) ; strcpy ( exename , l ) ; if ( exepath ) { free ( exepath ) ; } * l = 0 ; exepath = bstrdup ( cargv0 ) ; if ( strstr ( exepath , PathSeparatorUp ) != NULL || strstr ( exepath , PathSeparatorCur ) != NULL || ! IsPathSeparator ( exepath [ 0 ] ) ) { if ( getcwd ( cpath , path_max ) ) { free ( exepath ) ; exepath = ( char * ) malloc ( strlen ( cpath ) + 1 + len ) ; strcpy ( exepath , cpath ) ; } } Dmsg2 ( 500 , " \n \n " , exepath , exename ) ; free ( cpath ) ; free ( cargv0 ) ; } }
Set daemon name.
Set daemon name .
/* use a dynamic PATH_MAX and allocate temporary variables */ /* make a canonical argv[0] */ /* no resolved_path available in cargv0, so populate it */ /* strip trailing filename and save exepath */ /* set pos of last path separator */ /* On Windows allow c: drive specification */ /* separate exepath from exename */ /* fallback to legacy code */