Exemplo n.º 1
0
// hiden danger for sPath buffer size;
int ndisk_parse(const wchar_t *path, NDiskEntry **entry, NDisk **disk, wchar_t *sPath) {
    wchar_t tmp[PATH_MAX], *sTmp = NULL;
    int r = NDISK_FATAL, i = 0;
    if(!path || wcscmp(path, L"") == 0 || wcscmp(path, L"\\") == 0) {
        return r;
    }
    sTmp = (wchar_t *)path + 1;
    while(*sTmp != '\0' && *sTmp != '\\') {
        sTmp++;
    }
    wcslcpy(tmp, path + 1, (int)(sTmp - path));
    if(dict_get_element_s(available_disks, tmp, (void **)disk) == DICT_OK) {
        for(i = 0; i < available_disk_entries_length; i++) {
            if(wcscmp((*disk)->type, available_disk_entries[i].name) == 0) {
                *entry = &available_disk_entries[i];
                wcslcpy(sPath, path + wcslen(tmp) + 1, PATH_MAX);
                if(*sPath == '\0') {
                    wcslcpy(sPath, L"\\", PATH_MAX);
                }
                r = NDISK_OK;
                break;
            }
        }
    }
    return r;
}
Exemplo n.º 2
0
/*
 * This module should never crash and will always return a value on *out.  If
 * any error occur during its runtime, it should be represented in a user
 * readable format on *out.
 */
void
cmd_color_exec(int argc, wchar_t **argv, wchar_t *out, size_t len)
{
	(void)argc;
	(void)argv;
	long long code;
	const wchar_t *errstr = NULL;

	if (argc != 2) {
		wcslcpy(out, ERR_BAD_ARG, len);
		return;
	}

	if (wcscmp(argv[1], L"reset") == 0) {
		wcslcpy(out, L"", len);
		return;
	}

	code = wcstonum(argv[1], 0, 255, &errstr);
	if (errstr != NULL) {
		wcslcpy(out, ERR_BAD_CODE, len);
		return;
	}

	swprintf(out, len, L"[38;5;%dm", code);
}
Exemplo n.º 3
0
/**
 * Add the mercurial branch at the beginning of the path. If a branch was
 * found, 1 is returned else 0.
 */
int
add_branch(wchar_t *s, enum version_control_system vcs)
{
	wchar_t org[MAXPATHLEN];
	wchar_t branch[MAX_BRANCH_LEN];
	size_t len;

	wcslcpy(org, s, MAXPATHLEN);

	switch (vcs) {
		case VCS_MERCURIAL:
			if (get_mercurial_branch(branch, MAX_BRANCH_LEN) == 0)
				return 0;
			break;
		case VCS_GIT:
			if (get_git_branch(branch, MAX_BRANCH_LEN) == 0)
				return 0;
			break;
		default:
			return 0;
	}

	len = wcslcpy(s, branch, MAX_BRANCH_LEN);

	s += len;
	*(s++) = ':';
	wcslcpy(s, org, MAXPATHLEN - len - 1);

	return 1;
}
Exemplo n.º 4
0
/**
 * Loop through the user-defined aliases and find the best match to
 * get the shortest path as possible.
 */
void
replace_aliases(wchar_t *s)
{
	int i, chosen = -1;
	size_t len, nlen, max = 0;
	wchar_t t[MAXPATHLEN], *org = s;

	for (i = 0; i < alias_count; i++) {
		len = wcslen(aliases[i].path);
		if (wcsncmp(aliases[i].path, s, len) == 0) {
			if (len > max) {
				chosen = i;
				max = len;
			}
		}
	}

	/* No alias found, you can leave now */
	if (chosen == -1)
		return;

	nlen = wcslen(aliases[chosen].name);
	s += (max - nlen);
	wcsncpy(s, aliases[chosen].name, nlen);

	wcslcpy(t, s, MAXPATHLEN);
	wcslcpy(org, t, MAXPATHLEN);
}
Exemplo n.º 5
0
HANDLE __stdcall FsFindFirstW(WCHAR* Path, WIN32_FIND_DATAW *FindData) {
    FindStruc *pFind = NULL;
    pFind = (FindStruc *)malloc(sizeof(FindStruc));
    memset(pFind, 0, sizeof(FindStruc));
    if(wcscmp(Path, L"\\") == 0) {
        pFind->findHandle = available_disks->first;
    } else {
        if(ndisk_parse(Path, &pFind->entry, &pFind->disk, pFind->sPath) != NDISK_OK || pFind->disk == NULL) {
            free(pFind);
            return INVALID_HANDLE_VALUE;
        }
        pFind->dict = dict_initialize();
        if(ndisk_dir(pFind->dict, pFind->entry, pFind->disk, pFind->sPath) == NDISK_FATAL) {
            free(pFind);
            return INVALID_HANDLE_VALUE;
        }
        pFind->findHandle = pFind->dict->first;
    }
    wcslcpy(pFind->path, Path, PATH_MAX);
    memset(FindData, 0, sizeof(WIN32_FIND_DATAW));
    FindData->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
    FindData->ftLastWriteTime.dwHighDateTime = 0xFFFFFFFF;
    FindData->ftLastWriteTime.dwLowDateTime = 0xFFFFFFFE;
    wcslcpy(FindData->cFileName, L"..", _countof(FindData->cFileName) - 1);
    return pFind;
}
Exemplo n.º 6
0
/*
 * Add the hostname in front of the path.
 *
 * Input:  /etc
 * Output: odin:/etc
 */
