⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Various utilities
   Copyright (C) 1994, 1995, 1996 the Free Software Foundation.
   Written 1994, 1995, 1996 by:
   Miguel de Icaza, Janne Kukonlehto, Dugan Porter,
   Jakub Jelinek, Mauricio Plaza.

   The file_date routine is mostly from GNU's fileutils package,
   written by Richard Stallman and David MacKenzie.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <config.h>
#include <stdio.h>
#if defined(__os2__)            /* OS/2 need io.h! .ado */
#    include <io.h>
#endif
#include <stdlib.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>
#include <signal.h>		/* my_system */
#include <limits.h>		/* INT_MAX */
#ifndef SCO_FLAVOR
#if defined(_MSC_VER)
#	include <sys/time.h___>
#else
#	include <sys/time.h>	/* alex: sys/select.h defines struct timeval */
#endif
#endif /* SCO_FLAVOR */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <errno.h>		/* my_system */
#ifdef SCO_FLAVOR
#	include <sys/timeb.h>	/* alex: for struct timeb, used in time.h */
#endif /* SCO_FLAVOR */
#include <time.h>
#ifndef OS2_NT
#   include <pwd.h>
#   include <grp.h>
#endif
#include <string.h>
#include <ctype.h>
#ifdef HAVE_SYS_SELECT_H
#  include <sys/select.h>
#endif

#ifdef __linux__
#    if defined(__GLIBC__) && (__GLIBC__ < 2)
#        include <linux/termios.h>	/* This is needed for TIOCLINUX */
#    else
#        include <termios.h>
#    endif
#  include <sys/ioctl.h>
#endif

#include "fs.h"
#include "mountlist.h"

/* From dialog.h (not wanting to include it as
   it requires including a lot of other files, too) */
int message (int error, char *header, char *text, ...);

#include "mad.h"
#if defined(HAVE_RX_H) && defined(HAVE_REGCOMP)
#include <rx.h>
#else
#include "regex.h"
#endif
#include "util.h"
#include "global.h"
#include "profile.h"
#include "user.h"		/* expand_format */
#include "../vfs/vfs.h"

/* "$Id: util.c 15091 2005-05-07 21:24:31Z sedwards $" */

char app_text [] = "Midnight-Commander";

int easy_patterns = 1;
int align_extensions = 1;
int tilde_trunc = 1;

struct mount_entry *mount_list = NULL;

#ifndef HAVE_STRDUP
char *strdup (const char *s)
{
    char *t = malloc (strlen (s)+1);
    strcpy (t, s);
    return t;
}
#endif

int is_printable (int c)
{
    static const unsigned char xterm_printable[] = {
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
        1,1,1,1,0,0,1,1,0,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
    };

    extern int xterm_flag;
    extern int eight_bit_clean;
    extern int full_eight_bits;

    c &= 0xff;
    if (eight_bit_clean){
        if (full_eight_bits){
	    if (xterm_flag)
	        return xterm_printable [c];
	    else
	        return (c > 31 && c != 127);
	} else
	    return ((c >31 && c < 127) || c >= 160);
    } else
	return (c > 31 && c < 127);
}

/* Returns the message dimensions (lines and columns) */
int msglen (char *text, int *lines)
{
    int max = 0;
    int line_len = 0;

    for (*lines = 1;*text; text++){
	if (*text == '\n'){
	    line_len = 0;
	    (*lines)++;
	} else {
	    line_len++;
	    if (line_len > max)
		max = line_len;
	}
    }
    return max;
}

char *trim (char *s, char *d, int len)
{
    int source_len = strlen (s);

    if (source_len > len){
	strcpy (d, s+(source_len-len));
	d [0] = '.';
	d [1] = '.';
	d [2] = '.';
    } else
	strcpy (d, s);
    return d;
}

char *
name_quote (const char *s, int quote_percent)
{
    char *ret, *d;

    d = ret = xmalloc (strlen (s)*2 + 2 + 1, "quote_name");
    if (*s == '-') {
        *d++ = '.';
        *d++ = '/';
    }

    for (; *s; s++, d++) {
	switch (*s)
	{
	    case '%':
		if (quote_percent)
		    *d++ = '%';
		break;
	    case '\'':
	    case '\\':
	    case '\r':
	    case '\n':
	    case '\t':
	    case '$':
	    case '?':
	    case '*':
	    case '(':
	    case ')':
	    case '[':
	    case ']':
	    case '"':
	    case '!':
	    case '&':
	    case '#':
	    case '`':
	    case ' ':
		*d++ = '\\';
	}
	*d = *s;
    }
    *d = '\0';
    return ret;
}

