| 1196 | */ |
| 1197 | |
| 1198 | cups_file_t * /* O - CUPS file or @code NULL@ if the file could not be opened */ |
| 1199 | cupsFileOpenFd(int fd, /* I - File descriptor */ |
| 1200 | const char *mode) /* I - Open mode */ |
| 1201 | { |
| 1202 | cups_file_t *fp; /* New CUPS file */ |
| 1203 | |
| 1204 | |
| 1205 | DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd, mode)); |
| 1206 | |
| 1207 | /* |
| 1208 | * Range check input... |
| 1209 | */ |
| 1210 | |
| 1211 | if (fd < 0 || !mode || |
| 1212 | (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's') || |
| 1213 | (*mode == 'a' && isdigit(mode[1] & 255))) |
| 1214 | return (NULL); |
| 1215 | |
| 1216 | /* |
| 1217 | * Allocate memory... |
| 1218 | */ |
| 1219 | |
| 1220 | if ((fp = calloc(1, sizeof(cups_file_t))) == NULL) |
| 1221 | return (NULL); |
| 1222 | |
| 1223 | /* |
| 1224 | * Open the file... |
| 1225 | */ |
| 1226 | |
| 1227 | fp->fd = fd; |
| 1228 | |
| 1229 | switch (*mode) |
| 1230 | { |
| 1231 | case 'a' : |
| 1232 | fp->pos = lseek(fd, 0, SEEK_END); |
| 1233 | |
| 1234 | case 'w' : |
| 1235 | fp->mode = 'w'; |
| 1236 | fp->ptr = fp->buf; |
| 1237 | fp->end = fp->buf + sizeof(fp->buf); |
| 1238 | |
| 1239 | #ifdef HAVE_LIBZ |
| 1240 | if (mode[1] >= '1' && mode[1] <= '9') |
| 1241 | { |
| 1242 | /* |
| 1243 | * Open a compressed stream, so write the standard gzip file |
| 1244 | * header... |
| 1245 | */ |
| 1246 | |
| 1247 | unsigned char header[10]; /* gzip file header */ |
| 1248 | time_t curtime; /* Current time */ |
| 1249 | |
| 1250 | |
| 1251 | curtime = time(NULL); |
| 1252 | header[0] = 0x1f; |
| 1253 | header[1] = 0x8b; |
| 1254 | header[2] = Z_DEFLATED; |
| 1255 | header[3] = 0; |