void
add_hostname(wchar_t *s)
{
	char buf[MAXHOSTNAMELEN], *c;
	wchar_t org[MAX_OUTPUT_LEN];
	wchar_t hostname[MAX_HOSTNAME_LEN];
	size_t len;

	wcslcpy(org, s, MAX_OUTPUT_LEN);

	/* We failed to get the hostname. Complain and die. */
	if (get_full_hostname(buf, MAXHOSTNAMELEN) != 0)
		return fatal("prwd: gethostname() failed");

	/* Find the first dot and stop right here. */
	c = strchr(buf, '.');
	if (c != NULL)
		*c = '\0';

	if (mbstowcs(hostname, buf, MAX_HOSTNAME_LEN) == -1)
		return fatal("prwd: mbstowcs(hostname, ...) failed");

	len = wcslcpy(s, hostname, MAX_HOSTNAME_LEN);

	s += len;
	*(s++) = ':';
	wcslcpy(s, org, MAX_OUTPUT_LEN - len - 1);
}
Exemplo n.º 7
0
/**
 * Take a string and replace all the full words by their first
 * letters, except the last one.
 */
void
newsgroupize(wchar_t *s)
{
	wchar_t t[MAXPATHLEN];
	wchar_t *last = NULL, *org = s;
	int idx = 0;

	if (s == NULL || *s == '\0')
		return;

	/* Root (/) can escape right now, it's as short as it can get */
	if (*s == L'/' && s[1] == '\0')
		return;

	/* Unless we are starting from a / (slash), we can use the first one */
	if (*s != L'/') {
		t[idx++] = *s;
	}

	/* Keep the first part if it's an alias (start by *) */
	if (*s == L'*') {
		wchar_t *sl = wcschr(s, L'/');

		if (sl) {
			wcslcpy(t, s, sl - s + 1);
			idx += sl - s - 1;
			s = sl;
			/* If we have no other /, keep the alias AND the last
			 * part, so we return without doing anything */
			sl = wcschr(s + 1, L'/');
			if (sl == NULL) {
				return;
			}
		}
	}
	t[idx++] = '/';
	t[idx] = '\0';

	/* For every component, add the first letter and a slash */
	while ((s = wcschr(s, L'/')) != NULL) {
		/* Cater for trailing slashes */
		if (s[1] == '\0')
			break;
		last = ++s;
		t[idx++] = (wchar_t)*s;
		t[idx++] = L'/';
	}

	/* idx is less than 4, we only have one slash, just keep org as is */
	if (idx < 4)
		return;

	/* Copy letters+slash making sure the last part is left untouched. */
	wcslcpy(org, t, idx);
	if (last != NULL)
		wcslcpy(org + idx - 2, last, wcslen(last) + 1);
}
Exemplo n.º 8
0
BOOL _SpySrv_RefreshProcList () 
{
  PVOID		pBuffer ;
  DWORD		nBufferSize ;
  DWORD		nBytesReturned=0 ;
  BOOL		bSuccess ;
  PROCESSLISTENTRY*	pEntry ;
  
  nBufferSize = 1024*1024 ;
  pBuffer = malloc (nBufferSize) ;

  bSuccess = DeviceIoControl (g_hDriver, IOCTL_GET_PROCESSLIST, 
			      NULL, 0,
			      pBuffer, nBufferSize,
			      &nBytesReturned, NULL) ;

  if( ! bSuccess ||  nBytesReturned==0 )
    {
      TRACE_ERROR (TEXT("Failed to get process list (error=%u)\n"),
		   GetLastError()) ;
      free (pBuffer) ;
      return FALSE ;
    }

  
  ProcList_Lock () ;

  ProcList_Clear () ;
  
  pEntry = pBuffer ;

  while( 1 )
    {
      PROCSTRUCT proc ;

      TRACE_INFO (TEXT("%d : %ls\n"), pEntry->nProcessId, pEntry->wszFilePath) ;

      proc.nProcessAddress	= pEntry->nProcessAddress ;
      proc.nProcessId		= pEntry->nProcessId ;
      proc.nState		= PS_HOOKED_WHILE_RUNNING ;
      wcslcpy (proc.szName, PathFindFileName(pEntry->wszFilePath), 32) ;
      wcslcpy (proc.szPath, pEntry->wszFilePath, MAX_PATH) ;

      ProcList_Add (&proc) ;

      if( pEntry->nNextEntry == 0 ) break ;
      
      pEntry = (PROCESSLISTENTRY*)( (BYTE*)pEntry + pEntry->nNextEntry ) ;
    }
  
  ProcList_Unlock () ;
    
  free (pBuffer) ;
  return TRUE ;
}
Exemplo n.º 9
0
/*
 * Replace all the words by their first letters, except the last one.
 *
 * Input:  /usr/local/share/doc
 * Output: /u/l/s/doc
 */