char *
fake_name_quote (const char *s, int quote_percent)
{
    return strdup (s);
}

/* If passed an empty txt (this usually means that there is an error)
 * in the upper layers, we return "/"
 */
char *name_trunc (char *txt, int trunc_len)
{
    static char x [MC_MAXPATHLEN+MC_MAXPATHLEN];
    int    txt_len;
    char *p;

    if (!txt)
	txt = PATH_SEP_STR;

    if (trunc_len > sizeof (x)-1){
	fprintf (stderr, _("name_trunc: too big"));
	trunc_len = sizeof (x)-1;
    }
    txt_len = strlen (txt);
    if (txt_len <= trunc_len)
	strcpy (x, txt);
    else if (tilde_trunc){
	int y = trunc_len % 2;
	strncpy (x, txt, (trunc_len/2)+y);
	strncpy (x+(trunc_len/2)+y, txt+txt_len-(trunc_len/2), trunc_len/2);
	x [(trunc_len/2)+y] = '~';
    } else {
	strncpy (x, txt, trunc_len-1);
	x [trunc_len-1] = '>';
    }
    x [trunc_len] = 0;
    for (p = x; *p; p++)
        if (!is_printable (*p))
            *p = '?';
    return x;
}

char *size_trunc (long int size)
{
    static char x [30];
    long int divisor = 1;
    char *xtra = "";

    if (size > 999999999L){
	divisor = 1024;
	xtra = "kb";
	if (size/divisor > 999999999L){
	    divisor = 1024*1024;
	    xtra = "Mb";
	}
    }
    sprintf (x, "%ld%s", (size/divisor), xtra);
    return x;
}

char *size_trunc_sep (long int size)
{
    static char x [60];
    int  count;
    char *p, *d, *y;

    p = y = size_trunc (size);
    p += strlen (p) - 1;
    d = x + sizeof (x) - 1;
    *d-- = 0;
    while (p >= y && isalpha (*p))
	*d-- = *p--;
    for (count = 0; p >= y; count++){
	if (count == 3){
	    *d-- = ',';
	    count = 0;
	}
	*d-- = *p--;
    }
    d++;
    if (*d == ',')
	d++;
    return d;
}

int is_exe (mode_t mode)
{
    if ((S_IXUSR & mode) || (S_IXGRP & mode) || (S_IXOTH & mode))
	return 1;
    return 0;
}

#define ismode(n,m) ((n & m) == m)

char *string_perm (mode_t mode_bits)
{
    static char mode [11];

    strcpy (mode, "----------");
    if (ismode (mode_bits, S_IFDIR)) mode [0] = 'd';
#ifdef S_IFSOCK
    if (ismode (mode_bits, S_IFSOCK)) mode [0] = 's';
#endif
    if (ismode (mode_bits, S_IXOTH)) mode [9] = 'x';
    if (ismode (mode_bits, S_IWOTH)) mode [8] = 'w';
    if (ismode (mode_bits, S_IROTH)) mode [7] = 'r';
    if (ismode (mode_bits, S_IXGRP)) mode [6] = 'x';
    if (ismode (mode_bits, S_IWGRP)) mode [5] = 'w';
    if (ismode (mode_bits, S_IRGRP)) mode [4] = 'r';
    if (ismode (mode_bits, S_IXUSR)) mode [3] = 'x';
    if (ismode (mode_bits, S_IWUSR)) mode [2] = 'w';
    if (ismode (mode_bits, S_IRUSR)) mode [1] = 'r';
#ifndef OS2_NT
    if (ismode (mode_bits, S_ISUID)) mode [3] = (mode [3] == 'x') ? 's' : 'S';
    if (ismode (mode_bits, S_ISGID)) mode [6] = (mode [6] == 'x') ? 's' : 'S';
    if (ismode (mode_bits, S_IFCHR)) mode [0] = 'c';
    if (ismode (mode_bits, S_IFBLK)) mode [0] = 'b';
    if (ismode (mode_bits, S_ISVTX)) mode [9] = (mode [9] == 'x') ? 't' : 'T';
    if (ismode (mode_bits, S_IFLNK)) mode [0] = 'l';
    if (ismode (mode_bits, S_IFIFO)) mode [0] = 's';
#endif
    return mode;
}

