static int debugCmdWriteTest(DebugContext *debugContext, int argc, char *argv[]) { uio_Handle *handle; const char testString[] = "Hello world!\n"; if (argc != 2) { fprintf(debugContext->err, "Invalid number of arguments.\n"); return 1; } handle = uio_open(debugContext->cwd, argv[1], O_WRONLY | O_CREAT | O_EXCL #ifdef WIN32 | O_BINARY #endif , 0644); if (handle == NULL) { fprintf(debugContext->err, "Could not open file: %s\n", strerror(errno)); return 1; } if (uio_write(handle, testString, sizeof testString) == -1) { fprintf(debugContext->err, "Write failed: %s\n", strerror(errno)); return 1; } uio_close(handle); return 0; }
int main (int argc, char **argv) { struct uio_info_t *info_list, *p; program_name = argv[0]; decode_switches (argc, argv); if (uio_offset < 0) { fprintf(stderr, "Negative offsets are not supported.\n"); usage (-EINVAL); } if (opt_help) usage(0); if (opt_version) version(0); info_list = uio_find_devices(uio_filter); if (!info_list) printf("No UIO devices found.\n"); p = info_list; while (p) { char dev_name[16]; int fd; uio_get_all_info(p); uio_get_device_attributes(p); snprintf(dev_name,sizeof(dev_name),"/dev/uio%d",p->uio_num); fd = open(dev_name,O_RDWR); if (fd<0) { close(fd); p = p->next; continue; } uio_single_mmap(p,uio_map,fd); if (opt_read) uio_read(p); if (opt_write) uio_write(p); close(fd); p = p->next; } uio_free_info(info_list); exit (0); }
/* * Copy a file with path srcName to a file with name newName. * If the destination already exists, the operation fails. * Links are followed. * Special files (fifos, char devices, block devices, etc) will be * read as long as there is data available and the destination will be * a regular file with that data. * The new file will have the same permissions as the old. * If an error occurs during copying, an attempt will be made to * remove the copy. */ int copyFile (uio_DirHandle *srcDir, const char *srcName, uio_DirHandle *dstDir, const char *newName) { uio_Handle *src, *dst; struct stat sb; #define BUFSIZE 65536 uint8 *buf, *bufPtr; ssize_t numInBuf, numWritten; src = uio_open (srcDir, srcName, O_RDONLY #ifdef WIN32 | O_BINARY #endif , 0); if (src == NULL) return -1; if (uio_fstat (src, &sb) == -1) return copyError (src, NULL, NULL, NULL, NULL); dst = uio_open (dstDir, newName, O_WRONLY | O_CREAT | O_EXCL #ifdef WIN32 | O_BINARY #endif , sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)); if (dst == NULL) return copyError (src, NULL, NULL, NULL, NULL); buf = HMalloc(BUFSIZE); // This was originally a statically allocated buffer, // but as this function might be run from a thread with // a small Stack, this is better. while (1) { numInBuf = uio_read (src, buf, BUFSIZE); if (numInBuf == -1) { if (errno == EINTR) continue; return copyError (src, dst, dstDir, newName, buf); } if (numInBuf == 0) break; bufPtr = buf; do { numWritten = uio_write (dst, bufPtr, numInBuf); if (numWritten == -1) { if (errno == EINTR) continue; return copyError (src, dst, dstDir, newName, buf); } numInBuf -= numWritten; bufPtr += numWritten; } while (numInBuf > 0); } HFree (buf); uio_close (src); uio_close (dst); errno = 0; return 0; }