void
newsgroupize(wchar_t *s)
{
	wchar_t buffer[MAX_OUTPUT_LEN];
	wchar_t *last = NULL, *org = s;
	int idx = 0;

	/* Already as short as we can get it. */
	if (s == NULL || wcslen(s) < 3)
		return;

	/*
	 * The path doesn't start with a '/', could be an alias, could be '~'.
	 * Copy everything until the first slash.
	 */
	if (*s != L'/') {
		do {
			buffer[idx++] = *(s++);
		} while (*s != L'/' && *s != L'\0');
	}

	/* We already reached the end, that means the string was fine as-is. */
	if (*s == L'\0')
		return;

	// do I have a slash?
	// yes: copy one letter, loop
	// no: break

	/* For every component, add the first letter and a slash. */
	for (;;) {
		/* Copy the slash and move on. */
		buffer[idx++] = *(s++);
		last = s;

		/* Is there more to come? */
		if ((s = wcschr(s, L'/')) == NULL)
			break;

		/* Trailing slash? */
		if (*(s + 1) == L'\0')
			break;

		buffer[idx++] = (wchar_t)*last;
	}

	/* Copy whatever is left (override the trailing NUL byte on buffer) */
	wcslcpy(buffer + idx, last, sizeof(buffer) - idx);

	/* Copy letters+slash making sure the last part is left untouched. */
	wcslcpy(org, buffer, sizeof(buffer));
}
Exemplo n.º 10
0
/**
 * Reduce the given string with the global max length and filler.
 */
void
quickcut(wchar_t *s, size_t len)
{
	wchar_t t[MAXPATHLEN];
	size_t	filler_len = wcslen(cfg_filler), cl = sizeof(wchar_t);

	if (s == NULL || len == 0 || *s == '\0' || len <= cfg_maxpwdlen)
		return;

	wcslcpy(t, cfg_filler, filler_len + cl);
	wcslcpy(t + filler_len, s + len - cfg_maxpwdlen + filler_len,
			cfg_maxpwdlen - filler_len + cl);
	wcslcpy(s, t, cfg_maxpwdlen + cl);
}
Exemplo n.º 11
0
NTSTATUS _ScanCache_AddFile (LPCWSTR szFile, SCANCACHEID * pnId)
{
    SCANCACHEENTRY	*pEntry ;

    TRACE_INFO (TEXT("Adding file %ls (index = %d)\n"), szFile, g_data.iNextEntry) ;

    ASSERT (g_data.bInitialized) ;
    ASSERT (ScanCache_IsLocked()) ;

    pEntry = &g_data.pEntries[g_data.iNextEntry] ;

    if( g_data.nEntries < g_data.nEntriesMax )
        g_data.nEntries++ ;

    g_data.iNextEntry++ ;
    if( g_data.iNextEntry >= g_data.nEntriesMax )
        g_data.iNextEntry = 0 ;

    wcslcpy (pEntry->szFile, szFile, MAX_PATH) ;
    pEntry->nResult = SCAN_NOT_SCANNED ;
    pEntry->liScanTime.QuadPart = 0 ;
    pEntry->nIdentifier = g_data.nNextIdentifier++ ;

    *pnId = pEntry->nIdentifier ;

    return STATUS_SUCCESS ;
}
Exemplo n.º 12
0
wchar_t *wrealpath(const wchar_t *pathname, wchar_t *resolved_path)
{
	char *tmp = wutil_wcs2str(pathname);
	char *narrow_res = realpath( tmp, 0 );
	wchar_t *res;

	if( !narrow_res )
		return 0;

	if( resolved_path )
	{
		wchar_t *tmp2 = str2wcs( narrow_res );
		wcslcpy( resolved_path, tmp2, PATH_MAX );
		free( tmp2 );
		res = resolved_path;
	}
	else
	{
		res = str2wcs( narrow_res );
	}

    free( narrow_res );

	return res;
}
Exemplo n.º 13
0
wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path) {
    cstring narrow_path = wcs2string(pathname);
    char *narrow_res = realpath(narrow_path.c_str(), NULL);

    if (!narrow_res) return NULL;

    wchar_t *res;
    wcstring wide_res = str2wcstring(narrow_res);
    if (resolved_path) {
        wcslcpy(resolved_path, wide_res.c_str(), PATH_MAX);
        res = resolved_path;
    } else {
        res = wcsdup(wide_res.c_str());
    }

#if __APPLE__ && __DARWIN_C_LEVEL < 200809L
// OS X Snow Leopard is broken with respect to the dynamically allocated buffer returned by
// realpath(). It's not dynamically allocated so attempting to free that buffer triggers a
// malloc/free error. Thus we don't attempt the free in this case.
#else
    free(narrow_res);
#endif

    return res;
}
Exemplo n.º 14
0
/**
 * Reduce the given string to the smallest it could get to fit within
 * the global max length and without cutting any word.
 */