static char *
strip_password (char *path)
{
    char *at, *inner_colon, *dir;

    if ((dir = strchr (path, PATH_SEP)) != NULL)
	*dir = '\0';
    /* search for any possible user */
    at = strchr (path, '@');

    /* We have a username */
    if (at) {
        *at = 0;
        inner_colon = strchr (path, ':');
	*at = '@';
        if (inner_colon)
            strcpy (inner_colon, at);
    }
    if (dir)
	*dir = PATH_SEP;
    return (path);
}

char *strip_home_and_password(char *dir)
{
    static char newdir [MC_MAXPATHLEN], *p, *q;

    if (home_dir && !strncmp (dir, home_dir, strlen (home_dir))){
	newdir [0] = '~';
	strcpy (&newdir [1], &dir [strlen (home_dir)]);
	return newdir;
    }
#ifdef USE_NETCODE
    else if (!strncmp (dir, "ftp://", 6)) {
	strip_password (strcpy (newdir, dir) + 6);
        if ((p = strchr (newdir + 6, PATH_SEP)) != NULL) {
            *p = 0;
	    q = ftpfs_gethome (newdir);
	    *p = PATH_SEP;
            if (q != 0 && strcmp (q, PATH_SEP_STR) && !strncmp (p, q, strlen (q) - 1)) {
                strcpy (p, "/~");
                strcat (newdir, p + strlen (q) - 1);
            }
        }
        return newdir;
    } else if (!strncmp (dir, "mc:", 3)) {
        char  *pth;
	strcpy (newdir, dir);
	if (newdir[3] == '/' && newdir[4] == '/') {
	    pth = newdir + 5;
    	    strip_password ( newdir + 5);
	} else {
	    pth = newdir + 3;
	    strip_password (newdir + 3);
	}
        if ((p = strchr (pth, PATH_SEP)) != NULL) {
            *p = 0;
	    q = mcfs_gethome (newdir);
            *p = PATH_SEP;
            if (q != NULL ) {
		if (strcmp (q, PATH_SEP_STR) && !strncmp (p, q, strlen (q) - 1)) {
                   strcpy (p, "/~");
                   strcat (newdir, p + strlen (q) - 1);
		}
                free (q);
            }
        }
	return (newdir);
    }
#endif
    return dir;
}

static char *maybe_start_group (char *d, int do_group, int *was_wildcard)
{
    if (!do_group)
	return d;
    if (*was_wildcard)
	return d;
    *was_wildcard = 1;
    *d++ = '\\';
    *d++ = '(';
    return d;
}

static char *maybe_end_group (char *d, int do_group, int *was_wildcard)
{
    if (!do_group)
	return d;
    if (!*was_wildcard)
	return d;
    *was_wildcard = 0;
    *d++ = '\\';
    *d++ = ')';
    return d;
}

/* If shell patterns are on converts a shell pattern to a regular
   expression. Called by regexp_match and mask_rename. */
/* Shouldn't we support [a-fw] type wildcards as well ?? */
char *convert_pattern (char *pattern, int match_type, int do_group)
{
    char *s, *d;
    char *new_pattern;
    int was_wildcard = 0;

    if (easy_patterns){
	new_pattern = malloc (MC_MAXPATHLEN);
	d = new_pattern;
	if (match_type == match_file)
	    *d++ = '^';
	for (s = pattern; *s; s++, d++){
	    switch (*s){
	    case '*':
		d = maybe_start_group (d, do_group, &was_wildcard);
		*d++ = '.';
		*d   = '*';
		break;

	    case '?':
		d = maybe_start_group (d, do_group, &was_wildcard);
		*d = '.';
		break;

	    case '.':
		d = maybe_end_group (d, do_group, &was_wildcard);
		*d++ = '\\';
		*d   = '.';
		break;

	    default:
		d = maybe_end_group (d, do_group, &was_wildcard);
		*d = *s;
		break;
	    }
	}
	d = maybe_end_group (d, do_group, &was_wildcard);
	if (match_type == match_file)
	    *d++ = '$';
	*d = 0;
	return new_pattern;
    } else
	return strdup (pattern);
}

