File: mpv_unix_common.c

package info (click to toggle)
mped 3.3.17-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,288 kB
  • ctags: 1,635
  • sloc: ansic: 13,150; sh: 393; makefile: 232; perl: 66
file content (311 lines) | stat: -rw-r--r-- 5,438 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*

    mpv_unix_common.c

    Code common to Unix versions (curses / X).

    mp - Programmer Text Editor

    Copyright (C) 1991/2005 Angel Ortega <angel@triptico.com>

    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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    http://www.triptico.com

*/

#include "config.h"

#if defined(CONFOPT_UNIX_LIKE)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "mp_core.h"
#include "mp_video.h"
#include "mp_iface.h"
#include "mp_lang.h"
#include "mp_synhi.h"
#include "mp_conf.h"

#if defined(CONFOPT_GLOB_H)
#include <glob.h>
#endif

/*******************
	Data
********************/

/*******************
	Code
********************/

int _unix_strcasecmp(char * s1, char * s2)
{
	return(strcasecmp(s1,s2));
}


/**
 * _mpv_strip_cwd - Strips current working directory
 * @buf: buffer containing a file name
 * @size: size of the buffer
 *
 * If the file name contained in @buf begins with
 * the current working directory, strips it (effectively
 * converting an absolute pathname to relative). If
 * the file name path is different from the
 * current working directory, nothing is done.
 */
void _mpv_strip_cwd(char * buf, int size)
{
	char tmp[1024];
	int l;
	char * ptr;

	if(getcwd(tmp,sizeof(tmp))==NULL)
		return;

	l=strlen(tmp);
	if(strncmp(tmp,buf,l)==0)
	{
		ptr=&buf[l];
		if(*ptr=='/') ptr++;

		strncpy(tmp,ptr,sizeof(tmp));
		strncpy(buf,tmp,size);
	}
}


FILE * _unix_fopen(char * file, char * mode)
{
	FILE * f=NULL;

	if(strcmp(file, "-") == 0)
	{
		int fd;

		/* if file is -, duplicate stdin or stdout
		   depending on the output mode */
		fd=dup(mode[0] == 'w' ? 1 : 0);
		f=fdopen(fd, mode);
	}
	else
	if(_mpi_break_hardlinks && !strcmp(mode, "w"))
	{
		struct stat st;

		if(stat(file, &st) != -1)
		{
			/* file exists and metainformation
			   is stored in st; unlink now */
			unlink(file);

			if((f=fopen(file, mode)) != NULL)
			{
				/* tries to restore original owner
				   and file permissions */
				fchmod(fileno(f), st.st_mode);
				fchown(fileno(f), st.st_uid, st.st_gid);
			}
		}
	}

	/* if not already open, do as usual */
	if(f == NULL)
		f=fopen(file, mode);

	return(f);
}


mp_txt * _unix_glob(char * spec)
{
	mp_txt * txt=NULL;
	struct stat s;

#if defined(CONFOPT_GLOB_H)
	int n;
	glob_t globbuf;
	char * ptr;

	if(spec[0]=='\0') spec="*";

	globbuf.gl_offs=1;
	if(glob(spec,GLOB_MARK,NULL,&globbuf))
		return(NULL);

	txt=mp_create_sys_txt("<glob>");

	MP_SAVE_STATE();

	for(n=0;globbuf.gl_pathv[n]!=NULL;n++)
	{
		ptr=globbuf.gl_pathv[n];

		if(stat(ptr,&s)==-1) continue;
		if(s.st_mode & S_IFDIR) continue;

		mp_put_str(txt,ptr,1);
		mp_put_char(txt,'\n',1);
	}

	globfree(&globbuf);

	mp_move_left(txt);
	mp_delete_char(txt);
	mp_move_bof(txt);

	MP_RESTORE_STATE();

#else /* CONFOPT_GLOB_H */

	/* no glob: simulate it piping from ls */

	FILE * f;
	char tmp[1024];

	if(*spec=='\0' || strcmp(spec,"*")==0)
		strcpy(tmp,"ls");
	else
		snprintf(tmp,sizeof(tmp),"ls %s",spec);

	if((f=popen(tmp,"r"))==NULL)
		return(NULL);

	txt=mp_create_sys_txt("<glob>");

	MP_SAVE_STATE();

	while(fgets(tmp,sizeof(tmp),f)!=NULL)
	{
		tmp[strlen(tmp)-1]='\0';

		if(tmp[0]=='\0') continue;
		if(stat(tmp,&s)==-1) continue;
		if(s.st_mode & S_IFDIR) continue;

		mp_put_str(txt,tmp,1);
		mp_put_char(txt,'\n',1);
	}

	if(pclose(f)==-1)
	{
		mp_delete_sys_txt(txt);
		txt=NULL;
	}

	mp_move_left(txt);
	mp_delete_char(txt);
	mp_move_bof(txt);

	MP_RESTORE_STATE();

#endif /* CONFOPT_GLOB_H */

	return(txt);
}

int _unix_popen(mp_txt *txt,char *cmd,char *mode)
{
	FILE *f;
	int c;
	mp_txt *ctxt;
	int ret=-1;
  
	if (mode[0] == 'w')
	{
		ctxt=mp_get_tmp_txt(txt);
		mp_move_bof(ctxt);

		mp_log("exec: write to '%s'\n",cmd);

		if((f=popen(cmd,"w")) != NULL)
		{
			while((c=mp_get_char(ctxt)) != '\0')
				fputc(c,f);

			if(pclose(f) != -1)
				ret=0;
		}

		mp_end_tmp_txt();
	}
	else
	{
		mp_log("exec: read from '%s'\n",cmd);

		if((f=popen(cmd,"r")) != NULL)
		{
			while((c=fgetc(f)) != EOF)
				mp_put_char(txt,c,1);

			if(pclose(f) != -1)
				ret=0;
		}
	}

	return(ret);
}


int _unix_help(char * term, int synhi)
{
	char tmp[1024];
	FILE * f;
	mp_txt * txt;
	char ** ptr;

	sprintf(tmp,L("<help about '%s'>"),term);

	if(synhi==0 || (txt=mp_create_txt(tmp))==NULL)
		return(0);

	if((ptr=_mps_synhi[synhi - 1].helpers)!=NULL)
	{
		for(;*ptr!=NULL;ptr++)
		{
			snprintf(tmp,sizeof(tmp),*ptr,term);

			if((f=popen(tmp,"r"))!=NULL)
			{
				mp_load_file(txt,f);

				if(!pclose(f))
					break;
			}
		}
	}

	if(ptr==NULL || *ptr==NULL)
	{
		mp_delete_txt(txt);
		mpv_alert(L("No help for '%s'"),term);
		return(0);
	}

	mps_auto_synhi(txt);
	txt->type=MP_TYPE_READ_ONLY;
	txt->mod=0;

	return(1);
}

#endif /* CONFOPT_UNIX_LIKE */