void
cleancut(wchar_t *s)
{
	int flen;
	wchar_t *last = NULL, t[MAXPATHLEN], *org = s;

	/* NULL or empty input, nothing to touch */
	if (s == NULL || *s == '\0')
		return;

	/* Nothing needs to be cropped */
	if (wcslen(s) <= cfg_maxpwdlen)
		return;

	/* As long as we can't fit 's' within the maxpwdlen, keep trimming */
	flen = wcslen(cfg_filler);
	while ((int)wcslen(s) > (cfg_maxpwdlen - flen)) {
		s++;
		s = wcschr(s, '/');
		if (s == NULL)
			break;
		last = s;
	}

	/* The last element was too long, keep it */
	if (s == NULL) {
		/* 
		 * last has never been touched, this means we only have
		 * one slash, revert s to its original value, there is
		 * nothing we can crop.
		 */
		if (last == NULL) {
			s = org;
			goto cleancut_final;
		} else {
			s = last;
		}
	}

	s -= flen;
	wcsncpy(s, cfg_filler, flen);

cleancut_final:
	wcslcpy(t, s, MAXPATHLEN);
	wcslcpy(org, t, MAXPATHLEN);
}
Exemplo n.º 15
0
void ndisks_save() {
    wchar_t tmp[PATH_MAX];
    if(available_disks == NULL || !available_disks->length) {
        return;
    }
    wcslcpy(tmp, my_dir, PATH_MAX);
    wcscat_s(tmp, PATH_MAX, _config_file);
    DeleteFileW(tmp);
    dict_traverse(available_disks, ndisk_save_enumerator, (void **)&tmp);
}
Exemplo n.º 16
0
/**
 * Add a new alias to the list.
 */
void
add_alias(wchar_t *name, wchar_t *value, int linenum)
{
	if (wcslen(value) < wcslen(name)) {
		fatal("prwdrc: alias name should not be longer than the value.\n");
		return;
	}
	if (wcschr(name, '/') != NULL) {
		fatal("prwdrc: alias name should not contain any '/' (slash).\n");
		return;
	}
	if (alias_count >= MAX_ALIASES - 1) {
		fatal("prwdrc: you cannot have more than %d aliases.\n", MAX_ALIASES);
		return;
	}
	wcslcpy(aliases[alias_count].name, name, ALIAS_NAME_LEN);
	wcslcpy(aliases[alias_count].path, value, MAXPATHLEN);
	alias_count++;
}
Exemplo n.º 17
0
wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path) {
    if (pathname.size() == 0) return NULL;

    cstring real_path("");
    cstring narrow_path = wcs2string(pathname);

    // Strip trailing slashes. This is needed to be bug-for-bug compatible with GNU realpath which
    // treats "/a//" as equivalent to "/a" whether or not /a exists.
    while (narrow_path.size() > 1 && narrow_path.at(narrow_path.size() - 1) == '/') {
        narrow_path.erase(narrow_path.size() - 1, 1);
    }

    char *narrow_res = realpath(narrow_path.c_str(), NULL);
    if (narrow_res) {
        real_path.append(narrow_res);
    } else {
        size_t pathsep_idx = narrow_path.rfind('/');
        if (pathsep_idx == 0) {
            // If the only pathsep is the first character then it's an absolute path with a
            // single path component and thus doesn't need conversion.
            real_path = narrow_path;
        } else {
            if (pathsep_idx == cstring::npos) {
                // No pathsep means a single path component relative to pwd.
                narrow_res = realpath(".", NULL);
                if (!narrow_res) DIE("unexpected realpath(\".\") failure");
                pathsep_idx = 0;
            } else {
                // Only call realpath() on the portion up to the last component.
                narrow_res = realpath(narrow_path.substr(0, pathsep_idx).c_str(), NULL);
                if (!narrow_res) return NULL;
                pathsep_idx++;
            }
            real_path.append(narrow_res);
            // This test is to deal with pathological cases such as /../../x => //x.
            if (real_path.size() > 1) real_path.append("/");
            real_path.append(narrow_path.substr(pathsep_idx, cstring::npos));
        }
    }
#if __APPLE__ && __DARWIN_C_LEVEL < 200809L
// OS X Snow Leopard is broken with respect to the dynamically allocated buffer returned by
// realpath(). It's not dynamically allocated so attempting to free that buffer triggers a
// malloc/free error. Thus we don't attempt the free in this case.
#else
    free(narrow_res);
#endif

    wcstring wreal_path = str2wcstring(real_path);
    if (resolved_path) {
        wcslcpy(resolved_path, wreal_path.c_str(), PATH_MAX);
        return resolved_path;
    }
    return wcsdup(wreal_path.c_str());
}
Exemplo n.º 18
0
Arquivo: main.c Projeto: pbleser/mdp
/*
 * Spawn the editor on a file.
 */