int regexp_match (char *pattern, char *string, int match_type)
{
    static regex_t r;
    static char *old_pattern = NULL;
    static int old_type;
    int    rval;

    if (!old_pattern || STRCOMP (old_pattern, pattern) || old_type != match_type){
	if (old_pattern){
	    regfree (&r);
	    free (old_pattern);
	}
	pattern = convert_pattern (pattern, match_type, 0);
	if (regcomp (&r, pattern, REG_EXTENDED|REG_NOSUB|MC_ARCH_FLAGS)) {
	    free (pattern);
	    return -1;
	}
	old_pattern = pattern;
	old_type = match_type;
    }
    rval = !regexec (&r, string, 0, NULL, 0);
    return rval;
}

char *extension (char *filename)
{
    char *d;

    if (!strlen (filename))
	return "";

    d = filename + strlen (filename) - 1;
    for (;d >= filename; d--){
	if (*d == '.')
	    return d+1;
    }
    return "";
}

/* This routine uses the fact that x is at most 14 chars or so */
char *split_extension (char *x, int pad)
{
    return x;

    /* Buggy code
    if (!align_extensions)
	return x;

    if (strlen (x) >= pad)
	return x;

    if ((ext = extension (x)) == x || *ext == 0)
	return x;

    strcpy (xbuf, x);
    for (i = strlen (x); i < pad; i++)
	xbuf [i] = ' ';
    xbuf [pad] = 0;

    l = strlen (ext);
    for (i = 0; i < l; i++)
	xbuf [pad-i] = *(ext+l-i-1);
    for (i = xbuf + (ext - x); i <
    return xbuf; */
}

#ifndef HAVE_MAD
void *do_xmalloc (int size)
{
    void *m = malloc (size);

    if (!m){
	fprintf (stderr, "memory exhausted\n");
	exit (1);
    }
    return m;
}
#endif /* HAVE_MAD */

int get_int (char *file, char *key, int def)
{
    return GetPrivateProfileInt (app_text, key, def, file);
}

int set_int (char *file, char *key, int value)
{
    char buffer [30];

    sprintf (buffer,  "%d", value);
    return WritePrivateProfileString (app_text, key, buffer, file);
}

int exist_file (char *name)
{
    return access (name, R_OK) == 0;
}

char *load_file (char *filename)
{
    FILE *data_file;
    struct stat s;
    char *data;
    long read_size,i;

    if (stat (filename, &s) != 0){
	return 0;
    }
#ifdef OS2_NT
    if ((data_file = fopen (filename, "rt")) == NULL){
#else
    if ((data_file = fopen (filename, "r")) == NULL){
#endif
	return 0;
    }
    data = (char *) xmalloc (s.st_size+1, "util, load_file");
#ifdef OS2_NT
    memset(data,0,s.st_size+1);
#endif
    read_size = fread (data, 1, s.st_size, data_file);
    data [read_size] = 0;
    fclose (data_file);

    if (read_size > 0){
	return data;
    }
    else {
	free (data);
	return 0;
    }
}

char *file_date (time_t when)
{
    static char timebuf [40];
    time_t current_time = time ((time_t) 0);

#ifdef OS2_NT
    char   *p;

    p = ctime (&when);
    strcpy (timebuf, p ? p : "-----");
#else
    strcpy (timebuf, ctime (&when));
#endif
    if (current_time > when + 6L * 30L * 24L * 60L * 60L /* Old. */
	|| current_time < when - 60L * 60L) /* In the future. */
    {
	/* The file is fairly old or in the future.
	   POSIX says the cutoff is 6 months old;
	   approximate this by 6*30 days.
	   Allow a 1 hour slop factor for what is considered "the future",
	   to allow for NFS server/client clock disagreement.
	   Show the year instead of the time of day.  */
	strcpy (timebuf + 11, timebuf + 19);
    }
    timebuf[16] = 0;
    return &timebuf [4];
}

/* Like file_date, but packs the data to fit in 10 columns */
char *file_date_pck (time_t when)
{
    /* FIXME: Should return only 10 chars, not 14 */
    return file_date (when);
}

char *extract_line (char *s, char *top)
{
    static char tmp_line [500];
    char *t = tmp_line;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -