~ubuntu-branches/ubuntu/lucid/python-scipy/lucid

« back to all changes in this revision

Viewing changes to Lib/sandbox/xplt/src/play/unix/files.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-07 14:12:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070107141212-mm0ebkh5b37hcpzn
* Remove build dependency on python-numpy-dev.
* python-scipy: Depend on python-numpy instead of python-numpy-dev.
* Package builds on other archs than i386. Closes: #402783.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * files.c -- $Id: files.c 685 2003-03-08 15:26:51Z travo $
 
3
 * UNIX version of play file operations
 
4
 *
 
5
 * Copyright (c) 1999.  See accompanying LEGAL file for details.
 
6
 */
 
7
#ifndef _POSIX_SOURCE
 
8
/* to get fileno declared */
 
9
#define _POSIX_SOURCE 1
 
10
#endif
 
11
#ifndef _XOPEN_SOURCE
 
12
/* to get popen declared */
 
13
#define _XOPEN_SOURCE 1
 
14
#endif
 
15
 
 
16
#include "config.h"
 
17
#include "pstdio.h"
 
18
#include "pstdlib.h"
 
19
#include "playu.h"
 
20
 
 
21
#include <stdio.h>
 
22
#include <string.h>
 
23
#include <unistd.h>
 
24
#include <sys/types.h>
 
25
#include <sys/stat.h>
 
26
 
 
27
struct p_file {
 
28
  FILE *fp;  /* use FILE* for text file operations */
 
29
  int fd;    /* use file descriptor for binary file operations */
 
30
  int binary;
 
31
};
 
32
 
 
33
p_file *
 
34
p_fopen(const char *unix_name, const char *mode)
 
35
{
 
36
  FILE *fp = fopen(u_pathname(unix_name), mode);
 
37
  p_file *f = fp? p_malloc(sizeof(p_file)) : 0;
 
38
  if (f) {
 
39
    f->fp = fp;
 
40
    f->fd = fileno(fp);
 
41
    for (; mode[0] ; mode++) if (mode[0]=='b') break;
 
42
    f->binary = (mode[0]=='b');
 
43
  }
 
44
  return f;
 
45
}
 
46
 
 
47
p_file *
 
48
p_popen(const char *command, const char *mode)
 
49
{
 
50
  FILE *fp = popen(command, mode[0]=='w'? "w" : "r");
 
51
  p_file *f = fp? p_malloc(sizeof(p_file)) : 0;
 
52
  if (f) {
 
53
    f->fp = fp;
 
54
    f->fd = fileno(fp);
 
55
    f->binary = 2;
 
56
  }
 
57
  return f;
 
58
}
 
59
 
 
60
int
 
61
p_fclose(p_file *file)
 
62
{
 
63
  int flag = (file->binary&2)? pclose(file->fp) : fclose(file->fp);
 
64
  p_free(file);
 
65
  return flag;
 
66
}
 
67
 
 
68
char *
 
69
p_fgets(p_file *file, char *buf, int buflen)
 
70
{
 
71
  return fgets(buf, buflen, file->fp);
 
72
}
 
73
 
 
74
int
 
75
p_fputs(p_file *file, const char *buf)
 
76
{
 
77
  return fputs(buf, file->fp);
 
78
}
 
79
 
 
80
unsigned long
 
81
p_fread(p_file *file, void *buf, unsigned long nbytes)
 
82
{
 
83
  if (file->binary & 1)
 
84
    return read(file->fd, buf, nbytes);
 
85
  else
 
86
    return fread(buf, 1, nbytes, file->fp);
 
87
}
 
88
 
 
89
unsigned long
 
90
p_fwrite(p_file *file, const void *buf, unsigned long nbytes)
 
91
{
 
92
  if (file->binary & 1)
 
93
    return write(file->fd, buf, nbytes);
 
94
  else
 
95
    return fwrite(buf, 1, nbytes, file->fp);
 
96
}
 
97
 
 
98
unsigned long
 
99
p_ftell(p_file *file)
 
100
{
 
101
  if (file->binary & 1)
 
102
    return lseek(file->fd, 0L, SEEK_CUR);
 
103
  else if (!(file->binary & 2))
 
104
    return ftell(file->fp);
 
105
  else
 
106
    return -1L;
 
107
}
 
108
 
 
109
int
 
110
p_fseek(p_file *file, unsigned long addr)
 
111
{
 
112
  if (file->binary & 1)
 
113
    return -(lseek(file->fd, addr, SEEK_SET)==-1L);
 
114
  else if (!(file->binary & 2))
 
115
    return fseek(file->fp, addr, SEEK_SET);
 
116
  else
 
117
    return -1;
 
118
}
 
119
 
 
120
int
 
121
p_fflush(p_file *file)
 
122
{
 
123
  return (file->binary & 1)? 0 : fflush(file->fp);
 
124
}
 
125
 
 
126
int
 
127
p_feof(p_file *file)
 
128
{
 
129
  return feof(file->fp);  /* does not work for file->binary */
 
130
}
 
131
 
 
132
int
 
133
p_ferror(p_file *file)
 
134
{
 
135
  int flag = ferror(file->fp);
 
136
  clearerr(file->fp);
 
137
  return flag;
 
138
}
 
139
 
 
140
unsigned long
 
141
p_fsize(p_file *file)
 
142
{
 
143
  struct stat buf;
 
144
  if (fstat(file->fd, &buf)) return 0;
 
145
  return buf.st_size;
 
146
}
 
147
 
 
148
int
 
149
p_remove(const char *unix_name)
 
150
{
 
151
  return remove(u_pathname(unix_name));
 
152
}
 
153
 
 
154
int
 
155
p_rename(const char *unix_old, const char *unix_new)
 
156
{
 
157
  char old[P_WKSIZ+1];
 
158
  old[0] = '\0';
 
159
  strncat(old, u_pathname(unix_old), P_WKSIZ);
 
160
  return rename(old, u_pathname(unix_new));
 
161
}