void
spawn_editor(char *path)
{
	char s[MAXPATHLEN];

	if (wcslen(cfg_editor) == 0) {
		if (wcslen(editor) == 0) {
			wcslcpy(cfg_editor, L"/usr/bin/vi", MAXPATHLEN);
		} else {
			wcslcpy(cfg_editor, editor, MAXPATHLEN);
		}
	}

	snprintf(s, MAXPATHLEN, "%ls \"%s\"", cfg_editor, path);

	debug("spawn_editor: %s", s);

	if (system(s) != 0)
		err(1, "unable to spawn editor: %s", s);
}
Exemplo n.º 19
0
BOOL __stdcall FsFindNextW(HANDLE Hdl,WIN32_FIND_DATAW *FindData) {
    FindStruc *pFind = (FindStruc *)Hdl;
    WIN32_FIND_DATAW *fData = NULL;
    NDisk *disk = NULL;
    if(pFind == NULL || pFind->isLast) {
        return FALSE;
    }
    if (wcscmp(pFind->path, L"\\") == 0) {
        memset(FindData, 0, sizeof(WIN32_FIND_DATAW));
        if(pFind->findHandle == NULL) {
            FindData->dwFileAttributes = FILE_ATTRIBUTE_READONLY;
            FindData->ftLastWriteTime.dwHighDateTime = 0xFFFFFFFF;
            FindData->ftLastWriteTime.dwLowDateTime = 0xFFFFFFFE;
            wcslcpy(FindData->cFileName, L"[²Ù×÷]ÐÂÔöÍøÅÌ", _countof(FindData->cFileName) - 1);
            pFind->isLast = TRUE;
            return TRUE;
        } else {
            disk = (NDisk *)pFind->findHandle->value;
            FindData->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
            FindData->ftLastWriteTime.dwHighDateTime = 0xFFFFFFFF;
            FindData->ftLastWriteTime.dwLowDateTime = 0xFFFFFFFE;
            wcslcpy(FindData->cFileName, disk->nickname, _countof(FindData->cFileName) - 1);
            pFind->findHandle = pFind->findHandle->next;
            return TRUE;
        }
    } else {
        if(pFind->findHandle != NULL) {
            fData = (WIN32_FIND_DATAW *)pFind->findHandle->value;
            FindData->dwFileAttributes = fData->dwFileAttributes;
            FindData->nFileSizeHigh = fData->nFileSizeHigh;
            FindData->nFileSizeLow = fData->nFileSizeLow;
            FindData->ftCreationTime = fData->ftCreationTime;
            FindData->ftLastWriteTime = fData->ftLastWriteTime;
            FindData->ftLastAccessTime = fData->ftLastAccessTime;
            wcslcpy(FindData->cFileName, fData->cFileName, _countof(FindData->cFileName) - 1);
            pFind->findHandle = pFind->findHandle->next;
            return TRUE;
        }
    }
    return FALSE;
}
Exemplo n.º 20
0
size_t
SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen)
{
#if defined(HAVE_WCSLCPY)
    return wcslcpy(dst, src, maxlen);
#else
    size_t srclen = SDL_wcslen(src);
    if (maxlen > 0) {
        size_t len = SDL_min(srclen, maxlen - 1);
        SDL_memcpy(dst, src, len * sizeof(wchar_t));
        dst[len] = '\0';
    }
    return srclen;
#endif /* HAVE_WCSLCPY */
}
Exemplo n.º 21
0
int __stdcall FsExecuteFileW(HWND MainWin, WCHAR* RemoteName, WCHAR* Verb) {
    INT_PTR dlgResult = 0;
    if(wcscmp(Verb, L"open") == 0 && wcscmp(RemoteName, L"\\[²Ù×÷]ÐÂÔöÍøÅÌ") == 0) {
        if(!available_disk_entries_length) {
            RequestProcW(PluginNumber, RT_MsgOK, L"ÅäÖôíÎó", L"ÍøÅ̽ű¾´íÎó, ûÓÐÉèÖÃÈκÎÓÐЧµÄÍøÅÌÅäÖÃÎļþ. ", NULL, 0);
            return FS_EXEC_ERROR;
        }
        dlgResult = DialogBoxW((HINSTANCE)hInst, MAKEINTRESOURCEW(IDD_NEW), GetActiveWindow(), NewdiskProc);
        if(dlgResult == IDOK) {
            wcslcpy(RemoteName, L"\\", PATH_MAX);
            return FS_EXEC_SYMLINK;
        }
        return FS_EXEC_OK;
    }
    return FS_EXEC_ERROR;
}
Exemplo n.º 22
0
/*
 * Add the UID indicator.
 *
 * Input:              /etc
 * Output if non-root: /etc$
 * Output if root:     /etc#
 */
void
add_uid_indicator(wchar_t *s)
{
	wchar_t buf[MAX_OUTPUT_LEN];
	wchar_t c;

	if (getuid() == 0) {
		c = L'#';
	} else {
		c = L'$';
	}

	swprintf(buf, MAX_OUTPUT_LEN, L"%ls%lc", s, c);

	wcslcpy(s, buf, MAX_OUTPUT_LEN);
}
Exemplo n.º 23
0
wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path) {
    cstring tmp = wcs2string(pathname);
    char narrow_buff[PATH_MAX];
    char *narrow_res = realpath(tmp.c_str(), narrow_buff);
    wchar_t *res;

    if (!narrow_res) return 0;

    const wcstring wide_res = str2wcstring(narrow_res);
    if (resolved_path) {
        wcslcpy(resolved_path, wide_res.c_str(), PATH_MAX);
        res = resolved_path;
    } else {
        res = wcsdup(wide_res.c_str());
    }
    return res;
}
Exemplo n.º 24
0
/*
 * Dump all the aliases starting with $ as shell variable. This output is meant
 * to be used with eval in your profile file.
 *
 * This thing could be doing some shell escaping on the path, but I'd be
 * surprised anyone using this software would have such monstrosities on their
 * computers.
 */
void
dump_alias_vars(void)
{
	int i;
	wchar_t path[MAX_OUTPUT_LEN];

	for (i = 0; i < alias_count; i++) {
		if (aliases[i].name[0] == '$') {
			wcslcpy(path, aliases[i].path, MAX_OUTPUT_LEN);
			expand_prefix_aliases(path, MAX_OUTPUT_LEN);
			if (!wc_file_exists(path))
				continue;
			/* Skip the '$' */
			wprintf(L"export %ls=\"%ls\"\n", aliases[i].name + 1,
					path);
		}
	}
}
Exemplo n.º 25
0
BOOL _SpySrv_Log (PROCADDR nProcessAddr, PCFILTCOND pCond,
		  DWORD nReaction, BOOL bAlert)
{
  UINT		nProcessId ;
  DWORD		dwEventId ;
  TCHAR		szProcess[MAX_PATH] = TEXT("???") ;
  
  TRACE ; 

  // get process name
  {
    PROCSTRUCT * pProc ;

    ProcList_Lock () ;
    pProc = ProcList_Get (nProcessAddr) ;

    nProcessId = pProc->nProcessId ;

    if( pProc ) {
      wcslcpy (szProcess, pProc->szPath, MAX_PATH) ;
    } else {
      wsprintf (szProcess, TEXT("Process %d"), nProcessId) ;
      TRACE_WARNING (TEXT("Process %d not in list (addr=0x08X)\n"), 
		     nProcessId, nProcessAddr) ;
    }

    ProcList_Unlock () ;
  }
    
  dwEventId = EventLog_Add (nProcessId, szProcess, nReaction,
			    bAlert ? RULE_ALERT : RULE_LOG,
			    pCond) ;
  
  if( bAlert ) 
    {
      Sounds_Play (SOUND_ALERT) ;
      PostMessage (g_hwndMain, WM_SPYNOTIFY, SN_ALERT, dwEventId) ;
    }
  
  PostMessage (g_hwndMain, WM_SPYNOTIFY, SN_EVENTLOGGED, dwEventId) ;

  return TRUE ;
}
Exemplo n.º 26
0
/**
 * Sets the value of the given variable, also do some type check
 * just in case.
 */
void
set_variable(wchar_t *name, wchar_t *value, int linenum)
{
	/* set maxlength <int> */
	if (wcscmp(name, L"maxlength") == 0) {
		if (value == NULL || *value == '\0') {
			cfg_maxpwdlen = 0;
			return;
		}
		cfg_maxpwdlen = wcstoumax(value, NULL, 10);
		if (cfg_maxpwdlen > 255)
			fatal("prwdrc: invalid number for set maxlength.\n");

	/* set filler <string> */
	} else if (wcscmp(name, L"filler") == 0) {
		if (value == NULL || *value == '\0') {
			*cfg_filler = '\0';
			return;
		}
		wcslcpy(cfg_filler, value, FILLER_LEN);

	/* set cleancut <bool> */
	} else if (wcscmp(name, L"cleancut") == 0) {
		cfg_cleancut = (value != NULL && *value == 'o') ? 1 : 0;

	/* set mercurial <bool> */
	} else if (wcscmp(name, L"mercurial") == 0) {
		cfg_mercurial = (value != NULL && *value == 'o') ? 1 : 0;

	/* set git <bool> */
	} else if (wcscmp(name, L"git") == 0) {
		cfg_git = (value != NULL && *value == 'o') ? 1 : 0;

	/* set newsgroup <bool> */
	} else if (wcscmp(name, L"newsgroup") == 0) {
		cfg_newsgroup = (value != NULL && *value == 'o') ? 1 : 0;

	/* ??? */
	} else {
		fatal("prwdrc: unknown variable for set on line %d.\n", linenum);
	}
}
Exemplo n.º 27
0
void ndisks_load() {
    wchar_t tmp[SECTIONS_BUFFER_SIZE], sTmp[SECTIONS_BUFFER_SIZE], *pTmp, rTmp[SECTIONS_BUFFER_SIZE];
    size_t i = 0, j = 0;
    NDisk disk;
    if(available_disks == NULL) {
        return;
    }
    wcslcpy(tmp, my_dir, SECTIONS_BUFFER_SIZE);
    wcscat_s(tmp, SECTIONS_BUFFER_SIZE, _config_file);
    GetPrivateProfileSectionNamesW(sTmp, SECTIONS_BUFFER_SIZE, tmp);
    for(i = 0; i < SECTIONS_BUFFER_SIZE; i++) {
        if(sTmp[0] == '\0') {
            break;
        }
        if(sTmp[i] == '\0') {
            pTmp = sTmp + j;
            if(!pTmp || wcscmp(pTmp, L"") == 0) {
                continue;
            }
            j = i + 1;
            memset(&disk, 0, sizeof(NDisk));
            GetPrivateProfileStringW(pTmp, L"type", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
            if(!rTmp || wcscmp(rTmp, L"") == 0) {
                continue;
            }
            disk.type = _wcsdup(rTmp);
            GetPrivateProfileStringW(pTmp, L"username", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
            if(!rTmp || wcscmp(rTmp, L"") == 0) {
                free(disk.type);
                continue;
            }
            disk.username = _wcsdup(rTmp);
            disk.nickname = _wcsdup(pTmp);
            GetPrivateProfileStringW(pTmp, L"token", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
            disk.token = _wcsdup(rTmp);
            GetPrivateProfileStringW(pTmp, L"secret", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
            disk.secret = _wcsdup(rTmp);
            dict_set_element_s(available_disks, pTmp, &disk, sizeof(NDisk), ndisk_destroy_s);
        }
    }
}
Exemplo n.º 28
0
wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path)
{
    cstring narrow_path = wcs2string(pathname);
    char *narrow_res = realpath(narrow_path.c_str(), NULL);

    if (!narrow_res)
        return NULL;

    wchar_t *res;
    wcstring wide_res = str2wcstring(narrow_res);
    if (resolved_path)
    {
        wcslcpy(resolved_path, wide_res.c_str(), PATH_MAX);
        res = resolved_path;
    }
    else
    {
        res = wcsdup(wide_res.c_str());
    }

    free(narrow_res);

    return res;
}
Exemplo n.º 29
0
DWORD _SpySrv_RequestFromDriver (LPVOID pBuffer, DWORD nSize) 
{
  SDNMHDR	*p = pBuffer ;
  VOID		*pSerial ;
  UINT		nSerialSize ;
  DWORD		nResponseSize = 0 ;

  TRACE ; 

  ASSERT (pBuffer) ;
  ASSERT (nSize>0) ;
  ASSERT (nSize>=sizeof(SDNMHDR)) ;
  
  TRACE_INFO (TEXT(" /----REQ-%d----\\ (size=%d\n"), p->dwCode, nSize) ;

  switch( p->dwCode )
    {
    case SDN_ASK:
      {
	DWORD		nReaction ;
	FILTCOND	cond ;

	pSerial = ((SDNASK*)p)->data ;
	nSerialSize = nSize - sizeof(SDNASK) ;
	
	ASSERT (nSerialSize>0) ;

	if( ! FiltCond_Unserialize (&cond, pSerial, nSerialSize) )
	  {
	    TRACE_ERROR (TEXT("FiltCond_Unserialize failed\n")) ;
	    nReaction = RULE_ACCEPT ;	    
	  }
	else
	  {   
	    nReaction = _SpySrv_Ask (((SDNASK*)p)->nProcessAddress, 
				     ((SDNASK*)p)->nDefReaction, 
				     &cond) ;
	  }
	
	*((DWORD*)pBuffer) = nReaction ;
	nResponseSize = sizeof(DWORD) ;
      }
      break ;

    case SDN_LOG:
    case SDN_ALERT:
      {
	DWORD		nReaction ;
	FILTCOND	cond ;

	nReaction = ((SDNLOG*)p)->dwReaction ;

	pSerial = ((SDNLOG*)p)->data ;
	nSerialSize = nSize - sizeof(SDNLOG) ;

	if( ! FiltCond_Unserialize (&cond, pSerial, nSerialSize) )
	  {
	    TRACE_ERROR (TEXT("FiltCond_Unserialize failed\n")) ;
	    nReaction = RULE_ACCEPT ;	    
	  }
	else
	  {
	    _SpySrv_Log (((SDNLOG*)p)->nProcessAddress, &cond, nReaction, p->dwCode==SDN_ALERT) ;
	  }
      }
      break ;

    case SDN_SCANFILE:
      {
	DWORD		nScanResult ;

	nScanResult = SpySrv_ScanFile (((SDNSCANFILE*)pBuffer)->wszFilePath, FALSE) ;

	*((DWORD*)pBuffer) = nScanResult ;
	nResponseSize = sizeof(DWORD) ;
      }
      break ;

    case SDN_PROCESSCREATED:
      {
	SDNPROCESSCREATED * pSdnpc = pBuffer ;
	PROCSTRUCT	proc ;
	
	proc.nProcessAddress	= pSdnpc->nProcessAddress ;
	proc.nProcessId		= pSdnpc->nProcessId ;
	proc.nState		= PS_HOOKED_SINCE_BIRTH ;
	wcslcpy (proc.szName, PathFindFileName(pSdnpc->wszFilePath), 32) ;
	wcslcpy (proc.szPath, pSdnpc->wszFilePath, MAX_PATH) ;
	
	ProcList_Lock () ;
	ProcList_Add (&proc) ;
	ProcList_Unlock () ;
	  
	PostMessage (g_hwndMain, WM_SPYNOTIFY, SN_PROCESSCREATED, pSdnpc->nProcessAddress) ;
      }
      break ;

    case SDN_PIDCHANGED:
      {
  	SDNPIDCHANGED	*pSdnpc = pBuffer ;
  	PROCSTRUCT	*pProc ;

	ProcList_Lock () ;	
	pProc = ProcList_Get (pSdnpc->nProcessAddress) ;
	if( pProc ) 
	  {
	    TRACE_ALWAYS (TEXT("PID changed %d -> %d\n"), pProc->nProcessId, pSdnpc->nNewProcessId) ; 
	    pProc->nProcessId = pSdnpc->nNewProcessId ;  
	  }
	ProcList_Unlock () ;

	// This notification has been disabled because it caused a dead-lock.
	// PostMessage (g_hwndMain, WM_SPYNOTIFY, SN_PIDCHANGED, pSdnpc->nProcessAddress) ;
      }
      break ;

    case SDN_PROCESSTERMINATED:
      {
	SDNPROCESSTERMINATED * pSdnpt = pBuffer ;

	TRACE_INFO (TEXT("Process terminated 0x%08X\n"),pSdnpt->nProcessAddress) ; 
	
	ProcList_Lock () ;
	ProcList_Remove (pSdnpt->nProcessAddress) ;
	ProcList_Unlock () ;
	  
	PostMessage (g_hwndMain, WM_SPYNOTIFY, SN_PROCESSTERMINATED, pSdnpt->nProcessAddress) ;
      }
      break ;

    default:

      TRACE_WARNING (TEXT("Driver request not handled (code=%d)\n"),  p->dwCode) ;
    }

  TRACE_INFO (TEXT(" \\----ANS------/\n")) ;

  return nResponseSize ;
}
Exemplo n.º 30
0
DWORD _SpySrv_Ask (PROCADDR nProcessAddress, UINT nDefReaction, PFILTCOND pCond)
{
  TCHAR		szProcess[MAX_PATH] = TEXT("???") ;
  UINT		nProcessId ;
  int		nResult ;  
  
  TRACE ; 
  
  // get process name
  {
    PROCSTRUCT * pProc ;
    
    ProcList_Lock () ;
    pProc = ProcList_Get (nProcessAddress) ;

    nProcessId = pProc->nProcessId ;
    
    if( pProc ) {
      wcslcpy (szProcess, pProc->szPath, MAX_PATH) ;
    } else {
      wsprintf (szProcess, TEXT("Process %d"), nProcessId) ;
      TRACE_WARNING (TEXT("Process %d not in list (addr=0x%08X\n"), 
		     nProcessId, nProcessAddress) ;
    }
    
    ProcList_Unlock () ;
  }
  
  Sounds_Play (SOUND_ASK) ;
  
  TRACE_INFO (TEXT("  /----ASK----\\\n")) ;

 askdlg:

  nResult = AskDlg_DialogBox (g_hInstance, g_hwndMain, szProcess, nProcessId, nDefReaction, pCond) ;

  if( nResult==ASKDLG_CREATEFILTER )
    {
      FILTRULE	* pRule ;
	  
      // alloc new rule
      pRule = (FILTRULE*) calloc (1, sizeof(FILTRULE)) ;

      // fill params as they appear in the ask dialog
      pRule->nReaction	= nDefReaction ;
      pRule->nVerbosity	= RULE_LOG ;
      pRule->nOptions	= 0 ;
      FiltCond_Dup (&pRule->condition, pCond) ;
      
      // show rule dialog
      if( IDOK!=RuleDlg_DialogBox (g_hInstance, g_hwndMain, szProcess, pRule, FALSE) )
	{
	  free (pRule) ;
	  goto askdlg ;
	}

      // verify that this rule matches the current event
      if( ! FiltCond_Check(&pRule->condition,pCond) )
	{
	  MessageBox (g_hwndMain, 
		      STR_DEF(_RULE_DOESNT_MATCH,
			      TEXT("The rule you defined doesn't match current event")), 
		      TEXT(APPLICATION_NAME), MB_ICONERROR) ;
	  free (pRule) ;
	  goto askdlg ;	      
	}

      SpySrv_AddRuleForProgram (pRule, szProcess) ;
      
      nResult = pRule->nReaction ;
    }

  if( nResult==ASKDLG_UNHOOK )
    {
      SpySrv_IgnoreProcess (nProcessAddress, TRUE) ;    
      nResult = RULE_ACCEPT ;
    }

  TRACE_INFO (TEXT("  \\----ASK-%d--/\n"), nResult) ;

  return nResult ;
}